dedicated.cpp

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

Generated on Thu Oct 1 11:03:12 2009 for OpenTTD by  doxygen 1.5.6