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