]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/daemon.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // demon - support code for writing UNIXoid demons
22 #include <Security/daemon.h>
23 #include <Security/logging.h>
24 #include <Security/debugging.h>
25 #include <sys/types.h>
35 // Daemonize this process, the UNIX way.
39 // fork with slight resilience
40 for (int forkTries
= 1; forkTries
<= 5; forkTries
++) {
43 // we are the daemon process (Har! Har!)
45 case -1: // parent: fork failed
49 Syslog::warning("fork() short on resources (errno=%d); retrying", errno
);
53 Syslog::error("fork() failed (errno=%d)", errno
);
57 // @@@ we could close an assurance loop here, but we don't (yet?)
62 // fork succeeded; we are the child; parent is terminating
64 // create new session (the magic set-me-apart system call)
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...
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().
84 // WARNING: Don't even THINK of doing this in a setuid-anything program.
86 bool executeSelf(char **argv
)
88 static const char reExecEnv
[] = "_RE_EXECUTE";
89 if (getenv(reExecEnv
)) { // was re-executed
90 secdebug("daemon", "self-execution complete");
94 setenv(reExecEnv
, "go", 1);
95 secdebug("daemon", "self-executing (ouch!)");
97 perror("re-execution");
98 Syslog::error("Re-execution attempt failed");
104 } // end namespace Daemon
105 } // end namespace Security