]>
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
26 #include <Security/daemon.h>
27 #include <Security/logging.h>
28 #include <sys/types.h>
40 // Daemonize this process, the UNIX way.
44 // fork with slight resilience
45 for (int forkTries
= 1; forkTries
<= 5; forkTries
++) {
48 // we are the daemon process (Har! Har!)
50 case -1: // parent: fork failed
54 Syslog::warning("fork() short on resources (errno=%d); retrying", errno
);
58 Syslog::error("fork() failed (errno=%d)", errno
);
62 // @@@ we could close an assurance loop here, but we don't (yet?)
67 // fork succeeded; we are the child; parent is terminating
69 // create new session (the magic set-me-apart system call)
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...
84 } // end namespace Daemon
86 } // end namespace Security