]> git.saurik.com Git - apple/libc.git/blame - threads/lu_utils.c
Libc-391.2.5.tar.gz
[apple/libc.git] / threads / lu_utils.c
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9385eb3d
A
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 */
e9ce8d39
A
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>
9385eb3d
A
34#include <stdlib.h>
35#include <sys/types.h>
36#include <unistd.h>
e9ce8d39 37#include <mach/mach.h>
9385eb3d 38#include <mach/error.h>
e9ce8d39
A
39#include <servers/bootstrap.h>
40
41mach_port_t _lu_port = MACH_PORT_NULL;
5b2abdfb 42static name_t LOOKUP_NAME = "lookup daemon v2";
e9ce8d39
A
43
44mach_port_t _lookupd_port(mach_port_t port) {
3d9156a7
A
45 kern_return_t ret;
46
e9ce8d39 47 if (port != MACH_PORT_NULL) {
3d9156a7 48 ret = bootstrap_register(bootstrap_port, LOOKUP_NAME, port);
e9ce8d39
A
49 if (ret != BOOTSTRAP_SUCCESS) {
50 mach_error("bootstrap_register() failed", ret);
51 abort();
52 }
53 return port;
3d9156a7
A
54 } else if ((_lu_port == MACH_PORT_NULL) && (getpid() > 1)) {
55 ret = bootstrap_look_up(bootstrap_port, LOOKUP_NAME, &_lu_port);
56 if (ret != BOOTSTRAP_SUCCESS && ret != BOOTSTRAP_UNKNOWN_SERVICE) {
57 mach_error("bootstrap_look_up() failed", ret);
58 _lu_port = MACH_PORT_NULL;
59 }
e9ce8d39
A
60 }
61 return _lu_port;
62}
63
64/* called as child starts up. mach ports aren't inherited: trash cache */
65void
66_lu_fork_child()
67{
e9ce8d39 68 _lu_port = MACH_PORT_NULL;
e9ce8d39
A
69}
70
71void
72_lu_setport(mach_port_t desired)
73{
74 if (_lu_port != MACH_PORT_NULL) {
75 mach_port_deallocate(mach_task_self(), _lu_port);
76 }
77 _lu_port = desired;
78}
79
e9ce8d39
A
80int
81_lu_running(void)
82{
3d9156a7
A
83 return ((_lu_port != MACH_PORT_NULL) ||
84 (_lookupd_port(MACH_PORT_NULL) != MACH_PORT_NULL));
e9ce8d39 85}