daemon.c revision bbe1b32b
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*/
30bbe1b32bSmrg/* $XFree86: xc/programs/xfs/os/daemon.c,v 1.12 2002/10/20 21:42:50 tsi Exp $ */
31bbe1b32bSmrg
32bbe1b32bSmrg#include <X11/Xos.h>
33bbe1b32bSmrg#include <sys/types.h>
34bbe1b32bSmrg#include <errno.h>
35bbe1b32bSmrg#include <stdlib.h>
36bbe1b32bSmrg
37bbe1b32bSmrg#if defined(USG)
38bbe1b32bSmrg# include <termios.h>
39bbe1b32bSmrg#else
40bbe1b32bSmrg# include <sys/ioctl.h>
41bbe1b32bSmrg#endif
42bbe1b32bSmrg#ifdef hpux
43bbe1b32bSmrg# include <sys/ptyio.h>
44bbe1b32bSmrg#endif
45bbe1b32bSmrg
46bbe1b32bSmrg#ifdef X_NOT_POSIX
47bbe1b32bSmrg# define Pid_t int
48bbe1b32bSmrg#else
49bbe1b32bSmrg# define Pid_t pid_t
50bbe1b32bSmrg#endif
51bbe1b32bSmrg
52bbe1b32bSmrg#include "os.h"
53bbe1b32bSmrg
54bbe1b32bSmrg#if defined(__GLIBC__) || defined(CSRG_BASED)
55bbe1b32bSmrg#define HAS_DAEMON
56bbe1b32bSmrg#endif
57bbe1b32bSmrg
58bbe1b32bSmrg#ifndef X_NOT_POSIX
59bbe1b32bSmrg#define HAS_SETSID
60bbe1b32bSmrg#endif
61bbe1b32bSmrg
62bbe1b32bSmrg#ifndef HAS_SETSID
63bbe1b32bSmrg
64bbe1b32bSmrg#define setsid() MySetsid()
65bbe1b32bSmrg
66bbe1b32bSmrgstatic Pid_t
67bbe1b32bSmrgMySetsid(void)
68bbe1b32bSmrg{
69bbe1b32bSmrg#if defined(TIOCNOTTY) || defined(TCCLRCTTY) || defined(TIOCTTY)
70bbe1b32bSmrg    int fd;
71bbe1b32bSmrg#endif
72bbe1b32bSmrg    int stat;
73bbe1b32bSmrg
74bbe1b32bSmrg    fd = open("/dev/tty", O_RDWR);
75bbe1b32bSmrg    if (fd >= 0) {
76bbe1b32bSmrg#if defined(USG) && defined(TCCLRCTTY)
77bbe1b32bSmrg	int zero = 0;
78bbe1b32bSmrg	(void) ioctl (fd, TCCLRCTTY, &zero);
79bbe1b32bSmrg#elif (defined(SYSV) || defined(SVR4)) && defined(TIOCTTY)
80bbe1b32bSmrg	int zero = 0;
81bbe1b32bSmrg	(void) ioctl (i, TIOCTTY, &zero);
82bbe1b32bSmrg#elif defined(TIOCNOTTY)
83bbe1b32bSmrg	(void) ioctl (i, TIOCNOTTY, (char *) 0);    /* detach, BSD style */
84bbe1b32bSmrg#endif
85bbe1b32bSmrg        close(fd);
86bbe1b32bSmrg    }
87bbe1b32bSmrg
88bbe1b32bSmrg#if defined(SYSV) || defined(__QNXNTO__)
89bbe1b32bSmrg    return setpgrp();
90bbe1b32bSmrg#else
91bbe1b32bSmrg    return setpgid(0, getpid());
92bbe1b32bSmrg#endif
93bbe1b32bSmrg}
94bbe1b32bSmrg
95bbe1b32bSmrg#endif /* !HAS_SETSID */
96bbe1b32bSmrg
97bbe1b32bSmrg
98bbe1b32bSmrg/* detach */
99bbe1b32bSmrgvoid
100bbe1b32bSmrgBecomeDaemon ()
101bbe1b32bSmrg{
102bbe1b32bSmrg    /* If our C library has the daemon() function, just use it. */
103bbe1b32bSmrg#ifdef HAS_DAEMON
104bbe1b32bSmrg    daemon (0, 0);
105bbe1b32bSmrg#else
106bbe1b32bSmrg
107bbe1b32bSmrg    switch (fork()) {
108bbe1b32bSmrg    case -1:
109bbe1b32bSmrg	/* error */
110bbe1b32bSmrg	FatalError("daemon fork failed, %s\n", strerror(errno));
111bbe1b32bSmrg	break;
112bbe1b32bSmrg    case 0:
113bbe1b32bSmrg	/* child */
114bbe1b32bSmrg	break;
115bbe1b32bSmrg    default:
116bbe1b32bSmrg	/* parent */
117bbe1b32bSmrg	exit(0);
118bbe1b32bSmrg    }
119bbe1b32bSmrg
120bbe1b32bSmrg    if (setsid() == -1)
121bbe1b32bSmrg	FatalError("setting session id for daemon failed: %s\n",
122bbe1b32bSmrg		   strerror(errno));
123bbe1b32bSmrg
124bbe1b32bSmrg    chdir("/");
125bbe1b32bSmrg
126bbe1b32bSmrg    close (0);
127bbe1b32bSmrg    close (1);
128bbe1b32bSmrg    close (2);
129bbe1b32bSmrg
130bbe1b32bSmrg    /*
131bbe1b32bSmrg     * Set up the standard file descriptors.
132bbe1b32bSmrg     */
133bbe1b32bSmrg    (void) open ("/dev/null", O_RDWR);
134bbe1b32bSmrg    (void) dup2 (0, 1);
135bbe1b32bSmrg    (void) dup2 (0, 2);
136bbe1b32bSmrg
137bbe1b32bSmrg#endif /* HAS_DAEMON */
138bbe1b32bSmrg}
139