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

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

Issue 579083003: Revert "Drop 10.5 support: use ServiceManagement.framework directly." (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Use an autorelease pool in the CFPropertyToLaunchData() implementation 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>
17 #include <launch.h> 18 #include <launch.h>
18 #include <ServiceManagement/ServiceManagement.h> 19 #include <time.h>
19 20
20 #include "base/mac/foundation_util.h" 21 #include "base/mac/scoped_launch_data.h"
21 #include "base/mac/scoped_cftyperef.h" 22 #include "util/mac/launchd.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.
30 23
31 namespace { 24 namespace {
32 25
33 // Wraps the necessary functions from ServiceManagement.framework to avoid the 26 launch_data_t LaunchDataDictionaryForJob(const std::string& label) {
34 // deprecation warnings when using the 10.10 SDK. 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);
35 31
36 #pragma GCC diagnostic push 32 base::mac::ScopedLaunchData response(launch_msg(request));
37 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 33 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) {
34 return NULL;
35 }
38 36
39 Boolean CallSMJobSubmit(CFStringRef domain, 37 return response.release();
40 CFDictionaryRef job,
41 AuthorizationRef authorization,
42 CFErrorRef *error) {
43 return SMJobSubmit(domain, job, authorization, error);
44 } 38 }
45 39
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
61 } // namespace 40 } // namespace
62 41
63 namespace crashpad { 42 namespace crashpad {
64 43
65 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) { 44 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) {
66 return CallSMJobSubmit(kSMDomainUserLaunchd, job_cf, NULL, NULL); 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;
67 } 78 }
68 79
69 bool ServiceManagementRemoveJob(const std::string& label, bool wait) { 80 bool ServiceManagementRemoveJob(const std::string& label, bool wait) {
70 base::ScopedCFTypeRef<CFStringRef> label_cf( 81 base::mac::ScopedLaunchData request(
71 base::SysUTF8ToCFStringRef(label)); 82 launch_data_alloc(LAUNCH_DATA_DICTIONARY));
72 return CallSMJobRemove(kSMDomainUserLaunchd, label_cf, NULL, wait, NULL); 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;
73 } 114 }
74 115
75 bool ServiceManagementIsJobLoaded(const std::string& label) { 116 bool ServiceManagementIsJobLoaded(const std::string& label) {
76 base::ScopedCFTypeRef<CFStringRef> label_cf( 117 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
77 base::SysUTF8ToCFStringRef(label)); 118 if (!dictionary) {
78 base::ScopedCFTypeRef<CFDictionaryRef> job_dictionary( 119 return false;
79 CallSMJobCopyDictionary(kSMDomainUserLaunchd, label_cf)); 120 }
80 return job_dictionary != NULL; 121
122 return true;
81 } 123 }
82 124
83 pid_t ServiceManagementIsJobRunning(const std::string& label) { 125 pid_t ServiceManagementIsJobRunning(const std::string& label) {
84 base::ScopedCFTypeRef<CFStringRef> label_cf( 126 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
85 base::SysUTF8ToCFStringRef(label)); 127 if (!dictionary) {
86 base::ScopedCFTypeRef<CFDictionaryRef> job_dictionary( 128 return 0;
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 }
97 } 129 }
98 return 0; 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);
99 } 137 }
100 138
101 } // namespace crashpad 139 } // 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