blob: c55b09026e5465d6d7861415eb579d5aa6e483bf [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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__
7
8#include <map>
9#include <string>
Darin Petkov6f03a3b2010-11-10 14:27:14 -080010#include <tr1/memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <vector>
Darin Petkov6f03a3b2010-11-10 14:27:14 -080012
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include <glib.h>
Darin Petkov6f03a3b2010-11-10 14:27:14 -080014
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include "base/basictypes.h"
Chris Masone790e62e2010-08-12 10:41:18 -070016#include "base/logging.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),
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.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.
100 std::map<int, std::tr1::shared_ptr<SubprocessRecord> > subprocess_records_;
101
adlr@google.com3defe6a2009-12-04 20:57:17 +0000102 DISALLOW_COPY_AND_ASSIGN(Subprocess);
103};
104
105} // namespace chromeos_update_engine
106
107#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__