summaryrefslogtreecommitdiff
path: root/subprocess.h
diff options
context:
space:
mode:
authoradlr@google.com <adlr@google.com@06c00378-0e64-4dae-be16-12b19f9950a1>2009-12-04 20:57:17 +0000
committeradlr@google.com <adlr@google.com@06c00378-0e64-4dae-be16-12b19f9950a1>2009-12-04 20:57:17 +0000
commit3defe6acb3609e70e851a6eff062577d25a2af9d (patch)
tree341e979027fde117dd8906483db7a5c703a2e1cf /subprocess.h
parentc98a7edf648aad88b3f66df3b5a7d43d6a6d7fa9 (diff)
Missed new files in last commit
Review URL: http://codereview.chromium.org/465067 git-svn-id: svn://chrome-svn/chromeos/trunk@336 06c00378-0e64-4dae-be16-12b19f9950a1
Diffstat (limited to 'subprocess.h')
-rw-r--r--subprocess.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/subprocess.h b/subprocess.h
new file mode 100644
index 00000000..92064a65
--- /dev/null
+++ b/subprocess.h
@@ -0,0 +1,79 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__
+
+#include <map>
+#include <string>
+#include <vector>
+#include <glib.h>
+#include "chromeos/obsolete_logging.h"
+#include "base/basictypes.h"
+
+// The Subprocess class is a singleton. It's used to spawn off a subprocess
+// and get notified when the subprocess exits. The result of Exec() can
+// be saved and used to cancel the callback request. If you know you won't
+// call CancelExec(), you may safely lose the return value from Exec().
+
+namespace chromeos_update_engine {
+
+class Subprocess {
+ public:
+ static void Init() {
+ CHECK(!subprocess_singleton_);
+ subprocess_singleton_ = new Subprocess;
+ }
+
+ typedef void(*ExecCallback)(int return_code, void *p);
+
+ // Returns a tag > 0 on success.
+ uint32 Exec(const std::vector<std::string>& cmd,
+ ExecCallback callback,
+ void* p);
+
+ // Used to cancel the callback. The process will still run to completion.
+ void CancelExec(uint32 tag);
+
+ // Executes a command synchronously. Returns true on success.
+ static bool SynchronousExec(const std::vector<std::string>& cmd,
+ int* return_code);
+
+ // Gets the one instance
+ static Subprocess& Get() {
+ return *subprocess_singleton_;
+ }
+
+ // Returns true iff there is at least one subprocess we're waiting on.
+ bool SubprocessInFlight() {
+ for (std::map<int, SubprocessCallbackRecord>::iterator it =
+ callback_records_.begin();
+ it != callback_records_.end(); ++it) {
+ if (it->second.callback)
+ return true;
+ }
+ return false;
+ }
+ private:
+ // The global instance
+ static Subprocess* subprocess_singleton_;
+
+ // Callback for when any subprocess terminates. This calls the user
+ // requested callback.
+ static void GChildExitedCallback(GPid pid, gint status, gpointer data);
+
+ struct SubprocessCallbackRecord {
+ ExecCallback callback;
+ void* callback_data;
+ };
+
+ std::map<int, SubprocessCallbackRecord> callback_records_;
+
+ Subprocess() {}
+ DISALLOW_COPY_AND_ASSIGN(Subprocess);
+};
+
+} // namespace chromeos_update_engine
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SUBPROCESS_H__