2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file dedicated.cpp Forking support for dedicated servers. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "stdafx.h"
|
2020-05-17 21:32:10 +00:00
|
|
|
#include "fileio_func.h"
|
|
|
|
#include <string>
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2020-05-17 21:32:10 +00:00
|
|
|
std::string _log_file; ///< File to reroute output of a forked OpenTTD to
|
|
|
|
std::unique_ptr<FILE, FileDeleter> _log_fd; ///< File to reroute output of a forked OpenTTD to
|
2010-07-19 17:46:53 +00:00
|
|
|
|
2019-03-04 18:21:13 +00:00
|
|
|
#if defined(UNIX)
|
2004-12-23 14:46:16 +00:00
|
|
|
|
2005-07-25 16:33:58 +00:00
|
|
|
#include <unistd.h>
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2019-03-04 19:51:46 +00:00
|
|
|
#if defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)
|
2007-03-06 21:14:48 +00:00
|
|
|
/* Solaris has, in certain situation, pid_t defined as long, while in other
|
2009-09-21 18:36:33 +00:00
|
|
|
* cases it has it defined as int... this handles all cases nicely.
|
|
|
|
*/
|
2007-03-06 21:14:48 +00:00
|
|
|
# define PRINTF_PID_T "%ld"
|
|
|
|
#else
|
|
|
|
# define PRINTF_PID_T "%d"
|
|
|
|
#endif
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DedicatedFork()
|
2004-12-18 14:19:21 +00:00
|
|
|
{
|
|
|
|
/* Fork the program */
|
2005-02-23 09:13:12 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
2004-12-18 14:19:21 +00:00
|
|
|
case -1:
|
|
|
|
perror("Unable to fork");
|
|
|
|
exit(1);
|
2005-08-06 16:55:05 +00:00
|
|
|
|
|
|
|
case 0: { // We're the child
|
2004-12-18 14:19:21 +00:00
|
|
|
/* Open the log-file to log all stuff too */
|
2020-05-17 21:32:10 +00:00
|
|
|
_log_fd.reset(fopen(_log_file.c_str(), "a"));
|
|
|
|
if (!_log_fd) {
|
2004-12-18 14:19:21 +00:00
|
|
|
perror("Unable to open logfile");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Redirect stdout and stderr to log-file */
|
2020-05-17 21:32:10 +00:00
|
|
|
if (dup2(fileno(_log_fd.get()), fileno(stdout)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stdout");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-05-17 21:32:10 +00:00
|
|
|
if (dup2(fileno(_log_fd.get()), fileno(stderr)) == -1) {
|
2005-04-02 16:46:23 +00:00
|
|
|
perror("Rerouting stderr");
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2005-08-06 16:55:05 +00:00
|
|
|
}
|
|
|
|
|
2004-12-18 14:19:21 +00:00
|
|
|
default:
|
2007-02-23 18:55:07 +00:00
|
|
|
/* We're the parent */
|
2004-12-18 14:19:21 +00:00
|
|
|
printf("Loading dedicated server...\n");
|
2007-03-06 21:14:48 +00:00
|
|
|
printf(" - Forked to background with pid " PRINTF_PID_T "\n", pid);
|
2004-12-18 14:19:21 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
#endif
|