OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
2 // 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_PROCESS_H_ | |
6 #define CHROMEOS_PROCESS_H_ | |
7 | |
8 #include <sys/types.h> | |
9 | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include <base/string_util.h> | |
14 | |
15 namespace chromeos { | |
16 // Manages a process. Can create the process, attach to an existing | |
17 // process by pid or pid file, and kill the process. Upon destruction | |
18 // any managed process is killed with SIGKILL. Use Release() to | |
19 // release the process from management. | |
20 class Process { | |
21 public: | |
22 Process(); | |
23 virtual ~Process(); | |
24 | |
25 // Adds |arg| to the executable command-line to be run. The | |
26 // executable name itself is the first argument. | |
27 virtual void AddArg(const std::string& arg) = 0; | |
28 | |
29 // Adds |option| and |value| as an option with a string value to the command | |
30 // line to be run. | |
31 inline void AddStringOption(const std::string& option, const std::string& valu e) { | |
petkov
2011/02/28 08:00:14
80 chars
kmixter1
2011/03/02 01:38:07
Done.
| |
32 AddArg(option); | |
33 AddArg(value); | |
34 } | |
35 | |
36 // Adds |option| and |value| as an option which takes an integer | |
37 // value to the command line to be run. | |
38 inline void AddIntOption(const std::string& option, int value) { | |
39 AddArg(option); | |
40 AddArg(StringPrintf("%d", value)); | |
41 } | |
42 | |
43 // Redirects stderr and stdout to |output_file|. | |
44 virtual void RedirectOutput(const std::string& output_file) = 0; | |
45 | |
46 // Starts this process, returning true if successful. | |
47 virtual bool Start() = 0; | |
48 | |
49 // Waits for this process to finish. Returns the process's exit status if it | |
50 // 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
| |
51 virtual int Wait() = 0; | |
52 | |
53 // This function handles the signal that a child has completed. The |pid| and | |
54 // |status| of the child's exit are passed in. The function returns true if | |
55 // it successfully handles the signal - otherwise it returns false if the pid | |
56 // does not match this process. | |
57 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.
| |
58 | |
59 // 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.
| |
60 virtual int Run() = 0; | |
61 | |
62 // Returns the pid of this process or else returns 0 if there is no | |
63 // corresponding process (either because it has not yet been started | |
64 // or has since exited). | |
65 virtual pid_t GetPid() = 0; | |
66 | |
67 // Kills this process with |signal|. If process is not a child, returns immed iately | |
petkov
2011/02/28 08:00:14
80 chars the whole docstring
kmixter1
2011/03/02 01:38:07
Done.
| |
68 // with a value based on whether kill was successful. If the process is a chi ld and | |
69 // |timeout| is non-zero, returns true if the process is able to be reaped wit hin | |
70 // the given |timeout| in seconds. | |
71 virtual bool Kill(int signal, int timeout) = 0; | |
72 | |
73 // 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.
| |
74 // this object no longer refers to a process. | |
75 virtual void Reset(pid_t pid) = 0; | |
76 | |
77 // Same as Reset but reads the pid from |pid_file|. Returns false only when t he | |
petkov
2011/02/28 08:00:14
80 chars
kmixter1
2011/03/02 01:38:07
Done.
| |
78 // file cannot be read/parsed. | |
79 virtual bool ResetPidByFile(const std::string& pid_file) = 0; | |
80 | |
81 // Releases the process so that on destruction, the process is not killed. | |
82 virtual pid_t Release() = 0; | |
83 | |
84 // Returns if |pid| is a currently running process. | |
85 static bool ProcessExists(pid_t pid); | |
86 }; | |
87 | |
88 class ProcessImpl : public Process { | |
89 public: | |
90 ProcessImpl(); | |
91 virtual ~ProcessImpl(); | |
92 | |
93 virtual void AddArg(const std::string& arg); | |
94 virtual void RedirectOutput(const std::string& output_file); | |
95 virtual bool Start(); | |
96 virtual int Wait(); | |
97 virtual bool HandleSigChild(pid_t pid, int status); | |
98 virtual int Run(); | |
99 virtual pid_t GetPid(); | |
100 virtual bool Kill(int signal, int timeout); | |
101 virtual void Reset(pid_t pid); | |
102 virtual bool ResetPidByFile(const std::string& pid_file); | |
103 virtual pid_t Release(); | |
104 | |
105 protected: | |
106 std::string output_file_; | |
107 pid_t pid_; | |
108 std::vector<std::string> arguments_; | |
109 }; | |
110 | |
111 } // namespace chromeos | |
112 | |
113 #endif // CHROMEOS_PROCESS_H | |
OLD | NEW |