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 ignore(void);
72 static void crash_dump(void);
73 static void create_pidfile(char *, pid_t);
76 static void loc_NTInit(void);
77 static void loc_NTTerm(void);
81 * Lock to synchronize player threads with update and shutdown.
82 * Update and shutdown takes it exclusive, commands take it shared.
84 empth_rwlock_t *play_lock;
87 * Is a thread attempting to take an exclusive play_lock?
88 * Threads holding a shared play_lock must not sleep while this is
91 int play_wrlock_wanted;
93 static char pidfname[] = "server.pid";
95 /* Run as daemon? If yes, detach from controlling terminal etc. */
96 static int daemonize = 1;
99 help(char *program_name, char *complaint)
102 fprintf(stderr, "%s: %s\n", program_name, complaint);
103 fprintf(stderr, "Try -h for help.\n");
107 print_usage(char *program_name)
109 printf("Usage: %s [OPTION]...\n"
110 " -d debug mode, implies -E abort\n"
111 " -e CONFIG-FILE configuration file\n"
113 " -E ACTION what to do on oops: abort, crash-dump, nothing (default)\n"
114 " -h display this help and exit\n"
116 " -i install service `%s'\n"
117 " -I NAME install service NAME\n"
119 " -p threading debug mode, implies -d\n"
121 " -u uninstall service `%s'\n"
122 " -U NAME uninstall service NAME\n"
124 " -s enable stack checking\n"
125 " -R RANDOM-SEED random seed\n"
126 " -v display version information and exit\n",
127 program_name, dflt_econfig
129 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
135 main(int argc, char **argv)
137 static char *oops_key[] = { "abort", "crash-dump", "nothing", NULL };
138 static void (*oops_hndlr[])(void) = { abort, crash_dump, ignore };
141 int install_service_set = 0;
142 char *program_name = NULL;
143 char *service_name = NULL;
144 int remove_service_set = 0;
146 char *config_file = NULL;
148 unsigned seed = time(NULL);
150 oops_handler = ignore;
153 # define XOPTS "iI:uU:"
157 while ((op = getopt(argc, argv, "de:E:hpsR:v" XOPTS)) != EOF) {
160 flags |= EMPTH_PRINT;
163 oops_handler = abort;
167 config_file = optarg;
170 idx = stmtch(optarg, oops_key, 0, sizeof(*oops_key));
172 help(argv[0], "invalid argument for -E");
175 oops_handler = oops_hndlr[idx];
179 service_name = optarg;
182 install_service_set++;
185 service_name = optarg;
188 remove_service_set++;
192 flags |= EMPTH_STACKCHECK;
195 seed = strtoul(optarg, NULL, 10);
198 printf("%s\n\n%s", version, legal);
201 print_usage(argv[0]);
210 if ((!daemonize || flags || config_file != NULL) &&
211 remove_service_set) {
212 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
213 "-u or -U options\n");
216 if ((!daemonize || flags) && install_service_set) {
217 fprintf(stderr, "Can't use -d, -p or -s with either "
218 "-i or -I options\n");
221 if (install_service_set && remove_service_set) {
222 fprintf(stderr, "Can't use both -u or -U and -i or -I "
227 if (remove_service_set)
228 return remove_service(service_name);
229 if (install_service_set) {
230 program_name = _fullpath(NULL, argv[0], 0);
231 if (config_file != NULL)
232 config_file = _fullpath(NULL, config_file, 0);
237 if (emp_config(config_file) < 0)
240 if (read_builtin_tables() < 0)
242 if (read_custom_tables() < 0)
244 if (chdir(gamedir)) {
245 fprintf(stderr, "Can't chdir to %s (%s)\n",
246 gamedir, strerror(errno));
251 if (install_service_set)
252 return install_service(program_name, service_name, config_file);
258 if (daemonize != 0) {
259 SERVICE_TABLE_ENTRY DispatchTable[]={
260 {"Empire Server", service_main},
263 if (StartServiceCtrlDispatcher(DispatchTable))
267 * If it is service startup error then exit otherwise
268 * start server in the foreground
270 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
271 logerror("Failed to dispatch service (%lu)",
281 if (disassoc() < 0) {
282 logerror("Can't become daemon (%s)", strerror(errno));
291 sig = empth_wait_for_signal();
294 /* if you make changes here, also update relo() */
325 * Initialize for serving, acquire resources.
328 init_server(unsigned seed)
346 start_server(int flags)
351 create_pidfile(pidfname, pid);
352 logerror("------------------------------------------------------");
353 logerror("Empire server (pid %d) started", (int)pid);
355 empth_init((void **)&player, flags);
357 if (journal_startup() < 0)
360 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
361 empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
362 empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
369 * Finish serving, release resources.
383 create_pidfile(char *fname, pid_t pid)
385 FILE *pidf = fopen(fname, "w");
387 || fprintf(pidf, "%d\n", (int)pid) < 0
389 logerror("Can't write PID file (%s)", strerror(errno));
399 logerror("Shutdown commencing (cleaning up threads.)");
401 play_wrlock_wanted = 1;
402 for (p = player_next(0); p != 0; p = player_next(p)) {
403 if (p->state != PS_PLAYING)
405 pr_flash(p, "Server shutting down...\n");
406 p->state = PS_SHUTDOWN;
409 pr_flash(p, "Shutdown aborting command\n");
411 empth_wakeup(p->proc);
413 empth_rwlock_wrlock(play_lock);
415 logerror("Server shutting down on signal %d", sig);
417 logerror("Server shutting down at deity's request");
432 WORD wVersionRequested;
435 wVersionRequested = MAKEWORD(2, 0);
436 rc = WSAStartup(wVersionRequested, &wsaData);
438 logerror("WSAStartup failed. %d", rc);