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

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

Issue 467923003: Add ServiceManagement*() and their tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@process_util
Patch Set: Created 6 years, 4 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
OLDNEW
(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 <launch.h>
18 #include <time.h>
19
20 #include "base/mac/foundation_util.h"
21 #include "base/mac/scoped_cftyperef.h"
22 #include "base/mac/scoped_launch_data.h"
23 #include "base/strings/sys_string_conversions.h"
24 #include "util/mac/launchd.h"
25
26 namespace {
27
28 launch_data_t LaunchDataDictionaryForJob(const std::string& label) {
29 base::mac::ScopedLaunchData request(
30 launch_data_alloc(LAUNCH_DATA_DICTIONARY));
31 launch_data_dict_insert(
32 request, launch_data_new_string(label.c_str()), LAUNCH_KEY_GETJOB);
33
34 base::mac::ScopedLaunchData response(launch_msg(request));
35 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) {
36 return NULL;
37 }
38
39 return response.release();
40 }
41
42 } // namespace
43
44 namespace crashpad {
45
46 bool ServiceManagementSubmitJob(CFDictionaryRef job_cf) {
47 base::mac::ScopedLaunchData job_launch(CFPropertyToLaunchData(job_cf));
48 if (!job_launch.get()) {
49 return false;
50 }
51
52 base::mac::ScopedLaunchData jobs(launch_data_alloc(LAUNCH_DATA_ARRAY));
53 launch_data_array_set_index(jobs, job_launch.release(), 0);
54
55 base::mac::ScopedLaunchData request(
56 launch_data_alloc(LAUNCH_DATA_DICTIONARY));
57 launch_data_dict_insert(request, jobs.release(), LAUNCH_KEY_SUBMITJOB);
58
59 base::mac::ScopedLaunchData response(launch_msg(request));
60
61 if (launch_data_get_type(response) != LAUNCH_DATA_ARRAY) {
62 return false;
63 }
64
65 if (launch_data_array_get_count(response) != 1) {
66 return false;
67 }
68
69 launch_data_t response_element = launch_data_array_get_index(response, 0);
70 if (launch_data_get_type(response_element) != LAUNCH_DATA_ERRNO) {
71 return false;
72 }
73
74 int err = launch_data_get_errno(response_element);
75 if (err != 0) {
76 return false;
77 }
78
79 return true;
80 }
81
82 bool ServiceManagementRemoveJob(const std::string& label, bool wait) {
83 base::mac::ScopedLaunchData request(
84 launch_data_alloc(LAUNCH_DATA_DICTIONARY));
85 launch_data_dict_insert(
86 request, launch_data_new_string(label.c_str()), LAUNCH_KEY_REMOVEJOB);
87
88 base::mac::ScopedLaunchData response(launch_msg(request));
89 if (launch_data_get_type(response) != LAUNCH_DATA_ERRNO) {
90 return false;
91 }
92
93 int err = launch_data_get_errno(response);
94 if (err == EINPROGRESS) {
95 if (wait) {
96 // TODO(mark): Use a kqueue to wait for the process to exit. To avoid a
97 // race, the kqueue would need to be set up prior to asking launchd to
98 // remove the job. Even so, the job’s PID may change between the time it’s
99 // obtained and the time the kqueue is set up, so this is nontrivial.
100 do {
101 timespec sleep_time;
102 sleep_time.tv_sec = 0;
103 sleep_time.tv_nsec = 1E5; // 100 microseconds
104 nanosleep(&sleep_time, NULL);
105 } while (ServiceManagementIsJobLoaded(label));
106 }
107
108 return true;
109 }
110
111 if (err != 0) {
112 return false;
113 }
114
115 return true;
116 }
117
118 bool ServiceManagementIsJobLoaded(const std::string& label) {
119 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
120 if (!dictionary) {
121 return false;
122 }
123
124 return true;
125 }
126
127 pid_t ServiceManagementIsJobRunning(const std::string& label) {
128 base::mac::ScopedLaunchData dictionary(LaunchDataDictionaryForJob(label));
129 if (!dictionary) {
130 return 0;
131 }
132
133 launch_data_t pid = launch_data_dict_lookup(dictionary, LAUNCH_JOBKEY_PID);
134 if (launch_data_get_type(pid) != LAUNCH_DATA_INTEGER) {
135 return 0;
136 }
137
138 return launch_data_get_integer(pid);
139 }
140
141 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698