Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: util/mac/service_management.cc

Issue 572323002: Drop 10.5 support: use ServiceManagement.framework directly (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/mac/service_management.h ('k') | util/mac/service_management_test.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
18 #include <launch.h> 17 #include <launch.h>
19 #include <time.h> 18 #include <ServiceManagement/ServiceManagement.h>
20 19
21 #include "base/mac/scoped_launch_data.h" 20 #include "base/mac/foundation_util.h"
22 #include "util/mac/launchd.h" 21 #include "base/mac/scoped_cftyperef.h"
22 #include "base/strings/sys_string_conversions.h"
23
24 // ServiceManagement.framework is available on 10.6 and later, but it’s
25 // deprecated in 10.10. In case ServiceManagement.framework stops working in the
26 // future, an alternative implementation using launch_msg() is available. This
27 // implementation works on 10.5 and later, however, launch_msg() is also
28 // deprecated in 10.10. The alternative implementation can be resurrected from
29 // source control history.
23 30
24 namespace { 31 namespace {
25 32
26 launch_data_t LaunchDataDictionaryForJob(const std::string& label) { 33 // Wraps the necessary functions from ServiceManagement.framework to avoid the
27 base::mac::ScopedLaunchData request( 34 // deprecation warnings when using the 10.10 SDK.
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 35
32 base::mac::ScopedLaunchData response(launch_msg(request)); 36 #pragma GCC diagnostic push
Robert Sesek 2014/09/16 16:00:26 Does clang interpret this GCC pragma and do the sa
Mark Mentovai 2014/09/16 21:39:02 rsesek wrote:
33 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) { 37 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 return NULL;
35 }
36 38
37 return response.release(); 39 Boolean CallSMJobSubmit(CFStringRef domain,
40 CFDictionaryRef job,
41 AuthorizationRef authorization,
42 CFErrorRef *error) {
43 return SMJobSubmit(domain, job, authorization, error);
38 } 44 }
39 45
46 Boolean CallSMJobRemove(CFStringRef domain,
47 CFStringRef job_label,
48 AuthorizationRef authorization,
49 Boolean wait,
50 CFErrorRef *error) {
51 return SMJobRemove(domain, job_label, authorization, wait, error);
52 }
53
54 CFDictionaryRef CallSMJobCopyDictionary(
55 CFStringRef domain, CFStringRef job_label) {
56 return SMJobCopyDictionary(domain, job_label);
57 }
58
59 #pragma GCC diagnostic pop
60
40 } // namespace 61 } // namespace
41 62
42 namespace crashpad { 63 namespace crashpad {
43 64
44 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) { 65 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) {
45 base::mac::ScopedLaunchData job_launch(CFPropertyToLaunchData(job_cf)); 66 return CallSMJobSubmit(kSMDomainUserLaunchd, job_cf, NULL, NULL);
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 } 67 }
79 68
80 bool ServiceManagementRemoveJob(const std::string& label, bool wait) { 69 bool ServiceManagementRemoveJob(const std::string& label, bool wait) {
81 base::mac::ScopedLaunchData request( 70 base::ScopedCFTypeRef<CFStringRef> label_cf(
82 launch_data_alloc(LAUNCH_DATA_DICTIONARY)); 71 base::SysUTF8ToCFStringRef(label));
83 launch_data_dict_insert( 72 return CallSMJobRemove(kSMDomainUserLaunchd, label_cf, NULL, wait, NULL);
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 } 73 }
115 74
116 bool ServiceManagementIsJobLoaded(const std::string& label) { 75 bool ServiceManagementIsJobLoaded(const std::string& label) {
117 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label)); 76 base::ScopedCFTypeRef<CFStringRef> label_cf(
118 if (!dictionary) { 77 base::SysUTF8ToCFStringRef(label));
119 return false; 78 base::ScopedCFTypeRef<CFDictionaryRef> job_dictionary(
120 } 79 CallSMJobCopyDictionary(kSMDomainUserLaunchd, label_cf));
121 80 return job_dictionary != NULL;
122 return true;
123 } 81 }
124 82
125 pid_t ServiceManagementIsJobRunning(const std::string& label) { 83 pid_t ServiceManagementIsJobRunning(const std::string& label) {
126 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label)); 84 base::ScopedCFTypeRef<CFStringRef> label_cf(
127 if (!dictionary) { 85 base::SysUTF8ToCFStringRef(label));
128 return 0; 86 base::ScopedCFTypeRef<CFDictionaryRef> job_dictionary(
87 CallSMJobCopyDictionary(kSMDomainUserLaunchd, label_cf));
88 if (job_dictionary != NULL) {
89 CFNumberRef pid_cf = base::mac::CFCast<CFNumberRef>(
90 CFDictionaryGetValue(job_dictionary, CFSTR(LAUNCH_JOBKEY_PID)));
91 if (pid_cf) {
92 pid_t pid;
93 if (CFNumberGetValue(pid_cf, kCFNumberIntType, &pid)) {
94 return pid;
95 }
96 }
129 } 97 }
130 98 return 0;
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 } 99 }
138 100
139 } // namespace crashpad 101 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/mac/service_management.h ('k') | util/mac/service_management_test.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698