Crossfire Server, Branches 1.12  R18729
daemon.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_daemon_c =
3  * "$Id: daemon.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The author can be reached via e-mail to crossfire-devel@real-time.com
27 */
28 
56 #include <global.h>
57 #ifndef __CEXTRACT__
58 #include <sproto.h>
59 #endif
60 #include <sys/ioctl.h>
61 #ifdef hpux
62 #include <sys/ptyio.h>
63 #endif
64 
65 #include <errno.h>
66 #include <stdio.h>
67 #include <sys/file.h>
68 
72 void become_daemon(void) {
73  register int i;
74  int forkresult;
75 
76  fputs("\n========================\n", logfile);
77  fputs("Begin New Server Session\n", logfile);
78  fputs("========================\n\n", logfile);
79  /*
80  * fork so that the process goes into the background automatically. Also
81  * has a nice side effect of having the child process get inherited by
82  * init (pid 1).
83  */
84 
85  if ((forkresult = fork())) { /* if parent */
86  if (forkresult < 0) {
87  perror("Fork error!");
88  }
89  exit(0); /* then no more work to do */
90  }
91 
92  /*
93  * Close standard file descriptors and get rid of controlling tty
94  */
95 
96  close(0);
97  close(1);
98  close(2);
99 
100  /*
101  * Set up the standard file descriptors.
102  */
103  (void)open("/dev/null", O_RDONLY); /* root inode already in core */
104  (void)dup2(0, 1);
105  (void)dup2(0, 2);
106 
107  if ((i = open("/dev/tty", O_RDWR)) >= 0) { /* did open succeed? */
108 #if (defined(SYSV) || defined(hpux)) && defined(TIOCTTY)
109  int zero = 0;
110  (void)ioctl(i, TIOCTTY, &zero);
111 #else
112 
113 # ifdef HAVE_SYS_TERMIOS_H
114 # include <sys/termios.h>
115 # else
116 # ifdef HAVE_SYS_TTYCOM_H
117 # include <sys/ttycom.h>
118 # endif
119 # endif
120  (void)ioctl(i, TIOCNOTTY, (char *)0); /* detach, BSD style */
121 #endif
122  (void)close(i);
123  }
124 
125 #ifdef HAVE_SETSID
126  setsid();
127 #else
128  /* Are these really different? */
129 # if defined(SYSV) || defined(SVR4)
130  setpgrp(0, 0);
131 # else /* Non SYSV machines */
132  setpgrp(0, getpid());
133 # endif
134 #endif
135 }
EXTERN FILE * logfile
Definition: global.h:220
void become_daemon(void)
Definition: daemon.c:72
#define getpid()
Definition: win32.h:69