2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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: Thread and signal initialization for Empire Server
30 * Known contributors to this file:
32 * Steve McClure, 1996, 1998
40 #include <sys/ioctl.h>
51 #include "../lib/gen/getopt.h"
60 #include "empthread.h"
70 #include "prototypes.h"
72 static void create_pidfile(char *, pid_t);
75 static void loc_NTInit(void);
76 static void loc_NTTerm(void);
79 static char pidfname[] = "server.pid";
81 /* Run as daemon? If yes, detach from controlling terminal etc. */
85 print_usage(char *program_name)
87 printf("Usage: %s [OPTION]...\n"
89 " -e CONFIG-FILE configuration file\n"
91 " -h display this help and exit\n"
93 " -i install service `%s'\n"
94 " -I NAME install service NAME\n"
96 " -p threading debug mode, implies -d\n"
98 " -r remove service `%s'\n"
99 " -R NAME remove service NAME\n"
101 " -s enable stack checking\n"
102 " -v display version information and exit\n",
103 program_name, dflt_econfig
105 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
111 main(int argc, char **argv)
115 int install_service_set = 0;
116 char *program_name = NULL;
117 char *service_name = NULL;
118 int remove_service_set = 0;
120 char *config_file = NULL;
124 # define XOPTS "iI:rR:"
128 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
131 flags |= EMPTH_PRINT;
138 config_file = optarg;
142 service_name = optarg;
147 install_service_set++;
150 service_name = optarg;
155 remove_service_set++;
159 flags |= EMPTH_STACKCHECK;
162 printf("%s\n\n%s", version, legal);
165 print_usage(argv[0]);
168 fprintf(stderr, "Try -h for help.\n");
174 if ((debug || flags || config_file != NULL) &&
175 remove_service_set) {
176 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
177 "-r or -R options\n");
180 if ((debug || flags) && install_service_set) {
181 fprintf(stderr, "Can't use -d, -p or -s with either "
182 "-i or -I options\n");
185 if (install_service_set && remove_service_set) {
186 fprintf(stderr, "Can't use both -r or -R and -i or -I "
194 if (remove_service_set)
195 return remove_service(service_name);
196 if (install_service_set) {
197 program_name = _fullpath(NULL, argv[0], 0);
198 if (config_file != NULL)
199 config_file = _fullpath(NULL, config_file, 0);
203 if (emp_config(config_file) < 0)
206 if (chdir(builtindir)) {
207 fprintf(stderr, "Can't chdir to %s (%s)\n", builtindir, strerror(errno));
210 if (read_builtin_tables() < 0)
212 if (chdir(configdir)) {
213 fprintf(stderr, "Can't chdir to %s (%s)\n", configdir, strerror(errno));
216 if (read_config_tables() < 0)
218 if (chdir(gamedir)) {
219 fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
224 if (install_service_set)
225 return install_service(program_name, service_name, config_file);
231 if (daemonize != 0) {
232 SERVICE_TABLE_ENTRY DispatchTable[]={{"Empire Server", service_main},{NULL, NULL}};
233 if (StartServiceCtrlDispatcher(DispatchTable))
237 * If it is service startup error then exit otherwise
238 * start server in the foreground
240 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
241 logerror("Failed to dispatch service (%lu)", GetLastError());
250 if (disassoc() < 0) {
251 logerror("Can't become daemon (%s)", strerror(errno));
260 CANT_HAPPEN("main thread terminated");
267 * Initialize for serving, acquire resources.
276 update_policy_check();
283 if (opt_MOB_ACCESS) {
284 /* This fixes up mobility upon restart */
295 start_server(int flags)
299 struct sigaction act;
303 create_pidfile(pidfname, pid);
304 logerror("------------------------------------------------------");
305 logerror("Empire server (pid %d) started", (int)pid);
308 /* signal() should not be used with mit pthreads. Anyway if u
309 have a posix threads u definitly have posix signals -- Sasha */
311 sigemptyset(&act.sa_mask);
312 act.sa_handler = shutdwn;
313 sigaction(SIGTERM, &act, NULL);
314 sigaction(SIGINT, &act, NULL);
315 act.sa_handler = panic;
316 sigaction(SIGBUS, &act, NULL);
317 sigaction(SIGSEGV, &act, NULL);
318 sigaction(SIGILL, &act, NULL);
319 sigaction(SIGFPE, &act, NULL);
320 act.sa_handler = SIG_IGN;
321 sigaction(SIGPIPE, &act, NULL);
324 empth_init((void **)&player, flags);
326 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
327 "AcceptPlayers", "Accept network connections", 0);
328 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
329 "KillIdle", "Kills idle player connections", 0);
330 empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
331 "Schedules updates to occur", 0);
332 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
333 "DeleteItems", "Deletes old lost items", 0);
334 if (opt_MOB_ACCESS) {
335 /* Start the mobility access check thread */
336 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
337 "MobilityCheck", "Writes the timestamp file", 0);
341 empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
342 "MarketUpdate", "Updates the market", 0);
347 * Finish serving, release resources.
360 create_pidfile(char *fname, pid_t pid)
362 FILE *pidf = fopen(fname, "w");
364 || fprintf(pidf, "%d\n", (int)pid) < 0
366 logerror("Can't write PID file (%s)", strerror(errno));
371 /* we're going down. try to close the files at least */
376 struct sigaction act;
379 sigemptyset(&act.sa_mask);
380 act.sa_handler = SIG_DFL;
381 sigaction(SIGBUS, &act, NULL);
382 sigaction(SIGSEGV, &act, NULL);
383 sigaction(SIGILL, &act, NULL);
384 sigaction(SIGFPE, &act, NULL);
385 logerror("server received fatal signal %d", sig);
388 if (CANT_HAPPEN(sig != SIGBUS && sig != SIGSEGV
389 && sig != SIGILL && sig != SIGFPE))
402 logerror("Shutdown commencing (cleaning up threads.)");
404 for (p = player_next(0); p != 0; p = player_next(p)) {
405 if (p->state != PS_PLAYING)
407 pr_flash(p, "Server shutting down...\n");
408 p->state = PS_SHUTDOWN;
411 pr_flash(p, "Shutdown aborting command\n");
413 empth_wakeup(p->proc);
417 /* Sleep and let some cleanup happen - note this doesn't work
418 when called from a signal handler, since we may or may not
419 be in the right thread. So we just pass by and kill 'em
422 empth_sleep(now + 1);
425 for (p = player_next(0); p != 0; p = player_next(p)) {
428 empth_terminate(p->proc);
429 p = player_delete(p);
432 logerror("Server shutting down on signal %d", sig);
434 logerror("Server shutting down at deity's request");
451 WORD wVersionRequested;
454 wVersionRequested = MAKEWORD(2, 0);
455 rc = WSAStartup(wVersionRequested, &wsaData);
457 logerror("WSAStartup failed. %d", rc);