Turn a conditional into an assertion in ConLoad
[openttd/fttd.git] / src / dedicated.cpp
blob6342bc24768809b1888557f49a7eaecf21b67d0d
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file dedicated.cpp Forking support for dedicated servers. */
12 #include "stdafx.h"
14 #ifdef ENABLE_NETWORK
16 char *_log_file = NULL; ///< File to reroute output of a forked OpenTTD to
17 FILE *_log_fd = NULL; ///< File to reroute output of a forked OpenTTD to
19 #if defined(UNIX) && !defined(__MORPHOS__)
21 #include <unistd.h>
23 #if (defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)) || defined(__HAIKU__)
24 /* Solaris has, in certain situation, pid_t defined as long, while in other
25 * cases it has it defined as int... this handles all cases nicely.
26 * Haiku has also defined pid_t as a long.
28 # define PRINTF_PID_T "%ld"
29 #else
30 # define PRINTF_PID_T "%d"
31 #endif
33 void DedicatedFork()
35 /* Fork the program */
36 pid_t pid = fork();
37 switch (pid) {
38 case -1:
39 perror("Unable to fork");
40 exit(1);
42 case 0: { // We're the child
43 /* Open the log-file to log all stuff too */
44 _log_fd = fopen(_log_file, "a");
45 if (_log_fd == NULL) {
46 perror("Unable to open logfile");
47 exit(1);
49 /* Redirect stdout and stderr to log-file */
50 if (dup2(fileno(_log_fd), fileno(stdout)) == -1) {
51 perror("Rerouting stdout");
52 exit(1);
54 if (dup2(fileno(_log_fd), fileno(stderr)) == -1) {
55 perror("Rerouting stderr");
56 exit(1);
58 break;
61 default:
62 /* We're the parent */
63 printf("Loading dedicated server...\n");
64 printf(" - Forked to background with pid " PRINTF_PID_T "\n", pid);
65 exit(0);
68 #endif
70 #else
72 /** Empty helper function call for NOT(UNIX and not MORPHOS) systems */
73 void DedicatedFork() {}
75 #endif /* ENABLE_NETWORK */