]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviafd.c
configd-963.200.27.tar.gz
[apple/configd.git] / configd.tproj / _notifyviafd.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2006, 2008-2011, 2015 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 * April 5, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <unistd.h>
35 #include <sys/fileport.h>
36 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/un.h>
41
42 #include "configd.h"
43 #include "session.h"
44
45
46 __private_extern__
47 int
48 __SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store)
49 {
50 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
51 CFStringRef sessionKey;
52 CFDictionaryRef info;
53
54 if (storePrivate->notifyStatus != NotifierNotRegistered) {
55 /* sorry, you can only have one notification registered at once */
56 return kSCStatusNotifierActive;
57 }
58
59 /* push out a notification if any changes are pending */
60 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), storePrivate->server);
61 info = CFDictionaryGetValue(sessionData, sessionKey);
62 CFRelease(sessionKey);
63 if (info && CFDictionaryContainsKey(info, kSCDChangedKeys)) {
64 CFNumberRef sessionNum;
65
66 if (needsNotification == NULL)
67 needsNotification = CFSetCreateMutable(NULL,
68 0,
69 &kCFTypeSetCallBacks);
70
71 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
72 CFSetAddValue(needsNotification, sessionNum);
73 CFRelease(sessionNum);
74 }
75
76 return kSCStatusOK;
77 }
78
79
80 __private_extern__
81 kern_return_t
82 _notifyviafd(mach_port_t server,
83 fileport_t fileport,
84 int identifier,
85 int *sc_status
86 )
87 {
88 int fd = -1;
89 int flags;
90 serverSessionRef mySession = getSession(server);
91 SCDynamicStorePrivateRef storePrivate;
92
93 /* get notification file descriptor */
94 fd = fileport_makefd(fileport);
95 mach_port_deallocate(mach_task_self(), fileport);
96 if (fd < 0) {
97 *sc_status = errno;
98 return KERN_SUCCESS;
99 }
100
101 flags = fcntl(fd, F_GETFL, 0);
102 if (flags == -1) {
103 *sc_status = errno;
104 goto fail;
105 }
106
107 flags |= O_NONBLOCK;
108 if (fcntl(fd, F_SETFL, flags) == -1) {
109 *sc_status = errno;
110 goto fail;
111 }
112
113 if (mySession == NULL) {
114 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
115 goto fail;
116 }
117 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
118
119 /* do common sanity checks */
120 *sc_status = __SCDynamicStoreNotifyFileDescriptor(mySession->store);
121
122 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
123 if (*sc_status != kSCStatusOK) {
124 goto fail;
125 }
126
127 /* set notifier active */
128 storePrivate->notifyStatus = Using_NotifierInformViaFD;
129 storePrivate->notifyFile = fd;
130 storePrivate->notifyFileIdentifier = identifier;
131
132 return KERN_SUCCESS;
133
134 fail :
135
136 if (fd >= 0) close(fd);
137 return KERN_SUCCESS;
138 }