1bbe1b32bSmrg/* 2bbe1b32bSmrg 3bbe1b32bSmrgCopyright (c) 1988 X Consortium 4bbe1b32bSmrg 5bbe1b32bSmrgPermission is hereby granted, free of charge, to any person obtaining 6bbe1b32bSmrga copy of this software and associated documentation files (the 7bbe1b32bSmrg"Software"), to deal in the Software without restriction, including 8bbe1b32bSmrgwithout limitation the rights to use, copy, modify, merge, publish, 9bbe1b32bSmrgdistribute, sublicense, and/or sell copies of the Software, and to 10bbe1b32bSmrgpermit persons to whom the Software is furnished to do so, subject to 11bbe1b32bSmrgthe following conditions: 12bbe1b32bSmrg 13bbe1b32bSmrgThe above copyright notice and this permission notice shall be included 14bbe1b32bSmrgin all copies or substantial portions of the Software. 15bbe1b32bSmrg 16bbe1b32bSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17bbe1b32bSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18bbe1b32bSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19bbe1b32bSmrgIN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR 20bbe1b32bSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21bbe1b32bSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22bbe1b32bSmrgOTHER DEALINGS IN THE SOFTWARE. 23bbe1b32bSmrg 24bbe1b32bSmrgExcept as contained in this notice, the name of the X Consortium shall 25bbe1b32bSmrgnot be used in advertising or otherwise to promote the sale, use or 26bbe1b32bSmrgother dealings in this Software without prior written authorization 27bbe1b32bSmrgfrom the X Consortium. 28bbe1b32bSmrg 29bbe1b32bSmrg*/ 30ce6676dbSmrg 3134f90d55Smrg#include "config.h" 32bbe1b32bSmrg 33bbe1b32bSmrg#include <X11/Xos.h> 34bbe1b32bSmrg#include <sys/types.h> 35bbe1b32bSmrg#include <errno.h> 36bbe1b32bSmrg#include <stdlib.h> 37bbe1b32bSmrg 38bbe1b32bSmrg#include "os.h" 39bbe1b32bSmrg 40bbe1b32bSmrg/* detach */ 41bbe1b32bSmrgvoid 42ce6676dbSmrgBecomeDaemon (void) 43bbe1b32bSmrg{ 44bbe1b32bSmrg /* If our C library has the daemon() function, just use it. */ 45ce6676dbSmrg#ifdef HAVE_DAEMON 46ce6676dbSmrg if (daemon (0, 0) < 0) { 47ce6676dbSmrg /* error */ 48ce6676dbSmrg FatalError("daemon() failed, %s\n", strerror(errno)); 49ce6676dbSmrg } 50ce6676dbSmrg 51ce6676dbSmrg /* Open/reopen log file on stderr */ 52ce6676dbSmrg#ifdef USE_SYSLOG 53ce6676dbSmrg if (!UseSyslog) 54ce6676dbSmrg#endif 55ce6676dbSmrg CloseErrors(); 56ce6676dbSmrg InitErrors(); 57bbe1b32bSmrg#else 58bbe1b32bSmrg 59bbe1b32bSmrg switch (fork()) { 60bbe1b32bSmrg case -1: 61bbe1b32bSmrg /* error */ 62bbe1b32bSmrg FatalError("daemon fork failed, %s\n", strerror(errno)); 63bbe1b32bSmrg break; 64bbe1b32bSmrg case 0: 65bbe1b32bSmrg /* child */ 66bbe1b32bSmrg break; 67bbe1b32bSmrg default: 68bbe1b32bSmrg /* parent */ 69bbe1b32bSmrg exit(0); 70bbe1b32bSmrg } 71bbe1b32bSmrg 72bbe1b32bSmrg if (setsid() == -1) 73bbe1b32bSmrg FatalError("setting session id for daemon failed: %s\n", 74bbe1b32bSmrg strerror(errno)); 75bbe1b32bSmrg 76bbe1b32bSmrg chdir("/"); 77bbe1b32bSmrg 78ce6676dbSmrg DetachStdio(); 79ce6676dbSmrg#endif /* HAVE_DAEMON */ 80ce6676dbSmrg} 81ce6676dbSmrg 82ce6676dbSmrgvoid 83ce6676dbSmrgDetachStdio (void) 84ce6676dbSmrg{ 85ce6676dbSmrg int nullfd; 86bbe1b32bSmrg close (0); 87bbe1b32bSmrg close (1); 88bbe1b32bSmrg close (2); 89bbe1b32bSmrg 90bbe1b32bSmrg /* 91bbe1b32bSmrg * Set up the standard file descriptors. 92bbe1b32bSmrg */ 93ce6676dbSmrg nullfd = open ("/dev/null", O_RDWR); 94e1db7cd1Smrg if (nullfd == -1) { 95e1db7cd1Smrg FatalError("opening /dev/null failed: %s\n", strerror(errno)); 96e1db7cd1Smrg } 97ce6676dbSmrg if (nullfd != 0) { 98e1db7cd1Smrg if (dup2(nullfd, 0) == -1) { 99e1db7cd1Smrg FatalError("dup2 of /dev/null to fd 0 failed: %s\n", 100e1db7cd1Smrg strerror(errno)); 101e1db7cd1Smrg } 102ce6676dbSmrg close(nullfd); 103ce6676dbSmrg } 104e1db7cd1Smrg if (dup2 (0, 1) == -1) { 105e1db7cd1Smrg FatalError("dup2 of /dev/null to fd 1 failed: %s\n", 106e1db7cd1Smrg strerror(errno)); 107e1db7cd1Smrg } 108ce6676dbSmrg 109ce6676dbSmrg#ifdef USE_SYSLOG 110ce6676dbSmrg if (UseSyslog) { 111e1db7cd1Smrg if (dup2 (0, 2) == -1) { 112e1db7cd1Smrg FatalError("dup2 of /dev/null to fd 2 failed: %s\n", 113e1db7cd1Smrg strerror(errno)); 114e1db7cd1Smrg } 115ce6676dbSmrg return; 116ce6676dbSmrg } 117ce6676dbSmrg#endif 118bbe1b32bSmrg 119ce6676dbSmrg /* open/reopen log file on stderr */ 120ce6676dbSmrg CloseErrors(); 121ce6676dbSmrg InitErrors(); 122bbe1b32bSmrg} 123