2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD 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, version 2.
|
|
|
|
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file dedicated.cpp Forking support for dedicated servers. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2005-07-25 16:33:58 +00:00
|
|
|
#if defined(UNIX) && !defined(__MORPHOS__)
|
2004-12-23 14:46:16 +00:00
|
|
|
|
2005-07-25 16:33:58 +00:00
|
|
|
#include "variables.h"
|
2005-07-23 17:22:39 +00:00
|
|
|
|
2005-07-25 16:33:58 +00:00
|
|
|
#include <unistd.h>
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2009-09-21 18:36:33 +00:00
|
|
|
#if (defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)) || defined(__HAIKU__)
|
2007-03-06 21:14:48 +00:00
|
|
|
/* Solaris has, in certain situation, pid_t defined as long, while in other
|
2009-09-21 18:36:33 +00:00
|
|
|
* cases it has it defined as int... this handles all cases nicely.
|
|
|
|
* Haiku has also defined pid_t as a long.
|
|
|
|
*/
|
2007-03-06 21:14:48 +00:00
|
|
|
# define PRINTF_PID_T "%ld"
|
|
|
|
#else
|
|
|
|
# define PRINTF_PID_T "%d"
|
|
|
|
#endif
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DedicatedFork()
|
2004-12-18 14:19:21 +00:00
|
|
|
{
|
|
|
|
/* Fork the program */
|
2005-02-23 09:13:12 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
2004-12-18 14:19:21 +00:00
|
|
|
case -1:
|
|
|
|
perror("Unable to fork");
|
|
|
|
exit(1);
|
2005-08-06 16:55:05 +00:00
|
|
|
|
|
|
|
case 0: { // We're the child
|
2009-01-10 00:31:47 +00:00
|
|
|
FILE *f;
|
2004-12-18 14:19:21 +00:00
|
|
|
|
|
|
|
/* Open the log-file to log all stuff too */
|
2005-08-06 16:55:05 +00:00
|
|
|
f = fopen(_log_file, "a");
|
|
|
|
if (f == NULL) {
|
2004-12-18 14:19:21 +00:00
|
|
|
perror("Unable to open logfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Redirect stdout and stderr to log-file */
|
2005-08-06 16:55:05 +00:00
|
|
|
if (dup2(fileno(f), fileno(stdout)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stdout");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-08-06 16:55:05 +00:00
|
|
|
if (dup2(fileno(f), fileno(stderr)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stderr");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2005-08-06 16:55:05 +00:00
|
|
|
}
|
|
|
|
|
2004-12-18 14:19:21 +00:00
|
|
|
default:
|
2007-02-23 18:55:07 +00:00
|
|
|
/* We're the parent */
|
2004-12-18 14:19:21 +00:00
|
|
|
printf("Loading dedicated server...\n");
|
2007-03-06 21:14:48 +00:00
|
|
|
printf(" - Forked to background with pid " PRINTF_PID_T "\n", pid);
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/** Empty helper function call for NOT(UNIX and not MORPHOS) systems */
|
2007-03-07 11:47:46 +00:00
|
|
|
void DedicatedFork() {}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|