]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_notifyviafd.c
configd-1109.101.1.tar.gz
[apple/configd.git] / configd.tproj / _notifyviafd.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2006, 2008-2011, 2015, 2019 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 serverSessionRef mySession;
51 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
52
53 if (storePrivate->notifyStatus != NotifierNotRegistered) {
54 /* sorry, you can only have one notification registered at once */
55 return kSCStatusNotifierActive;
56 }
57
58 /* push out a notification if any changes are pending */
59 mySession = getSession(storePrivate->server);
60 if (mySession->changedKeys != NULL) {
61 CFNumberRef sessionNum;
62
63 if (needsNotification == NULL)
64 needsNotification = CFSetCreateMutable(NULL,
65 0,
66 &kCFTypeSetCallBacks);
67
68 sessionNum = CFNumberCreate(NULL, kCFNumberIntType, &storePrivate->server);
69 CFSetAddValue(needsNotification, sessionNum);
70 CFRelease(sessionNum);
71 }
72
73 return kSCStatusOK;
74 }
75
76
77 __private_extern__
78 kern_return_t
79 _notifyviafd(mach_port_t server,
80 fileport_t fileport,
81 int identifier,
82 int *sc_status
83 )
84 {
85 int fd = -1;
86 int flags;
87 serverSessionRef mySession = getSession(server);
88 SCDynamicStorePrivateRef storePrivate;
89
90 /* get notification file descriptor */
91 fd = fileport_makefd(fileport);
92 mach_port_deallocate(mach_task_self(), fileport);
93 if (fd < 0) {
94 *sc_status = errno;
95 return KERN_SUCCESS;
96 }
97
98 flags = fcntl(fd, F_GETFL, 0);
99 if (flags == -1) {
100 *sc_status = errno;
101 goto fail;
102 }
103
104 flags |= O_NONBLOCK;
105 if (fcntl(fd, F_SETFL, flags) == -1) {
106 *sc_status = errno;
107 goto fail;
108 }
109
110 if (mySession == NULL) {
111 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
112 goto fail;
113 }
114 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
115
116 /* do common sanity checks */
117 *sc_status = __SCDynamicStoreNotifyFileDescriptor(mySession->store);
118
119 /* check status of __SCDynamicStoreNotifyFileDescriptor() */
120 if (*sc_status != kSCStatusOK) {
121 goto fail;
122 }
123
124 /* set notifier active */
125 storePrivate->notifyStatus = Using_NotifierInformViaFD;
126 storePrivate->notifyFile = fd;
127 storePrivate->notifyFileIdentifier = identifier;
128
129 return KERN_SUCCESS;
130
131 fail :
132
133 if (fd >= 0) close(fd);
134 return KERN_SUCCESS;
135 }