Crossfire Server, Branch 1.12  R12190
daemon.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_daemon_c =
00003  *   "$Id: daemon.c 11578 2009-02-23 22:02:27Z lalo $";
00004  */
00005 
00006 /*
00007     CrossFire, A Multiplayer game for X-windows
00008 
00009     Copyright (C) 2002 Mark Wedel & Crossfire Development Team
00010     Copyright (C) 1992 Frank Tore Johansen
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025 
00026     The author can be reached via e-mail to crossfire-devel@real-time.com
00027 */
00028 
00056 #include <global.h>
00057 #ifndef __CEXTRACT__
00058 #include <sproto.h>
00059 #endif
00060 #include <sys/ioctl.h>
00061 #ifdef hpux
00062 #include <sys/ptyio.h>
00063 #endif
00064 
00065 #include <errno.h>
00066 #include <stdio.h>
00067 #include <sys/file.h>
00068 
00072 void become_daemon(void) {
00073     register int i;
00074     int forkresult;
00075 
00076     fputs("\n========================\n", logfile);
00077     fputs("Begin New Server Session\n", logfile);
00078     fputs("========================\n\n", logfile);
00079     /*
00080      * fork so that the process goes into the background automatically. Also
00081      * has a nice side effect of having the child process get inherited by
00082      * init (pid 1).
00083      */
00084 
00085     if ((forkresult = fork())) {   /* if parent */
00086         if (forkresult < 0) {
00087             perror("Fork error!");
00088         }
00089         exit(0);    /* then no more work to do */
00090     }
00091 
00092     /*
00093      * Close standard file descriptors and get rid of controlling tty
00094      */
00095 
00096     close(0);
00097     close(1);
00098     close(2);
00099 
00100     /*
00101      * Set up the standard file descriptors.
00102      */
00103     (void)open("/dev/null", O_RDONLY);  /* root inode already in core */
00104     (void)dup2(0, 1);
00105     (void)dup2(0, 2);
00106 
00107     if ((i = open("/dev/tty", O_RDWR)) >= 0) {  /* did open succeed? */
00108 #if (defined(SYSV) || defined(hpux)) && defined(TIOCTTY)
00109         int zero = 0;
00110         (void)ioctl(i, TIOCTTY, &zero);
00111 #else
00112 
00113 #  ifdef HAVE_SYS_TERMIOS_H
00114 #    include <sys/termios.h>
00115 #  else
00116 #    ifdef HAVE_SYS_TTYCOM_H
00117 #      include <sys/ttycom.h>
00118 #    endif
00119 #  endif
00120         (void)ioctl(i, TIOCNOTTY, (char *)0);     /* detach, BSD style */
00121 #endif
00122         (void)close(i);
00123     }
00124 
00125 #ifdef HAVE_SETSID
00126     setsid();
00127 #else
00128     /* Are these really different?  */
00129 #  if defined(SYSV) || defined(SVR4)
00130     setpgrp(0, 0);
00131 #  else /* Non SYSV machines */
00132     setpgrp(0, getpid());
00133 #  endif
00134 #endif
00135 }