| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/mac/launchd.h" | 5 #include "base/mac/launchd.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_launch_data.h" | 8 #include "base/mac/scoped_launch_data.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 namespace mac { | 11 namespace mac { |
| 12 | 12 |
| 13 // MessageForJob sends a single message to launchd with a simple dictionary | 13 // MessageForJob sends a single message to launchd with a simple dictionary |
| 14 // mapping |operation| to |job_label|, and returns the result of calling | 14 // mapping |operation| to |job_label|, and returns the result of calling |
| 15 // launch_msg to send that message. On failure, returns NULL. The caller | 15 // launch_msg to send that message. On failure, returns NULL. The caller |
| 16 // assumes ownership of the returned launch_data_t object. | 16 // assumes ownership of the returned launch_data_t object. |
| 17 launch_data_t MessageForJob(const std::string& job_label, | 17 launch_data_t MessageForJob(const std::string& job_label, |
| 18 const char* operation) { | 18 const char* operation) { |
| 19 // launch_data_alloc returns something that needs to be freed. | 19 // launch_data_alloc returns something that needs to be freed. |
| 20 ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY)); | 20 ScopedLaunchData message(launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
| 21 if (!message) { | 21 if (!message.is_valid()) { |
| 22 LOG(ERROR) << "launch_data_alloc"; | 22 LOG(ERROR) << "launch_data_alloc"; |
| 23 return NULL; | 23 return NULL; |
| 24 } | 24 } |
| 25 | 25 |
| 26 // launch_data_new_string returns something that needs to be freed, but | 26 // launch_data_new_string returns something that needs to be freed, but |
| 27 // the dictionary will assume ownership when launch_data_dict_insert is | 27 // the dictionary will assume ownership when launch_data_dict_insert is |
| 28 // called, so put it in a scoper and .release() it when given to the | 28 // called, so put it in a scoper and .release() it when given to the |
| 29 // dictionary. | 29 // dictionary. |
| 30 ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str())); | 30 ScopedLaunchData job_label_launchd(launch_data_new_string(job_label.c_str())); |
| 31 if (!job_label_launchd) { | 31 if (!job_label_launchd.is_valid()) { |
| 32 LOG(ERROR) << "launch_data_new_string"; | 32 LOG(ERROR) << "launch_data_new_string"; |
| 33 return NULL; | 33 return NULL; |
| 34 } | 34 } |
| 35 | 35 |
| 36 if (!launch_data_dict_insert(message, | 36 if (!launch_data_dict_insert(message.get(), job_label_launchd.release(), |
| 37 job_label_launchd.release(), | |
| 38 operation)) { | 37 operation)) { |
| 39 return NULL; | 38 return NULL; |
| 40 } | 39 } |
| 41 | 40 |
| 42 return launch_msg(message); | 41 return launch_msg(message.get()); |
| 43 } | 42 } |
| 44 | 43 |
| 45 pid_t PIDForJob(const std::string& job_label) { | 44 pid_t PIDForJob(const std::string& job_label) { |
| 46 ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB)); | 45 ScopedLaunchData response(MessageForJob(job_label, LAUNCH_KEY_GETJOB)); |
| 47 if (!response) { | 46 if (!response.is_valid()) { |
| 48 return -1; | 47 return -1; |
| 49 } | 48 } |
| 50 | 49 |
| 51 launch_data_type_t response_type = launch_data_get_type(response); | 50 launch_data_type_t response_type = launch_data_get_type(response.get()); |
| 52 if (response_type != LAUNCH_DATA_DICTIONARY) { | 51 if (response_type != LAUNCH_DATA_DICTIONARY) { |
| 53 if (response_type == LAUNCH_DATA_ERRNO) { | 52 if (response_type == LAUNCH_DATA_ERRNO) { |
| 54 LOG(ERROR) << "PIDForJob: error " << launch_data_get_errno(response); | 53 LOG(ERROR) << "PIDForJob: error " |
| 54 << launch_data_get_errno(response.get()); |
| 55 } else { | 55 } else { |
| 56 LOG(ERROR) << "PIDForJob: expected dictionary, got " << response_type; | 56 LOG(ERROR) << "PIDForJob: expected dictionary, got " << response_type; |
| 57 } | 57 } |
| 58 return -1; | 58 return -1; |
| 59 } | 59 } |
| 60 | 60 |
| 61 launch_data_t pid_data = launch_data_dict_lookup(response, | 61 launch_data_t pid_data = |
| 62 LAUNCH_JOBKEY_PID); | 62 launch_data_dict_lookup(response.get(), LAUNCH_JOBKEY_PID); |
| 63 if (!pid_data) | 63 if (!pid_data) |
| 64 return 0; | 64 return 0; |
| 65 | 65 |
| 66 if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) { | 66 if (launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) { |
| 67 LOG(ERROR) << "PIDForJob: expected integer"; | 67 LOG(ERROR) << "PIDForJob: expected integer"; |
| 68 return -1; | 68 return -1; |
| 69 } | 69 } |
| 70 | 70 |
| 71 return launch_data_get_integer(pid_data); | 71 return launch_data_get_integer(pid_data); |
| 72 } | 72 } |
| 73 | 73 |
| 74 } // namespace mac | 74 } // namespace mac |
| 75 } // namespace base | 75 } // namespace base |
| OLD | NEW |