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 |
The author can be reached via e-mail to <crossfire@schmorp.de> |
22 |
*/ |
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 |
#ifndef __CEXTRACT__ |
52 |
# include <sproto.h> |
53 |
#endif |
54 |
#include <sys/ioctl.h> |
55 |
#ifdef hpux |
56 |
# include <sys/ptyio.h> |
57 |
#endif |
58 |
|
59 |
#include <errno.h> |
60 |
#include <stdio.h> |
61 |
#include <sys/file.h> |
62 |
|
63 |
FILE * BecomeDaemon (const char *filename) |
64 |
{ |
65 |
FILE *logfile; |
66 |
int forkresult; |
67 |
|
68 |
if ((logfile = fopen (filename, "a")) == NULL) |
69 |
{ |
70 |
fprintf (stderr, "Couldn't create logfile %s: %s\n", filename, strerror (errno)); |
71 |
exit (0); |
72 |
} |
73 |
fputs ("\n========================\n", logfile); |
74 |
fputs ("Begin New Server Session\n", logfile); |
75 |
fputs ("========================\n\n", logfile); |
76 |
fflush (logfile); |
77 |
/* |
78 |
* fork so that the process goes into the background automatically. Also |
79 |
* has a nice side effect of having the child process get inherited by |
80 |
* init (pid 1). |
81 |
*/ |
82 |
|
83 |
if ((forkresult = fork ())) |
84 |
{ /* if parent */ |
85 |
if (forkresult < 0) |
86 |
{ |
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 ("/", O_RDONLY); /* root inode already in core */ |
104 |
(void) dup2 (0, 1); |
105 |
(void) dup2 (0, 2); |
106 |
|
107 |
setsid (); |
108 |
return (logfile); |
109 |
} |