]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. | |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
22 | * | |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
e9ce8d39 A |
25 | /* |
26 | * Port and memory management for doing lookups to the lookup server | |
27 | * Copyright (C) 1989 by NeXT, Inc. | |
28 | */ | |
29 | /* | |
30 | * HISTORY | |
31 | * 27-Mar-90 Gregg Kellogg (gk) at NeXT | |
32 | * Changed to use bootstrap port instead of service port. | |
33 | * | |
34 | */ | |
35 | #include <stdio.h> | |
9385eb3d A |
36 | #include <stdlib.h> |
37 | #include <sys/types.h> | |
38 | #include <unistd.h> | |
e9ce8d39 | 39 | #include <mach/mach.h> |
9385eb3d | 40 | #include <mach/error.h> |
e9ce8d39 A |
41 | #include <servers/bootstrap.h> |
42 | ||
43 | mach_port_t _lu_port = MACH_PORT_NULL; | |
5b2abdfb | 44 | static name_t LOOKUP_NAME = "lookup daemon v2"; |
e9ce8d39 A |
45 | |
46 | mach_port_t _lookupd_port(mach_port_t port) { | |
47 | if (port != MACH_PORT_NULL) { | |
48 | kern_return_t ret; | |
49 | mach_port_t bp; | |
50 | ret = task_get_bootstrap_port(mach_task_self(), &bp); | |
51 | if (ret != KERN_SUCCESS) { | |
52 | mach_error("task_get_bootstrap_port() failed", ret); | |
53 | abort(); | |
54 | } | |
55 | if (bp == MACH_PORT_NULL) { | |
56 | mach_error("task_get_bootstrap_port() returned MACH_PORT_NULL!", 0); | |
57 | return MACH_PORT_NULL; | |
58 | } | |
59 | ret = bootstrap_register(bp, LOOKUP_NAME, port); | |
60 | if (ret != BOOTSTRAP_SUCCESS) { | |
61 | mach_error("bootstrap_register() failed", ret); | |
62 | abort(); | |
63 | } | |
64 | return port; | |
65 | } | |
66 | return _lu_port; | |
67 | } | |
68 | ||
69 | /* called as child starts up. mach ports aren't inherited: trash cache */ | |
70 | void | |
71 | _lu_fork_child() | |
72 | { | |
73 | mach_port_t bp; | |
74 | kern_return_t ret; | |
75 | int pid = getpid(); | |
76 | ||
77 | _lu_port = MACH_PORT_NULL; | |
78 | if (pid > 2) { | |
79 | ret = task_get_bootstrap_port(mach_task_self(), &bp); | |
80 | if (ret != KERN_SUCCESS) { | |
81 | mach_error("task_get_bootstrap_port() failed", ret); | |
82 | abort(); | |
83 | } | |
84 | if (bp == MACH_PORT_NULL) { | |
85 | fprintf(stderr, "task_get_bootstrap_port() returned MACH_PORT_NULL!\n"); | |
86 | return; | |
87 | } | |
88 | ret = bootstrap_look_up(bp, LOOKUP_NAME, &_lu_port); | |
89 | if (ret != BOOTSTRAP_SUCCESS && ret != BOOTSTRAP_UNKNOWN_SERVICE) { | |
90 | mach_error("bootstrap_look_up() failed", ret); | |
91 | _lu_port = MACH_PORT_NULL; | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | void | |
97 | _lu_setport(mach_port_t desired) | |
98 | { | |
99 | if (_lu_port != MACH_PORT_NULL) { | |
100 | mach_port_deallocate(mach_task_self(), _lu_port); | |
101 | } | |
102 | _lu_port = desired; | |
103 | } | |
104 | ||
105 | static int | |
106 | port_valid(mach_port_t port) | |
107 | { | |
108 | mach_port_type_t ptype; | |
109 | kern_return_t ret; | |
110 | ||
111 | ret = mach_port_type(mach_task_self(), port, &ptype); | |
112 | if (ret != KERN_SUCCESS) { | |
113 | mach_error("mach_port_type() failed", ret); | |
114 | return 0; | |
115 | } | |
116 | return 1; | |
117 | } | |
118 | ||
119 | int | |
120 | _lu_running(void) | |
121 | { | |
122 | if (_lu_port == MACH_PORT_NULL) { | |
123 | _lu_fork_child(); | |
124 | } | |
125 | if (_lu_port != MACH_PORT_NULL) { | |
126 | if (port_valid(_lu_port)) { | |
127 | return (1); | |
128 | } | |
129 | _lu_fork_child(); | |
130 | if (port_valid(_lu_port)) { | |
131 | return (1); | |
132 | } | |
133 | } | |
134 | return 0; | |
135 | } | |
136 |