1/*
2 * Copyright (c) 2000-2004, 2006, 2009, 2011 Apple 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__
42int
43__SCDynamicStoreNotifySignal(SCDynamicStoreRef store, pid_t pid, int sig)
44{
45	SCDynamicStorePrivateRef	storePrivate = (SCDynamicStorePrivateRef)store;
46	CFStringRef			sessionKey;
47	CFDictionaryRef			info;
48
49	if (storePrivate->notifyStatus != NotifierNotRegistered) {
50		/* sorry, you can only have one notification registered at once */
51		return kSCStatusNotifierActive;
52	}
53
54	if (pid == getpid()) {
55		/* sorry, you can't request that configd be signalled */
56		return kSCStatusInvalidArgument;
57	}
58
59	if ((sig <= 0) || (sig > NSIG)) {
60		/* sorry, you must specify a valid signal */
61		return kSCStatusInvalidArgument;
62	}
63
64	/* push out a notification if any changes are pending */
65	sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
66	info = CFDictionaryGetValue(sessionData, sessionKey);
67	CFRelease(sessionKey);
68	if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
69		CFNumberRef	sessionNum;
70
71		if (needsNotification == NULL)
72			needsNotification = CFSetCreateMutable(NULL,
73							       0,
74							       &kCFTypeSetCallBacks);
75
76		sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
77		CFSetAddValue(needsNotification, sessionNum);
78		CFRelease(sessionNum);
79	}
80
81	return kSCStatusOK;
82}
83
84
85__private_extern__
86kern_return_t
87_notifyviasignal(mach_port_t	server,
88		 task_t		task,
89		 int		sig,
90		 int		*sc_status)
91{
92	serverSessionRef		mySession  = getSession(server);
93	pid_t				pid;
94	kern_return_t			status;
95	SCDynamicStorePrivateRef	storePrivate;
96
97	if (mySession == NULL) {
98		/* sorry, you must have an open session to play */
99		*sc_status = kSCStatusNoStoreSession;
100		if (task != TASK_NULL) {
101			(void) mach_port_deallocate(mach_task_self(), task);
102		}
103		return KERN_SUCCESS;
104	}
105	storePrivate = (SCDynamicStorePrivateRef)mySession->store;
106
107	if (task != TASK_NULL) {
108		status = pid_for_task(task, &pid);
109		if (status != KERN_SUCCESS) {
110			/* could not determine pid for task */
111			*sc_status = kSCStatusFailed;
112			(void) mach_port_deallocate(mach_task_self(), task);
113			return KERN_SUCCESS;
114		}
115	} else {
116		/* sorry, you must specify a valid task */
117		*sc_status = kSCStatusInvalidArgument;
118		return KERN_SUCCESS;
119	}
120
121	*sc_status = __SCDynamicStoreNotifySignal(mySession->store, pid, sig);
122	if (*sc_status != kSCStatusOK) {
123		__MACH_PORT_DEBUG(TRUE, "*** _notifyviasignal __SCDynamicStoreNotifySignal failed: releasing task)", task);
124		(void) mach_port_deallocate(mach_task_self(), task);
125		return KERN_SUCCESS;
126	}
127
128	__MACH_PORT_DEBUG(TRUE, "*** _notifyviasignal", task);
129	storePrivate->notifyStatus     = Using_NotifierInformViaSignal;
130	storePrivate->notifySignal     = sig;
131	storePrivate->notifySignalTask = task;
132
133	return KERN_SUCCESS;
134}
135