src/dedicated.cpp
author belugas
Sun, 15 Jun 2008 02:48:25 +0000
changeset 10965 a2b5f6f9be0c
parent 10429 1b99254f9607
permissions -rw-r--r--
(svn r13519) -Feature[newGRF]: Implement var 63, variational action2 variable for Houses.
Or, in more simple terms, the check for the animation frame of nearby house.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2177
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2177
diff changeset
     2
10429
1b99254f9607 (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: 6928
diff changeset
     3
/** @file dedicated.cpp Forking support for dedicated servers. */
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5835
diff changeset
     4
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     5
#include "stdafx.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     6
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     7
#ifdef ENABLE_NETWORK
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
2195
8960c86bf103 (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__)
8960c86bf103 (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
8960c86bf103 (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
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 1959
diff changeset
    12
#include "variables.h"
781
9717ff353c17 (svn r1248) -Add: initial OS/2 support (read docs/ReadMe_OS2.txt) (orudge)
truelight
parents: 774
diff changeset
    13
2195
8960c86bf103 (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>
8960c86bf103 (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
73d67828ea5e (svn r2691) Separate dedicated video driver and fix bemidi declarations
tron
parents: 2163
diff changeset
    16
6558
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    17
#if defined(SUNOS) && !defined(_LP64) && !defined(_I32LPx)
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    18
/* Solaris has, in certain situation, pid_t defined as long, while in other
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    19
 *  cases it has it defined as int... this handles all cases nicely. */
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    20
# define PRINTF_PID_T "%ld"
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    21
#else
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    22
# define PRINTF_PID_T "%d"
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    23
#endif
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    24
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6558
diff changeset
    25
void DedicatedFork()
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    26
{
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    27
	/* Fork the program */
1406
2d5baabe4046 (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    28
	pid_t pid = fork();
2d5baabe4046 (svn r1910) Move two variables out of variables.h which are only used locally
tron
parents: 1301
diff changeset
    29
	switch (pid) {
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    30
		case -1:
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    31
			perror("Unable to fork");
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    32
			exit(1);
2294
a938c5ecb1c4 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    33
a938c5ecb1c4 (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
a938c5ecb1c4 (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
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    36
a526dc96fbfc (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
a938c5ecb1c4 (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");
a938c5ecb1c4 (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
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    40
				perror("Unable to open logfile");
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    41
				exit(1);
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    42
			}
a526dc96fbfc (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
a938c5ecb1c4 (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
6429294a2009 (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    45
				perror("Rerouting stdout");
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    46
				exit(1);
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    47
			}
2294
a938c5ecb1c4 (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
6429294a2009 (svn r2130) Various spelling fixes in messages.
pasky
parents: 1614
diff changeset
    49
				perror("Rerouting stderr");
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    50
				exit(1);
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    51
			}
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    52
			break;
2294
a938c5ecb1c4 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    53
		}
a938c5ecb1c4 (svn r2818) Don't tell the world about a local variable which is only used once
tron
parents: 2195
diff changeset
    54
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    55
		default:
6451
7baba06b4b85 (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5835
diff changeset
    56
			/* We're the parent */
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    57
			printf("Loading dedicated server...\n");
6558
384bfec543ac (svn r9035) -Fix [SunOS]: Solaris sometimes has pid_t defined as long. Fix warnings in those cases
truelight
parents: 6451
diff changeset
    58
			printf("  - Forked to background with pid " PRINTF_PID_T "\n", pid);
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    59
			exit(0);
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    60
	}
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 702
diff changeset
    61
}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
#endif
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    64
#else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    66
/** Empty helper function call for NOT(UNIX and not MORPHOS) systems */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6558
diff changeset
    67
void DedicatedFork() {}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    68
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    69
#endif /* ENABLE_NETWORK */