OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium 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 "chrome/browser/mac/launchd.h" |
| 6 |
| 7 #include <launch.h> |
| 8 #include <signal.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/mac/scoped_launch_data.h" |
| 12 |
| 13 namespace launchd { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // MessageForJob sends a single message to launchd with a simple dictionary |
| 18 // mapping |operation| to |job_label|, and returns the result of calling |
| 19 // launch_msg to send that message. On failure, returns NULL. |
| 20 launch_data_t MessageForJob(const std::string& job_label, |
| 21 const char* operation) { |
| 22 ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
| 23 if (!message) { |
| 24 LOG(ERROR) << "launch_data_alloc"; |
| 25 return NULL; |
| 26 } |
| 27 |
| 28 ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str())); |
| 29 if (!job_label_launchd) { |
| 30 LOG(ERROR) << "launch_data_new_string"; |
| 31 return NULL; |
| 32 } |
| 33 |
| 34 if (!launch_data_dict_insert(message, |
| 35 job_label_launchd.release(), |
| 36 operation)) { |
| 37 return NULL; |
| 38 } |
| 39 |
| 40 return launch_msg(message); |
| 41 } |
| 42 |
| 43 // The internal implementation of StartJob and StopJob. |
| 44 int StartStopJob(const std::string& job_label, bool start) { |
| 45 ScopedLaunchData response(MessageForJob(job_label, |
| 46 start ? LAUNCH_KEY_STARTJOB : |
| 47 LAUNCH_KEY_STOPJOB)); |
| 48 if (!response) { |
| 49 return -1; |
| 50 } |
| 51 |
| 52 if (launch_data_get_type(response) != LAUNCH_DATA_ERRNO) { |
| 53 LOG(ERROR) << "StartStopJob: expected errno"; |
| 54 return -1; |
| 55 } |
| 56 |
| 57 int err = launch_data_get_errno(response); |
| 58 if (err) { |
| 59 LOG(ERROR) << "StartStopJob: " << err; |
| 60 } |
| 61 |
| 62 return err; |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 int StartJob(const std::string& job_label) { |
| 68 return StartStopJob(job_label, true); |
| 69 } |
| 70 |
| 71 int StopJob(const std::string& job_label) { |
| 72 return StartStopJob(job_label, false); |
| 73 } |
| 74 |
| 75 void RestartJob(const std::string& job_label) { |
| 76 if (StopJob(job_label) == 0) { |
| 77 StartJob(job_label); |
| 78 } |
| 79 } |
| 80 |
| 81 int PIDForJob(const std::string& job_label) { |
| 82 ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB)); |
| 83 if (!response) { |
| 84 return -1; |
| 85 } |
| 86 |
| 87 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) { |
| 88 LOG(ERROR) << "PIDForJob: expected dictionary"; |
| 89 return -1; |
| 90 } |
| 91 |
| 92 launch_data_t pid_data = launch_data_dict_lookup(response, |
| 93 LAUNCH_JOBKEY_PID); |
| 94 if (!pid_data) { |
| 95 LOG(ERROR) << "PIDForJob: no pid"; |
| 96 return -1; |
| 97 } |
| 98 |
| 99 if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) { |
| 100 LOG(ERROR) << "PIDForJob: expected integer"; |
| 101 return -1; |
| 102 } |
| 103 |
| 104 int pid = launch_data_get_integer(pid_data); |
| 105 return pid; |
| 106 } |
| 107 |
| 108 void SignalJob(const std::string& job_name, int signal) { |
| 109 int pid = PIDForJob(job_name); |
| 110 if (pid < 0) { |
| 111 return; |
| 112 } |
| 113 |
| 114 kill(pid, signal); |
| 115 } |
| 116 |
| 117 } // namespace launchd |
OLD | NEW |