]> git.saurik.com Git - apple/launchd.git/blame - launchd/src/libbootstrap.c
launchd-258.1.tar.gz
[apple/launchd.git] / launchd / src / libbootstrap.c
CommitLineData
5b0a4722
A
1/*
2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21#include "config.h"
22#include "libbootstrap_public.h"
23#include "libbootstrap_private.h"
24
25#include "libvproc_public.h"
26#include "libvproc_private.h"
27
28#include <mach/mach.h>
29#include <mach/vm_map.h>
30#include <sys/types.h>
31#include <sys/syslog.h>
32#include <sys/stat.h>
33#include <pthread.h>
34
35#include "protocol_vproc.h"
36
37kern_return_t
38bootstrap_create_server(mach_port_t bp, cmd_t server_cmd, uid_t server_uid, boolean_t on_demand, mach_port_t *server_port)
39{
40 return vproc_mig_create_server(bp, server_cmd, server_uid, on_demand, server_port);
41}
42
43kern_return_t
44bootstrap_subset(mach_port_t bp, mach_port_t requestor_port, mach_port_t *subset_port)
45{
46 return vproc_mig_subset(bp, requestor_port, subset_port);
47}
48
49kern_return_t
50bootstrap_unprivileged(mach_port_t bp, mach_port_t *unpriv_port)
51{
52 kern_return_t kr;
53
54 *unpriv_port = MACH_PORT_NULL;
55
56 kr = mach_port_mod_refs(mach_task_self(), bp, MACH_PORT_RIGHT_SEND, 1);
57
58 if (kr == KERN_SUCCESS) {
59 *unpriv_port = bp;
60 }
61
62 return kr;
63}
64
65kern_return_t
66bootstrap_parent(mach_port_t bp, mach_port_t *parent_port)
67{
68 return vproc_mig_parent(bp, parent_port);
69}
70
71kern_return_t
72bootstrap_set_policy(mach_port_t bp, pid_t target_pid, uint64_t flags, const char *target_service)
73{
74 return vproc_mig_set_service_policy(bp, target_pid, flags, target_service ? (char *)target_service : "");
75}
76
77kern_return_t
78bootstrap_register(mach_port_t bp, name_t service_name, mach_port_t sp)
79{
80 return bootstrap_register2(bp, service_name, sp, 0);
81}
82
83kern_return_t
84bootstrap_register2(mach_port_t bp, name_t service_name, mach_port_t sp, uint64_t flags)
85{
86 kern_return_t kr = vproc_mig_register2(bp, service_name, sp, flags);
87
88 if (kr == VPROC_ERR_TRY_PER_USER) {
89 mach_port_t puc;
90
91 if (vproc_mig_lookup_per_user_context(bp, 0, &puc) == 0) {
92 kr = vproc_mig_register2(puc, service_name, sp, flags);
93 mach_port_deallocate(mach_task_self(), puc);
94 }
95 }
96
97 return kr;
98}
99
100kern_return_t
101bootstrap_create_service(mach_port_t bp, name_t service_name, mach_port_t *sp)
102{
103 return vproc_mig_create_service(bp, service_name, sp);
104}
105
106kern_return_t
107bootstrap_check_in(mach_port_t bp, name_t service_name, mach_port_t *sp)
108{
109 return vproc_mig_check_in(bp, service_name, sp);
110}
111
112kern_return_t
113bootstrap_look_up_per_user(mach_port_t bp, name_t service_name, uid_t target_user, mach_port_t *sp)
114{
115 struct stat sb;
116 kern_return_t kr;
117 mach_port_t puc;
118
119 if (pthread_main_np() && (stat("/AppleInternal", &sb) != -1)) {
120 _vproc_log(LOG_WARNING, "Please review the comments in 4890134.");
121 }
122
123 if ((kr = vproc_mig_lookup_per_user_context(bp, target_user, &puc)) != 0) {
124 return kr;
125 }
126
127 kr = vproc_mig_look_up2(puc, service_name, sp, 0, 0);
128 mach_port_deallocate(mach_task_self(), puc);
129
130 return kr;
131}
132
133
134kern_return_t
135bootstrap_look_up(mach_port_t bp, name_t service_name, mach_port_t *sp)
136{
137 return bootstrap_look_up2(bp, service_name, sp, 0, 0);
138}
139
140kern_return_t
141bootstrap_look_up2(mach_port_t bp, name_t service_name, mach_port_t *sp, pid_t target_pid, uint64_t flags)
142{
143 kern_return_t kr;
144 mach_port_t puc;
145
146 if ((kr = vproc_mig_look_up2(bp, service_name, sp, target_pid, flags)) != VPROC_ERR_TRY_PER_USER) {
147 return kr;
148 }
149
150 if ((kr = vproc_mig_lookup_per_user_context(bp, 0, &puc)) != 0) {
151 return kr;
152 }
153
154 kr = vproc_mig_look_up2(puc, service_name, sp, target_pid, flags);
155 mach_port_deallocate(mach_task_self(), puc);
156
157 return kr;
158}
159
160kern_return_t
161bootstrap_status(mach_port_t bp, name_t service_name, bootstrap_status_t *service_active)
162{
163 mach_port_t p;
164
165 if (bootstrap_check_in(bp, service_name, &p) == BOOTSTRAP_SUCCESS) {
166 mach_port_mod_refs(mach_task_self(), p, MACH_PORT_RIGHT_RECEIVE, -1);
167 *service_active = BOOTSTRAP_STATUS_ON_DEMAND;
168 return BOOTSTRAP_SUCCESS;
169 } else if (bootstrap_look_up(bp, service_name, &p) == BOOTSTRAP_SUCCESS) {
170 mach_port_deallocate(mach_task_self(), p);
171 *service_active = BOOTSTRAP_STATUS_ACTIVE;
172 return BOOTSTRAP_SUCCESS;
173 }
174
175 return BOOTSTRAP_UNKNOWN_SERVICE;
176}
177
178kern_return_t
179bootstrap_info(mach_port_t bp,
180 name_array_t *service_names, mach_msg_type_number_t *service_namesCnt,
181 bootstrap_status_array_t *service_active, mach_msg_type_number_t *service_activeCnt)
182{
183 return vproc_mig_info(bp, service_names, service_namesCnt,
184 service_active, service_activeCnt);
185}
186
187const char *
188bootstrap_strerror(kern_return_t r)
189{
190 switch (r) {
191 case BOOTSTRAP_SUCCESS:
192 return "Success";
193 case BOOTSTRAP_NOT_PRIVILEGED:
194 return "Permission denied";
195 case BOOTSTRAP_NAME_IN_USE:
196 case BOOTSTRAP_SERVICE_ACTIVE:
197 return "Service name already exists";
198 case BOOTSTRAP_UNKNOWN_SERVICE:
199 return "Unknown service name";
200 case BOOTSTRAP_BAD_COUNT:
201 return "Too many lookups were requested in one request";
202 case BOOTSTRAP_NO_MEMORY:
203 return "Out of memory";
204 default:
205 return mach_error_string(r);
206 }
207}