2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 * Modification History
25 * 24 March 2000 Allan Nathanson (ajn@apple.com)
35 #include <sys/types.h>
37 #include <objc/objc-runtime.h>
40 #include "configd_server.h"
41 #include "plugin_support.h"
44 const char *signames[] = {
45 "" , "HUP" , "INT" , "QUIT", "ILL" , "TRAP", "ABRT", "EMT" ,
46 "FPE" , "KILL", "BUS" , "SEGV", "SYS" , "PIPE", "ALRM", "TERM",
47 "URG" , "STOP", "TSTP" , "CONT", "CHLD" , "TTIN", "TTOU", "IO" ,
48 "XCPU", "XFSZ", "VTALRM", "PROF", "WINCH", "INFO", "USR1", "USR2"
53 usage(const char *prog)
55 SCDLog(LOG_INFO, CFSTR("%s: [-d] [-v] [-b] [-t plugin-bundle-path]"), prog);
56 SCDLog(LOG_INFO, CFSTR("options:"));
57 SCDLog(LOG_INFO, CFSTR("\t-d\tenable debugging"));
58 SCDLog(LOG_INFO, CFSTR("\t-v\tenable verbose logging"));
59 SCDLog(LOG_INFO, CFSTR("\t-b\tdisable loading of ALL plug-ins"));
60 SCDLog(LOG_INFO, CFSTR("\t-t\tload/test the specified plug-in"));
61 SCDLog(LOG_INFO, CFSTR("\t\t (Note: only the plug-in will be started)"), prog);
72 * Note: we can't use SCDLog() since the signal may be received while the
73 * logging thread lock is held.
75 if (SCDOptionGet(NULL, kSCDOptionUseSyslog)) {
76 syslog (LOG_INFO, "caught SIG%s" , signames[signum]);
78 fprintf(stderr, "caught SIG%s\n", signames[signum]);
97 signal(SIGTERM, parent_exit);
104 /* child: becomes the daemon (see below) */
105 signal(SIGTERM, SIG_DFL);
109 /* parent: wait for signal, then exit */
112 (void) wait4(child_pid, (int *)&status, 0, 0);
113 if (WIFEXITED(status)) {
115 "*** configd (daemon) failed to start, exit status=%d",
116 WEXITSTATUS(status));
119 "*** configd (daemon) failed to start, received signal=%d",
132 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
133 (void)dup2(fd, STDIN_FILENO);
134 (void)dup2(fd, STDOUT_FILENO);
135 (void)dup2(fd, STDERR_FILENO);
144 main (int argc, const char *argv[])
148 const char *prog = argv[0];
149 boolean_t loadBundles = TRUE;
150 const char *testBundle = NULL;
151 struct sigaction nact;
153 /* process any arguments */
155 while ((opt = getopt(argc, argv, "bdt:v")) != -1) {
161 SCDOptionSet(NULL, kSCDOptionDebug, TRUE);
167 SCDOptionSet(NULL, kSCDOptionVerbose, TRUE);
178 * display an error if configd is already running and we are
179 * not executing/testing a bundle.
181 if ((testBundle == NULL) && (server_active() == TRUE)) {
182 exit (EX_UNAVAILABLE);
187 SCDOptionSet(NULL, kSCDOptionIsServer, TRUE); /* Use the config API's "server" code */
188 SCDOptionSet(NULL, kSCDOptionUseCFRunLoop, TRUE); /* Use the CFRunLoop */
190 /* check credentials */
193 if (!SCDOptionGet(NULL, kSCDOptionDebug)) {
195 fprintf(stderr, "%s: permission denied.\n", prog);
202 if ((testBundle == NULL) && !SCDOptionGet(NULL, kSCDOptionDebug)) {
203 if (fork_child() == -1) {
204 fprintf(stderr, "configd: fork() failed, %s\n", strerror(errno));
207 /* now the child process, parent waits in fork_child */
209 /* log via syslog() facility */
210 openlog("configd", (LOG_NDELAY | LOG_PID), LOG_DAEMON);
211 SCDOptionSet(NULL, kSCDOptionUseSyslog, TRUE);
214 /* add signal handler to catch a SIGPIPE */
216 nact.sa_handler = catcher;
217 sigemptyset(&nact.sa_mask);
218 nact.sa_flags = SA_RESTART;
220 if (sigaction(SIGPIPE, &nact, NULL) == -1) {
222 CFSTR("sigaction(SIGPIPE, ...) failed: %s"),
228 objc_setMultithreaded(YES);
230 if (testBundle == NULL) {
231 /* initialize primary (cache management) thread */
234 /* load/initialize/start bundles into the secondary thread */
238 if (!SCDOptionGet(NULL, kSCDOptionDebug)) {
239 /* synchronize with parent process */
240 kill(getppid(), SIGTERM);
247 if (testBundle == NULL) {
248 /* start primary (cache management) thread */
251 /* load/initialize/start specified plug-in */
252 plugin_exec((void *)testBundle);
255 exit (EX_OK); // insure the process exit status is 0
256 return 0; // ...and make main fit the ANSI spec.