OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/mac/service_management.h" |
| 16 |
| 17 #include <errno.h> |
| 18 #include <launch.h> |
| 19 #include <time.h> |
| 20 |
| 21 #include "base/mac/scoped_launch_data.h" |
| 22 #include "util/mac/launchd.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 launch_data_t LaunchDataDictionaryForJob(const std::string& label) { |
| 27 base::mac::ScopedLaunchData request( |
| 28 launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
| 29 launch_data_dict_insert( |
| 30 request, launch_data_new_string(label.c_str()), LAUNCH_KEY_GETJOB); |
| 31 |
| 32 base::mac::ScopedLaunchData response(launch_msg(request)); |
| 33 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) { |
| 34 return NULL; |
| 35 } |
| 36 |
| 37 return response.release(); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 namespace crashpad { |
| 43 |
| 44 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) { |
| 45 base::mac::ScopedLaunchData job_launch(CFPropertyToLaunchData(job_cf)); |
| 46 if (!job_launch.get()) { |
| 47 return false; |
| 48 } |
| 49 |
| 50 base::mac::ScopedLaunchData jobs(launch_data_alloc(LAUNCH_DATA_ARRAY)); |
| 51 launch_data_array_set_index(jobs, job_launch.release(), 0); |
| 52 |
| 53 base::mac::ScopedLaunchData request( |
| 54 launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
| 55 launch_data_dict_insert(request, jobs.release(), LAUNCH_KEY_SUBMITJOB); |
| 56 |
| 57 base::mac::ScopedLaunchData response(launch_msg(request)); |
| 58 |
| 59 if (launch_data_get_type(response) != LAUNCH_DATA_ARRAY) { |
| 60 return false; |
| 61 } |
| 62 |
| 63 if (launch_data_array_get_count(response) != 1) { |
| 64 return false; |
| 65 } |
| 66 |
| 67 launch_data_t response_element = launch_data_array_get_index(response, 0); |
| 68 if (launch_data_get_type(response_element) != LAUNCH_DATA_ERRNO) { |
| 69 return false; |
| 70 } |
| 71 |
| 72 int err = launch_data_get_errno(response_element); |
| 73 if (err != 0) { |
| 74 return false; |
| 75 } |
| 76 |
| 77 return true; |
| 78 } |
| 79 |
| 80 bool ServiceManagementRemoveJob(const std::string& label, bool wait) { |
| 81 base::mac::ScopedLaunchData request( |
| 82 launch_data_alloc(LAUNCH_DATA_DICTIONARY)); |
| 83 launch_data_dict_insert( |
| 84 request, launch_data_new_string(label.c_str()), LAUNCH_KEY_REMOVEJOB); |
| 85 |
| 86 base::mac::ScopedLaunchData response(launch_msg(request)); |
| 87 if (launch_data_get_type(response) != LAUNCH_DATA_ERRNO) { |
| 88 return false; |
| 89 } |
| 90 |
| 91 int err = launch_data_get_errno(response); |
| 92 if (err == EINPROGRESS) { |
| 93 if (wait) { |
| 94 // TODO(mark): Use a kqueue to wait for the process to exit. To avoid a |
| 95 // race, the kqueue would need to be set up prior to asking launchd to |
| 96 // remove the job. Even so, the job’s PID may change between the time it’s |
| 97 // obtained and the time the kqueue is set up, so this is nontrivial. |
| 98 do { |
| 99 timespec sleep_time; |
| 100 sleep_time.tv_sec = 0; |
| 101 sleep_time.tv_nsec = 1E5; // 100 microseconds |
| 102 nanosleep(&sleep_time, NULL); |
| 103 } while (ServiceManagementIsJobLoaded(label)); |
| 104 } |
| 105 |
| 106 return true; |
| 107 } |
| 108 |
| 109 if (err != 0) { |
| 110 return false; |
| 111 } |
| 112 |
| 113 return true; |
| 114 } |
| 115 |
| 116 bool ServiceManagementIsJobLoaded(const std::string& label) { |
| 117 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label)); |
| 118 if (!dictionary) { |
| 119 return false; |
| 120 } |
| 121 |
| 122 return true; |
| 123 } |
| 124 |
| 125 pid_t ServiceManagementIsJobRunning(const std::string& label) { |
| 126 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label)); |
| 127 if (!dictionary) { |
| 128 return 0; |
| 129 } |
| 130 |
| 131 launch_data_t pid = launch_data_dict_lookup(dictionary, LAUNCH_JOBKEY_PID); |
| 132 if (launch_data_get_type(pid) != LAUNCH_DATA_INTEGER) { |
| 133 return 0; |
| 134 } |
| 135 |
| 136 return launch_data_get_integer(pid); |
| 137 } |
| 138 |
| 139 } // namespace crashpad |
OLD | NEW |