2 * Copyright (c) 2000-2011, 2013 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * October 30, 2003 Allan Nathanson <ajn@apple.com>
28 * - add plugin "stop()" function support
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
33 * 24 March 2000 Allan Nathanson <ajn@apple.com>
46 #include <sys/types.h>
49 #include <objc/objc-runtime.h>
50 #include <servers/bootstrap.h>
52 #include <vproc_priv.h>
55 #include "configd_server.h"
56 #include "plugin_support.h"
58 #if TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
59 #include <CoreFoundation/CFUserNotification.h>
60 #endif // TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
63 Boolean _configd_verbose = FALSE; /* TRUE if verbose logging enabled */
66 FILE *_configd_trace = NULL; /* non-NULL if tracing enabled */
69 CFMutableSetRef _plugins_allowed = NULL; /* bundle identifiers to allow when loading */
72 CFMutableSetRef _plugins_exclude = NULL; /* bundle identifiers to exclude from loading */
75 Boolean _plugins_fork = FALSE; /* TRUE if plugins should be exec'd in their own process */
78 CFMutableSetRef _plugins_verbose = NULL; /* bundle identifiers to enable verbose logging */
80 static CFMachPortRef termRequested = NULL; /* Mach port used to notify runloop of a shutdown request */
83 static const struct option longopts[] = {
84 // { "include-plugin", required_argument, 0, 'A' },
85 // { "no-bundles", no_argument, 0, 'b' },
86 // { "exclude-plugin", required_argument, 0, 'B' },
87 // { "no-fork", no_argument, 0, 'd' },
88 // { "fork-all", no_argument, 0, 'f' },
89 // { "test-bundle", required_argument, 0, 't' },
90 // { "verbose", no_argument, 0, 'v' },
91 // { "verbose-bundle", required_argument, 0, 'V' },
92 { "help", no_argument, 0, '?' },
98 usage(const char *prog)
100 SCPrint(TRUE, stderr, CFSTR("%s: [-d] [-v] [-V bundleID] [-b] [-B bundleID] [-A bundleID] [-t bundle-path]\n"), prog);
101 SCPrint(TRUE, stderr, CFSTR("options:\n"));
102 SCPrint(TRUE, stderr, CFSTR("\t-d\tdisable daemon/run in foreground\n"));
103 SCPrint(TRUE, stderr, CFSTR("\t-v\tenable verbose logging\n"));
104 SCPrint(TRUE, stderr, CFSTR("\t-V\tenable verbose logging for the specified plug-in\n"));
105 SCPrint(TRUE, stderr, CFSTR("\t-f\tload ALL plug-ins in a separate process\n"));
106 SCPrint(TRUE, stderr, CFSTR("\t-b\tdisable loading of ALL plug-ins\n"));
107 SCPrint(TRUE, stderr, CFSTR("\t-B\tdisable loading of the specified plug-in\n"));
108 SCPrint(TRUE, stderr, CFSTR("\t-A\tenable loading of the specified plug-in\n"));
109 SCPrint(TRUE, stderr, CFSTR("\t-t\tload/test the specified plug-in\n"));
110 SCPrint(TRUE, stderr, CFSTR("\t\t (Note: only the plug-in will be started)\n"));
121 if (termRequested != NULL) {
124 * if we've received a [shutdown] SIGTERM
125 * and we are syslog'ing than it's likely
126 * that syslogd is also being term'd. As
127 * such, let's also push any remaining log
128 * messages to stdout/stderr.
134 * send message to indicate that a request has been made
135 * for the daemon to be shutdown.
137 _SC_sendMachMessage(CFMachPortGetPort(termRequested), 0);
149 term(CFMachPortRef port, void *msg, CFIndex size, void *info)
154 wait = plugin_term(&status);
156 // if we are not waiting on a plugin
157 status = server_shutdown();
178 /* close any open FDs */
179 for (i = getdtablesize()-1; i>=0; i--) close(i);
181 /* set stdin, stdout, stderr */
182 fd = open(_PATH_DEVNULL, O_RDWR, 0);
187 (void) dup2(fd, STDIN_FILENO);
190 ofd = open("/var/log/configd.log", O_WRONLY|O_APPEND, 0);
192 if (fd > STDIN_FILENO) {
197 (void) dup2(fd, STDOUT_FILENO);
198 (void) dup2(fd, STDERR_FILENO);
199 if (fd > STDERR_FILENO) {
204 SCTrace(TRUE, stdout, CFSTR("start\n"));
214 /* set _configd_trace */
215 fd = open("/var/log/configd.trace", O_WRONLY|O_APPEND, 0);
217 _configd_trace = fdopen(fd, "a");
218 SCTrace(TRUE, _configd_trace, CFSTR("start\n"));
230 signal(SIGTERM, parent_exit);
237 /* child: becomes the daemon (see below) */
238 signal(SIGTERM, SIG_DFL);
242 /* parent: wait for signal, then exit */
245 (void) wait4(child_pid, (int *)&status, 0, 0);
246 if (WIFEXITED(status)) {
248 "*** configd (daemon) failed to start, exit status=%d",
249 WEXITSTATUS(status));
252 "*** configd (daemon) failed to start, received signal=%d",
269 termMPCopyDescription(const void *info)
271 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SIGTERM MP>"));
276 main(int argc, char * const argv[])
278 CFMachPortContext context = { 0
282 , termMPCopyDescription
284 Boolean forceForeground = FALSE;
285 Boolean forcePlugin = FALSE;
286 int64_t is_launchd_job = 0;
287 mach_port_limits_t limits;
288 Boolean loadBundles = TRUE;
289 struct sigaction nact;
291 // extern int optind;
292 const char *prog = argv[0];
293 CFRunLoopSourceRef rls;
294 kern_return_t status;
296 const char *testBundle = NULL;
298 _plugins_allowed = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
299 _plugins_exclude = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
300 _plugins_verbose = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
302 /* process any arguments */
304 while ((opt = getopt_long(argc, argv, "A:bB:dt:vV:f", longopts, NULL)) != -1) {
307 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
308 CFSetSetValue(_plugins_allowed, str);
315 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
316 CFSetSetValue(_plugins_exclude, str);
320 forceForeground = TRUE;
326 _configd_verbose = TRUE;
329 if (strcmp(optarg, "com.apple.SystemConfiguration") == 0) {
332 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
333 CFSetSetValue(_plugins_verbose, str);
338 _plugins_fork = TRUE;
348 /* check credentials */
349 #if !TARGET_IPHONE_SIMULATOR
351 fprintf(stderr, "%s: permission denied.\n", prog);
354 #endif // !TARGET_IPHONE_SIMULATOR
356 /* check if we have been started by launchd */
357 vproc_swap_integer(NULL, VPROC_GSK_IS_MANAGED, NULL, &is_launchd_job);
359 #if TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
360 // if launchd job, check to see if we have been restarted
361 if (is_launchd_job) {
364 vproc_swap_integer(NULL, VPROC_GSK_LAST_EXIT_STATUS, NULL, &status);
365 if ((status != 0) && _SC_isAppleInternal()) {
368 // if we've been restarted
369 fd = open("/var/run/configd-crash-reported", O_WRONLY|O_CREAT|O_EXCL, 0644);
371 // if we have not yet alerted the user
372 CFUserNotificationDisplayNotice(0,
373 kCFUserNotificationStopAlertLevel,
377 CFSTR("\"configd\" restarted"),
378 CFSTR("Please collect the crash report and file a Radar."),
384 #endif // TARGET_OS_EMBEDDED && !defined(DO_NOT_INFORM)
386 /* ensure that forked plugins behave */
387 if ((testBundle != NULL) && (getenv("__FORKED_PLUGIN__") != NULL)) {
391 /* if needed, daemonize */
392 if (!forceForeground && !is_launchd_job) {
394 * if we haven't been asked to run in the foreground
395 * and have not been started by launchd (i.e. we're
396 * not already running as a Foreground process) then
399 if (fork_child() == -1) {
400 fprintf(stderr, "configd: fork() failed, %s\n", strerror(errno));
405 * Note: we are now the child process. The parent
406 * waits/exits in fork_child.
411 * close file descriptors, establish stdin/stdout/stderr,
414 if (!forceForeground || forcePlugin) {
415 int facility = LOG_DAEMON;
416 int logopt = LOG_CONS|LOG_NDELAY|LOG_PID;
419 if (!is_launchd_job && !forcePlugin) {
423 if (_configd_verbose) {
427 if (stat("/etc/rc.cdrom", &statbuf) == 0) {
428 facility = LOG_INSTALL;
431 openlog("configd", logopt, facility);
433 _sc_log = FALSE; /* redirect SCLog() to stdout/stderr */
436 /* check/enable trace logging */
439 /* add signal handler to catch a SIGHUP */
440 nact.sa_handler = catcher;
441 sigemptyset(&nact.sa_mask);
442 nact.sa_flags = SA_RESTART;
443 if (sigaction(SIGHUP, &nact, NULL) == -1) {
444 SCLog(_configd_verbose, LOG_ERR,
445 CFSTR("sigaction(SIGHUP, ...) failed: %s"),
449 /* add signal handler to catch a SIGPIPE */
450 if (sigaction(SIGPIPE, &nact, NULL) == -1) {
451 SCLog(_configd_verbose, LOG_ERR,
452 CFSTR("sigaction(SIGPIPE, ...) failed: %s"),
456 /* add signal handler to catch a SIGTERM */
457 if (sigaction(SIGTERM, &nact, NULL) == -1) {
458 SCLog(_configd_verbose, LOG_ERR,
459 CFSTR("sigaction(SIGTERM, ...) failed: %s"),
463 /* add signal handler to catch a SIGINT */
464 if (sigaction(SIGINT, &nact, NULL) == -1) {
465 SCLog(_configd_verbose, LOG_ERR,
466 CFSTR("sigaction(SIGINT, ...) failed: %s"),
470 /* create the "shutdown requested" notification port */
471 termRequested = CFMachPortCreate(NULL, term, &context, NULL);
473 /* set queue limit */
474 limits.mpl_qlimit = 1;
475 status = mach_port_set_attributes(mach_task_self(),
476 CFMachPortGetPort(termRequested),
477 MACH_PORT_LIMITS_INFO,
478 (mach_port_info_t)&limits,
479 MACH_PORT_LIMITS_INFO_COUNT);
480 if (status != KERN_SUCCESS) {
481 perror("mach_port_set_attributes");
484 /* add to our runloop */
485 rls = CFMachPortCreateRunLoopSource(NULL, termRequested, 0);
486 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
489 if (testBundle == NULL) {
490 /* initialize primary (store management) thread */
493 if (!forceForeground && !is_launchd_job) {
494 /* synchronize with parent process */
495 kill(getppid(), SIGTERM);
498 /* load/initialize/start bundles into the secondary thread */
500 /* start plug-in initialization */
504 /* start primary (store management) thread */
507 /* load/initialize/start specified plug-in */
508 plugin_exec((void *)testBundle);
511 exit (EX_OK); /* insure the process exit status is 0 */
512 return 0; /* ...and make main fit the ANSI spec. */