blob: 1b1149479388e162110cfff3a7942d5457d9fff2 [file] [log] [blame]
Darin Petkov85d02b72011-05-17 13:25:51 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_SUBPROCESS_H_
6#define UPDATE_ENGINE_SUBPROCESS_H_
adlr@google.com3defe6a2009-12-04 20:57:17 +00007
Ben Chan05735a12014-09-03 07:48:22 -07008#include <glib.h>
9
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include <map>
Alex Deymobc91a272014-05-20 16:45:33 -070011#include <memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <string>
13#include <vector>
Darin Petkov6f03a3b2010-11-10 14:27:14 -080014
Ben Chan05735a12014-09-03 07:48:22 -070015#include <base/logging.h>
16#include <base/macros.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000017
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
23namespace chromeos_update_engine {
24
25class Subprocess {
26 public:
Darin Petkov6f03a3b2010-11-10 14:27:14 -080027 typedef void(*ExecCallback)(int return_code,
28 const std::string& output,
29 void *p);
30
adlr@google.com3defe6a2009-12-04 20:57:17 +000031 static void Init() {
32 CHECK(!subprocess_singleton_);
33 subprocess_singleton_ = new Subprocess;
34 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000035
36 // Returns a tag > 0 on success.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070037 uint32_t Exec(const std::vector<std::string>& cmd,
38 ExecCallback callback,
39 void* p);
adlr@google.com3defe6a2009-12-04 20:57:17 +000040
41 // Used to cancel the callback. The process will still run to completion.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042 void CancelExec(uint32_t tag);
adlr@google.com3defe6a2009-12-04 20:57:17 +000043
Darin Petkov85d02b72011-05-17 13:25:51 -070044 // 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 Reyes5a232832010-10-12 16:20:54 -070047 static bool SynchronousExecFlags(const std::vector<std::string>& cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -070048 GSpawnFlags flags,
Andrew de los Reyes5a232832010-10-12 16:20:54 -070049 int* return_code,
Darin Petkov85d02b72011-05-17 13:25:51 -070050 std::string* stdout);
adlr@google.com3defe6a2009-12-04 20:57:17 +000051 static bool SynchronousExec(const std::vector<std::string>& cmd,
Darin Petkov85d02b72011-05-17 13:25:51 -070052 int* return_code,
53 std::string* stdout);
adlr@google.com3defe6a2009-12-04 20:57:17 +000054
55 // Gets the one instance
56 static Subprocess& Get() {
57 return *subprocess_singleton_;
58 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080059
adlr@google.com3defe6a2009-12-04 20:57:17 +000060 // Returns true iff there is at least one subprocess we're waiting on.
Darin Petkov6f03a3b2010-11-10 14:27:14 -080061 bool SubprocessInFlight();
62
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 private:
Darin Petkov6f03a3b2010-11-10 14:27:14 -080064 struct SubprocessRecord {
65 SubprocessRecord()
66 : tag(0),
Alex Vakulenko88b591f2014-08-28 16:48:57 -070067 callback(nullptr),
68 callback_data(nullptr),
69 gioout(nullptr),
Darin Petkov6f03a3b2010-11-10 14:27:14 -080070 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.com3defe6a2009-12-04 20:57:17 +000080
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 Watersa7fcafa2010-09-21 10:27:03 -070085 // Callback which runs in the child before exec to redirect stderr onto
86 // stdout.
87 static void GRedirectStderrToStdout(gpointer user_data);
88
Darin Petkov6f03a3b2010-11-10 14:27:14 -080089 // 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.com3defe6a2009-12-04 20:57:17 +000094
Darin Petkov6f03a3b2010-11-10 14:27:14 -080095 // The global instance.
96 static Subprocess* subprocess_singleton_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000097
Darin Petkov6f03a3b2010-11-10 14:27:14 -080098 // A map from the asynchronous subprocess tag (see Exec) to the subprocess
99 // record structure for all active asynchronous subprocesses.
Ben Chanf9cb98c2014-09-21 18:31:30 -0700100 std::map<int, std::shared_ptr<SubprocessRecord>> subprocess_records_;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800101
adlr@google.com3defe6a2009-12-04 20:57:17 +0000102 DISALLOW_COPY_AND_ASSIGN(Subprocess);
103};
104
105} // namespace chromeos_update_engine
106
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700107#endif // UPDATE_ENGINE_SUBPROCESS_H_