]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDNotifierInformViaFD.c
77a7611391dd9bcacf1e68c54f90654e23945eba
[apple/configd.git] / SystemConfiguration.fproj / SCDNotifierInformViaFD.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2008-2011, 2013, 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 <mach/mach.h>
35 #include <mach/mach_error.h>
36
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCPrivate.h>
39 #include "SCDynamicStoreInternal.h"
40 #include "config.h" /* MiG generated file */
41
42 #include <paths.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
47
48 Boolean
49 SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store,
50 int32_t identifier,
51 int *fd)
52 {
53 int fildes[2] = { -1, -1 };
54 fileport_t fileport = MACH_PORT_NULL;
55 int ret;
56 int sc_status;
57 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
58 kern_return_t status;
59
60 if (store == NULL) {
61 /* sorry, you must provide a session */
62 _SCErrorSet(kSCStatusNoStoreSession);
63 return FALSE;
64 }
65
66 if (storePrivate->server == MACH_PORT_NULL) {
67 /* sorry, you must have an open session to play */
68 _SCErrorSet(kSCStatusNoStoreServer);
69 return FALSE;
70 }
71
72 if (storePrivate->notifyStatus != NotifierNotRegistered) {
73 /* sorry, you can only have one notification registered at once */
74 _SCErrorSet(kSCStatusNotifierActive);
75 return FALSE;
76 }
77
78 ret = pipe(fildes);
79 if (ret == -1) {
80 _SCErrorSet(errno);
81 SC_log(LOG_ERR, "pipe() failed: %s", strerror(errno));
82 goto fail;
83 }
84
85 /*
86 * send fildes[1], the sender's fd, to configd using a fileport and
87 * return fildes[0] to the caller.
88 */
89
90 fileport = MACH_PORT_NULL;
91 ret = fileport_makeport(fildes[1], &fileport);
92 if (ret < 0) {
93 _SCErrorSet(errno);
94 SC_log(LOG_ERR, "fileport_makeport() failed: %s", strerror(errno));
95 goto fail;
96 }
97
98 retry :
99
100 status = notifyviafd(storePrivate->server,
101 fileport,
102 identifier,
103 (int *)&sc_status);
104
105 if (__SCDynamicStoreCheckRetryAndHandleError(store,
106 status,
107 &sc_status,
108 "SCDynamicStoreNotifyFileDescriptor notifyviafd()")) {
109 goto retry;
110 }
111
112 if (status != KERN_SUCCESS) {
113 _SCErrorSet(status);
114 goto fail;
115 }
116
117 if (sc_status != kSCStatusOK) {
118 _SCErrorSet(sc_status);
119 goto fail;
120 }
121
122 /* the SCDynamicStore server now has a copy of the write side, close our reference */
123 (void) close(fildes[1]);
124
125 /* and keep the read side */
126 *fd = fildes[0];
127
128 /* set notifier active */
129 storePrivate->notifyStatus = Using_NotifierInformViaFD;
130
131 return TRUE;
132
133 fail :
134
135 if (fildes[0] != -1) close(fildes[0]);
136 if (fildes[1] != -1) close(fildes[1]);
137 return FALSE;
138 }