src/dedicated.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 9111 48ce04029fe4
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2177
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2177
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 6432
diff changeset
     3
/** @file dedicated.cpp Forking support for dedicated servers. */
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5584
diff changeset
     4
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     5
#include "stdafx.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     6
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
2195
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2186
diff changeset
     9
#if defined(UNIX) && !defined(__MORPHOS__)
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2186
diff changeset
    10
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2186
diff changeset
    11
#include "openttd.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 1959
diff changeset
    12
#include "variables.h"
781
4c9177888196 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    13
2195
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2186
diff changeset
    14
#include <sys/types.h>
ed135f2dc5f8 (svn r2710) Simplify dedicated server code a bit and don't compile it at all, if network support ist disabled
tron
parents: 2186
diff changeset
    15
#include <unistd.h>
2177
b0f207d47f8e (svn r2691) Separate dedicated video driver and fix bemidi declarations
tron
parents: 2163
diff changeset
    16
6232
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    17
#if defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    18
/* Solaris has, in certain situation, pid_t defined as long, while in other
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    19
 *  cases it has it defined as int... this handles all cases nicely. */
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    20
# define PRINTF_PID_T "%ld"
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    21
#else
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    22
# define PRINTF_PID_T "%d"
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    23
#endif
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    24
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6232
diff changeset
    25
void DedicatedFork()
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    26
{
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    27
	/* Fork the program */
1406
f5da270a654f (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    28
	pid_t pid = fork();
f5da270a654f (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    29
	switch (pid) {
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    30
		case -1:
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    31
			perror("Unable to fork");
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    32
			exit(1);
2294
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    33
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    34
		case 0: { // We're the child
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    35
			FILE* f;
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    36
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    37
			/* Open the log-file to log all stuff too */
2294
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    38
			f = fopen(_log_file, "a");
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    39
			if (f == NULL) {
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    40
				perror("Unable to open logfile");
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    41
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    42
			}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    43
			/* Redirect stdout and stderr to log-file */
2294
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    44
			if (dup2(fileno(f), fileno(stdout)) == -1) {
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    45
				perror("Rerouting stdout");
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    46
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    47
			}
2294
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    48
			if (dup2(fileno(f), fileno(stderr)) == -1) {
1626
b0cda32b478a (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    49
				perror("Rerouting stderr");
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    50
				exit(1);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    51
			}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    52
			break;
2294
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    53
		}
4b7408a29936 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    54
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    55
		default:
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5584
diff changeset
    56
			/* We're the parent */
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    57
			printf("Loading dedicated server...\n");
6232
2c26bcc97fca (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6125
diff changeset
    58
			printf("  - Forked to background with pid " PRINTF_PID_T "\n", pid);
704
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    59
			exit(0);
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    60
	}
e843dd369938 (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    61
}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    64
#else
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
6432
226650eb2ef3 (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6247
diff changeset
    66
/** Empty helper function call for NOT(UNIX and not MORPHOS) systems */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6232
diff changeset
    67
void DedicatedFork() {}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    68
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    69
#endif /* ENABLE_NETWORK */