Atrinik Server 2.5
server/daemon.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00035 void become_daemon(char *filename)
00036 {
00037 #ifndef WIN32
00038     pid_t pid, sid;
00039     time_t now = time(NULL);
00040 
00041     /* Fork off the parent process */
00042     pid = fork();
00043 
00044     if (pid < 0)
00045     {
00046         exit(EXIT_FAILURE);
00047     }
00048 
00049     /* If we got a good PID, then
00050      * we can exit the parent process. */
00051     if (pid > 0)
00052     {
00053         exit(EXIT_SUCCESS);
00054     }
00055 
00056     /* Change the file mode mask */
00057     umask(0);
00058 
00059     logfile = fopen(filename, "a");
00060 
00061     LOG(llevInfo, "******************************************************\n");
00062     LOG(llevInfo, "* New server session initialized at %.16s *\n", ctime(&now));
00063     LOG(llevInfo, "******************************************************\n\n");
00064 
00065     /* Create a new SID for the child process */
00066     sid = setsid();
00067 
00068     if (sid < 0)
00069     {
00070         /* Log the failure */
00071         exit(EXIT_FAILURE);
00072     }
00073 
00074     /* Close out the standard file descriptors */
00075     close(STDIN_FILENO);
00076     close(STDOUT_FILENO);
00077     close(STDERR_FILENO);
00078 #else
00079     (void) filename;
00080 #endif
00081 }