2 * Copyright (c) 2000-2009 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>
50 #include <vproc_priv.h>
53 #include "configd_server.h"
54 #include "plugin_support.h"
57 Boolean _configd_verbose = FALSE; /* TRUE if verbose logging enabled */
60 FILE *_configd_trace = NULL; /* non-NULL if tracing enabled */
63 CFMutableSetRef _plugins_exclude = NULL; /* bundle identifiers to exclude from loading */
66 Boolean _plugins_fork = FALSE; /* TRUE if plugins should be exec'd in their own process */
69 CFMutableSetRef _plugins_verbose = NULL; /* bundle identifiers to enable verbose logging */
71 static CFMachPortRef termRequested = NULL; /* Mach port used to notify runloop of a shutdown request */
74 static const struct option longopts[] = {
75 // { "no-bundles", no_argument, 0, 'b' },
76 // { "exclude-plugin", required_argument, 0, 'B' },
77 // { "no-fork", no_argument, 0, 'd' },
78 // { "test-bundle", required_argument, 0, 't' },
79 // { "verbose", no_argument, 0, 'v' },
80 // { "verbose-bundle", required_argument, 0, 'V' },
81 { "help", no_argument, 0, '?' },
87 usage(const char *prog)
89 SCPrint(TRUE, stderr, CFSTR("%s: [-d] [-v] [-V bundleID] [-b] [-B bundleID] [-t bundle-path]\n"), prog);
90 SCPrint(TRUE, stderr, CFSTR("options:\n"));
91 SCPrint(TRUE, stderr, CFSTR("\t-d\tdisable daemon/run in foreground\n"));
92 SCPrint(TRUE, stderr, CFSTR("\t-v\tenable verbose logging\n"));
93 SCPrint(TRUE, stderr, CFSTR("\t-V\tenable verbose logging for the specified plug-in\n"));
94 SCPrint(TRUE, stderr, CFSTR("\t-b\tdisable loading of ALL plug-ins\n"));
95 SCPrint(TRUE, stderr, CFSTR("\t-B\tdisable loading of the specified plug-in\n"));
96 SCPrint(TRUE, stderr, CFSTR("\t-t\tload/test the specified plug-in\n"));
97 SCPrint(TRUE, stderr, CFSTR("\t\t (Note: only the plug-in will be started)\n"));
108 if (termRequested != NULL) {
111 * if we've received a [shutdown] SIGTERM
112 * and we are syslog'ing than it's likely
113 * that syslogd is also being term'd. As
114 * such, let's also push any remaining log
115 * messages to stdout/stderr.
121 * send message to indicate that a request has been made
122 * for the daemon to be shutdown.
124 _SC_sendMachMessage(CFMachPortGetPort(termRequested), 0);
136 term(CFMachPortRef port, void *msg, CFIndex size, void *info)
141 wait = plugin_term(&status);
143 // if we are not waiting on a plugin
144 status = server_shutdown();
165 /* close any open FDs */
166 for (i = getdtablesize()-1; i>=0; i--) close(i);
168 /* set stdin, stdout, stderr */
169 fd = open(_PATH_DEVNULL, O_RDWR, 0);
174 (void) dup2(fd, STDIN_FILENO);
177 ofd = open("/var/log/configd.log", O_WRONLY|O_APPEND, 0);
179 if (fd > STDIN_FILENO) {
184 (void) dup2(fd, STDOUT_FILENO);
185 (void) dup2(fd, STDERR_FILENO);
186 if (fd > STDERR_FILENO) {
191 SCTrace(TRUE, stdout, CFSTR("start\n"));
201 /* set _configd_trace */
202 fd = open("/var/log/configd.trace", O_WRONLY|O_APPEND, 0);
204 _configd_trace = fdopen(fd, "a");
205 SCTrace(TRUE, _configd_trace, CFSTR("start\n"));
217 signal(SIGTERM, parent_exit);
224 /* child: becomes the daemon (see below) */
225 signal(SIGTERM, SIG_DFL);
229 /* parent: wait for signal, then exit */
232 (void) wait4(child_pid, (int *)&status, 0, 0);
233 if (WIFEXITED(status)) {
235 "*** configd (daemon) failed to start, exit status=%d",
236 WEXITSTATUS(status));
239 "*** configd (daemon) failed to start, received signal=%d",
261 fp = fopen("/var/run/configd.pid", "w");
263 fprintf(fp, "%d\n", getpid());
270 termMPCopyDescription(const void *info)
272 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SIGTERM MP>"));
277 main(int argc, char * const argv[])
279 CFMachPortContext context = { 0
283 , termMPCopyDescription
285 Boolean forceForeground = FALSE;
286 Boolean forcePlugin = FALSE;
287 int64_t is_launchd_job = 0;
288 mach_port_limits_t limits;
289 Boolean loadBundles = TRUE;
290 struct sigaction nact;
293 const char *prog = argv[0];
294 CFRunLoopSourceRef rls;
295 kern_return_t status;
297 const char *testBundle = NULL;
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, "bB:dt:vV:f", longopts, NULL)) != -1) {
310 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
311 CFSetSetValue(_plugins_exclude, str);
315 forceForeground = TRUE;
321 _configd_verbose = TRUE;
324 if (strcmp(optarg, "com.apple.SystemConfiguration") == 0) {
327 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
328 CFSetSetValue(_plugins_verbose, str);
333 _plugins_fork = TRUE;
343 /* check credentials */
345 fprintf(stderr, "%s: permission denied.\n", prog);
349 /* check if we have been started by launchd */
350 vproc_swap_integer(NULL, VPROC_GSK_IS_MANAGED, NULL, &is_launchd_job);
352 /* ensure that forked plugins behave */
353 if ((testBundle != NULL) && (getenv("__FORKED_PLUGIN__") != NULL)) {
357 /* if needed, daemonize */
358 if (!forceForeground && !is_launchd_job) {
360 * if we haven't been asked to run in the foreground
361 * and have not been started by launchd (i.e. we're
362 * not already running as a Foreground process) then
365 if (fork_child() == -1) {
366 fprintf(stderr, "configd: fork() failed, %s\n", strerror(errno));
371 * Note: we are now the child process. The parent
372 * waits/exits in fork_child.
377 * close file descriptors, establish stdin/stdout/stderr,
380 if (!forceForeground || forcePlugin) {
381 int facility = LOG_DAEMON;
382 int logopt = LOG_CONS|LOG_NDELAY|LOG_PID;
385 if (!is_launchd_job && !forcePlugin) {
389 if (_configd_verbose) {
393 if (stat("/etc/rc.cdrom", &statbuf) == 0) {
394 facility = LOG_INSTALL;
397 openlog("configd", logopt, facility);
399 _sc_log = FALSE; /* redirect SCLog() to stdout/stderr */
402 /* check/enable trace logging */
405 /* record process id */
406 if (testBundle == NULL) {
410 /* add signal handler to catch a SIGHUP */
411 nact.sa_handler = catcher;
412 sigemptyset(&nact.sa_mask);
413 nact.sa_flags = SA_RESTART;
414 if (sigaction(SIGHUP, &nact, NULL) == -1) {
415 SCLog(_configd_verbose, LOG_ERR,
416 CFSTR("sigaction(SIGHUP, ...) failed: %s"),
420 /* add signal handler to catch a SIGPIPE */
421 if (sigaction(SIGPIPE, &nact, NULL) == -1) {
422 SCLog(_configd_verbose, LOG_ERR,
423 CFSTR("sigaction(SIGPIPE, ...) failed: %s"),
427 /* add signal handler to catch a SIGTERM */
428 if (sigaction(SIGTERM, &nact, NULL) == -1) {
429 SCLog(_configd_verbose, LOG_ERR,
430 CFSTR("sigaction(SIGTERM, ...) failed: %s"),
434 /* add signal handler to catch a SIGINT */
435 if (sigaction(SIGINT, &nact, NULL) == -1) {
436 SCLog(_configd_verbose, LOG_ERR,
437 CFSTR("sigaction(SIGINT, ...) failed: %s"),
441 /* create the "shutdown requested" notification port */
442 termRequested = CFMachPortCreate(NULL, term, &context, NULL);
444 /* set queue limit */
445 limits.mpl_qlimit = 1;
446 status = mach_port_set_attributes(mach_task_self(),
447 CFMachPortGetPort(termRequested),
448 MACH_PORT_LIMITS_INFO,
449 (mach_port_info_t)&limits,
450 MACH_PORT_LIMITS_INFO_COUNT);
451 if (status != KERN_SUCCESS) {
452 perror("mach_port_set_attributes");
455 /* add to our runloop */
456 rls = CFMachPortCreateRunLoopSource(NULL, termRequested, 0);
457 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
460 if (testBundle == NULL) {
461 /* initialize primary (store management) thread */
464 if (!forceForeground && !is_launchd_job) {
465 /* synchronize with parent process */
466 kill(getppid(), SIGTERM);
469 /* load/initialize/start bundles into the secondary thread */
471 /* start plug-in initialization */
475 /* start primary (store management) thread */
478 /* load/initialize/start specified plug-in */
479 plugin_exec((void *)testBundle);
482 exit (EX_OK); /* insure the process exit status is 0 */
483 return 0; /* ...and make main fit the ANSI spec. */