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
; ///< Filename to reroute output of a forked OpenTTD to
15 std::optional
<FileHandle
> _log_fd
; ///< File to reroute output of a forked OpenTTD to
21 #include "safeguards.h"
25 /* Fork the program */
29 perror("Unable to fork");
32 case 0: { // We're the child
33 /* Open the log-file to log all stuff too */
34 _log_fd
= FileHandle::Open(_log_file
, "a");
35 if (!_log_fd
.has_value()) {
36 perror("Unable to open logfile");
39 /* Redirect stdout and stderr to log-file */
40 if (dup2(fileno(*_log_fd
), fileno(stdout
)) == -1) {
41 perror("Rerouting stdout");
44 if (dup2(fileno(*_log_fd
), fileno(stderr
)) == -1) {
45 perror("Rerouting stderr");
52 /* We're the parent */
53 Debug(net
, 0, "Loading dedicated server...");
54 Debug(net
, 0, " - Forked to background with pid {}", pid
);