Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_SUBPROCESS_H_ |
| 6 | #define UPDATE_ENGINE_SUBPROCESS_H_ |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 8 | #include <glib.h> |
| 9 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include <map> |
Alex Deymo | bc91a27 | 2014-05-20 16:45:33 -0700 | [diff] [blame] | 11 | #include <memory> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 14 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
| 16 | #include <base/macros.h> |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 17 | #include <chromeos/message_loops/message_loop.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | |
| 20 | // The Subprocess class is a singleton. It's used to spawn off a subprocess |
| 21 | // and get notified when the subprocess exits. The result of Exec() can |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 22 | // be saved and used to cancel the callback request and kill your process. If |
| 23 | // you know you won't call KillExec(), you may safely lose the return value |
| 24 | // from Exec(). |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
| 28 | class Subprocess { |
| 29 | public: |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 30 | typedef void(*ExecCallback)(int return_code, |
| 31 | const std::string& output, |
| 32 | void *p); |
| 33 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | static void Init() { |
| 35 | CHECK(!subprocess_singleton_); |
| 36 | subprocess_singleton_ = new Subprocess; |
| 37 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 38 | |
| 39 | // Returns a tag > 0 on success. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | uint32_t Exec(const std::vector<std::string>& cmd, |
| 41 | ExecCallback callback, |
| 42 | void* p); |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 43 | uint32_t ExecFlags(const std::vector<std::string>& cmd, |
| 44 | GSpawnFlags flags, |
| 45 | bool redirect_stderr_to_stdout, |
| 46 | ExecCallback callback, |
| 47 | void* p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 49 | // Kills the running process with SIGTERM and ignores the callback. |
| 50 | void KillExec(uint32_t tag); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 52 | // Executes a command synchronously. Returns true on success. If |stdout| is |
| 53 | // non-null, the process output is stored in it, otherwise the output is |
| 54 | // logged. Note that stderr is redirected to stdout. |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 55 | static bool SynchronousExecFlags(const std::vector<std::string>& cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 56 | GSpawnFlags flags, |
Andrew de los Reyes | 5a23283 | 2010-10-12 16:20:54 -0700 | [diff] [blame] | 57 | int* return_code, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 58 | std::string* stdout); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | static bool SynchronousExec(const std::vector<std::string>& cmd, |
Darin Petkov | 85d02b7 | 2011-05-17 13:25:51 -0700 | [diff] [blame] | 60 | int* return_code, |
| 61 | std::string* stdout); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 63 | // Gets the one instance. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | static Subprocess& Get() { |
| 65 | return *subprocess_singleton_; |
| 66 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 67 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | // 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] | 69 | bool SubprocessInFlight(); |
| 70 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | private: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 72 | FRIEND_TEST(SubprocessTest, CancelTest); |
| 73 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 74 | struct SubprocessRecord { |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 75 | SubprocessRecord() = default; |
| 76 | |
| 77 | uint32_t tag{0}; |
| 78 | chromeos::MessageLoop::TaskId task_id{chromeos::MessageLoop::kTaskIdNull}; |
| 79 | |
| 80 | ExecCallback callback{nullptr}; |
| 81 | void* callback_data{nullptr}; |
| 82 | |
| 83 | GPid pid; |
| 84 | |
| 85 | int stdout_fd{-1}; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 86 | std::string stdout; |
| 87 | }; |
| 88 | |
| 89 | Subprocess() {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 90 | |
| 91 | // Callback for when any subprocess terminates. This calls the user |
| 92 | // requested callback. |
| 93 | static void GChildExitedCallback(GPid pid, gint status, gpointer data); |
| 94 | |
Kenneth Waters | a7fcafa | 2010-09-21 10:27:03 -0700 | [diff] [blame] | 95 | // Callback which runs in the child before exec to redirect stderr onto |
| 96 | // stdout. |
| 97 | static void GRedirectStderrToStdout(gpointer user_data); |
| 98 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 99 | // Callback which runs whenever there is input available on the subprocess |
| 100 | // stdout pipe. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 101 | static void OnStdoutReady(SubprocessRecord* record); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 102 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 103 | // The global instance. |
| 104 | static Subprocess* subprocess_singleton_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 105 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 106 | // A map from the asynchronous subprocess tag (see Exec) to the subprocess |
| 107 | // record structure for all active asynchronous subprocesses. |
Alex Deymo | 29b8153 | 2015-07-09 11:51:49 -0700 | [diff] [blame] | 108 | std::map<uint32_t, std::shared_ptr<SubprocessRecord>> subprocess_records_; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 109 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | DISALLOW_COPY_AND_ASSIGN(Subprocess); |
| 111 | }; |
| 112 | |
| 113 | } // namespace chromeos_update_engine |
| 114 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 115 | #endif // UPDATE_ENGINE_SUBPROCESS_H_ |