2 * Copyright (c) 2000-2007 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>
44 #include <sys/types.h>
47 #include <objc/objc-runtime.h>
48 #include <servers/bootstrap.h>
51 #include "configd_server.h"
52 #include "plugin_support.h"
55 Boolean _configd_verbose = FALSE; /* TRUE if verbose logging enabled */
58 FILE *_configd_trace = NULL; /* non-NULL if tracing enabled */
61 CFMutableSetRef _plugins_exclude = NULL; /* bundle identifiers to exclude from loading */
64 CFMutableSetRef _plugins_verbose = NULL; /* bundle identifiers to enable verbose logging */
66 static CFMachPortRef termRequested = NULL; /* Mach port used to notify runloop of a shutdown request */
69 static const struct option longopts[] = {
70 // { "no-bundles", no_argument, 0, 'b' },
71 // { "exclude-plugin", required_argument, 0, 'B' },
72 // { "no-fork", no_argument, 0, 'd' },
73 // { "test-bundle", required_argument, 0, 't' },
74 // { "verbose", no_argument, 0, 'v' },
75 // { "verbose-bundle", required_argument, 0, 'V' },
76 { "help", no_argument, 0, '?' },
82 usage(const char *prog)
84 SCPrint(TRUE, stderr, CFSTR("%s: [-d] [-v] [-V bundleID] [-b] [-B bundleID] [-t bundle-path]\n"), prog);
85 SCPrint(TRUE, stderr, CFSTR("options:\n"));
86 SCPrint(TRUE, stderr, CFSTR("\t-d\tdisable daemon/run in foreground\n"));
87 SCPrint(TRUE, stderr, CFSTR("\t-v\tenable verbose logging\n"));
88 SCPrint(TRUE, stderr, CFSTR("\t-V\tenable verbose logging for the specified plug-in\n"));
89 SCPrint(TRUE, stderr, CFSTR("\t-b\tdisable loading of ALL plug-ins\n"));
90 SCPrint(TRUE, stderr, CFSTR("\t-B\tdisable loading of the specified plug-in\n"));
91 SCPrint(TRUE, stderr, CFSTR("\t-t\tload/test the specified plug-in\n"));
92 SCPrint(TRUE, stderr, CFSTR("\t\t (Note: only the plug-in will be started)\n"));
98 allow_crash_reports(void)
100 mach_msg_type_number_t i;
101 exception_mask_t masks[EXC_TYPES_COUNT];
102 mach_msg_type_number_t n_masks = 0;
103 mach_port_t new_exception_port = MACH_PORT_NULL;
104 exception_port_t old_handlers[EXC_TYPES_COUNT];
105 exception_behavior_t old_behaviors[EXC_TYPES_COUNT];
106 thread_state_flavor_t old_flavors[EXC_TYPES_COUNT];
107 kern_return_t status;
109 status = bootstrap_look_up(bootstrap_port, "com.apple.ReportCrash.DirectoryService", &new_exception_port);
110 if (status != BOOTSTRAP_SUCCESS) {
112 CFSTR("allow_crash_reports bootstrap_look_up() failed: %s"),
113 bootstrap_strerror(status));
117 // get information about the original crash exception port for the task
118 status = task_get_exception_ports(mach_task_self(),
125 if (status != KERN_SUCCESS) {
127 CFSTR("allow_crash_reports task_get_exception_ports() failed: %s"),
128 mach_error_string(status));
132 // replace the original crash exception port with our new port
133 for (i = 0; i < n_masks; i++) {
134 status = task_set_exception_ports(mach_task_self(),
139 if (status != KERN_SUCCESS) {
141 CFSTR("allow_crash_reports task_set_exception_ports() failed: %s"),
142 mach_error_string(status));
156 if (termRequested != NULL) {
159 * if we've received a [shutdown] SIGTERM
160 * and we are syslog'ing than it's likely
161 * that syslogd is also being term'd. As
162 * such, let's also push any remaining log
163 * messages to stdout/stderr.
169 * send message to indicate that a request has been made
170 * for the daemon to be shutdown.
172 _SC_sendMachMessage(CFMachPortGetPort(termRequested), 0);
184 term(CFMachPortRef port, void *msg, CFIndex size, void *info)
189 wait = plugin_term(&status);
191 // if we are not waiting on a plugin
192 status = server_shutdown();
213 /* close any open FDs */
214 for (i = getdtablesize()-1; i>=0; i--) close(i);
216 /* set stdin, stdout, stderr */
217 fd = open(_PATH_DEVNULL, O_RDWR, 0);
222 (void) dup2(fd, STDIN_FILENO);
225 ofd = open("/var/log/configd.log", O_WRONLY|O_APPEND, 0);
227 if (fd > STDIN_FILENO) {
232 (void) dup2(fd, STDOUT_FILENO);
233 (void) dup2(fd, STDERR_FILENO);
234 if (fd > STDERR_FILENO) {
239 SCTrace(TRUE, stdout, CFSTR("start\n"));
249 /* set _configd_trace */
250 fd = open("/var/log/configd.trace", O_WRONLY|O_APPEND, 0);
252 _configd_trace = fdopen(fd, "a");
253 SCTrace(TRUE, _configd_trace, CFSTR("start\n"));
265 signal(SIGTERM, parent_exit);
272 /* child: becomes the daemon (see below) */
273 signal(SIGTERM, SIG_DFL);
277 /* parent: wait for signal, then exit */
280 (void) wait4(child_pid, (int *)&status, 0, 0);
281 if (WIFEXITED(status)) {
283 "*** configd (daemon) failed to start, exit status=%d",
284 WEXITSTATUS(status));
287 "*** configd (daemon) failed to start, received signal=%d",
309 fp = fopen("/var/run/configd.pid", "w");
311 fprintf(fp, "%d\n", getpid());
318 termMPCopyDescription(const void *info)
320 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SIGTERM MP>"));
325 main(int argc, char * const argv[])
327 CFMachPortContext context = { 0
331 , termMPCopyDescription
333 Boolean enableRestart = (argc <= 1); /* only if there are no arguments */
334 Boolean forceForeground = FALSE;
335 mach_port_limits_t limits;
336 Boolean loadBundles = TRUE;
337 struct sigaction nact;
340 const char *prog = argv[0];
341 CFRunLoopSourceRef rls;
342 mach_port_t service_port = MACH_PORT_NULL;
343 kern_return_t status;
345 const char *testBundle = NULL;
347 _plugins_exclude = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
348 _plugins_verbose = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
350 /* process any arguments */
352 while ((opt = getopt_long(argc, argv, "bB:dt:vV:", longopts, NULL)) != -1) {
358 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
359 CFSetSetValue(_plugins_exclude, str);
363 forceForeground = TRUE;
369 _configd_verbose = TRUE;
372 if (strcmp(optarg, "com.apple.SystemConfiguration") == 0) {
375 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
376 CFSetSetValue(_plugins_verbose, str);
389 * display an error if configd is already running and we are
390 * not solely executing/testing a bundle.
392 if (testBundle == NULL) {
393 if (server_active(&service_port)) {
394 exit (EX_UNAVAILABLE);
398 /* check credentials */
400 fprintf(stderr, "%s: permission denied.\n", prog);
404 if (!forceForeground && (service_port == MACH_PORT_NULL)) {
406 * if we haven't been asked to run in the foreground
407 * and have not been started by launchd (i.e. we're
408 * not already running as a Foreground process) then
411 if (fork_child() == -1) {
412 fprintf(stderr, "configd: fork() failed, %s\n", strerror(errno));
417 * Note: we are now the child process. The parent
418 * waits/exits in fork_child.
423 * close file descriptors, establish stdin/stdout/stderr,
426 if (!forceForeground) {
427 int facility = LOG_DAEMON;
428 int logopt = LOG_CONS|LOG_NDELAY|LOG_PID;
431 if (service_port == MACH_PORT_NULL) {
435 if (_configd_verbose) {
439 if (stat("/etc/rc.cdrom", &statbuf) == 0) {
440 facility = LOG_INSTALL;
443 openlog("configd", logopt, facility);
445 _sc_log = FALSE; /* redirect SCLog() to stdout/stderr */
448 /* enable crash reporting */
449 allow_crash_reports();
451 /* check/enable trace logging */
454 /* record process id */
455 if (testBundle == NULL) {
459 /* add signal handler to catch a SIGHUP */
460 nact.sa_handler = catcher;
461 sigemptyset(&nact.sa_mask);
462 nact.sa_flags = SA_RESTART;
463 if (sigaction(SIGHUP, &nact, NULL) == -1) {
464 SCLog(_configd_verbose, LOG_ERR,
465 CFSTR("sigaction(SIGHUP, ...) failed: %s"),
469 /* add signal handler to catch a SIGPIPE */
470 if (sigaction(SIGPIPE, &nact, NULL) == -1) {
471 SCLog(_configd_verbose, LOG_ERR,
472 CFSTR("sigaction(SIGPIPE, ...) failed: %s"),
476 /* add signal handler to catch a SIGTERM */
477 if (sigaction(SIGTERM, &nact, NULL) == -1) {
478 SCLog(_configd_verbose, LOG_ERR,
479 CFSTR("sigaction(SIGTERM, ...) failed: %s"),
483 /* add signal handler to catch a SIGINT */
484 if (sigaction(SIGINT, &nact, NULL) == -1) {
485 SCLog(_configd_verbose, LOG_ERR,
486 CFSTR("sigaction(SIGINT, ...) failed: %s"),
490 /* create the "shutdown requested" notification port */
491 termRequested = CFMachPortCreate(NULL, term, &context, NULL);
493 /* set queue limit */
494 limits.mpl_qlimit = 1;
495 status = mach_port_set_attributes(mach_task_self(),
496 CFMachPortGetPort(termRequested),
497 MACH_PORT_LIMITS_INFO,
498 (mach_port_info_t)&limits,
499 MACH_PORT_LIMITS_INFO_COUNT);
500 if (status != KERN_SUCCESS) {
501 perror("mach_port_set_attributes");
504 /* add to our runloop */
505 rls = CFMachPortCreateRunLoopSource(NULL, termRequested, 0);
506 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
509 if (testBundle == NULL) {
510 /* initialize primary (store management) thread */
511 server_init(service_port, enableRestart);
513 if (!forceForeground && (service_port == MACH_PORT_NULL)) {
514 /* synchronize with parent process */
515 kill(getppid(), SIGTERM);
518 /* load/initialize/start bundles into the secondary thread */
520 /* start plug-in initialization */
524 /* start primary (store management) thread */
527 /* load/initialize/start specified plug-in */
528 plugin_exec((void *)testBundle);
531 exit (EX_OK); /* insure the process exit status is 0 */
532 return 0; /* ...and make main fit the ANSI spec. */