]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/configd.m
configd-1061.141.1.tar.gz
[apple/configd.git] / configd.tproj / configd.m
1 /*
2 * Copyright (c) 2000-2011, 2013-2020 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 * October 30, 2003 Allan Nathanson <ajn@apple.com>
28 * - add plugin "stop()" function support
29 *
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
32 *
33 * 24 March 2000 Allan Nathanson <ajn@apple.com>
34 * - created
35 */
36
37 #define DO_NOT_INFORM
38
39 #include <getopt.h>
40 #include <stdio.h>
41 #include <sysexits.h>
42 #include <syslog.h>
43 #include <unistd.h>
44 #include <paths.h>
45 #include <fcntl.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49 #include <objc/objc-runtime.h>
50 #include <servers/bootstrap.h>
51 #include <vproc.h>
52 #include <vproc_priv.h>
53
54 #include "configd.h"
55 #include "configd_server.h"
56 #include "plugin_support.h"
57 #include "SCDynamicStoreInternal.h"
58
59 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
60 #include <CoreFoundation/CFUserNotification.h>
61 #endif // TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
62
63 __private_extern__
64 Boolean _configd_verbose = FALSE; /* TRUE if verbose logging enabled */
65
66 __private_extern__
67 CFMutableSetRef _plugins_allowed = NULL; /* bundle identifiers to allow when loading */
68
69 __private_extern__
70 CFMutableSetRef _plugins_exclude = NULL; /* bundle identifiers to exclude from loading */
71
72 __private_extern__
73 CFMutableSetRef _plugins_verbose = NULL; /* bundle identifiers to enable verbose logging */
74
75 static CFMachPortRef termRequested = NULL; /* Mach port used to notify runloop of a shutdown request */
76
77
78 static const struct option longopts[] = {
79 // { "include-plugin", required_argument, 0, 'A' },
80 // { "no-bundles", no_argument, 0, 'b' },
81 // { "exclude-plugin", required_argument, 0, 'B' },
82 // { "no-fork", no_argument, 0, 'd' },
83 // { "test-bundle", required_argument, 0, 't' },
84 // { "verbose", no_argument, 0, 'v' },
85 // { "verbose-bundle", required_argument, 0, 'V' },
86 { "help", no_argument, 0, '?' },
87 { 0, 0, 0, 0 }
88 };
89
90
91 static void
92 usage(const char *prog)
93 {
94 SCPrint(TRUE, stderr, CFSTR("%s: [-d] [-v] [-V bundleID] [-b] [-B bundleID] [-A bundleID] [-t bundle-path]\n"), prog);
95 SCPrint(TRUE, stderr, CFSTR("options:\n"));
96 SCPrint(TRUE, stderr, CFSTR("\t-d\tdisable daemon/run in foreground\n"));
97 SCPrint(TRUE, stderr, CFSTR("\t-v\tenable verbose logging\n"));
98 SCPrint(TRUE, stderr, CFSTR("\t-V\tenable verbose logging for the specified plug-in\n"));
99 SCPrint(TRUE, stderr, CFSTR("\t-f\tload ALL plug-ins in a separate process\n"));
100 SCPrint(TRUE, stderr, CFSTR("\t-b\tdisable loading of ALL plug-ins\n"));
101 SCPrint(TRUE, stderr, CFSTR("\t-B\tdisable loading of the specified plug-in\n"));
102 SCPrint(TRUE, stderr, CFSTR("\t-A\tenable loading of the specified plug-in\n"));
103 SCPrint(TRUE, stderr, CFSTR("\t-t\tload/test the specified plug-in\n"));
104 SCPrint(TRUE, stderr, CFSTR("\t\t (Note: only the plug-in will be started)\n"));
105 exit (EX_USAGE);
106 }
107
108
109 __private_extern__ os_log_t
110 __configd_SCDynamicStore()
111 {
112 static os_log_t log = NULL;
113
114 if (log == NULL) {
115 log = os_log_create("com.apple.SystemConfiguration", "SCDynamicStore");
116 }
117
118 return log;
119 }
120
121
122 static void
123 catcher(int signum)
124 {
125 switch (signum) {
126 case SIGINT :
127 case SIGTERM :
128 if (termRequested != NULL) {
129 if (_sc_log > kSCLogDestinationFile) {
130 /*
131 * if we've received a [shutdown] SIGTERM
132 * and we are syslog'ing than it's likely
133 * that syslogd is also being term'd. As
134 * such, let's also push any remaining log
135 * messages to stdout/stderr.
136 */
137 _sc_log = kSCLogDestinationBoth;
138 }
139
140 /*
141 * send message to indicate that a request has been made
142 * for the daemon to be shutdown.
143 */
144 _SC_sendMachMessage(CFMachPortGetPort(termRequested), 0);
145 } else {
146 _exit(EX_OK);
147 }
148 break;
149 }
150
151 return;
152 }
153
154
155 static void
156 term(CFMachPortRef port, void *msg, CFIndex size, void *info)
157 {
158 #pragma unused(port)
159 #pragma unused(msg)
160 #pragma unused(size)
161 #pragma unused(info)
162 int status = EX_OK;
163 Boolean wait;
164
165 wait = plugin_term(&status);
166 if (!wait) {
167 // if we are not waiting on a plugin
168 exit (status);
169 }
170
171 return;
172 }
173
174
175 static void
176 parent_exit(int i)
177 {
178 #pragma unused(i)
179 _exit (0);
180 }
181
182
183 static void
184 init_fds()
185 {
186 int fd;
187 int i;
188
189 /* close any open FDs */
190 for (i = getdtablesize()-1; i>=0; i--) close(i);
191
192 /* set stdin, stdout, stderr */
193 fd = open(_PATH_DEVNULL, O_RDWR, 0);
194 if (fd != -1) {
195 int ofd;
196
197 // stdin
198 (void) dup2(fd, STDIN_FILENO);
199
200 // stdout, stderr
201 ofd = open("/var/log/configd.log", O_WRONLY|O_APPEND, 0);
202 if (ofd != -1) {
203 if (fd > STDIN_FILENO) {
204 (void) close(fd);
205 }
206 fd = ofd;
207 }
208 (void) dup2(fd, STDOUT_FILENO);
209 (void) dup2(fd, STDERR_FILENO);
210 if (fd > STDERR_FILENO) {
211 (void) close(fd);
212 }
213 }
214
215 return;
216 }
217
218
219 static int
220 fork_child()
221 {
222 int child_pid;
223
224 signal(SIGTERM, parent_exit);
225 child_pid = fork();
226 switch (child_pid) {
227 case -1: {
228 return -1;
229 }
230 case 0: {
231 /* child: becomes the daemon (see below) */
232 signal(SIGTERM, SIG_DFL);
233 break;
234 }
235 default: {
236 /* parent: wait for signal, then exit */
237 int status;
238
239 (void) wait4(child_pid, (int *)&status, 0, 0);
240 if (WIFEXITED(status)) {
241 fprintf(stderr,
242 "*** configd (daemon) failed to start, exit status=%d\n",
243 WEXITSTATUS(status));
244 } else {
245 fprintf(stderr,
246 "*** configd (daemon) failed to start, received signal=%d\n",
247 WTERMSIG(status));
248 }
249 fflush (stderr);
250 exit (EX_SOFTWARE);
251 }
252 }
253
254 if (setsid() == -1)
255 return -1;
256
257 (void) chdir("/");
258
259 return 0;
260 }
261
262 static CFStringRef
263 termMPCopyDescription(const void *info)
264 {
265 #pragma unused(info)
266 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SIGTERM MP>"));
267 }
268
269
270 int
271 main(int argc, char * const argv[])
272 {
273 CFMachPortContext context = { 0
274 , (void *)1
275 , NULL
276 , NULL
277 , termMPCopyDescription
278 };
279 Boolean forceForeground = FALSE;
280 Boolean forcePlugin = FALSE;
281 int64_t is_launchd_job = 0;
282 mach_port_limits_t limits;
283 Boolean loadBundles = TRUE;
284 struct sigaction nact;
285 int opt;
286 // extern int optind;
287 const char *prog = argv[0];
288 CFRunLoopSourceRef rls;
289 kern_return_t status;
290 CFStringRef str;
291 const char *testBundle = NULL;
292
293 _plugins_allowed = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
294 _plugins_exclude = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
295 _plugins_verbose = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
296
297 /* process any arguments */
298
299 while ((opt = getopt_long(argc, argv, "A:bB:dt:vV:", longopts, NULL)) != -1) {
300 switch(opt) {
301 case 'A':
302 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
303 CFSetSetValue(_plugins_allowed, str);
304 CFRelease(str);
305 break;
306 case 'b':
307 loadBundles = FALSE;
308 break;
309 case 'B':
310 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
311 CFSetSetValue(_plugins_exclude, str);
312 CFRelease(str);
313 break;
314 case 'd':
315 forceForeground = TRUE;
316 break;
317 case 't':
318 testBundle = optarg;
319 break;
320 case 'v':
321 _configd_verbose = TRUE;
322 break;
323 case 'V':
324 if (strcmp(optarg, "com.apple.SystemConfiguration") == 0) {
325 _sc_verbose = TRUE;
326 } else {
327 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
328 CFSetSetValue(_plugins_verbose, str);
329 CFRelease(str);
330 }
331 break;
332 case '?':
333 default :
334 usage(prog);
335 }
336 }
337 // argc -= optind;
338 // argv += optind;
339
340 /* check credentials */
341 #if !TARGET_OS_SIMULATOR
342 if (getuid() != 0) {
343 fprintf(stderr, "%s: permission denied.\n", prog);
344 exit (EX_NOPERM);
345 }
346 #endif // !TARGET_OS_SIMULATOR
347
348 /* check if we have been started by launchd */
349 vproc_swap_integer(NULL, VPROC_GSK_IS_MANAGED, NULL, &is_launchd_job);
350
351 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
352 // if launchd job, check to see if we have been restarted
353 if (is_launchd_job) {
354 int64_t status = 0;
355
356 vproc_swap_integer(NULL, VPROC_GSK_LAST_EXIT_STATUS, NULL, &status);
357 if ((status != 0) && _SC_isAppleInternal()) {
358 int fd;
359
360 // if we've been restarted
361 fd = open("/var/run/configd-crash-reported", O_WRONLY|O_CREAT|O_EXCL, 0644);
362 if (fd >= 0) {
363 // if we have not yet alerted the user
364 CFUserNotificationDisplayNotice(0,
365 kCFUserNotificationStopAlertLevel,
366 NULL,
367 NULL,
368 NULL,
369 CFSTR("\"configd\" restarted"),
370 CFSTR("Please collect the crash report and file a Radar."),
371 NULL);
372 close(fd);
373 }
374 }
375 }
376 #endif // TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR && !defined(DO_NOT_INFORM)
377
378 /* ensure that forked plugins behave */
379 if (testBundle != NULL) {
380 forcePlugin = TRUE;
381 }
382
383 /* if needed, daemonize */
384 if (!forceForeground && !is_launchd_job) {
385 /*
386 * if we haven't been asked to run in the foreground
387 * and have not been started by launchd (i.e. we're
388 * not already running as a Foreground process) then
389 * daemonize ourself.
390 */
391 if (fork_child() == -1) {
392 fprintf(stderr, "configd: fork() failed: %s\n", strerror(errno));
393 exit (1);
394 }
395
396 /*
397 * Note: we are now the child process. The parent
398 * waits/exits in fork_child.
399 */
400 }
401
402 /*
403 * close file descriptors, establish stdin/stdout/stderr,
404 * setup logging.
405 */
406 if (!forceForeground || forcePlugin) {
407 if (!is_launchd_job && !forcePlugin) {
408 init_fds();
409 }
410
411 if (_SC_isInstallEnvironment()) {
412 openlog("configd",
413 LOG_CONS|LOG_NDELAY|LOG_PID, // logopt
414 LOG_INSTALL); // facility
415 }
416 } else {
417 _sc_log = kSCLogDestinationFile; /* redirect SCLog() to stdout/stderr */
418 }
419
420 /* add signal handler to catch a SIGHUP */
421 nact.sa_handler = catcher;
422 sigemptyset(&nact.sa_mask);
423 nact.sa_flags = SA_RESTART;
424 if (sigaction(SIGHUP, &nact, NULL) == -1) {
425 SC_log(LOG_ERR, "sigaction(SIGHUP, ...) failed: %s", strerror(errno));
426 }
427
428 /* add signal handler to catch a SIGPIPE */
429 if (sigaction(SIGPIPE, &nact, NULL) == -1) {
430 SC_log(LOG_ERR, "sigaction(SIGPIPE, ...) failed: %s", strerror(errno));
431 }
432
433 /* add signal handler to catch a SIGTERM */
434 if (sigaction(SIGTERM, &nact, NULL) == -1) {
435 SC_log(LOG_ERR, "sigaction(SIGTERM, ...) failed: %s", strerror(errno));
436 }
437
438 /* add signal handler to catch a SIGINT */
439 if (sigaction(SIGINT, &nact, NULL) == -1) {
440 SC_log(LOG_ERR, "sigaction(SIGINT, ...) failed: %s", strerror(errno));
441 }
442
443 /* create the "shutdown requested" notification port */
444 termRequested = CFMachPortCreate(NULL, term, &context, NULL);
445
446 /* set queue limit */
447 limits.mpl_qlimit = 1;
448 status = mach_port_set_attributes(mach_task_self(),
449 CFMachPortGetPort(termRequested),
450 MACH_PORT_LIMITS_INFO,
451 (mach_port_info_t)&limits,
452 MACH_PORT_LIMITS_INFO_COUNT);
453 if (status != KERN_SUCCESS) {
454 perror("mach_port_set_attributes");
455 }
456
457 /* add to our runloop */
458 rls = CFMachPortCreateRunLoopSource(NULL, termRequested, 0);
459 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
460 CFRelease(rls);
461
462 if (testBundle == NULL) {
463 /* don't complain about having lots of SCDynamicStore objects */
464 _SCDynamicStoreSetSessionWatchLimit(0);
465
466 /* initialize primary (store management) thread */
467 server_init();
468
469 if (!forceForeground && !is_launchd_job) {
470 /* synchronize with parent process */
471 kill(getppid(), SIGTERM);
472 }
473
474 /* load/initialize/start bundles into the secondary thread */
475 if (loadBundles) {
476 /* start plug-in initialization */
477 plugin_init();
478 }
479
480 /* start main thread */
481 CFRunLoopRun();
482 } else {
483 /* load/initialize/start specified plug-in */
484 plugin_exec((void *)testBundle);
485 }
486
487 exit (EX_OK); /* insure the process exit status is 0 */
488 return 0; /* ...and make main fit the ANSI spec. */
489 }