| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2011 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_SUBPROCESS_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_SUBPROCESS_H_ | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 20 | #include <unistd.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 21 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | #include <map> | 
| Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 23 | #include <memory> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include <string> | 
|  | 25 | #include <vector> | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 26 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 27 | #include <base/callback.h> | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 28 | #include <base/files/file_descriptor_watcher_posix.h> | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 29 | #include <base/logging.h> | 
|  | 30 | #include <base/macros.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 31 | #include <brillo/asynchronous_signal_handler_interface.h> | 
|  | 32 | #include <brillo/message_loops/message_loop.h> | 
| Amin Hassani | 4bd46b3 | 2020-09-15 15:30:09 -0700 | [diff] [blame] | 33 | #ifdef __CHROMEOS__ | 
|  | 34 | #include <brillo/process/process.h> | 
|  | 35 | #include <brillo/process/process_reaper.h> | 
|  | 36 | #else | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 37 | #include <brillo/process.h> | 
|  | 38 | #include <brillo/process_reaper.h> | 
| Amin Hassani | 4bd46b3 | 2020-09-15 15:30:09 -0700 | [diff] [blame] | 39 | #endif  // __CHROMEOS__ | 
| Vyshu | 852f57d | 2020-10-09 17:35:14 +0000 | [diff] [blame] | 40 | #include <gtest/gtest_prod.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | // The Subprocess class is a singleton. It's used to spawn off a subprocess | 
|  | 43 | // and get notified when the subprocess exits. The result of Exec() can | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 44 | // be saved and used to cancel the callback request and kill your process. If | 
|  | 45 | // you know you won't call KillExec(), you may safely lose the return value | 
|  | 46 | // from Exec(). | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 47 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 48 | // To create the Subprocess singleton just instantiate it with and call Init(). | 
|  | 49 | // You can't have two Subprocess instances initialized at the same time. | 
|  | 50 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | namespace chromeos_update_engine { | 
|  | 52 |  | 
|  | 53 | class Subprocess { | 
|  | 54 | public: | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 55 | enum Flags { | 
|  | 56 | kSearchPath = 1 << 0, | 
|  | 57 | kRedirectStderrToStdout = 1 << 1, | 
|  | 58 | }; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 59 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 60 | // Callback type used when an async process terminates. It receives the exit | 
|  | 61 | // code and the stdout output (and stderr if redirected). | 
|  | 62 | using ExecCallback = base::Callback<void(int, const std::string&)>; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 64 | Subprocess() = default; | 
|  | 65 |  | 
|  | 66 | // Destroy and unregister the Subprocess singleton. | 
|  | 67 | ~Subprocess(); | 
|  | 68 |  | 
|  | 69 | // Initialize and register the Subprocess singleton. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 70 | void Init(brillo::AsynchronousSignalHandlerInterface* async_signal_handler); | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | // Launches a process in the background and calls the passed |callback| when | 
| Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 73 | // the process exits. The file descriptors specified in |output_pipes| will | 
|  | 74 | // be available in the child as the writer end of a pipe. Use GetPipeFd() to | 
|  | 75 | // know the reader end in the parent. Only stdin, stdout, stderr and the file | 
|  | 76 | // descriptors in |output_pipes| will be open in the child. | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 77 | // Returns the process id of the new launched process or 0 in case of failure. | 
|  | 78 | pid_t Exec(const std::vector<std::string>& cmd, const ExecCallback& callback); | 
|  | 79 | pid_t ExecFlags(const std::vector<std::string>& cmd, | 
|  | 80 | uint32_t flags, | 
| Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 81 | const std::vector<int>& output_pipes, | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 82 | const ExecCallback& callback); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 |  | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 84 | // Kills the running process with SIGTERM and ignores the callback. | 
| Alex Deymo | e384bb2 | 2016-03-29 17:23:33 -0700 | [diff] [blame] | 85 | void KillExec(pid_t pid); | 
|  | 86 |  | 
|  | 87 | // Return the parent end of the pipe mapped onto |fd| in the child |pid|. This | 
|  | 88 | // file descriptor is available until the callback for the child |pid| | 
|  | 89 | // returns. After that the file descriptor will be closed. The passed |fd| | 
|  | 90 | // must be one of the file descriptors passed to ExecFlags() in | 
|  | 91 | // |output_pipes|, otherwise returns -1. | 
|  | 92 | int GetPipeFd(pid_t pid, int fd) const; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 |  | 
| Colin Cross | d76a8ac | 2021-12-21 13:08:20 -0800 | [diff] [blame] | 94 | // Executes a command synchronously. Returns true on success. If |stdout_str| | 
|  | 95 | // is non-null, the process output is stored in it, otherwise the output is | 
| Amin Hassani | 3a4caa1 | 2019-11-06 11:12:28 -0800 | [diff] [blame] | 96 | // logged. | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 97 | static bool SynchronousExec(const std::vector<std::string>& cmd, | 
| Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 98 | int* return_code, | 
| Colin Cross | d76a8ac | 2021-12-21 13:08:20 -0800 | [diff] [blame] | 99 | std::string* stdout_str, | 
|  | 100 | std::string* stderr_str); | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 101 | static bool SynchronousExecFlags(const std::vector<std::string>& cmd, | 
|  | 102 | uint32_t flags, | 
|  | 103 | int* return_code, | 
| Colin Cross | d76a8ac | 2021-12-21 13:08:20 -0800 | [diff] [blame] | 104 | std::string* stdout_str, | 
|  | 105 | std::string* stderr_str); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 106 |  | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 107 | // Gets the one instance. | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 108 | static Subprocess& Get() { return *subprocess_singleton_; } | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 109 |  | 
| Amin Hassani | a885954 | 2018-03-07 16:24:43 -0800 | [diff] [blame] | 110 | // Tries to log all in flight processes's output. It is used right before | 
|  | 111 | // exiting the update_engine, probably when the subprocess caused a system | 
|  | 112 | // shutdown. | 
|  | 113 | void FlushBufferedLogsAtExit(); | 
|  | 114 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 115 | private: | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 116 | FRIEND_TEST(SubprocessTest, CancelTest); | 
|  | 117 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 118 | struct SubprocessRecord { | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 119 | explicit SubprocessRecord(const ExecCallback& callback) | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 120 | : callback(callback) {} | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 121 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 122 | // The callback supplied by the caller. | 
|  | 123 | ExecCallback callback; | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 124 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 125 | // The ProcessImpl instance managing the child process. Destroying this | 
|  | 126 | // will close our end of the pipes we have open. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 127 | brillo::ProcessImpl proc; | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 128 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 129 | // These are used to monitor the stdout of the running process, including | 
|  | 130 | // the stderr if it was redirected. | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 131 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_controller; | 
| Tianjie | 934b847 | 2020-06-24 23:10:49 -0700 | [diff] [blame] | 132 |  | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 133 | int stdout_fd{-1}; | 
| Colin Cross | d76a8ac | 2021-12-21 13:08:20 -0800 | [diff] [blame] | 134 | std::string stdout_str; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 135 | }; | 
|  | 136 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 137 | // Callback which runs whenever there is input available on the subprocess | 
|  | 138 | // stdout pipe. | 
| Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 139 | static void OnStdoutReady(SubprocessRecord* record); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 |  | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 141 | // Callback for when any subprocess terminates. This calls the user | 
|  | 142 | // requested callback. | 
|  | 143 | void ChildExitedCallback(const siginfo_t& info); | 
|  | 144 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 145 | // The global instance. | 
|  | 146 | static Subprocess* subprocess_singleton_; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 148 | // A map from the asynchronous subprocess tag (see Exec) to the subprocess | 
|  | 149 | // record structure for all active asynchronous subprocesses. | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 150 | std::map<pid_t, std::unique_ptr<SubprocessRecord>> subprocess_records_; | 
|  | 151 |  | 
|  | 152 | // Used to watch for child processes. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 153 | brillo::ProcessReaper process_reaper_; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 154 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | DISALLOW_COPY_AND_ASSIGN(Subprocess); | 
|  | 156 | }; | 
|  | 157 |  | 
|  | 158 | }  // namespace chromeos_update_engine | 
|  | 159 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 160 | #endif  // UPDATE_ENGINE_COMMON_SUBPROCESS_H_ |