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