]> git.saurik.com Git - apple/libc.git/blame - threads/lu_utils.c
Libc-262.3.2.tar.gz
[apple/libc.git] / threads / lu_utils.c
CommitLineData
e9ce8d39
A
1/*
2 * Port and memory management for doing lookups to the lookup server
3 * Copyright (C) 1989 by NeXT, Inc.
4 */
5/*
6 * HISTORY
7 * 27-Mar-90 Gregg Kellogg (gk) at NeXT
8 * Changed to use bootstrap port instead of service port.
9 *
10 */
11#include <stdio.h>
12#include <mach/mach.h>
13#include <servers/bootstrap.h>
14
15mach_port_t _lu_port = MACH_PORT_NULL;
5b2abdfb 16static name_t LOOKUP_NAME = "lookup daemon v2";
e9ce8d39
A
17
18mach_port_t _lookupd_port(mach_port_t port) {
19 if (port != MACH_PORT_NULL) {
20 kern_return_t ret;
21 mach_port_t bp;
22 ret = task_get_bootstrap_port(mach_task_self(), &bp);
23 if (ret != KERN_SUCCESS) {
24 mach_error("task_get_bootstrap_port() failed", ret);
25 abort();
26 }
27 if (bp == MACH_PORT_NULL) {
28 mach_error("task_get_bootstrap_port() returned MACH_PORT_NULL!", 0);
29 return MACH_PORT_NULL;
30 }
31 ret = bootstrap_register(bp, LOOKUP_NAME, port);
32 if (ret != BOOTSTRAP_SUCCESS) {
33 mach_error("bootstrap_register() failed", ret);
34 abort();
35 }
36 return port;
37 }
38 return _lu_port;
39}
40
41/* called as child starts up. mach ports aren't inherited: trash cache */
42void
43_lu_fork_child()
44{
45 mach_port_t bp;
46 kern_return_t ret;
47 int pid = getpid();
48
49 _lu_port = MACH_PORT_NULL;
50 if (pid > 2) {
51 ret = task_get_bootstrap_port(mach_task_self(), &bp);
52 if (ret != KERN_SUCCESS) {
53 mach_error("task_get_bootstrap_port() failed", ret);
54 abort();
55 }
56 if (bp == MACH_PORT_NULL) {
57 fprintf(stderr, "task_get_bootstrap_port() returned MACH_PORT_NULL!\n");
58 return;
59 }
60 ret = bootstrap_look_up(bp, LOOKUP_NAME, &_lu_port);
61 if (ret != BOOTSTRAP_SUCCESS && ret != BOOTSTRAP_UNKNOWN_SERVICE) {
62 mach_error("bootstrap_look_up() failed", ret);
63 _lu_port = MACH_PORT_NULL;
64 }
65 }
66}
67
68void
69_lu_setport(mach_port_t desired)
70{
71 if (_lu_port != MACH_PORT_NULL) {
72 mach_port_deallocate(mach_task_self(), _lu_port);
73 }
74 _lu_port = desired;
75}
76
77static int
78port_valid(mach_port_t port)
79{
80 mach_port_type_t ptype;
81 kern_return_t ret;
82
83 ret = mach_port_type(mach_task_self(), port, &ptype);
84 if (ret != KERN_SUCCESS) {
85 mach_error("mach_port_type() failed", ret);
86 return 0;
87 }
88 return 1;
89}
90
91int
92_lu_running(void)
93{
94 if (_lu_port == MACH_PORT_NULL) {
95 _lu_fork_child();
96 }
97 if (_lu_port != MACH_PORT_NULL) {
98 if (port_valid(_lu_port)) {
99 return (1);
100 }
101 _lu_fork_child();
102 if (port_valid(_lu_port)) {
103 return (1);
104 }
105 }
106 return 0;
107}
108