Chromium Code Reviews| Index: chromeos/process.h |
| diff --git a/chromeos/process.h b/chromeos/process.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab0c82f5b107f5a7530354c4dc9ac8e86b171825 |
| --- /dev/null |
| +++ b/chromeos/process.h |
| @@ -0,0 +1,113 @@ |
| +// Copyright (c) 2011 The Chromium OS 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_PROCESS_H_ |
| +#define CHROMEOS_PROCESS_H_ |
| + |
| +#include <sys/types.h> |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include <base/string_util.h> |
| + |
| +namespace chromeos { |
| +// Manages a process. Can create the process, attach to an existing |
| +// process by pid or pid file, and kill the process. Upon destruction |
| +// any managed process is killed with SIGKILL. Use Release() to |
| +// release the process from management. |
| +class Process { |
| + public: |
| + Process(); |
| + virtual ~Process(); |
| + |
| + // Adds |arg| to the executable command-line to be run. The |
| + // executable name itself is the first argument. |
| + virtual void AddArg(const std::string& arg) = 0; |
| + |
| + // Adds |option| and |value| as an option with a string value to the command |
| + // line to be run. |
| + inline void AddStringOption(const std::string& option, const std::string& value) { |
|
petkov
2011/02/28 08:00:14
80 chars
kmixter1
2011/03/02 01:38:07
Done.
|
| + AddArg(option); |
| + AddArg(value); |
| + } |
| + |
| + // Adds |option| and |value| as an option which takes an integer |
| + // value to the command line to be run. |
| + inline void AddIntOption(const std::string& option, int value) { |
| + AddArg(option); |
| + AddArg(StringPrintf("%d", value)); |
| + } |
| + |
| + // Redirects stderr and stdout to |output_file|. |
| + virtual void RedirectOutput(const std::string& output_file) = 0; |
| + |
| + // Starts this process, returning true if successful. |
| + virtual bool Start() = 0; |
| + |
| + // Waits for this process to finish. Returns the process's exit status if it |
| + // exited normally, or otherwise returns -1. |
|
petkov
2011/02/28 08:00:14
is it an issue that you can't distinguish between
kmixter1
2011/03/02 01:38:07
Exit statuses are 8 bits and unsigned, so -1 is no
|
| + virtual int Wait() = 0; |
| + |
| + // This function handles the signal that a child has completed. The |pid| and |
| + // |status| of the child's exit are passed in. The function returns true if |
| + // it successfully handles the signal - otherwise it returns false if the pid |
| + // does not match this process. |
| + virtual bool HandleSigChild(pid_t pid, int status) = 0; |
|
petkov
2011/02/28 08:00:14
I don't quite get what this method does -- and I c
kmixter1
2011/03/02 01:38:07
Removed.
|
| + |
| + // Start and wait for this process to finish. Returns same value as Wait(). |
|
petkov
2011/02/28 08:00:14
Same question on the return value as Wait.
kmixter1
2011/03/02 01:38:07
Done.
|
| + virtual int Run() = 0; |
| + |
| + // Returns the pid of this process or else returns 0 if there is no |
| + // corresponding process (either because it has not yet been started |
| + // or has since exited). |
| + virtual pid_t GetPid() = 0; |
| + |
| + // Kills this process with |signal|. If process is not a child, returns immediately |
|
petkov
2011/02/28 08:00:14
80 chars the whole docstring
kmixter1
2011/03/02 01:38:07
Done.
|
| + // with a value based on whether kill was successful. If the process is a child and |
| + // |timeout| is non-zero, returns true if the process is able to be reaped within |
| + // the given |timeout| in seconds. |
| + virtual bool Kill(int signal, int timeout) = 0; |
| + |
| + // Resets this Process object to refer to the process with |pid|. If |pid| is zero, |
|
petkov
2011/02/28 08:00:14
80 chars
kmixter1
2011/03/02 01:38:07
Done.
|
| + // this object no longer refers to a process. |
| + virtual void Reset(pid_t pid) = 0; |
| + |
| + // Same as Reset but reads the pid from |pid_file|. Returns false only when the |
|
petkov
2011/02/28 08:00:14
80 chars
kmixter1
2011/03/02 01:38:07
Done.
|
| + // file cannot be read/parsed. |
| + virtual bool ResetPidByFile(const std::string& pid_file) = 0; |
| + |
| + // Releases the process so that on destruction, the process is not killed. |
| + virtual pid_t Release() = 0; |
| + |
| + // Returns if |pid| is a currently running process. |
| + static bool ProcessExists(pid_t pid); |
| +}; |
| + |
| +class ProcessImpl : public Process { |
| + public: |
| + ProcessImpl(); |
| + virtual ~ProcessImpl(); |
| + |
| + virtual void AddArg(const std::string& arg); |
| + virtual void RedirectOutput(const std::string& output_file); |
| + virtual bool Start(); |
| + virtual int Wait(); |
| + virtual bool HandleSigChild(pid_t pid, int status); |
| + virtual int Run(); |
| + virtual pid_t GetPid(); |
| + virtual bool Kill(int signal, int timeout); |
| + virtual void Reset(pid_t pid); |
| + virtual bool ResetPidByFile(const std::string& pid_file); |
| + virtual pid_t Release(); |
| + |
| + protected: |
| + std::string output_file_; |
| + pid_t pid_; |
| + std::vector<std::string> arguments_; |
| +}; |
| + |
| +} // namespace chromeos |
| + |
| +#endif // CHROMEOS_PROCESS_H |