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