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