# dedicated.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013
    00014 #ifdef ENABLE_NETWORK
    00015
    00016 #if defined(UNIX) && !defined(__MORPHOS__)
    00017
    00018 #include "variables.h"
    00019
    00020 #include <unistd.h>
    00021
    00022 #if (defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)) || defined(__HAIKU__)
    00023 /* Solaris has, in certain situation, pid_t defined as long, while in other
    00024  *  cases it has it defined as int... this handles all cases nicely.
    00025  * Haiku has also defined pid_t as a long.
    00026  */
    00027 # define PRINTF_PID_T "%ld"
    00028 #else
    00029 # define PRINTF_PID_T "%d"
    00030 #endif
    00031
    00032 void DedicatedFork()
    00033 {
    00034   /* Fork the program */
    00035   pid_t pid = fork();
    00036   switch (pid) {
    00037     case -1:
    00038       perror("Unable to fork");
    00039       exit(1);
    00040
    00041     case 0: { // We're the child
    00042       FILE *f;
    00043
    00044       /* Open the log-file to log all stuff too */
    00045       f = fopen(_log_file, "a");
    00046       if (f == NULL) {
    00047         perror("Unable to open logfile");
    00048         exit(1);
    00049       }
    00050       /* Redirect stdout and stderr to log-file */
    00051       if (dup2(fileno(f), fileno(stdout)) == -1) {
    00052         perror("Rerouting stdout");
    00053         exit(1);
    00054       }
    00055       if (dup2(fileno(f), fileno(stderr)) == -1) {
    00056         perror("Rerouting stderr");
    00057         exit(1);
    00058       }
    00059       break;
    00060     }
    00061
    00062     default:
    00063       /* We're the parent */
    00064       printf("Loading dedicated server...\n");
    00065       printf("  - Forked to background with pid " PRINTF_PID_T "\n", pid);
    00066       exit(0);
    00067   }
    00068 }
    00069 #endif
    00070
    00071 #else
    00072
    00074 void DedicatedFork() {}
    00075
    00076 #endif /* ENABLE_NETWORK */