1 |
elmex |
1.1 |
/* |
2 |
|
|
CrossFire, A Multiplayer game for X-windows |
3 |
|
|
|
4 |
|
|
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 |
|
|
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or modify |
8 |
|
|
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
|
21 |
root |
1.6 |
The author can be reached via e-mail to <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
/* |
25 |
|
|
* BecomeDaemon.c |
26 |
|
|
* shamelessly swiped from xdm source code. |
27 |
|
|
* Appropriate copyrights kept, and hereby follow |
28 |
|
|
* ERic mehlhaff, 3/11/92 |
29 |
|
|
* |
30 |
|
|
* xdm - display manager daemon |
31 |
|
|
* |
32 |
|
|
* $XConsortium: daemon.c,v 1.5 89/01/20 10:43:49 jim Exp $ |
33 |
|
|
* |
34 |
|
|
* Copyright 1988 Massachusetts Institute of Technology |
35 |
|
|
* |
36 |
|
|
* Permission to use, copy, modify, and distribute this software and its |
37 |
|
|
* documentation for any purpose and without fee is hereby granted, provided |
38 |
|
|
* that the above copyright notice appear in all copies and that both that |
39 |
|
|
* copyright notice and this permission notice appear in supporting |
40 |
|
|
* documentation, and that the name of M.I.T. not be used in advertising or |
41 |
|
|
* publicity pertaining to distribution of the software without specific, |
42 |
|
|
* written prior permission. M.I.T. makes no representations about the |
43 |
|
|
* suitability of this software for any purpose. It is provided "as is" |
44 |
|
|
* without express or implied warranty. |
45 |
|
|
* |
46 |
|
|
* Author: Keith Packard, MIT X Consortium |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
#include <global.h> |
51 |
root |
1.7 |
#include <sproto.h> |
52 |
elmex |
1.1 |
#include <sys/ioctl.h> |
53 |
|
|
#include <errno.h> |
54 |
|
|
#include <stdio.h> |
55 |
|
|
#include <sys/file.h> |
56 |
|
|
|
57 |
root |
1.5 |
FILE * BecomeDaemon (const char *filename) |
58 |
elmex |
1.1 |
{ |
59 |
|
|
FILE *logfile; |
60 |
|
|
int forkresult; |
61 |
|
|
|
62 |
root |
1.5 |
if ((logfile = fopen (filename, "a")) == NULL) |
63 |
|
|
{ |
64 |
|
|
fprintf (stderr, "Couldn't create logfile %s: %s\n", filename, strerror (errno)); |
65 |
|
|
exit (0); |
66 |
|
|
} |
67 |
|
|
fputs ("\n========================\n", logfile); |
68 |
|
|
fputs ("Begin New Server Session\n", logfile); |
69 |
|
|
fputs ("========================\n\n", logfile); |
70 |
|
|
fflush (logfile); |
71 |
|
|
/* |
72 |
|
|
* fork so that the process goes into the background automatically. Also |
73 |
|
|
* has a nice side effect of having the child process get inherited by |
74 |
|
|
* init (pid 1). |
75 |
|
|
*/ |
76 |
|
|
|
77 |
|
|
if ((forkresult = fork ())) |
78 |
|
|
{ /* if parent */ |
79 |
|
|
if (forkresult < 0) |
80 |
|
|
{ |
81 |
|
|
perror ("Fork error!"); |
82 |
|
|
} |
83 |
|
|
exit (0); /* then no more work to do */ |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
/* |
87 |
|
|
* Close standard file descriptors and get rid of controlling tty |
88 |
|
|
*/ |
89 |
|
|
|
90 |
|
|
close (0); |
91 |
|
|
close (1); |
92 |
|
|
close (2); |
93 |
|
|
|
94 |
|
|
/* |
95 |
|
|
* Set up the standard file descriptors. |
96 |
|
|
*/ |
97 |
|
|
(void) open ("/", O_RDONLY); /* root inode already in core */ |
98 |
|
|
(void) dup2 (0, 1); |
99 |
|
|
(void) dup2 (0, 2); |
100 |
|
|
|
101 |
|
|
setsid (); |
102 |
|
|
return (logfile); |
103 |
elmex |
1.1 |
} |