1 |
root |
1.1 |
/* |
2 |
|
|
* static char *rcsid_daemon_c = |
3 |
|
|
* "$Id: daemon.c,v 1.6 2005/09/04 16:58:12 akirschbaum Exp $"; |
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 |
|
|
|
29 |
|
|
/* |
30 |
|
|
* BecomeDaemon.c |
31 |
|
|
* shamelessly swiped from xdm source code. |
32 |
|
|
* Appropriate copyrights kept, and hereby follow |
33 |
|
|
* ERic mehlhaff, 3/11/92 |
34 |
|
|
* |
35 |
|
|
* xdm - display manager daemon |
36 |
|
|
* |
37 |
|
|
* $XConsortium: daemon.c,v 1.5 89/01/20 10:43:49 jim Exp $ |
38 |
|
|
* |
39 |
|
|
* Copyright 1988 Massachusetts Institute of Technology |
40 |
|
|
* |
41 |
|
|
* Permission to use, copy, modify, and distribute this software and its |
42 |
|
|
* documentation for any purpose and without fee is hereby granted, provided |
43 |
|
|
* that the above copyright notice appear in all copies and that both that |
44 |
|
|
* copyright notice and this permission notice appear in supporting |
45 |
|
|
* documentation, and that the name of M.I.T. not be used in advertising or |
46 |
|
|
* publicity pertaining to distribution of the software without specific, |
47 |
|
|
* written prior permission. M.I.T. makes no representations about the |
48 |
|
|
* suitability of this software for any purpose. It is provided "as is" |
49 |
|
|
* without express or implied warranty. |
50 |
|
|
* |
51 |
|
|
* Author: Keith Packard, MIT X Consortium |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
#include <global.h> |
56 |
|
|
#ifndef __CEXTRACT__ |
57 |
|
|
#include <sproto.h> |
58 |
|
|
#endif |
59 |
|
|
#include <sys/ioctl.h> |
60 |
|
|
#ifdef hpux |
61 |
|
|
#include <sys/ptyio.h> |
62 |
|
|
#endif |
63 |
|
|
|
64 |
|
|
#include <errno.h> |
65 |
|
|
#include <stdio.h> |
66 |
|
|
#include <sys/file.h> |
67 |
|
|
|
68 |
|
|
FILE *BecomeDaemon (char *filename) |
69 |
|
|
{ |
70 |
|
|
FILE *logfile; |
71 |
|
|
register int i; |
72 |
|
|
int forkresult; |
73 |
|
|
|
74 |
|
|
if((logfile=fopen(filename,"a"))==NULL){ |
75 |
|
|
fprintf(stderr, "Couldn't create logfile %s: %s\n", filename, strerror_local(errno)); |
76 |
|
|
exit(0); |
77 |
|
|
} |
78 |
|
|
fputs("\n========================\n",logfile); |
79 |
|
|
fputs("Begin New Server Session\n",logfile); |
80 |
|
|
fputs("========================\n\n",logfile); |
81 |
|
|
fflush(logfile); |
82 |
|
|
/* |
83 |
|
|
* fork so that the process goes into the background automatically. Also |
84 |
|
|
* has a nice side effect of having the child process get inherited by |
85 |
|
|
* init (pid 1). |
86 |
|
|
*/ |
87 |
|
|
|
88 |
|
|
if ( (forkresult = fork ()) ){ /* if parent */ |
89 |
|
|
if(forkresult < 0 ){ |
90 |
|
|
perror("Fork error!"); |
91 |
|
|
} |
92 |
|
|
exit (0); /* then no more work to do */ |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/* |
96 |
|
|
* Close standard file descriptors and get rid of controlling tty |
97 |
|
|
*/ |
98 |
|
|
|
99 |
|
|
close (0); |
100 |
|
|
close (1); |
101 |
|
|
close (2); |
102 |
|
|
|
103 |
|
|
/* |
104 |
|
|
* Set up the standard file descriptors. |
105 |
|
|
*/ |
106 |
|
|
(void) open ("/", O_RDONLY); /* root inode already in core */ |
107 |
|
|
(void) dup2 (0, 1); |
108 |
|
|
(void) dup2 (0, 2); |
109 |
|
|
|
110 |
|
|
if ((i = open ("/dev/tty", O_RDWR)) >= 0) { /* did open succeed? */ |
111 |
|
|
#if (defined(SYSV) || defined(hpux)) && defined(TIOCTTY) |
112 |
|
|
int zero = 0; |
113 |
|
|
(void) ioctl (i, TIOCTTY, &zero); |
114 |
|
|
#else |
115 |
|
|
|
116 |
|
|
# ifdef HAVE_SYS_TERMIOS_H |
117 |
|
|
# include <sys/termios.h> |
118 |
|
|
# else |
119 |
|
|
# ifdef HAVE_SYS_TTYCOM_H |
120 |
|
|
# include <sys/ttycom.h> |
121 |
|
|
# endif |
122 |
|
|
# endif |
123 |
|
|
(void) ioctl (i, TIOCNOTTY, (char *) 0); /* detach, BSD style */ |
124 |
|
|
#endif |
125 |
|
|
(void) close (i); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
#ifdef HAVE_SETSID |
130 |
|
|
setsid(); |
131 |
|
|
#else |
132 |
|
|
/* Are these really different? */ |
133 |
|
|
# if defined(SYSV) || defined(SVR4) |
134 |
|
|
setpgrp (0, 0); |
135 |
|
|
# else /* Non SYSV machines */ |
136 |
|
|
setpgrp (0, getpid()); |
137 |
|
|
# endif |
138 |
|
|
#endif |
139 |
|
|
return(logfile); |
140 |
|
|
} |