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 #include <chromeos/process.h> | |
6 | |
7 #include <sys/stat.h> | |
8 #include <sys/types.h> | |
9 #include <sys/wait.h> | |
10 #include <fcntl.h> | |
11 | |
12 #include <base/eintr_wrapper.h> | |
13 #include <base/file_util.h> | |
14 #include <base/logging.h> | |
15 #include <base/string_number_conversions.h> | |
16 #include <base/string_util.h> | |
17 #include <base/time.h> | |
18 | |
19 namespace chromeos { | |
20 | |
21 Process::Process() { | |
22 } | |
23 | |
24 Process::~Process() { | |
25 } | |
26 | |
27 bool Process::ProcessExists(pid_t pid) { | |
28 return file_util::DirectoryExists(FilePath(StringPrintf("/proc/%d", pid))); | |
29 } | |
30 | |
31 ProcessImpl::ProcessImpl() : pid_(0) { | |
32 } | |
33 | |
34 ProcessImpl::~ProcessImpl() { | |
35 Reset(0); | |
36 } | |
37 | |
38 void ProcessImpl::AddArg(const std::string& arg) { | |
39 arguments_.push_back(arg); | |
40 } | |
41 | |
42 void ProcessImpl::RedirectOutput(const std::string& output_file) { | |
43 output_file_ = output_file; | |
44 } | |
45 | |
46 bool ProcessImpl::Start() { | |
47 // Copy off a writeable version of arguments. Exec wants to be able | |
48 // to write both the array and the strings. | |
49 int total_args_size = 0; | |
50 | |
51 for (size_t i = 0; i < arguments_.size(); ++i) { | |
52 total_args_size += arguments_[i].size() + 1; | |
53 } | |
54 | |
55 scoped_array<char> buffer(new char[total_args_size]); | |
56 scoped_array<char*> argv(new char*[arguments_.size() + 1]); | |
57 | |
58 char* buffer_pointer = &buffer[0]; | |
59 for (size_t i = 0; i < arguments_.size(); ++i) { | |
60 argv[i] = buffer_pointer; | |
61 strcpy(buffer_pointer, arguments_[i].c_str()); | |
62 buffer_pointer += arguments_[i].size(); | |
petkov
2011/02/28 08:00:14
+ 1 here and remove ++buffer_pointer below?
kmixter1
2011/03/02 01:38:07
Done.
| |
63 ++buffer_pointer; | |
64 } | |
65 argv[arguments_.size()] = NULL; | |
66 | |
67 pid_ = fork(); | |
68 int saved_errno = errno; | |
69 if (pid_ < 0) { | |
70 LOG(ERROR) << "Fork failed: " << saved_errno; | |
71 return false; | |
72 } | |
73 | |
74 if (pid_ == 0) { | |
75 if (!output_file_.empty()) { | |
76 int output_handle = HANDLE_EINTR( | |
77 open(output_file_.c_str(), | |
78 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666)); | |
79 int saved_errno = errno; | |
80 if (output_handle < 0) { | |
81 LOG(ERROR) << "Could not create " << output_file_ | |
82 << ": " << saved_errno; | |
83 // Avoid exit() to avoid atexit handlers from parent. | |
84 _exit(127); | |
85 } | |
86 dup2(output_handle, 1); | |
87 dup2(output_handle, 2); | |
88 close(output_handle); | |
89 } | |
90 execv(argv[0], &argv[0]); | |
91 saved_errno = errno; | |
92 LOG(ERROR) << "Exec of " << argv[0] << " failed: " << saved_errno; | |
93 _exit(127); | |
94 } | |
95 return true; | |
96 } | |
97 | |
98 int ProcessImpl::Wait() { | |
99 int status = 0; | |
100 if (pid_ == 0) { | |
101 LOG(ERROR) << "Process not running"; | |
102 return -1; | |
103 } | |
104 if (HANDLE_EINTR(waitpid(pid_, &status, 0)) < 0) { | |
105 int saved_errno = errno; | |
106 LOG(ERROR) << "Problem waiting for pid " << pid_ << ": " << saved_errno; | |
107 return -1; | |
108 } | |
109 pid_t old_pid = pid_; | |
110 pid_ = 0; | |
111 if (!WIFEXITED(status)) { | |
112 LOG(ERROR) << "Process " << old_pid << " did not exit normally: " | |
113 << status; | |
114 return -1; | |
115 } | |
116 return WEXITSTATUS(status); | |
117 } | |
118 | |
119 bool ProcessImpl::HandleSigChild(pid_t pid, int status) { | |
120 if (pid_ == 0 || pid != pid_) return false; | |
121 pid_ = 0; | |
petkov
2011/02/28 08:00:14
So is this ReleaseIfSamePID?
kmixter1
2011/03/02 01:38:07
Conceptually it is. It's also what you do to clea
| |
122 return true; | |
123 } | |
124 | |
125 int ProcessImpl::Run() { | |
126 if (!Start()) { | |
127 return -1; | |
128 } | |
129 return Wait(); | |
130 } | |
131 | |
132 pid_t ProcessImpl::GetPid() { | |
133 return pid_; | |
134 } | |
135 | |
136 bool ProcessImpl::Kill(int signal, int timeout) { | |
137 if (pid_ == 0) { | |
138 // Passing pid == 0 to kill is committing suicide. Check specifically. | |
139 LOG(ERROR) << "Process not running"; | |
140 return false; | |
141 } | |
142 if (kill(pid_, signal) < 0) { | |
143 int saved_errno = errno; | |
144 LOG(ERROR) << "Unable to send signal to " << pid_ << " error " | |
145 << saved_errno; | |
146 return false; | |
147 } | |
148 base::TimeTicks start_signal = base::TimeTicks::Now(); | |
149 do { | |
150 int status = 0; | |
151 pid_t w = waitpid(pid_, &status, WNOHANG); | |
152 int saved_errno = errno; | |
153 if (w < 0) { | |
154 if (saved_errno == ECHILD) | |
155 return true; | |
156 LOG(ERROR) << "Waitpid returned " << w << ", errno " << saved_errno; | |
157 return false; | |
158 } | |
159 if (w > 0) { | |
160 pid_ = 0; | |
161 return true; | |
162 } | |
163 usleep(100); | |
164 } while ((base::TimeTicks::Now() - start_signal).InSecondsF() <= timeout); | |
165 LOG(INFO) << "process " << pid_ << " did not exit from signal " << signal | |
166 << " in " << timeout << " seconds"; | |
167 return false; | |
168 } | |
169 | |
170 void ProcessImpl::Reset(pid_t pid) { | |
171 arguments_.clear(); | |
172 if (pid_) | |
173 Kill(SIGKILL, 0); | |
174 pid_ = pid; | |
175 } | |
176 | |
177 bool ProcessImpl::ResetPidByFile(const std::string& pid_file) { | |
178 std::string contents; | |
179 if (!file_util::ReadFileToString(FilePath(pid_file), &contents)) { | |
180 LOG(ERROR) << "Could not read pid file" << pid_file; | |
181 return false; | |
182 } | |
183 TrimWhitespaceASCII(contents, TRIM_TRAILING, &contents); | |
184 int64 pid_int64 = 0; | |
185 if (!base::StringToInt64(contents, &pid_int64)) { | |
186 LOG(ERROR) << "Unexpected pid file contents"; | |
187 return false; | |
188 } | |
189 Reset(pid_int64); | |
190 return true; | |
191 } | |
192 | |
193 pid_t ProcessImpl::Release() { | |
194 pid_t old_pid = pid_; | |
195 pid_ = 0; | |
196 return old_pid; | |
197 } | |
198 | |
199 } // namespace chromeos | |
OLD | NEW |