OLD | NEW |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_PROCESS_H_ | 5 #ifndef CHROMEOS_PROCESS_H_ |
6 #define CHROMEOS_PROCESS_H_ | 6 #define CHROMEOS_PROCESS_H_ |
7 | 7 |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } | 44 } |
45 | 45 |
46 // Redirects stderr and stdout to |output_file|. | 46 // Redirects stderr and stdout to |output_file|. |
47 virtual void RedirectOutput(const std::string& output_file) = 0; | 47 virtual void RedirectOutput(const std::string& output_file) = 0; |
48 | 48 |
49 // Indicates we want to redirect |child_fd| in the child process's | 49 // Indicates we want to redirect |child_fd| in the child process's |
50 // file table to a pipe. |child_fd| will be available for reading | 50 // file table to a pipe. |child_fd| will be available for reading |
51 // from child process's perspective iff |is_input|. | 51 // from child process's perspective iff |is_input|. |
52 virtual void RedirectUsingPipe(int child_fd, bool is_input) = 0; | 52 virtual void RedirectUsingPipe(int child_fd, bool is_input) = 0; |
53 | 53 |
| 54 // Set the real/effective/saved user ID of the child process. |
| 55 virtual void SetUid(uid_t uid) = 0; |
| 56 |
| 57 // Set the real/effective/saved group ID of the child process. |
| 58 virtual void SetGid(gid_t gid) = 0; |
| 59 |
54 // Gets the pipe file descriptor mapped to the process's |child_fd|. | 60 // Gets the pipe file descriptor mapped to the process's |child_fd|. |
55 virtual int GetPipe(int child_fd) = 0; | 61 virtual int GetPipe(int child_fd) = 0; |
56 | 62 |
57 // Starts this process, returning true if successful. | 63 // Starts this process, returning true if successful. |
58 virtual bool Start() = 0; | 64 virtual bool Start() = 0; |
59 | 65 |
60 // Waits for this process to finish. Returns the process's exit | 66 // Waits for this process to finish. Returns the process's exit |
61 // status if it exited normally, or otherwise returns -1. | 67 // status if it exited normally, or otherwise returns -1. Note |
| 68 // that kErrorExitStatus may be returned if an error occurred |
| 69 // after forking and before execing the child process. |
62 virtual int Wait() = 0; | 70 virtual int Wait() = 0; |
63 | 71 |
64 // Start and wait for this process to finish. Returns same value as | 72 // Start and wait for this process to finish. Returns same value as |
65 // Wait(). | 73 // Wait(). |
66 virtual int Run() = 0; | 74 virtual int Run() = 0; |
67 | 75 |
68 // Returns the pid of this process or else returns 0 if there is no | 76 // Returns the pid of this process or else returns 0 if there is no |
69 // corresponding process (either because it has not yet been started | 77 // corresponding process (either because it has not yet been started |
70 // or has since exited). | 78 // or has since exited). |
71 virtual pid_t pid() = 0; | 79 virtual pid_t pid() = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
83 | 91 |
84 // Same as Reset but reads the pid from |pid_file|. Returns false | 92 // Same as Reset but reads the pid from |pid_file|. Returns false |
85 // only when the file cannot be read/parsed. | 93 // only when the file cannot be read/parsed. |
86 virtual bool ResetPidByFile(const std::string& pid_file) = 0; | 94 virtual bool ResetPidByFile(const std::string& pid_file) = 0; |
87 | 95 |
88 // Releases the process so that on destruction, the process is not killed. | 96 // Releases the process so that on destruction, the process is not killed. |
89 virtual pid_t Release() = 0; | 97 virtual pid_t Release() = 0; |
90 | 98 |
91 // Returns if |pid| is a currently running process. | 99 // Returns if |pid| is a currently running process. |
92 static bool ProcessExists(pid_t pid); | 100 static bool ProcessExists(pid_t pid); |
| 101 |
| 102 // When returned from Wait or Run, indicates an error may have occurred |
| 103 // creating the process. |
| 104 enum { kErrorExitStatus = 127 }; |
93 }; | 105 }; |
94 | 106 |
95 class ProcessImpl : public Process { | 107 class ProcessImpl : public Process { |
96 public: | 108 public: |
97 ProcessImpl(); | 109 ProcessImpl(); |
98 virtual ~ProcessImpl(); | 110 virtual ~ProcessImpl(); |
99 | 111 |
100 virtual void AddArg(const std::string& arg); | 112 virtual void AddArg(const std::string& arg); |
101 virtual void RedirectOutput(const std::string& output_file); | 113 virtual void RedirectOutput(const std::string& output_file); |
102 virtual void RedirectUsingPipe(int child_fd, bool is_input); | 114 virtual void RedirectUsingPipe(int child_fd, bool is_input); |
| 115 virtual void SetUid(uid_t uid); |
| 116 virtual void SetGid(gid_t gid); |
103 virtual int GetPipe(int child_fd); | 117 virtual int GetPipe(int child_fd); |
104 virtual bool Start(); | 118 virtual bool Start(); |
105 virtual int Wait(); | 119 virtual int Wait(); |
106 virtual int Run(); | 120 virtual int Run(); |
107 virtual pid_t pid(); | 121 virtual pid_t pid(); |
108 virtual bool Kill(int signal, int timeout); | 122 virtual bool Kill(int signal, int timeout); |
109 virtual void Reset(pid_t pid); | 123 virtual void Reset(pid_t pid); |
110 virtual bool ResetPidByFile(const std::string& pid_file); | 124 virtual bool ResetPidByFile(const std::string& pid_file); |
111 virtual pid_t Release(); | 125 virtual pid_t Release(); |
112 | 126 |
113 protected: | 127 protected: |
114 struct PipeInfo { | 128 struct PipeInfo { |
115 PipeInfo() : parent_fd_(-1), child_fd_(-1), is_input_(false) {} | 129 PipeInfo() : parent_fd_(-1), child_fd_(-1), is_input_(false) {} |
116 // Parent (our) side of the pipe to the the child process. | 130 // Parent (our) side of the pipe to the the child process. |
117 int parent_fd_; | 131 int parent_fd_; |
118 // Child's side of the pipe to the parent. | 132 // Child's side of the pipe to the parent. |
119 int child_fd_; | 133 int child_fd_; |
120 // Is this an input or output pipe from child's perspective. | 134 // Is this an input or output pipe from child's perspective. |
121 bool is_input_; | 135 bool is_input_; |
122 }; | 136 }; |
123 typedef std::map<int, PipeInfo> PipeMap; | 137 typedef std::map<int, PipeInfo> PipeMap; |
124 | 138 |
125 std::string output_file_; | |
126 std::vector<std::string> arguments_; | |
127 // Map of child target file descriptors (first) to information about | |
128 // pipes created (second). | |
129 PipeMap pipe_map_; | |
130 | |
131 void UpdatePid(pid_t new_pid); | 139 void UpdatePid(pid_t new_pid); |
132 bool PopulatePipeMap(); | 140 bool PopulatePipeMap(); |
133 | 141 |
134 private: | 142 private: |
135 // Pid of currently managed process or 0 if no currently managed | 143 // Pid of currently managed process or 0 if no currently managed |
136 // process. pid must not be modified except by calling | 144 // process. pid must not be modified except by calling |
137 // UpdatePid(new_pid). | 145 // UpdatePid(new_pid). |
138 pid_t pid_; | 146 pid_t pid_; |
| 147 std::string output_file_; |
| 148 std::vector<std::string> arguments_; |
| 149 // Map of child target file descriptors (first) to information about |
| 150 // pipes created (second). |
| 151 PipeMap pipe_map_; |
| 152 uid_t uid_; |
| 153 gid_t gid_; |
139 }; | 154 }; |
140 | 155 |
141 } // namespace chromeos | 156 } // namespace chromeos |
142 | 157 |
143 #endif // CHROMEOS_PROCESS_H | 158 #endif // CHROMEOS_PROCESS_H |
OLD | NEW |