dedicated.cpp
Go to the documentation of this file.00001
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
00017
00018 # define PRINTF_PID_T "%ld"
00019 #else
00020 # define PRINTF_PID_T "%d"
00021 #endif
00022
00023 void DedicatedFork()
00024 {
00025
00026 pid_t pid = fork();
00027 switch (pid) {
00028 case -1:
00029 perror("Unable to fork");
00030 exit(1);
00031
00032 case 0: {
00033 FILE *f;
00034
00035
00036 f = fopen(_log_file, "a");
00037 if (f == NULL) {
00038 perror("Unable to open logfile");
00039 exit(1);
00040 }
00041
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
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