2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * main.c: Empire Server main, startup and shutdown
30 * Known contributors to this file:
32 * Steve McClure, 1996, 1998
34 * Ron Koenderink, 2004-2005
35 * Markus Armbruster, 2005-2008
53 #include "empthread.h"
65 #include "prototypes.h"
71 static void create_pidfile(char *, pid_t);
74 static void loc_NTInit(void);
75 static void loc_NTTerm(void);
79 * Lock to synchronize player threads with update and shutdown.
80 * Update and shutdown takes it exclusive, commands take it shared.
82 empth_rwlock_t *play_lock;
85 * Is a thread attempting to take an exclusive play_lock?
86 * Threads holding a shared play_lock must not sleep while this is
89 int play_wrlock_wanted;
91 static char pidfname[] = "server.pid";
93 /* Run as daemon? If yes, detach from controlling terminal etc. */
94 static int daemonize = 1;
97 help(char *program_name, char *complaint)
100 fprintf(stderr, "%s: %s\n", program_name, complaint);
101 fprintf(stderr, "Try -h for help.\n");
105 print_usage(char *program_name)
107 printf("Usage: %s [OPTION]...\n"
108 " -d debug mode, implies -E abort\n"
109 " -e CONFIG-FILE configuration file\n"
111 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
112 " -h display this help and exit\n"
114 " -i install service `%s'\n"
115 " -I NAME install service NAME\n"
117 " -p threading debug mode, implies -d\n"
119 " -u uninstall service `%s'\n"
120 " -U NAME uninstall service NAME\n"
122 " -s enable stack checking\n"
123 " -R RANDOM-SEED random seed\n"
124 " -v display version information and exit\n",
125 program_name, dflt_econfig
127 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
133 main(int argc, char **argv)
135 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
138 int install_service_set = 0;
139 char *program_name = NULL;
140 char *service_name = NULL;
141 int remove_service_set = 0;
143 char *config_file = NULL;
145 unsigned seed = time(NULL);
147 oops_action = OOPS_NOTHING;
150 # define XOPTS "iI:uU:"
154 while ((op = getopt(argc, argv, "de:E:hpsR:v" XOPTS)) != EOF) {
157 flags |= EMPTH_PRINT;
160 oops_action = OOPS_ABORT;
164 config_file = optarg;
167 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
169 help(argv[0], "invalid argument for -E");
176 service_name = optarg;
179 install_service_set++;
182 service_name = optarg;
185 remove_service_set++;
189 flags |= EMPTH_STACKCHECK;
192 seed = strtoul(optarg, NULL, 10);
195 printf("%s\n\n%s", version, legal);
198 print_usage(argv[0]);
207 if ((!daemonize || flags || config_file != NULL) &&
208 remove_service_set) {
209 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
210 "-u or -U options\n");
213 if ((!daemonize || flags) && install_service_set) {
214 fprintf(stderr, "Can't use -d, -p or -s with either "
215 "-i or -I options\n");
218 if (install_service_set && remove_service_set) {
219 fprintf(stderr, "Can't use both -u or -U and -i or -I "
224 if (remove_service_set)
225 return remove_service(service_name);
226 if (install_service_set) {
227 program_name = _fullpath(NULL, argv[0], 0);
228 if (config_file != NULL)
229 config_file = _fullpath(NULL, config_file, 0);
234 if (emp_config(config_file) < 0)
237 if (read_builtin_tables() < 0)
239 if (read_custom_tables() < 0)
241 if (chdir(gamedir)) {
242 fprintf(stderr, "Can't chdir to %s (%s)\n",
243 gamedir, strerror(errno));
248 if (install_service_set)
249 return install_service(program_name, service_name, config_file);
255 if (daemonize != 0) {
256 SERVICE_TABLE_ENTRY DispatchTable[]={
257 {"Empire Server", service_main},
260 if (StartServiceCtrlDispatcher(DispatchTable))
264 * If it is service startup error then exit otherwise
265 * start server in the foreground
267 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
268 logerror("Failed to dispatch service (%lu)",
278 if (disassoc() < 0) {
279 logerror("Can't become daemon (%s)", strerror(errno));
288 sig = empth_wait_for_signal();
291 /* if you make changes here, also update relo() */
309 * Initialize for serving, acquire resources.
312 init_server(unsigned seed)
330 start_server(int flags)
335 create_pidfile(pidfname, pid);
336 logerror("------------------------------------------------------");
337 logerror("Empire server (pid %d) started", (int)pid);
339 empth_init((void **)&player, flags);
341 if (journal_startup() < 0)
344 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
345 empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
346 empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
353 * Finish serving, release resources.
367 create_pidfile(char *fname, pid_t pid)
369 FILE *pidf = fopen(fname, "w");
371 || fprintf(pidf, "%d\n", (int)pid) < 0
373 logerror("Can't write PID file (%s)", strerror(errno));
383 logerror("Shutdown commencing (cleaning up threads.)");
385 play_wrlock_wanted = 1;
386 for (p = player_next(0); p != 0; p = player_next(p)) {
387 if (p->state != PS_PLAYING)
389 pr_flash(p, "Server shutting down...\n");
390 p->state = PS_SHUTDOWN;
393 pr_flash(p, "Shutdown aborting command\n");
395 empth_wakeup(p->proc);
397 empth_rwlock_wrlock(play_lock);
399 logerror("Server shutting down on signal %d", sig);
401 logerror("Server shutting down at deity's request");
416 WORD wVersionRequested;
419 wVersionRequested = MAKEWORD(2, 0);
420 rc = WSAStartup(wVersionRequested, &wsaData);
422 logerror("WSAStartup failed. %d", rc);