OpenTTD: src/dedicated.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

dedicated.cpp
Go to the documentation of this file.
1 /* $Id: dedicated.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
12 #include "stdafx.h"
13
14 #ifdef ENABLE_NETWORK
15
16 char *_log_file = NULL;
17 FILE *_log_fd = NULL;
18
19 #if defined(UNIX) && !defined(__MORPHOS__)
20
21 #include <unistd.h>
22
23 #include "safeguards.h"
24
25 #if (defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)) || defined(__HAIKU__)
26 /* Solaris has, in certain situation, pid_t defined as long, while in other
27 * cases it has it defined as int... this handles all cases nicely.
28 * Haiku has also defined pid_t as a long.
29 */
30 # define PRINTF_PID_T "%ld"
31 #else
32 # define PRINTF_PID_T "%d"
33 #endif
34
35 void DedicatedFork()
36 {
37 /* Fork the program */
38 pid_t pid = fork();
39 switch (pid) {
40 case -1:
41 perror("Unable to fork");
42 exit(1);
43
44 case 0: { // We're the child
45 /* Open the log-file to log all stuff too */
46 _log_fd = fopen(_log_file, "a");
47 if (_log_fd == NULL) {
48 perror("Unable to open logfile");
49 exit(1);
50 }
51 /* Redirect stdout and stderr to log-file */
52 if (dup2(fileno(_log_fd), fileno(stdout)) == -1) {
53 perror("Rerouting stdout");
54 exit(1);
55 }
56 if (dup2(fileno(_log_fd), fileno(stderr)) == -1) {
57 perror("Rerouting stderr");
58 exit(1);
59 }
60 break;
61 }
62
63 default:
64 /* We're the parent */
65 printf("Loading dedicated server...\n");
66 printf(" - Forked to background with pid " PRINTF_PID_T "\n", pid);
67 exit(0);
68 }
69 }
70 #endif
71
72 #else
73
75 void DedicatedFork() {}
76
77 #endif /* ENABLE_NETWORK */