Chromium Code Reviews| Index: chromeos/process.cc |
| diff --git a/chromeos/process.cc b/chromeos/process.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..41f18e9a9300ca37b476b33ad6593c1d41c02a99 |
| --- /dev/null |
| +++ b/chromeos/process.cc |
| @@ -0,0 +1,199 @@ |
| +// 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. |
| + |
| +#include <chromeos/process.h> |
| + |
| +#include <sys/stat.h> |
| +#include <sys/types.h> |
| +#include <sys/wait.h> |
| +#include <fcntl.h> |
| + |
| +#include <base/eintr_wrapper.h> |
| +#include <base/file_util.h> |
| +#include <base/logging.h> |
| +#include <base/string_number_conversions.h> |
| +#include <base/string_util.h> |
| +#include <base/time.h> |
| + |
| +namespace chromeos { |
| + |
| +Process::Process() { |
| +} |
| + |
| +Process::~Process() { |
| +} |
| + |
| +bool Process::ProcessExists(pid_t pid) { |
| + return file_util::DirectoryExists(FilePath(StringPrintf("/proc/%d", pid))); |
| +} |
| + |
| +ProcessImpl::ProcessImpl() : pid_(0) { |
| +} |
| + |
| +ProcessImpl::~ProcessImpl() { |
| + Reset(0); |
| +} |
| + |
| +void ProcessImpl::AddArg(const std::string& arg) { |
| + arguments_.push_back(arg); |
| +} |
| + |
| +void ProcessImpl::RedirectOutput(const std::string& output_file) { |
| + output_file_ = output_file; |
| +} |
| + |
| +bool ProcessImpl::Start() { |
| + // Copy off a writeable version of arguments. Exec wants to be able |
| + // to write both the array and the strings. |
| + int total_args_size = 0; |
| + |
| + for (size_t i = 0; i < arguments_.size(); ++i) { |
| + total_args_size += arguments_[i].size() + 1; |
| + } |
| + |
| + scoped_array<char> buffer(new char[total_args_size]); |
| + scoped_array<char*> argv(new char*[arguments_.size() + 1]); |
| + |
| + char* buffer_pointer = &buffer[0]; |
| + for (size_t i = 0; i < arguments_.size(); ++i) { |
| + argv[i] = buffer_pointer; |
| + strcpy(buffer_pointer, arguments_[i].c_str()); |
| + 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.
|
| + ++buffer_pointer; |
| + } |
| + argv[arguments_.size()] = NULL; |
| + |
| + pid_ = fork(); |
| + int saved_errno = errno; |
| + if (pid_ < 0) { |
| + LOG(ERROR) << "Fork failed: " << saved_errno; |
| + return false; |
| + } |
| + |
| + if (pid_ == 0) { |
| + if (!output_file_.empty()) { |
| + int output_handle = HANDLE_EINTR( |
| + open(output_file_.c_str(), |
| + O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666)); |
| + int saved_errno = errno; |
| + if (output_handle < 0) { |
| + LOG(ERROR) << "Could not create " << output_file_ |
| + << ": " << saved_errno; |
| + // Avoid exit() to avoid atexit handlers from parent. |
| + _exit(127); |
| + } |
| + dup2(output_handle, 1); |
| + dup2(output_handle, 2); |
| + close(output_handle); |
| + } |
| + execv(argv[0], &argv[0]); |
| + saved_errno = errno; |
| + LOG(ERROR) << "Exec of " << argv[0] << " failed: " << saved_errno; |
| + _exit(127); |
| + } |
| + return true; |
| +} |
| + |
| +int ProcessImpl::Wait() { |
| + int status = 0; |
| + if (pid_ == 0) { |
| + LOG(ERROR) << "Process not running"; |
| + return -1; |
| + } |
| + if (HANDLE_EINTR(waitpid(pid_, &status, 0)) < 0) { |
| + int saved_errno = errno; |
| + LOG(ERROR) << "Problem waiting for pid " << pid_ << ": " << saved_errno; |
| + return -1; |
| + } |
| + pid_t old_pid = pid_; |
| + pid_ = 0; |
| + if (!WIFEXITED(status)) { |
| + LOG(ERROR) << "Process " << old_pid << " did not exit normally: " |
| + << status; |
| + return -1; |
| + } |
| + return WEXITSTATUS(status); |
| +} |
| + |
| +bool ProcessImpl::HandleSigChild(pid_t pid, int status) { |
| + if (pid_ == 0 || pid != pid_) return false; |
| + 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
|
| + return true; |
| +} |
| + |
| +int ProcessImpl::Run() { |
| + if (!Start()) { |
| + return -1; |
| + } |
| + return Wait(); |
| +} |
| + |
| +pid_t ProcessImpl::GetPid() { |
| + return pid_; |
| +} |
| + |
| +bool ProcessImpl::Kill(int signal, int timeout) { |
| + if (pid_ == 0) { |
| + // Passing pid == 0 to kill is committing suicide. Check specifically. |
| + LOG(ERROR) << "Process not running"; |
| + return false; |
| + } |
| + if (kill(pid_, signal) < 0) { |
| + int saved_errno = errno; |
| + LOG(ERROR) << "Unable to send signal to " << pid_ << " error " |
| + << saved_errno; |
| + return false; |
| + } |
| + base::TimeTicks start_signal = base::TimeTicks::Now(); |
| + do { |
| + int status = 0; |
| + pid_t w = waitpid(pid_, &status, WNOHANG); |
| + int saved_errno = errno; |
| + if (w < 0) { |
| + if (saved_errno == ECHILD) |
| + return true; |
| + LOG(ERROR) << "Waitpid returned " << w << ", errno " << saved_errno; |
| + return false; |
| + } |
| + if (w > 0) { |
| + pid_ = 0; |
| + return true; |
| + } |
| + usleep(100); |
| + } while ((base::TimeTicks::Now() - start_signal).InSecondsF() <= timeout); |
| + LOG(INFO) << "process " << pid_ << " did not exit from signal " << signal |
| + << " in " << timeout << " seconds"; |
| + return false; |
| +} |
| + |
| +void ProcessImpl::Reset(pid_t pid) { |
| + arguments_.clear(); |
| + if (pid_) |
| + Kill(SIGKILL, 0); |
| + pid_ = pid; |
| +} |
| + |
| +bool ProcessImpl::ResetPidByFile(const std::string& pid_file) { |
| + std::string contents; |
| + if (!file_util::ReadFileToString(FilePath(pid_file), &contents)) { |
| + LOG(ERROR) << "Could not read pid file" << pid_file; |
| + return false; |
| + } |
| + TrimWhitespaceASCII(contents, TRIM_TRAILING, &contents); |
| + int64 pid_int64 = 0; |
| + if (!base::StringToInt64(contents, &pid_int64)) { |
| + LOG(ERROR) << "Unexpected pid file contents"; |
| + return false; |
| + } |
| + Reset(pid_int64); |
| + return true; |
| +} |
| + |
| +pid_t ProcessImpl::Release() { |
| + pid_t old_pid = pid_; |
| + pid_ = 0; |
| + return old_pid; |
| +} |
| + |
| +} // namespace chromeos |