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
31#include "config.h"
32
33#include <X11/Xos.h>
34#include <sys/types.h>
35#include <errno.h>
36#include <stdlib.h>
37
38#include "os.h"
39
40/* detach */
41void
42BecomeDaemon (void)
43{
44    /* If our C library has the daemon() function, just use it. */
45#ifdef HAVE_DAEMON
46    if (daemon (0, 0) < 0) {
47	/* error */
48	FatalError("daemon() failed, %s\n", strerror(errno));
49    }
50
51    /* Open/reopen log file on stderr */
52#ifdef USE_SYSLOG
53    if (!UseSyslog)
54#endif
55	CloseErrors();
56    InitErrors();
57#else
58
59    switch (fork()) {
60    case -1:
61	/* error */
62	FatalError("daemon fork failed, %s\n", strerror(errno));
63	break;
64    case 0:
65	/* child */
66	break;
67    default:
68	/* parent */
69	exit(0);
70    }
71
72    if (setsid() == -1)
73	FatalError("setting session id for daemon failed: %s\n",
74		   strerror(errno));
75
76    chdir("/");
77
78    DetachStdio();
79#endif /* HAVE_DAEMON */
80}
81
82void
83DetachStdio (void)
84{
85    int nullfd;
86    close (0);
87    close (1);
88    close (2);
89
90    /*
91     * Set up the standard file descriptors.
92     */
93    nullfd = open ("/dev/null", O_RDWR);
94    if (nullfd == -1) {
95	FatalError("opening /dev/null failed: %s\n", strerror(errno));
96    }
97    if (nullfd != 0) {
98	if (dup2(nullfd, 0) == -1) {
99	    FatalError("dup2 of /dev/null to fd 0 failed: %s\n",
100		       strerror(errno));
101	}
102	close(nullfd);
103    }
104    if (dup2 (0, 1) == -1) {
105	FatalError("dup2 of /dev/null to fd 1 failed: %s\n",
106		   strerror(errno));
107    }
108
109#ifdef USE_SYSLOG
110    if (UseSyslog) {
111	if (dup2 (0, 2) == -1) {
112	    FatalError("dup2 of /dev/null to fd 2 failed: %s\n",
113		       strerror(errno));
114	}
115	return;
116    }
117#endif
118
119    /* open/reopen log file on stderr */
120    CloseErrors();
121    InitErrors();
122}
123