2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * bootstrap -- fundamental service initiator and port server
26 * Mike DeMoney, NeXT, Inc.
27 * Copyright, 1990. All rights reserved.
29 * lists.c -- implementation of list handling routines
32 #include <mach/boolean.h>
33 #include <mach/mach_error.h>
39 #include <bsm/audit.h>
41 #include "bootstrap_internal.h"
47 bootstrap_info_t bootstraps
; /* head of list of all bootstrap ports */
48 server_t servers
; /* head of list of all servers */
49 service_t services
; /* head of list of all services */
50 unsigned nservices
; /* number of services in list */
59 #define NEW(type, num) ((type *)ckmalloc(sizeof(type) * num))
60 #define STREQ(a, b) (strcmp(a, b) == 0)
61 #define NELEM(x) (sizeof(x)/sizeof((x)[0]))
62 #define LAST_ELEMENT(x) ((x)[NELEM(x)-1])
67 bootstraps
.ref_count
= 2; /* make sure we never deallocate this one */
68 bootstraps
.next
= bootstraps
.prev
= &bootstraps
;
69 bootstraps
.parent
= &bootstraps
;
70 servers
.next
= servers
.prev
= &servers
;
71 services
.next
= services
.prev
= &services
;
77 bootstrap_info_t
*bootstrap
,
80 servertype_t servertype
,
85 syslog(LOG_DEBUG
, "adding new server \"%s\" with uid %d", cmd
, uid
);
86 serverp
= NEW(server_t
, 1);
87 if (serverp
!= NULL
) {
88 /* Doubly linked list */
89 servers
.prev
->next
= serverp
;
90 serverp
->prev
= servers
.prev
;
91 serverp
->next
= &servers
;
92 servers
.prev
= serverp
;
94 bootstrap
->ref_count
++;
95 serverp
->bootstrap
= bootstrap
;
97 serverp
->pid
= NO_PID
;
98 serverp
->task_port
= MACH_PORT_NULL
;
101 serverp
->auinfo
= auinfo
;
103 serverp
->port
= MACH_PORT_NULL
;
104 serverp
->servertype
= servertype
;
105 serverp
->activity
= 0;
106 serverp
->active_services
= 0;
107 strncpy(serverp
->cmd
, cmd
, sizeof serverp
->cmd
);
108 LAST_ELEMENT(serverp
->cmd
) = '\0';
115 bootstrap_info_t
*bootstrap
,
117 mach_port_t serviceport
,
119 servicetype_t servicetype
,
124 servicep
= NEW(service_t
, 1);
125 if (servicep
!= NULL
) {
126 /* Doubly linked list */
127 services
.prev
->next
= servicep
;
128 servicep
->prev
= services
.prev
;
129 servicep
->next
= &services
;
130 services
.prev
= servicep
;
134 strncpy(servicep
->name
, name
, sizeof servicep
->name
);
135 LAST_ELEMENT(servicep
->name
) = '\0';
136 servicep
->servicetype
= servicetype
;
137 servicep
->bootstrap
= bootstrap
;
138 servicep
->port
= serviceport
;
139 servicep
->server
= serverp
;
140 servicep
->isActive
= isActive
;
147 bootstrap_info_t
*parent
,
148 mach_port_t bootstrapport
,
149 mach_port_t requestorport
)
151 bootstrap_info_t
*bootstrap
;
153 bootstrap
= NEW(bootstrap_info_t
, 1);
154 if (bootstrap
!= NULL
) {
155 /* Doubly linked list */
156 bootstraps
.prev
->next
= bootstrap
;
157 bootstrap
->prev
= bootstraps
.prev
;
158 bootstrap
->next
= &bootstraps
;
159 bootstraps
.prev
= bootstrap
;
161 bootstrap
->bootstrap_port
= bootstrapport
;
162 bootstrap
->requestor_port
= requestorport
;
164 bootstrap
->ref_count
= 1;
165 bootstrap
->parent
= parent
;
172 lookup_bootstrap_by_port(mach_port_t port
)
174 bootstrap_info_t
*bootstrap
;
175 bootstrap_info_t
*first
;
178 bootstrap
= first
= FIRST(bootstraps
);
180 if (bootstrap
->bootstrap_port
== port
)
182 bootstrap
= NEXT(bootstrap
);
183 } while (bootstrap
!= first
);
185 for ( serverp
= FIRST(servers
)
186 ; !IS_END(serverp
, servers
)
187 ; serverp
= NEXT(serverp
))
189 if (port
== serverp
->port
)
190 return serverp
->bootstrap
;
196 lookup_bootstrap_by_req_port(mach_port_t port
)
198 bootstrap_info_t
*bootstrap
;
200 for ( bootstrap
= FIRST(bootstraps
)
201 ; !IS_END(bootstrap
, bootstraps
)
202 ; bootstrap
= NEXT(bootstrap
))
204 if (bootstrap
->requestor_port
== port
)
212 lookup_service_by_name(bootstrap_info_t
*bootstrap
, name_t name
)
218 for ( servicep
= FIRST(services
)
219 ; !IS_END(servicep
, services
)
220 ; servicep
= NEXT(servicep
))
222 if (!STREQ(name
, servicep
->name
))
224 if (bootstrap
&& servicep
->bootstrap
!= bootstrap
)
228 } while (bootstrap
!= &bootstraps
&&
229 (bootstrap
= bootstrap
->parent
));
234 unlink_service(service_t
*servicep
)
236 ASSERT(servicep
->prev
->next
== servicep
);
237 ASSERT(servicep
->next
->prev
== servicep
);
238 servicep
->prev
->next
= servicep
->next
;
239 servicep
->next
->prev
= servicep
->prev
;
240 servicep
->prev
= servicep
->next
= servicep
; // idempotent
244 delete_service(service_t
*servicep
)
246 unlink_service(servicep
);
247 switch (servicep
->servicetype
) {
249 syslog(LOG_INFO
, "Registered service %s deleted", servicep
->name
);
250 mach_port_deallocate(mach_task_self(), servicep
->port
);
253 syslog(LOG_INFO
, "Declared service %s now unavailable", servicep
->name
);
254 mach_port_deallocate(mach_task_self(), servicep
->port
);
255 mach_port_mod_refs(mach_task_self(), servicep
->port
,
256 MACH_PORT_RIGHT_RECEIVE
, -1);
259 syslog(LOG_ERR
, "unknown service type %d", servicep
->servicetype
);
267 delete_bootstrap_services(bootstrap_info_t
*bootstrap
)
273 for ( servicep
= FIRST(services
)
274 ; !IS_END(servicep
, services
)
277 next
= NEXT(servicep
);
278 if (bootstrap
!= servicep
->bootstrap
)
281 serverp
= servicep
->server
;
283 if (servicep
->isActive
&& serverp
)
284 serverp
->active_services
--;
286 delete_service(servicep
);
290 if (!active_server(serverp
))
291 delete_server(serverp
);
296 lookup_service_by_port(mach_port_t port
)
300 for ( servicep
= FIRST(services
)
301 ; !IS_END(servicep
, services
)
302 ; servicep
= NEXT(servicep
))
304 if (port
== servicep
->port
)
311 lookup_service_by_server(server_t
*serverp
)
315 for ( servicep
= FIRST(services
)
316 ; !IS_END(servicep
, services
)
317 ; servicep
= NEXT(servicep
))
319 if (serverp
== servicep
->server
)
326 lookup_server_by_task_port(mach_port_t port
)
330 for ( serverp
= FIRST(servers
)
331 ; !IS_END(serverp
, servers
)
332 ; serverp
= NEXT(serverp
))
334 if (port
== serverp
->task_port
)
341 lookup_server_by_port(mach_port_t port
)
345 for ( serverp
= FIRST(servers
)
346 ; !IS_END(serverp
, servers
)
347 ; serverp
= NEXT(serverp
))
349 if (port
== serverp
->port
)
356 delete_server(server_t
*serverp
)
361 syslog(LOG_INFO
, "Deleting server %s", serverp
->cmd
);
362 ASSERT(serverp
->prev
->next
== serverp
);
363 ASSERT(serverp
->next
->prev
== serverp
);
364 serverp
->prev
->next
= serverp
->next
;
365 serverp
->next
->prev
= serverp
->prev
;
367 for ( servicep
= FIRST(services
)
368 ; !IS_END(servicep
, services
)
371 next
= NEXT(servicep
);
372 if (serverp
== servicep
->server
)
373 delete_service(servicep
);
376 deallocate_bootstrap(serverp
->bootstrap
);
379 mach_port_mod_refs(mach_task_self(), serverp
->port
,
380 MACH_PORT_RIGHT_RECEIVE
, -1);
386 deactivate_bootstrap(bootstrap_info_t
*bootstrap
)
388 bootstrap_info_t
*deactivating_bootstraps
;
389 bootstrap_info_t
*query_bootstrap
;
390 bootstrap_info_t
*next_limit
;
391 bootstrap_info_t
*limit
;
394 * we need to recursively deactivate the whole subset tree below
395 * this point. But we don't want to do real recursion because
396 * we don't have a limit on the depth. So, build up a chain of
397 * active bootstraps anywhere underneath this one.
399 deactivating_bootstraps
= bootstrap
;
400 bootstrap
->deactivate
= NULL
;
401 for (next_limit
= deactivating_bootstraps
, limit
= NULL
402 ; deactivating_bootstraps
!= limit
403 ; limit
= next_limit
, next_limit
= deactivating_bootstraps
)
405 for (bootstrap
= deactivating_bootstraps
407 ; bootstrap
= bootstrap
->deactivate
)
409 for ( query_bootstrap
= FIRST(bootstraps
)
410 ; !IS_END(query_bootstrap
, bootstraps
)
411 ; query_bootstrap
= NEXT(query_bootstrap
))
413 if (query_bootstrap
->parent
== bootstrap
&&
414 query_bootstrap
->requestor_port
!= MACH_PORT_NULL
) {
415 mach_port_deallocate(
417 query_bootstrap
->requestor_port
);
418 query_bootstrap
->requestor_port
= MACH_PORT_NULL
;
419 query_bootstrap
->deactivate
= deactivating_bootstraps
;
420 deactivating_bootstraps
= query_bootstrap
;
427 * The list is ordered with the furthest away progeny being
428 * at the front, and concluding with the one we started with.
429 * This allows us to safely deactivate and remove the reference
430 * each holds on their parent without fear of the chain getting
431 * corrupted (because each active parent holds a reference on
432 * itself and that doesn't get removed until we reach its spot
436 bootstrap
= deactivating_bootstraps
;
437 deactivating_bootstraps
= bootstrap
->deactivate
;
439 syslog(LOG_INFO
, "deactivating bootstrap %x", bootstrap
->bootstrap_port
);
441 delete_bootstrap_services(bootstrap
);
443 mach_port_deallocate(mach_task_self(), bootstrap
->bootstrap_port
);
446 mach_port_t previous
;
447 mach_port_request_notification(
449 bootstrap
->bootstrap_port
,
450 MACH_NOTIFY_NO_SENDERS
,
452 bootstrap
->bootstrap_port
,
453 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
456 } while (deactivating_bootstraps
!= NULL
);
460 deallocate_bootstrap(bootstrap_info_t
*bootstrap
)
462 ASSERT(bootstrap
->prev
->next
== bootstrap
);
463 ASSERT(bootstrap
->next
->prev
== bootstrap
);
464 if (--bootstrap
->ref_count
> 0)
467 bootstrap
->prev
->next
= bootstrap
->next
;
468 bootstrap
->next
->prev
= bootstrap
->prev
;
469 deallocate_bootstrap(bootstrap
->parent
);
474 ckmalloc(unsigned nbytes
)
478 if ((cp
= malloc(nbytes
)) == NULL
) {
479 syslog(LOG_EMERG
, "malloc(): %m");