daemon.c revision bbe1b32b
1/* 2 3Copyright (c) 1988 X Consortium 4 5Permission is hereby granted, free of charge, to any person obtaining 6a copy of this software and associated documentation files (the 7"Software"), to deal in the Software without restriction, including 8without limitation the rights to use, copy, modify, merge, publish, 9distribute, sublicense, and/or sell copies of the Software, and to 10permit persons to whom the Software is furnished to do so, subject to 11the following conditions: 12 13The above copyright notice and this permission notice shall be included 14in all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR 20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22OTHER DEALINGS IN THE SOFTWARE. 23 24Except as contained in this notice, the name of the X Consortium shall 25not be used in advertising or otherwise to promote the sale, use or 26other dealings in this Software without prior written authorization 27from the X Consortium. 28 29*/ 30/* $XFree86: xc/programs/xfs/os/daemon.c,v 1.12 2002/10/20 21:42:50 tsi Exp $ */ 31 32#include <X11/Xos.h> 33#include <sys/types.h> 34#include <errno.h> 35#include <stdlib.h> 36 37#if defined(USG) 38# include <termios.h> 39#else 40# include <sys/ioctl.h> 41#endif 42#ifdef hpux 43# include <sys/ptyio.h> 44#endif 45 46#ifdef X_NOT_POSIX 47# define Pid_t int 48#else 49# define Pid_t pid_t 50#endif 51 52#include "os.h" 53 54#if defined(__GLIBC__) || defined(CSRG_BASED) 55#define HAS_DAEMON 56#endif 57 58#ifndef X_NOT_POSIX 59#define HAS_SETSID 60#endif 61 62#ifndef HAS_SETSID 63 64#define setsid() MySetsid() 65 66static Pid_t 67MySetsid(void) 68{ 69#if defined(TIOCNOTTY) || defined(TCCLRCTTY) || defined(TIOCTTY) 70 int fd; 71#endif 72 int stat; 73 74 fd = open("/dev/tty", O_RDWR); 75 if (fd >= 0) { 76#if defined(USG) && defined(TCCLRCTTY) 77 int zero = 0; 78 (void) ioctl (fd, TCCLRCTTY, &zero); 79#elif (defined(SYSV) || defined(SVR4)) && defined(TIOCTTY) 80 int zero = 0; 81 (void) ioctl (i, TIOCTTY, &zero); 82#elif defined(TIOCNOTTY) 83 (void) ioctl (i, TIOCNOTTY, (char *) 0); /* detach, BSD style */ 84#endif 85 close(fd); 86 } 87 88#if defined(SYSV) || defined(__QNXNTO__) 89 return setpgrp(); 90#else 91 return setpgid(0, getpid()); 92#endif 93} 94 95#endif /* !HAS_SETSID */ 96 97 98/* detach */ 99void 100BecomeDaemon () 101{ 102 /* If our C library has the daemon() function, just use it. */ 103#ifdef HAS_DAEMON 104 daemon (0, 0); 105#else 106 107 switch (fork()) { 108 case -1: 109 /* error */ 110 FatalError("daemon fork failed, %s\n", strerror(errno)); 111 break; 112 case 0: 113 /* child */ 114 break; 115 default: 116 /* parent */ 117 exit(0); 118 } 119 120 if (setsid() == -1) 121 FatalError("setting session id for daemon failed: %s\n", 122 strerror(errno)); 123 124 chdir("/"); 125 126 close (0); 127 close (1); 128 close (2); 129 130 /* 131 * Set up the standard file descriptors. 132 */ 133 (void) open ("/dev/null", O_RDWR); 134 (void) dup2 (0, 1); 135 (void) dup2 (0, 2); 136 137#endif /* HAS_DAEMON */ 138} 139