]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDNotifierInformViaFD.c
configd-42.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDNotifierInformViaFD.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * April 5, 2000 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
33 #include <mach/mach.h>
34 #include <mach/mach_error.h>
35
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCDynamicStoreInternal.h"
39 #include "config.h" /* MiG generated file */
40
41 #include <paths.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46
47 Boolean
48 SCDynamicStoreNotifyFileDescriptor(SCDynamicStoreRef store,
49 int32_t identifier,
50 int *fd)
51 {
52 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
53 kern_return_t status;
54 int sc_status;
55 struct sockaddr_un un;
56 int sock;
57
58 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCDynamicStoreNotifyFileDescriptor:"));
59
60 if (!store) {
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 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
79 _SCErrorSet(errno);
80 SCLog(_sc_verbose, LOG_NOTICE, CFSTR("socket: %s"), strerror(errno));
81 return FALSE;
82 }
83
84 /* establish a UNIX domain socket for server->client notification */
85 bzero(&un, sizeof(un));
86 un.sun_family = AF_UNIX;
87 snprintf(un.sun_path,
88 sizeof(un.sun_path)-1,
89 "%s%s-%d",
90 _PATH_VARTMP,
91 "SCDynamicStoreNotifyFileDescriptor",
92 storePrivate->server);
93
94 if (bind(sock, (struct sockaddr *)&un, sizeof(un)) == -1) {
95 _SCErrorSet(errno);
96 SCLog(_sc_verbose, LOG_NOTICE, CFSTR("bind: %s"), strerror(errno));
97 (void) close(sock);
98 return FALSE;
99 }
100
101 if (listen(sock, 0) == -1) {
102 _SCErrorSet(errno);
103 SCLog(_sc_verbose, LOG_NOTICE, CFSTR("listen: %s"), strerror(errno));
104 (void) close(sock);
105 return FALSE;
106 }
107
108 status = notifyviafd(storePrivate->server,
109 un.sun_path,
110 strlen(un.sun_path),
111 identifier,
112 (int *)&sc_status);
113
114 if (status != KERN_SUCCESS) {
115 if (status != MACH_SEND_INVALID_DEST)
116 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("notifyviafd(): %s"), mach_error_string(status));
117 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
118 storePrivate->server = MACH_PORT_NULL;
119 _SCErrorSet(status);
120 return FALSE;
121 }
122
123 *fd = accept(sock, 0, 0);
124 if (*fd == -1) {
125 _SCErrorSet(errno);
126 SCLog(_sc_verbose, LOG_NOTICE, CFSTR("accept: %s"), strerror(errno));
127 (void) close(sock);
128 return FALSE;
129 }
130 (void) close(sock);
131
132 /* set notifier active */
133 storePrivate->notifyStatus = Using_NotifierInformViaFD;
134
135 return TRUE;
136 }