]> git.saurik.com Git - apple/libc.git/blob - threads/lu_utils.c
850835563f6bd22979770977cc79c2fb2023d7a8
[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 static name_t LOOKUP_NAME = "lookup daemon v2";
43
44 mach_port_t _lookupd_port(mach_port_t port) {
45 if (port != MACH_PORT_NULL) {
46 kern_return_t ret;
47 mach_port_t bp;
48 ret = task_get_bootstrap_port(mach_task_self(), &bp);
49 if (ret != KERN_SUCCESS) {
50 mach_error("task_get_bootstrap_port() failed", ret);
51 abort();
52 }
53 if (bp == MACH_PORT_NULL) {
54 mach_error("task_get_bootstrap_port() returned MACH_PORT_NULL!", 0);
55 return MACH_PORT_NULL;
56 }
57 ret = bootstrap_register(bp, LOOKUP_NAME, port);
58 if (ret != BOOTSTRAP_SUCCESS) {
59 mach_error("bootstrap_register() failed", ret);
60 abort();
61 }
62 return port;
63 }
64 return _lu_port;
65 }
66
67 /* called as child starts up. mach ports aren't inherited: trash cache */
68 void
69 _lu_fork_child()
70 {
71 mach_port_t bp;
72 kern_return_t ret;
73 int pid = getpid();
74
75 _lu_port = MACH_PORT_NULL;
76 if (pid > 2) {
77 ret = task_get_bootstrap_port(mach_task_self(), &bp);
78 if (ret != KERN_SUCCESS) {
79 mach_error("task_get_bootstrap_port() failed", ret);
80 abort();
81 }
82 if (bp == MACH_PORT_NULL) {
83 fprintf(stderr, "task_get_bootstrap_port() returned MACH_PORT_NULL!\n");
84 return;
85 }
86 ret = bootstrap_look_up(bp, LOOKUP_NAME, &_lu_port);
87 if (ret != BOOTSTRAP_SUCCESS && ret != BOOTSTRAP_UNKNOWN_SERVICE) {
88 mach_error("bootstrap_look_up() failed", ret);
89 _lu_port = MACH_PORT_NULL;
90 }
91 }
92 }
93
94 void
95 _lu_setport(mach_port_t desired)
96 {
97 if (_lu_port != MACH_PORT_NULL) {
98 mach_port_deallocate(mach_task_self(), _lu_port);
99 }
100 _lu_port = desired;
101 }
102
103 static int
104 port_valid(mach_port_t port)
105 {
106 mach_port_type_t ptype;
107 kern_return_t ret;
108
109 ret = mach_port_type(mach_task_self(), port, &ptype);
110 if (ret != KERN_SUCCESS) {
111 mach_error("mach_port_type() failed", ret);
112 return 0;
113 }
114 return 1;
115 }
116
117 int
118 _lu_running(void)
119 {
120 if (_lu_port == MACH_PORT_NULL) {
121 _lu_fork_child();
122 }
123 if (_lu_port != MACH_PORT_NULL) {
124 if (port_valid(_lu_port)) {
125 return (1);
126 }
127 _lu_fork_child();
128 if (port_valid(_lu_port)) {
129 return (1);
130 }
131 }
132 return 0;
133 }
134