dedicated.cpp

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

Generated on Wed Oct 1 17:03:20 2008 for openttd by  doxygen 1.5.6