2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, 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-2006
50 #include "../lib/gen/getopt.h"
52 #include <sys/types.h>
57 #include "empthread.h"
68 #include "prototypes.h"
74 static void create_pidfile(char *, pid_t);
77 static void loc_NTInit(void);
78 static void loc_NTTerm(void);
81 static char pidfname[] = "server.pid";
83 /* Run as daemon? If yes, detach from controlling terminal etc. */
84 static int daemonize = 1;
87 print_usage(char *program_name)
89 printf("Usage: %s [OPTION]...\n"
91 " -e CONFIG-FILE configuration file\n"
93 " -h display this help and exit\n"
95 " -i install service `%s'\n"
96 " -I NAME install service NAME\n"
98 " -p threading debug mode, implies -d\n"
100 " -r remove service `%s'\n"
101 " -R NAME remove service NAME\n"
103 " -s enable stack checking\n"
104 " -v display version information and exit\n",
105 program_name, dflt_econfig
107 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
113 main(int argc, char **argv)
117 int install_service_set = 0;
118 char *program_name = NULL;
119 char *service_name = NULL;
120 int remove_service_set = 0;
122 char *config_file = NULL;
126 # define XOPTS "iI:rR:"
130 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
133 flags |= EMPTH_PRINT;
140 config_file = optarg;
144 service_name = optarg;
147 install_service_set++;
150 service_name = optarg;
153 remove_service_set++;
157 flags |= EMPTH_STACKCHECK;
160 printf("%s\n\n%s", version, legal);
163 print_usage(argv[0]);
166 fprintf(stderr, "Try -h for help.\n");
172 if ((debug || flags || config_file != NULL) &&
173 remove_service_set) {
174 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
175 "-r or -R options\n");
178 if ((debug || flags) && install_service_set) {
179 fprintf(stderr, "Can't use -d, -p or -s with either "
180 "-i or -I options\n");
183 if (install_service_set && remove_service_set) {
184 fprintf(stderr, "Can't use both -r or -R and -i or -I "
189 if (remove_service_set)
190 return remove_service(service_name);
191 if (install_service_set) {
192 program_name = _fullpath(NULL, argv[0], 0);
193 if (config_file != NULL)
194 config_file = _fullpath(NULL, config_file, 0);
198 if (emp_config(config_file) < 0)
201 if (chdir(configdir)) {
202 fprintf(stderr, "Can't chdir to %s (%s)\n",
203 configdir, strerror(errno));
206 if (chdir(builtindir)) {
207 fprintf(stderr, "Can't chdir to %s (%s)\n",
208 builtindir, strerror(errno));
211 if (read_builtin_tables() < 0)
213 if (chdir(configdir)) {
214 fprintf(stderr, "Can't chdir to %s (%s)\n",
215 configdir, strerror(errno));
218 if (read_custom_tables() < 0)
220 if (chdir(gamedir)) {
221 fprintf(stderr, "Can't chdir to %s (%s)\n",
222 gamedir, strerror(errno));
227 if (install_service_set)
228 return install_service(program_name, service_name, config_file);
234 if (daemonize != 0) {
235 SERVICE_TABLE_ENTRY DispatchTable[]={
236 {"Empire Server", service_main},
239 if (StartServiceCtrlDispatcher(DispatchTable))
243 * If it is service startup error then exit otherwise
244 * start server in the foreground
246 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
247 logerror("Failed to dispatch service (%lu)",
257 if (disassoc() < 0) {
258 logerror("Can't become daemon (%s)", strerror(errno));
266 sig = empth_wait_for_signal();
284 * Initialize for serving, acquire resources.
293 update_policy_check();
301 if (opt_MOB_ACCESS) {
302 /* This fixes up mobility upon restart */
311 start_server(int flags)
316 create_pidfile(pidfname, pid);
317 logerror("------------------------------------------------------");
318 logerror("Empire server (pid %d) started", (int)pid);
320 empth_init((void **)&player, flags);
322 if (journal_startup() < 0)
325 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
327 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
329 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
331 if (opt_MOB_ACCESS) {
332 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
341 * Finish serving, release resources.
355 create_pidfile(char *fname, pid_t pid)
357 FILE *pidf = fopen(fname, "w");
359 || fprintf(pidf, "%d\n", (int)pid) < 0
361 logerror("Can't write PID file (%s)", strerror(errno));
371 logerror("Shutdown commencing (cleaning up threads.)");
373 for (p = player_next(0); p != 0; p = player_next(p)) {
374 if (p->state != PS_PLAYING)
376 pr_flash(p, "Server shutting down...\n");
377 p->state = PS_SHUTDOWN;
380 pr_flash(p, "Shutdown aborting command\n");
382 empth_wakeup(p->proc);
384 empth_rwlock_wrlock(update_lock);
385 /* rely on player_kill_idle() for killing hung player threads */
387 logerror("Server shutting down on signal %d", sig);
389 logerror("Server shutting down at deity's request");
404 WORD wVersionRequested;
407 wVersionRequested = MAKEWORD(2, 0);
408 rc = WSAStartup(wVersionRequested, &wsaData);
410 logerror("WSAStartup failed. %d", rc);