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 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_SUBPROCESS_H_ |
| 18 | #define UPDATE_ENGINE_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> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 28 | #include <base/logging.h> |
| 29 | #include <base/macros.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 30 | #include <brillo/asynchronous_signal_handler_interface.h> |
| 31 | #include <brillo/message_loops/message_loop.h> |
| 32 | #include <brillo/process.h> |
| 33 | #include <brillo/process_reaper.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 34 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | |
| 36 | // The Subprocess class is a singleton. It's used to spawn off a subprocess |
| 37 | // and get notified when the subprocess exits. The result of Exec() can |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 38 | // be saved and used to cancel the callback request and kill your process. If |
| 39 | // you know you won't call KillExec(), you may safely lose the return value |
| 40 | // from Exec(). |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 42 | // To create the Subprocess singleton just instantiate it with and call Init(). |
| 43 | // You can't have two Subprocess instances initialized at the same time. |
| 44 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | namespace chromeos_update_engine { |
| 46 | |
| 47 | class Subprocess { |
| 48 | public: |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 49 | enum Flags { |
| 50 | kSearchPath = 1 << 0, |
| 51 | kRedirectStderrToStdout = 1 << 1, |
| 52 | }; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 53 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 54 | // Callback type used when an async process terminates. It receives the exit |
| 55 | // code and the stdout output (and stderr if redirected). |
| 56 | using ExecCallback = base::Callback<void(int, const std::string&)>; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 57 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 58 | Subprocess() = default; |
| 59 | |
| 60 | // Destroy and unregister the Subprocess singleton. |
| 61 | ~Subprocess(); |
| 62 | |
| 63 | // Initialize and register the Subprocess singleton. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 64 | void Init(brillo::AsynchronousSignalHandlerInterface* async_signal_handler); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 65 | |
| 66 | // Launches a process in the background and calls the passed |callback| when |
| 67 | // the process exits. |
| 68 | // Returns the process id of the new launched process or 0 in case of failure. |
| 69 | pid_t Exec(const std::vector<std::string>& cmd, const ExecCallback& callback); |
| 70 | pid_t ExecFlags(const std::vector<std::string>& cmd, |
| 71 | uint32_t flags, |
| 72 | const ExecCallback& callback); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 74 | // Kills the running process with SIGTERM and ignores the callback. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 75 | void KillExec(pid_t tag); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 76 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 77 | // Executes a command synchronously. Returns true on success. If |stdout| is |
| 78 | // non-null, the process output is stored in it, otherwise the output is |
| 79 | // logged. Note that stderr is redirected to stdout. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | static bool SynchronousExec(const std::vector<std::string>& cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 81 | int* return_code, |
| 82 | std::string* stdout); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 83 | static bool SynchronousExecFlags(const std::vector<std::string>& cmd, |
| 84 | uint32_t flags, |
| 85 | int* return_code, |
| 86 | std::string* stdout); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 87 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 88 | // Gets the one instance. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 89 | static Subprocess& Get() { |
| 90 | return *subprocess_singleton_; |
| 91 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 92 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | // Returns true iff there is at least one subprocess we're waiting on. |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 94 | bool SubprocessInFlight(); |
| 95 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 96 | private: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 97 | FRIEND_TEST(SubprocessTest, CancelTest); |
| 98 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 99 | struct SubprocessRecord { |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 100 | explicit SubprocessRecord(const ExecCallback& callback) |
| 101 | : callback(callback) {} |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 102 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 103 | // The callback supplied by the caller. |
| 104 | ExecCallback callback; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 105 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 106 | // The ProcessImpl instance managing the child process. Destroying this |
| 107 | // will close our end of the pipes we have open. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 108 | brillo::ProcessImpl proc; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 109 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 110 | // These are used to monitor the stdout of the running process, including |
| 111 | // the stderr if it was redirected. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 112 | brillo::MessageLoop::TaskId stdout_task_id{ |
| 113 | brillo::MessageLoop::kTaskIdNull}; |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 114 | int stdout_fd{-1}; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 115 | std::string stdout; |
| 116 | }; |
| 117 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 118 | // Callback which runs whenever there is input available on the subprocess |
| 119 | // stdout pipe. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 120 | static void OnStdoutReady(SubprocessRecord* record); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 122 | // Callback for when any subprocess terminates. This calls the user |
| 123 | // requested callback. |
| 124 | void ChildExitedCallback(const siginfo_t& info); |
| 125 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 126 | // The global instance. |
| 127 | static Subprocess* subprocess_singleton_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 129 | // A map from the asynchronous subprocess tag (see Exec) to the subprocess |
| 130 | // record structure for all active asynchronous subprocesses. |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 131 | std::map<pid_t, std::unique_ptr<SubprocessRecord>> subprocess_records_; |
| 132 | |
| 133 | // Used to watch for child processes. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame^] | 134 | brillo::ProcessReaper process_reaper_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 135 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(Subprocess); |
| 137 | }; |
| 138 | |
| 139 | } // namespace chromeos_update_engine |
| 140 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 141 | #endif // UPDATE_ENGINE_SUBPROCESS_H_ |