]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/configd.m
configd-204.tar.gz
[apple/configd.git] / configd.tproj / configd.m
1 /*
2 * Copyright (c) 2000-2007 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 #include <getopt.h>
38 #include <stdio.h>
39 #include <sysexits.h>
40 #include <syslog.h>
41 #include <unistd.h>
42 #include <paths.h>
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <objc/objc-runtime.h>
48 #include <servers/bootstrap.h>
49
50 #include "configd.h"
51 #include "configd_server.h"
52 #include "plugin_support.h"
53
54 __private_extern__
55 Boolean _configd_verbose = FALSE; /* TRUE if verbose logging enabled */
56
57 __private_extern__
58 FILE *_configd_trace = NULL; /* non-NULL if tracing enabled */
59
60 __private_extern__
61 CFMutableSetRef _plugins_exclude = NULL; /* bundle identifiers to exclude from loading */
62
63 __private_extern__
64 CFMutableSetRef _plugins_verbose = NULL; /* bundle identifiers to enable verbose logging */
65
66 static CFMachPortRef termRequested = NULL; /* Mach port used to notify runloop of a shutdown request */
67
68
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, '?' },
77 { 0, 0, 0, 0 }
78 };
79
80
81 static void
82 usage(const char *prog)
83 {
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"));
93 exit (EX_USAGE);
94 }
95
96
97 static void
98 allow_crash_reports(void)
99 {
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;
108
109 status = bootstrap_look_up(bootstrap_port, "com.apple.ReportCrash.DirectoryService", &new_exception_port);
110 if (status != BOOTSTRAP_SUCCESS) {
111 SCLog(TRUE, LOG_ERR,
112 CFSTR("allow_crash_reports bootstrap_look_up() failed: %s"),
113 bootstrap_strerror(status));
114 return;
115 }
116
117 // get information about the original crash exception port for the task
118 status = task_get_exception_ports(mach_task_self(),
119 EXC_MASK_CRASH,
120 masks,
121 &n_masks,
122 old_handlers,
123 old_behaviors,
124 old_flavors);
125 if (status != KERN_SUCCESS) {
126 SCLog(TRUE, LOG_ERR,
127 CFSTR("allow_crash_reports task_get_exception_ports() failed: %s"),
128 mach_error_string(status));
129 return;
130 }
131
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(),
135 masks[i],
136 new_exception_port,
137 old_behaviors[i],
138 old_flavors[i]);
139 if (status != KERN_SUCCESS) {
140 SCLog(TRUE, LOG_ERR,
141 CFSTR("allow_crash_reports task_set_exception_ports() failed: %s"),
142 mach_error_string(status));
143 }
144 }
145
146 return;
147 }
148
149
150 static void
151 catcher(int signum)
152 {
153 switch (signum) {
154 case SIGINT :
155 case SIGTERM :
156 if (termRequested != NULL) {
157 if (_sc_log > 0) {
158 /*
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.
164 */
165 _sc_log++;
166 }
167
168 /*
169 * send message to indicate that a request has been made
170 * for the daemon to be shutdown.
171 */
172 _SC_sendMachMessage(CFMachPortGetPort(termRequested), 0);
173 } else {
174 _exit(EX_OK);
175 }
176 break;
177 }
178
179 return;
180 }
181
182
183 static void
184 term(CFMachPortRef port, void *msg, CFIndex size, void *info)
185 {
186 int status = EX_OK;
187 Boolean wait;
188
189 wait = plugin_term(&status);
190 if (!wait) {
191 // if we are not waiting on a plugin
192 status = server_shutdown();
193 exit (status);
194 }
195
196 return;
197 }
198
199
200 static void
201 parent_exit(int i)
202 {
203 _exit (0);
204 }
205
206
207 static void
208 init_fds()
209 {
210 int fd;
211 int i;
212
213 /* close any open FDs */
214 for (i = getdtablesize()-1; i>=0; i--) close(i);
215
216 /* set stdin, stdout, stderr */
217 fd = open(_PATH_DEVNULL, O_RDWR, 0);
218 if (fd != -1) {
219 int ofd;
220
221 // stdin
222 (void) dup2(fd, STDIN_FILENO);
223
224 // stdout, stderr
225 ofd = open("/var/log/configd.log", O_WRONLY|O_APPEND, 0);
226 if (ofd != -1) {
227 if (fd > STDIN_FILENO) {
228 (void) close(fd);
229 }
230 fd = ofd;
231 }
232 (void) dup2(fd, STDOUT_FILENO);
233 (void) dup2(fd, STDERR_FILENO);
234 if (fd > STDERR_FILENO) {
235 (void) close(fd);
236 }
237 }
238
239 SCTrace(TRUE, stdout, CFSTR("start\n"));
240 return;
241 }
242
243
244 static void
245 set_trace()
246 {
247 int fd;
248
249 /* set _configd_trace */
250 fd = open("/var/log/configd.trace", O_WRONLY|O_APPEND, 0);
251 if (fd != -1) {
252 _configd_trace = fdopen(fd, "a");
253 SCTrace(TRUE, _configd_trace, CFSTR("start\n"));
254 }
255
256 return;
257 }
258
259
260 static int
261 fork_child()
262 {
263 int child_pid;
264
265 signal(SIGTERM, parent_exit);
266 child_pid = fork();
267 switch (child_pid) {
268 case -1: {
269 return -1;
270 }
271 case 0: {
272 /* child: becomes the daemon (see below) */
273 signal(SIGTERM, SIG_DFL);
274 break;
275 }
276 default: {
277 /* parent: wait for signal, then exit */
278 int status;
279
280 (void) wait4(child_pid, (int *)&status, 0, 0);
281 if (WIFEXITED(status)) {
282 fprintf(stderr,
283 "*** configd (daemon) failed to start, exit status=%d",
284 WEXITSTATUS(status));
285 } else {
286 fprintf(stderr,
287 "*** configd (daemon) failed to start, received signal=%d",
288 WTERMSIG(status));
289 }
290 fflush (stderr);
291 exit (EX_SOFTWARE);
292 }
293 }
294
295 if (setsid() == -1)
296 return -1;
297
298 (void) chdir("/");
299
300 return 0;
301 }
302
303
304 static void
305 writepid(void)
306 {
307 FILE *fp;
308
309 fp = fopen("/var/run/configd.pid", "w");
310 if (fp != NULL) {
311 fprintf(fp, "%d\n", getpid());
312 fclose(fp);
313 }
314 }
315
316
317 static CFStringRef
318 termMPCopyDescription(const void *info)
319 {
320 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SIGTERM MP>"));
321 }
322
323
324 int
325 main(int argc, char * const argv[])
326 {
327 CFMachPortContext context = { 0
328 , (void *)1
329 , NULL
330 , NULL
331 , termMPCopyDescription
332 };
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;
338 int opt;
339 extern int optind;
340 const char *prog = argv[0];
341 CFRunLoopSourceRef rls;
342 mach_port_t service_port = MACH_PORT_NULL;
343 kern_return_t status;
344 CFStringRef str;
345 const char *testBundle = NULL;
346
347 _plugins_exclude = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
348 _plugins_verbose = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
349
350 /* process any arguments */
351
352 while ((opt = getopt_long(argc, argv, "bB:dt:vV:", longopts, NULL)) != -1) {
353 switch(opt) {
354 case 'b':
355 loadBundles = FALSE;
356 break;
357 case 'B':
358 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
359 CFSetSetValue(_plugins_exclude, str);
360 CFRelease(str);
361 break;
362 case 'd':
363 forceForeground = TRUE;
364 break;
365 case 't':
366 testBundle = optarg;
367 break;
368 case 'v':
369 _configd_verbose = TRUE;
370 break;
371 case 'V':
372 if (strcmp(optarg, "com.apple.SystemConfiguration") == 0) {
373 _sc_verbose = TRUE;
374 } else {
375 str = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingMacRoman);
376 CFSetSetValue(_plugins_verbose, str);
377 CFRelease(str);
378 }
379 break;
380 case '?':
381 default :
382 usage(prog);
383 }
384 }
385 argc -= optind;
386 argv += optind;
387
388 /*
389 * display an error if configd is already running and we are
390 * not solely executing/testing a bundle.
391 */
392 if (testBundle == NULL) {
393 if (server_active(&service_port)) {
394 exit (EX_UNAVAILABLE);
395 }
396 }
397
398 /* check credentials */
399 if (getuid() != 0) {
400 fprintf(stderr, "%s: permission denied.\n", prog);
401 exit (EX_NOPERM);
402 }
403
404 if (!forceForeground && (service_port == MACH_PORT_NULL)) {
405 /*
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
409 * daemonize ourself.
410 */
411 if (fork_child() == -1) {
412 fprintf(stderr, "configd: fork() failed, %s\n", strerror(errno));
413 exit (1);
414 }
415
416 /*
417 * Note: we are now the child process. The parent
418 * waits/exits in fork_child.
419 */
420 }
421
422 /*
423 * close file descriptors, establish stdin/stdout/stderr,
424 * setup logging.
425 */
426 if (!forceForeground) {
427 int facility = LOG_DAEMON;
428 int logopt = LOG_CONS|LOG_NDELAY|LOG_PID;
429 struct stat statbuf;
430
431 if (service_port == MACH_PORT_NULL) {
432 init_fds();
433 }
434
435 if (_configd_verbose) {
436 logopt |= LOG_CONS;
437 }
438
439 if (stat("/etc/rc.cdrom", &statbuf) == 0) {
440 facility = LOG_INSTALL;
441 }
442
443 openlog("configd", logopt, facility);
444 } else {
445 _sc_log = FALSE; /* redirect SCLog() to stdout/stderr */
446 }
447
448 /* enable crash reporting */
449 allow_crash_reports();
450
451 /* check/enable trace logging */
452 set_trace();
453
454 /* record process id */
455 if (testBundle == NULL) {
456 writepid();
457 }
458
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"),
466 strerror(errno));
467 }
468
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"),
473 strerror(errno));
474 }
475
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"),
480 strerror(errno));
481 }
482
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"),
487 strerror(errno));
488 }
489
490 /* create the "shutdown requested" notification port */
491 termRequested = CFMachPortCreate(NULL, term, &context, NULL);
492
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");
502 }
503
504 /* add to our runloop */
505 rls = CFMachPortCreateRunLoopSource(NULL, termRequested, 0);
506 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
507 CFRelease(rls);
508
509 if (testBundle == NULL) {
510 /* initialize primary (store management) thread */
511 server_init(service_port, enableRestart);
512
513 if (!forceForeground && (service_port == MACH_PORT_NULL)) {
514 /* synchronize with parent process */
515 kill(getppid(), SIGTERM);
516 }
517
518 /* load/initialize/start bundles into the secondary thread */
519 if (loadBundles) {
520 /* start plug-in initialization */
521 plugin_init();
522 }
523
524 /* start primary (store management) thread */
525 server_loop();
526 } else {
527 /* load/initialize/start specified plug-in */
528 plugin_exec((void *)testBundle);
529 }
530
531 exit (EX_OK); /* insure the process exit status is 0 */
532 return 0; /* ...and make main fit the ANSI spec. */
533 }