(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / dedicated.cpp
blob83c6c1d217c86af6bd247377669d8ba90ce91125
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; ///< File to reroute output of a forked OpenTTD to
18 #if defined(UNIX) && !defined(__MORPHOS__)
20 #include <unistd.h>
22 #if (defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)) || defined(__HAIKU__)
23 /* Solaris has, in certain situation, pid_t defined as long, while in other
24 * cases it has it defined as int... this handles all cases nicely.
25 * Haiku has also defined pid_t as a long.
27 # define PRINTF_PID_T "%ld"
28 #else
29 # define PRINTF_PID_T "%d"
30 #endif
32 void DedicatedFork()
34 /* Fork the program */
35 pid_t pid = fork();
36 switch (pid) {
37 case -1:
38 perror("Unable to fork");
39 exit(1);
41 case 0: { // We're the child
42 FILE *f;
44 /* Open the log-file to log all stuff too */
45 f = fopen(_log_file, "a");
46 if (f == NULL) {
47 perror("Unable to open logfile");
48 exit(1);
50 /* Redirect stdout and stderr to log-file */
51 if (dup2(fileno(f), fileno(stdout)) == -1) {
52 perror("Rerouting stdout");
53 exit(1);
55 if (dup2(fileno(f), fileno(stderr)) == -1) {
56 perror("Rerouting stderr");
57 exit(1);
59 break;
62 default:
63 /* We're the parent */
64 printf("Loading dedicated server...\n");
65 printf(" - Forked to background with pid " PRINTF_PID_T "\n", pid);
66 exit(0);
69 #endif
71 #else
73 /** Empty helper function call for NOT(UNIX and not MORPHOS) systems */
74 void DedicatedFork() {}
76 #endif /* ENABLE_NETWORK */