]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/daemon.cpp
Security-164.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 #include <Security/daemon.h>
23 #include <Security/logging.h>
24 #include <Security/debugging.h>
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 namespace Security {
31 namespace Daemon {
32
33
34 //
35 // Daemonize this process, the UNIX way.
36 //
37 bool incarnate()
38 {
39 // fork with slight resilience
40 for (int forkTries = 1; forkTries <= 5; forkTries++) {
41 switch (fork()) {
42 case 0: // child
43 // we are the daemon process (Har! Har!)
44 break;
45 case -1: // parent: fork failed
46 switch (errno) {
47 case EAGAIN:
48 case ENOMEM:
49 Syslog::warning("fork() short on resources (errno=%d); retrying", errno);
50 sleep(forkTries);
51 continue;
52 default:
53 Syslog::error("fork() failed (errno=%d)", errno);
54 return false;
55 }
56 default: // parent
57 // @@@ we could close an assurance loop here, but we don't (yet?)
58 exit(0);
59 }
60 }
61
62 // fork succeeded; we are the child; parent is terminating
63
64 // create new session (the magic set-me-apart system call)
65 setsid();
66
67 // redirect standard channels to /dev/null
68 close(0); // fail silently in case 0 is closed
69 if (open("/dev/null", O_RDWR, 0) == 0) { // /dev/null could be missing, I suppose...
70 dup2(0, 1);
71 dup2(0, 2);
72 }
73
74 // ready to roll
75 return true;
76 }
77
78
79 //
80 // Re-execute myself.
81 // This is a pretty bad hack for libraries that are pretty broken and (essentially)
82 // don't work after a fork() unless you also exec().
83 //
84 // WARNING: Don't even THINK of doing this in a setuid-anything program.
85 //
86 bool executeSelf(char **argv)
87 {
88 static const char reExecEnv[] = "_RE_EXECUTE";
89 if (getenv(reExecEnv)) { // was re-executed
90 secdebug("daemon", "self-execution complete");
91 unsetenv(reExecEnv);
92 return true;
93 } else {
94 setenv(reExecEnv, "go", 1);
95 secdebug("daemon", "self-executing (ouch!)");
96 execv(argv[0], argv);
97 perror("re-execution");
98 Syslog::error("Re-execution attempt failed");
99 return false;
100 }
101 }
102
103
104 } // end namespace Daemon
105 } // end namespace Security