diff --git a/pokerth_server.pro b/pokerth_server.pro index 700d5a68..f493553d 100644 --- a/pokerth_server.pro +++ b/pokerth_server.pro @@ -107,6 +107,7 @@ win32{ } !win32{ DEPENDPATH += src/net/linux/ src/core/linux + SOURCES += src/core/linux/daemon.c } unix:!mac{ diff --git a/src/core/linux/daemon.c b/src/core/linux/daemon.c new file mode 100644 index 00000000..84794277 --- /dev/null +++ b/src/core/linux/daemon.c @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2007 by Lothar May * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifdef _WIN32 +#error This source code is not for Win32. +#endif + +int +daemon() +{ + int maxfd, fd = 0; + + if (getppid() == 1) + return 0; /* run_as_daemon has already been called. */ + + switch (fork()) + { + case 0: /* The child process. */ + break; + case -1: /* Fork failed. */ + return -1; + default: /* Exit the parent. */ + _exit(0); + } + /* Now running the child (daemon). */ + + if (setsid() < 0) /* Obtain a new process group. */ + return -1; + + switch (fork()) + { + case 0: + break; + case -1: + return -1; + default: + _exit(0); + } + + + chdir("/"); /* Change working directory. */ + + /* Close all open handles. */ + maxfd = sysconf(_SC_OPEN_MAX); + while (fd < maxfd) + close(fd++); + + /* Use /dev/null as stdin/stdout/stderr. */ + open("/dev/null", O_RDWR); + dup(0); + dup(0); + + /* Set default permissions for created files. */ + umask(027); + + return 0; +} + + diff --git a/src/pokerth_server.cpp b/src/pokerth_server.cpp index be3fc9b9..d6e80712 100644 --- a/src/pokerth_server.cpp +++ b/src/pokerth_server.cpp @@ -56,6 +56,11 @@ TerminateHandler(int signal) g_pokerthTerminate = 1; } +// TODO: Hack +#ifndef _WIN32 +int daemon(); +#endif + int main(int argc, char *argv[]) { @@ -63,6 +68,11 @@ main(int argc, char *argv[]) //_CrtSetBreakAlloc(164); + // TODO: Hack +#ifndef _WIN32 + daemon(); +#endif + signal(SIGTERM, TerminateHandler); signal(SIGINT, TerminateHandler);