]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/daemon.cpp
Security-30.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / daemon.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // demon - support code for writing UNIXoid demons
21 //
22 #ifdef __MWERKS__
23 # define _CPP_DEMON
24 #endif
25
26 #include <Security/daemon.h>
27 #include <Security/logging.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32
33 namespace Security
34 {
35
36 namespace Daemon
37 {
38
39 //
40 // Daemonize this process, the UNIX way.
41 //
42 bool incarnate()
43 {
44 // fork with slight resilience
45 for (int forkTries = 1; forkTries <= 5; forkTries++) {
46 switch (fork()) {
47 case 0: // child
48 // we are the daemon process (Har! Har!)
49 break;
50 case -1: // parent: fork failed
51 switch (errno) {
52 case EAGAIN:
53 case ENOMEM:
54 Syslog::warning("fork() short on resources (errno=%d); retrying", errno);
55 sleep(forkTries);
56 continue;
57 default:
58 Syslog::error("fork() failed (errno=%d)", errno);
59 return false;
60 }
61 default: // parent
62 // @@@ we could close an assurance loop here, but we don't (yet?)
63 exit(0);
64 }
65 }
66
67 // fork succeeded; we are the child; parent is terminating
68
69 // create new session (the magic set-me-apart system call)
70 setsid();
71
72 // redirect standard channels to /dev/null
73 close(0); // fail silently in case 0 is closed
74 if (open("/dev/null", O_RDWR, 0) == 0) { // /dev/null could be missing, I suppose...
75 dup2(0, 1);
76 dup2(0, 2);
77 }
78
79 // ready to roll
80 return true;
81 }
82
83
84 } // end namespace Daemon
85
86 } // end namespace Security