ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/daemon.C
Revision: 1.5
Committed: Sun Sep 10 15:59:57 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +46 -43 lines
Log Message:
indent

File Contents

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