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