2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file dedicated.cpp Forking support for dedicated servers. */
11 #include "fileio_func.h"
14 std::string _log_file
; ///< File to reroute output of a forked OpenTTD to
15 std::unique_ptr
<FILE, FileDeleter
> _log_fd
; ///< File to reroute output of a forked OpenTTD to
21 #include "safeguards.h"
23 #if defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)
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.
27 # define PRINTF_PID_T "%ld"
29 # define PRINTF_PID_T "%d"
34 /* Fork the program */
38 perror("Unable to fork");
41 case 0: { // We're the child
42 /* Open the log-file to log all stuff too */
43 _log_fd
.reset(fopen(_log_file
.c_str(), "a"));
45 perror("Unable to open logfile");
48 /* Redirect stdout and stderr to log-file */
49 if (dup2(fileno(_log_fd
.get()), fileno(stdout
)) == -1) {
50 perror("Rerouting stdout");
53 if (dup2(fileno(_log_fd
.get()), fileno(stderr
)) == -1) {
54 perror("Rerouting stderr");
61 /* We're the parent */
62 printf("Loading dedicated server...\n");
63 printf(" - Forked to background with pid " PRINTF_PID_T
"\n", pid
);