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-2007
53 #include "empthread.h"
64 #include "prototypes.h"
70 static void create_pidfile(char *, pid_t);
73 static void loc_NTInit(void);
74 static void loc_NTTerm(void);
78 * Lock to synchronize player threads with update and shutdown.
79 * Update and shutdown takes it exclusive, commands take it shared.
81 empth_rwlock_t *play_lock;
84 * Is a thread attempting to take an exclusive play_lock?
85 * Threads holding a shared play_lock must not sleep while this is
88 int play_wrlock_wanted;
90 static char pidfname[] = "server.pid";
92 /* Run as daemon? If yes, detach from controlling terminal etc. */
93 static int daemonize = 1;
96 print_usage(char *program_name)
98 printf("Usage: %s [OPTION]...\n"
100 " -e CONFIG-FILE configuration file\n"
102 " -h display this help and exit\n"
104 " -i install service `%s'\n"
105 " -I NAME install service NAME\n"
107 " -p threading debug mode, implies -d\n"
109 " -u uninstall service `%s'\n"
110 " -U NAME uninstall service NAME\n"
112 " -s enable stack checking\n"
113 " -R RANDOM-SEED random seed\n"
114 " -v display version information and exit\n",
115 program_name, dflt_econfig
117 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
123 main(int argc, char **argv)
127 int install_service_set = 0;
128 char *program_name = NULL;
129 char *service_name = NULL;
130 int remove_service_set = 0;
132 char *config_file = NULL;
134 unsigned seed = time(NULL);
139 # define XOPTS "iI:uU:"
143 while ((op = getopt(argc, argv, "de:hpsR:v" XOPTS)) != EOF) {
146 flags |= EMPTH_PRINT;
153 config_file = optarg;
157 service_name = optarg;
160 install_service_set++;
163 service_name = optarg;
166 remove_service_set++;
170 flags |= EMPTH_STACKCHECK;
173 seed = strtoul(optarg, NULL, 10);
176 printf("%s\n\n%s", version, legal);
179 print_usage(argv[0]);
182 fprintf(stderr, "Try -h for help.\n");
188 if ((debug || flags || config_file != NULL) &&
189 remove_service_set) {
190 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
191 "-u or -U options\n");
194 if ((debug || flags) && install_service_set) {
195 fprintf(stderr, "Can't use -d, -p or -s with either "
196 "-i or -I options\n");
199 if (install_service_set && remove_service_set) {
200 fprintf(stderr, "Can't use both -u or -U and -i or -I "
205 if (remove_service_set)
206 return remove_service(service_name);
207 if (install_service_set) {
208 program_name = _fullpath(NULL, argv[0], 0);
209 if (config_file != NULL)
210 config_file = _fullpath(NULL, config_file, 0);
214 if (emp_config(config_file) < 0)
217 if (read_builtin_tables() < 0)
219 if (read_custom_tables() < 0)
221 if (chdir(gamedir)) {
222 fprintf(stderr, "Can't chdir to %s (%s)\n",
223 gamedir, strerror(errno));
228 if (install_service_set)
229 return install_service(program_name, service_name, config_file);
235 if (daemonize != 0) {
236 SERVICE_TABLE_ENTRY DispatchTable[]={
237 {"Empire Server", service_main},
240 if (StartServiceCtrlDispatcher(DispatchTable))
244 * If it is service startup error then exit otherwise
245 * start server in the foreground
247 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
248 logerror("Failed to dispatch service (%lu)",
258 if (disassoc() < 0) {
259 logerror("Can't become daemon (%s)", strerror(errno));
267 sig = empth_wait_for_signal();
270 /* if you make changes here, also update relo() */
288 * Initialize for serving, acquire resources.
291 init_server(unsigned seed)
309 start_server(int flags)
314 create_pidfile(pidfname, pid);
315 logerror("------------------------------------------------------");
316 logerror("Empire server (pid %d) started", (int)pid);
318 empth_init((void **)&player, flags);
320 if (journal_startup() < 0)
323 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
324 empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
325 empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
332 * Finish serving, release resources.
346 create_pidfile(char *fname, pid_t pid)
348 FILE *pidf = fopen(fname, "w");
350 || fprintf(pidf, "%d\n", (int)pid) < 0
352 logerror("Can't write PID file (%s)", strerror(errno));
362 logerror("Shutdown commencing (cleaning up threads.)");
364 play_wrlock_wanted = 1;
365 for (p = player_next(0); p != 0; p = player_next(p)) {
366 if (p->state != PS_PLAYING)
368 pr_flash(p, "Server shutting down...\n");
369 p->state = PS_SHUTDOWN;
372 pr_flash(p, "Shutdown aborting command\n");
374 empth_wakeup(p->proc);
376 empth_rwlock_wrlock(play_lock);
378 logerror("Server shutting down on signal %d", sig);
380 logerror("Server shutting down at deity's request");
395 WORD wVersionRequested;
398 wVersionRequested = MAKEWORD(2, 0);
399 rc = WSAStartup(wVersionRequested, &wsaData);
401 logerror("WSAStartup failed. %d", rc);