]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDNotifierWait.c
configd-24.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDNotifierWait.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <mach/mach.h>
24 #include <mach/mach_error.h>
25
26 #include <SystemConfiguration/SCD.h>
27 #include "config.h" /* MiG generated file */
28 #include "SCDPrivate.h"
29
30
31 SCDStatus
32 SCDNotifierWait(SCDSessionRef session)
33 {
34 SCDSessionPrivateRef sessionPrivate = (SCDSessionPrivateRef)session;
35 kern_return_t status;
36 mach_port_t port;
37 mach_port_t oldNotify;
38 SCDStatus scd_status;
39 mach_msg_id_t msgid;
40
41 SCDLog(LOG_DEBUG, CFSTR("SCDNotifierWait:"));
42
43 if ((session == NULL) || (sessionPrivate->server == MACH_PORT_NULL)) {
44 return SCD_NOSESSION; /* you must have an open session to play */
45 }
46
47 if (SCDOptionGet(NULL, kSCDOptionIsServer)) {
48 /* sorry, neither the server nor any plug-ins can "wait" */
49 return SCD_FAILED;
50 }
51
52 if (sessionPrivate->notifyStatus != NotifierNotRegistered) {
53 /* sorry, you can only have one notification registered at once */
54 return SCD_NOTIFIERACTIVE;
55 }
56
57 SCDLog(LOG_DEBUG, CFSTR("Allocating port (for server response)"));
58 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
59 if (status != KERN_SUCCESS) {
60 SCDLog(LOG_DEBUG, CFSTR("mach_port_allocate(): %s"), mach_error_string(status));
61 return SCD_FAILED;
62 }
63 SCDLog(LOG_DEBUG, CFSTR(" port = %d"), port);
64
65 status = mach_port_insert_right(mach_task_self(),
66 port,
67 port,
68 MACH_MSG_TYPE_MAKE_SEND);
69 if (status != KERN_SUCCESS) {
70 SCDLog(LOG_DEBUG, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status));
71 (void) mach_port_destroy(mach_task_self(), port);
72 return SCD_FAILED;
73 }
74
75 /* Request a notification when/if the server dies */
76 status = mach_port_request_notification(mach_task_self(),
77 port,
78 MACH_NOTIFY_NO_SENDERS,
79 1,
80 port,
81 MACH_MSG_TYPE_MAKE_SEND_ONCE,
82 &oldNotify);
83 if (status != KERN_SUCCESS) {
84 SCDLog(LOG_DEBUG, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status));
85 (void) mach_port_destroy(mach_task_self(), port);
86 return SCD_FAILED;
87 }
88
89 #ifdef DEBUG
90 if (oldNotify != MACH_PORT_NULL) {
91 SCDLog(LOG_DEBUG, CFSTR("SCDNotifierWait(): why is oldNotify != MACH_PORT_NULL?"));
92 }
93 #endif /* DEBUG */
94
95 SCDLog(LOG_DEBUG, CFSTR("Requesting notification via mach port %d"), port);
96 status = notifyviaport(sessionPrivate->server,
97 port,
98 0,
99 (int *)&scd_status);
100
101 if (status != KERN_SUCCESS) {
102 if (status != MACH_SEND_INVALID_DEST)
103 SCDLog(LOG_DEBUG, CFSTR("notifyviaport(): %s"), mach_error_string(status));
104 (void) mach_port_destroy(mach_task_self(), sessionPrivate->server);
105 sessionPrivate->server = MACH_PORT_NULL;
106 return SCD_NOSERVER;
107 }
108
109 if (scd_status != SCD_OK) {
110 return scd_status;
111 }
112
113 /* set notifier active */
114 sessionPrivate->notifyStatus = Using_NotifierWait;
115
116 SCDLog(LOG_DEBUG, CFSTR("Waiting..."));
117
118 msgid = _waitForMachMessage(port);
119
120 /* set notifier inactive */
121 sessionPrivate->notifyStatus = NotifierNotRegistered;
122
123 if (msgid == MACH_NOTIFY_NO_SENDERS) {
124 /* the server closed the notifier port */
125 SCDLog(LOG_DEBUG, CFSTR(" notifier port closed, destroying port %d"), port);
126 return SCD_NOSERVER;
127 }
128
129 if (msgid == -1) {
130 /* one of the mach routines returned an error */
131 SCDLog(LOG_DEBUG, CFSTR(" communication with server failed, destroying port %d"), port);
132 (void) mach_port_destroy(mach_task_self(), port);
133 return SCD_NOSERVER;
134 }
135
136 SCDLog(LOG_DEBUG, CFSTR("Something changed, cancelling notification request"));
137 status = notifycancel(sessionPrivate->server,
138 (int *)&scd_status);
139
140 if (status != KERN_SUCCESS) {
141 if (status != MACH_SEND_INVALID_DEST)
142 SCDLog(LOG_DEBUG, CFSTR("notifycancel(): %s"), mach_error_string(status));
143 (void) mach_port_destroy(mach_task_self(), sessionPrivate->server);
144 sessionPrivate->server = MACH_PORT_NULL;
145 scd_status = SCD_NOSERVER;
146 }
147
148 (void) mach_port_destroy(mach_task_self(), port);
149
150 return scd_status;
151 }