]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviasignal.c
configd-130.tar.gz
[apple/configd.git] / configd.tproj / _notifyviasignal.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <unistd.h>
35 #include <sys/types.h>
36
37 #include "configd.h"
38 #include "configd_server.h"
39 #include "session.h"
40
41 __private_extern__
42 int
43 __SCDynamicStoreNotifySignal(SCDynamicStoreRef store, pid_t pid, int sig)
44 {
45 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
46 CFStringRef sessionKey;
47 CFDictionaryRef info;
48
49 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
50 return kSCStatusNoStoreSession; /* you must have an open session to play */
51 }
52
53 if (storePrivate->notifyStatus != NotifierNotRegistered) {
54 /* sorry, you can only have one notification registered at once */
55 return kSCStatusNotifierActive;
56 }
57
58 if (pid == getpid()) {
59 /* sorry, you can't request that configd be signalled */
60 return kSCStatusInvalidArgument;
61 }
62
63 if ((sig <= 0) || (sig > NSIG)) {
64 /* sorry, you must specify a valid signal */
65 return kSCStatusInvalidArgument;
66 }
67
68 /* push out a notification if any changes are pending */
69 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
70 info = CFDictionaryGetValue(sessionData, sessionKey);
71 CFRelease(sessionKey);
72 if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
73 CFNumberRef sessionNum;
74
75 if (needsNotification == NULL)
76 needsNotification = CFSetCreateMutable(NULL,
77 0,
78 &kCFTypeSetCallBacks);
79
80 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
81 CFSetAddValue(needsNotification, sessionNum);
82 CFRelease(sessionNum);
83 }
84
85 return kSCStatusOK;
86 }
87
88
89 __private_extern__
90 kern_return_t
91 _notifyviasignal(mach_port_t server,
92 task_t task,
93 int sig,
94 int *sc_status)
95 {
96 serverSessionRef mySession = getSession(server);
97 pid_t pid;
98 kern_return_t status;
99 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)mySession->store;
100 #ifdef NOTYET
101 mach_port_t oldNotify;
102 #endif /* NOTYET */
103
104 status = pid_for_task(task, &pid);
105 if (status != KERN_SUCCESS) {
106 *sc_status = kSCStatusFailed; /* could not determine pid for task */
107 return KERN_SUCCESS;
108 }
109
110 if (!mySession) {
111 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
112 return KERN_SUCCESS;
113 }
114
115 *sc_status = __SCDynamicStoreNotifySignal(mySession->store, pid, sig);
116 if (*sc_status != kSCStatusOK) {
117 if (task != TASK_NULL) {
118 (void) mach_port_destroy(mach_task_self(), task);
119 }
120 return KERN_SUCCESS;
121 }
122
123 #ifdef DEBUG
124 { mach_port_type_t pt;
125
126 status = mach_port_type(mach_task_self(), task, &pt);
127 if (status == MACH_MSG_SUCCESS) {
128 char rights[8], *rp = &rights[0];
129
130 if (pt & MACH_PORT_TYPE_SEND)
131 *rp++ = 'S';
132 if (pt & MACH_PORT_TYPE_RECEIVE)
133 *rp++ = 'R';
134 if (pt & MACH_PORT_TYPE_SEND_ONCE)
135 *rp++ = 'O';
136 if (pt & MACH_PORT_TYPE_PORT_SET)
137 *rp++ = 'P';
138 if (pt & MACH_PORT_TYPE_DEAD_NAME)
139 *rp++ = 'D';
140 *rp = '\0';
141
142 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Task %d, port rights = %s"), task, rights);
143 }
144 }
145 #endif /* DEBUG */
146
147 #ifdef NOTYET
148 /* Request a notification when/if the client dies */
149 status = mach_port_request_notification(mach_task_self(),
150 task,
151 MACH_NOTIFY_DEAD_NAME,
152 1,
153 task,
154 MACH_MSG_TYPE_MAKE_SEND_ONCE,
155 &oldNotify);
156 if (status != KERN_SUCCESS) {
157 SCLog(TRUE, LOG_DEBUG, CFSTR("_notifyviasignal mach_port_request_notification() failed: %s"), mach_error_string(status));
158 *sc_status = kSCStatusFailed;
159 return KERN_SUCCESS;
160 }
161
162 #ifdef NOTYET_DEBUG
163 if (oldNotify != MACH_PORT_NULL) {
164 SCLog(TRUE, LOG_ERR, CFSTR("_notifyviasignal(): why is oldNotify != MACH_PORT_NULL?"));
165 }
166 #endif /* NOTYET_DEBUG */
167
168 // add task notification port to the server's port set
169 status = mach_port_move_member(mach_task_self(), task, server_ports);
170 if (status != KERN_SUCCESS) {
171 SCLog(TRUE, LOG_DEBUG, CFSTR("_notifyviasignal mach_port_move_member() failed: %s"), mach_error_string(status));
172 *sc_status = kSCStatusFailed;
173 return KERN_SUCCESS;
174 }
175 #endif /* NOTYET */
176
177 storePrivate->notifyStatus = Using_NotifierInformViaSignal;
178 storePrivate->notifySignal = sig;
179 storePrivate->notifySignalTask = task;
180
181 return KERN_SUCCESS;
182 }