src/dedicated.cpp
changeset 5835 e0ff603ae0b7
parent 5726 8f399788f6c9
child 6268 4b5241e5dd10
equal deleted inserted replaced
5834:7bf92d5a5a0f 5835:e0ff603ae0b7
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 
       
     5 #ifdef ENABLE_NETWORK
       
     6 
       
     7 #if defined(UNIX) && !defined(__MORPHOS__)
       
     8 
       
     9 #include "openttd.h"
       
    10 #include "variables.h"
       
    11 
       
    12 #include <sys/types.h>
       
    13 #include <unistd.h>
       
    14 
       
    15 void DedicatedFork(void)
       
    16 {
       
    17 	/* Fork the program */
       
    18 	pid_t pid = fork();
       
    19 	switch (pid) {
       
    20 		case -1:
       
    21 			perror("Unable to fork");
       
    22 			exit(1);
       
    23 
       
    24 		case 0: { // We're the child
       
    25 			FILE* f;
       
    26 
       
    27 			/* Open the log-file to log all stuff too */
       
    28 			f = fopen(_log_file, "a");
       
    29 			if (f == NULL) {
       
    30 				perror("Unable to open logfile");
       
    31 				exit(1);
       
    32 			}
       
    33 			/* Redirect stdout and stderr to log-file */
       
    34 			if (dup2(fileno(f), fileno(stdout)) == -1) {
       
    35 				perror("Rerouting stdout");
       
    36 				exit(1);
       
    37 			}
       
    38 			if (dup2(fileno(f), fileno(stderr)) == -1) {
       
    39 				perror("Rerouting stderr");
       
    40 				exit(1);
       
    41 			}
       
    42 			break;
       
    43 		}
       
    44 
       
    45 		default:
       
    46 			// We're the parent
       
    47 			printf("Loading dedicated server...\n");
       
    48 			printf("  - Forked to background with pid %d\n", pid);
       
    49 			exit(0);
       
    50 	}
       
    51 }
       
    52 #endif
       
    53 
       
    54 #else
       
    55 
       
    56 void DedicatedFork(void) {}
       
    57 
       
    58 #endif /* ENABLE_NETWORK */