]> git.saurik.com Git - apple/libc.git/blob - threads/lu_utils.c
Libc-594.9.4.tar.gz
[apple/libc.git] / threads / lu_utils.c
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Port and memory management for doing lookups to the lookup server
25 * Copyright (C) 1989 by NeXT, Inc.
26 */
27 /*
28 * HISTORY
29 * 27-Mar-90 Gregg Kellogg (gk) at NeXT
30 * Changed to use bootstrap port instead of service port.
31 *
32 */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <mach/mach.h>
38 #include <mach/error.h>
39 #include <servers/bootstrap.h>
40
41 mach_port_t _lu_port = MACH_PORT_NULL;
42 mach_port_t _ds_port = MACH_PORT_NULL;
43 mach_port_t _mbr_port = MACH_PORT_NULL;
44
45 static name_t LOOKUP_NAME = "lookup daemon v2";
46
47 #ifndef kDSStdMachDSLookupPortName
48 #define kDSStdMachDSLookupPortName "com.apple.system.DirectoryService.libinfo_v1"
49 #define kDSStdMachDSMembershipPortName "com.apple.system.DirectoryService.membership_v1"
50 #endif
51
52 mach_port_t
53 _lookupd_port(mach_port_t port)
54 {
55 kern_return_t status;
56
57 if (port != MACH_PORT_NULL)
58 {
59 status = bootstrap_register(bootstrap_port, LOOKUP_NAME, port);
60 if (status != BOOTSTRAP_SUCCESS) abort();
61
62 return port;
63 }
64 else if ((_lu_port == MACH_PORT_NULL) && (getpid() > 1))
65 {
66 status = bootstrap_look_up(bootstrap_port, LOOKUP_NAME, &_lu_port);
67 if ((status != BOOTSTRAP_SUCCESS) && (status != BOOTSTRAP_UNKNOWN_SERVICE)) _lu_port = MACH_PORT_NULL;
68 }
69
70 return _lu_port;
71 }
72
73 /* called as child starts up. mach ports aren't inherited: trash cache */
74 void
75 _lu_fork_child()
76 {
77 _lu_port = MACH_PORT_NULL;
78 _ds_port = MACH_PORT_NULL;
79 _mbr_port = MACH_PORT_NULL;
80 }
81
82 void
83 _lu_setport(mach_port_t desired)
84 {
85 if (_lu_port != MACH_PORT_NULL) mach_port_deallocate(mach_task_self(), _lu_port);
86 _lu_port = desired;
87 }
88
89 int
90 _lu_running(void)
91 {
92 return ((_lu_port != MACH_PORT_NULL) || (_lookupd_port(MACH_PORT_NULL) != MACH_PORT_NULL));
93 }
94
95 int
96 _ds_running(void)
97 {
98 kern_return_t status;
99
100 if (_ds_port != MACH_PORT_NULL) return 1;
101
102 status = bootstrap_look_up(bootstrap_port, kDSStdMachDSLookupPortName, &_ds_port);
103 if ((status != BOOTSTRAP_SUCCESS) && (status != BOOTSTRAP_UNKNOWN_SERVICE)) _ds_port = MACH_PORT_NULL;
104
105 status = bootstrap_look_up(bootstrap_port, kDSStdMachDSMembershipPortName, &_mbr_port);
106 if ((status != BOOTSTRAP_SUCCESS) && (status != BOOTSTRAP_UNKNOWN_SERVICE)) _mbr_port = MACH_PORT_NULL;
107
108 return (_ds_port != MACH_PORT_NULL);
109 }