dedicated.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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
00024
00025
00026
00027 # define PRINTF_PID_T "%ld"
00028 #else
00029 # define PRINTF_PID_T "%d"
00030 #endif
00031
00032 void DedicatedFork()
00033 {
00034
00035 pid_t pid = fork();
00036 switch (pid) {
00037 case -1:
00038 perror("Unable to fork");
00039 exit(1);
00040
00041 case 0: {
00042 FILE *f;
00043
00044
00045 f = fopen(_log_file, "a");
00046 if (f == NULL) {
00047 perror("Unable to open logfile");
00048 exit(1);
00049 }
00050
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
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