2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 #include <mach/mach.h>
24 #include <mach/mach_error.h>
26 #include <SystemConfiguration/SCD.h>
27 #include "config.h" /* MiG generated file */
28 #include "SCDPrivate.h"
32 SCDNotifierWait(SCDSessionRef session
)
34 SCDSessionPrivateRef sessionPrivate
= (SCDSessionPrivateRef
)session
;
37 mach_port_t oldNotify
;
41 SCDLog(LOG_DEBUG
, CFSTR("SCDNotifierWait:"));
43 if ((session
== NULL
) || (sessionPrivate
->server
== MACH_PORT_NULL
)) {
44 return SCD_NOSESSION
; /* you must have an open session to play */
47 if (SCDOptionGet(NULL
, kSCDOptionIsServer
)) {
48 /* sorry, neither the server nor any plug-ins can "wait" */
52 if (sessionPrivate
->notifyStatus
!= NotifierNotRegistered
) {
53 /* sorry, you can only have one notification registered at once */
54 return SCD_NOTIFIERACTIVE
;
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
));
63 SCDLog(LOG_DEBUG
, CFSTR(" port = %d"), port
);
65 status
= mach_port_insert_right(mach_task_self(),
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
);
75 /* Request a notification when/if the server dies */
76 status
= mach_port_request_notification(mach_task_self(),
78 MACH_NOTIFY_NO_SENDERS
,
81 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
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
);
90 if (oldNotify
!= MACH_PORT_NULL
) {
91 SCDLog(LOG_DEBUG
, CFSTR("SCDNotifierWait(): why is oldNotify != MACH_PORT_NULL?"));
95 SCDLog(LOG_DEBUG
, CFSTR("Requesting notification via mach port %d"), port
);
96 status
= notifyviaport(sessionPrivate
->server
,
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
;
109 if (scd_status
!= SCD_OK
) {
113 /* set notifier active */
114 sessionPrivate
->notifyStatus
= Using_NotifierWait
;
116 SCDLog(LOG_DEBUG
, CFSTR("Waiting..."));
118 msgid
= _waitForMachMessage(port
);
120 /* set notifier inactive */
121 sessionPrivate
->notifyStatus
= NotifierNotRegistered
;
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
);
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
);
136 SCDLog(LOG_DEBUG
, CFSTR("Something changed, cancelling notification request"));
137 status
= notifycancel(sessionPrivate
->server
,
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
;
148 (void) mach_port_destroy(mach_task_self(), port
);