Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Unified Diff: chromeos/process.cc

Issue 6509006: libchromeos: add process control library, syslog logging capability, and run unit tests (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/common.git@master
Patch Set: sync Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chromeos/process.cc
diff --git a/chromeos/process.cc b/chromeos/process.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fbfbd0a30d7ddb572f16e258783027304761bc0a
--- /dev/null
+++ b/chromeos/process.cc
@@ -0,0 +1,185 @@
+// 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_util.h>
+
+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::SetOutput(const std::string& output_file) {
+ output_file_ = output_file;
+}
+
+bool ProcessImpl::Start() {
+ // 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.
+ // 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();
+ *buffer_pointer = '\0';
petkov 2011/02/25 19:20:16 no need -- strcpy copies '\0'.
kmixter1 2011/02/26 00:54:31 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);
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.
+ }
+ execv(argv[0], &argv[0]);
+ saved_errno = errno;
+ LOG(ERROR) << "Exec 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: " << saved_errno;
+ return -1;
+ }
+ pid_ = 0;
+ if (!WIFEXITED(status)) {
+ LOG(ERROR) << "Process did not exit normally: " << status;
+ return -1;
+ }
+ return WEXITSTATUS(status);
+}
+
+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;
+ }
+ time_t start_signal = time(NULL);
+ do {
+ int status = 0;
+ pid_t w = waitpid(pid_, &status, WNOHANG);
+ int saved_errno = errno;
+ if (w < 0) {
+ LOG(ERROR) << "Waitpid returned " << w << ", errno " << saved_errno;
+ return false;
+ }
+ if (w > 0) {
+ pid_ = 0;
+ return true;
+ }
+ if (timeout == 0)
+ break;
+ usleep(100);
+ } 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
+ 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;
+ }
+ char* end = NULL;
+ 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.
+ if (!end || end[0] != '\n' || end[1] != '\0') {
+ LOG(ERROR) << "Unexpected pid file contents";
+ return false;
+ }
+ Reset(pid);
+ return true;
+}
+
+pid_t ProcessImpl::Release() {
+ pid_t old_pid = pid_;
+ pid_ = 0;
+ return old_pid;
+}

Powered by Google App Engine
This is Rietveld 408576698