1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)daemon.c 5.4 (Berkeley) 07/31/91"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/fcntl.h> 13 #include <unistd.h> 14 #include <paths.h> 15 16 daemon(nochdir, noclose) 17 int nochdir, noclose; 18 { 19 int cpid; 20 21 if ((cpid = fork()) == -1) 22 return (-1); 23 if (cpid) 24 exit(0); 25 (void) setsid(); 26 if (!nochdir) 27 (void) chdir("/"); 28 if (!noclose) { 29 int devnull = open(_PATH_DEVNULL, O_RDWR, 0); 30 31 if (devnull != -1) { 32 (void) dup2(devnull, STDIN_FILENO); 33 (void) dup2(devnull, STDOUT_FILENO); 34 (void) dup2(devnull, STDERR_FILENO); 35 if (devnull > 2) 36 (void) close(devnull); 37 } 38 } 39 return (0); 40 } 41