Index: chrome/browser/mac/launchd.cc |
=================================================================== |
--- chrome/browser/mac/launchd.cc (revision 0) |
+++ chrome/browser/mac/launchd.cc (revision 0) |
@@ -0,0 +1,117 @@ |
+// Copyright (c) 2011 The Chromium 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 "chrome/browser/mac/launchd.h" |
+ |
+#include <launch.h> |
+#include <signal.h> |
+ |
+#include "base/logging.h" |
+#include "chrome/browser/mac/scoped_launch_data.h" |
+ |
+namespace launchd { |
+ |
+namespace { |
+ |
+// MessageForJob sends a single message to launchd with a simple dictionary |
+// mapping |operation| to |job_label|, and returns the result of calling |
+// launch_msg to send that message. On failure, returns NULL. |
+launch_data_t MessageForJob(const std::string& job_label, |
+ const char* operation) { |
+ ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
+ if (!message) { |
+ LOG(ERROR) << "launch_data_alloc"; |
+ return NULL; |
+ } |
+ |
+ ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str())); |
Robert Sesek
2011/06/29 18:26:42
The memory management here warrants a comment, I t
|
+ if (!job_label_launchd) { |
+ LOG(ERROR) << "launch_data_new_string"; |
+ return NULL; |
+ } |
+ |
+ if (!launch_data_dict_insert(message, |
+ job_label_launchd.release(), |
+ operation)) { |
+ return NULL; |
+ } |
+ |
+ return launch_msg(message); |
+} |
+ |
+// The internal implementation of StartJob and StopJob. |
+int StartStopJob(const std::string& job_label, bool start) { |
+ ScopedLaunchData response(MessageForJob(job_label, |
+ start ? LAUNCH_KEY_STARTJOB : |
+ LAUNCH_KEY_STOPJOB)); |
+ if (!response) { |
+ return -1; |
+ } |
+ |
+ if (launch_data_get_type(response) != LAUNCH_DATA_ERRNO) { |
+ LOG(ERROR) << "StartStopJob: expected errno"; |
+ return -1; |
+ } |
+ |
+ int err = launch_data_get_errno(response); |
+ if (err) { |
+ LOG(ERROR) << "StartStopJob: " << err; |
+ } |
+ |
+ return err; |
+} |
+ |
+} // namespace |
+ |
+int StartJob(const std::string& job_label) { |
+ return StartStopJob(job_label, true); |
+} |
+ |
+int StopJob(const std::string& job_label) { |
+ return StartStopJob(job_label, false); |
+} |
+ |
+void RestartJob(const std::string& job_label) { |
+ if (StopJob(job_label) == 0) { |
+ StartJob(job_label); |
+ } |
+} |
+ |
+int PIDForJob(const std::string& job_label) { |
+ ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB)); |
+ if (!response) { |
+ return -1; |
+ } |
+ |
+ if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) { |
+ LOG(ERROR) << "PIDForJob: expected dictionary"; |
+ return -1; |
+ } |
+ |
+ launch_data_t pid_data = launch_data_dict_lookup(response, |
+ LAUNCH_JOBKEY_PID); |
+ if (!pid_data) { |
+ LOG(ERROR) << "PIDForJob: no pid"; |
+ return -1; |
+ } |
+ |
+ if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) { |
+ LOG(ERROR) << "PIDForJob: expected integer"; |
+ return -1; |
+ } |
+ |
+ int pid = launch_data_get_integer(pid_data); |
+ return pid; |
+} |
+ |
+void SignalJob(const std::string& job_name, int signal) { |
+ int pid = PIDForJob(job_name); |
+ if (pid < 0) { |
+ return; |
+ } |
+ |
+ kill(pid, signal); |
+} |
+ |
+} // namespace launchd |
Property changes on: chrome/browser/mac/launchd.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |