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
51 #include "../lib/gen/getopt.h"
53 #include <sys/types.h>
58 #include "empthread.h"
69 #include "prototypes.h"
75 static void create_pidfile(char *, pid_t);
78 static void loc_NTInit(void);
79 static void loc_NTTerm(void);
82 static char pidfname[] = "server.pid";
84 /* Run as daemon? If yes, detach from controlling terminal etc. */
85 static int daemonize = 1;
88 print_usage(char *program_name)
90 printf("Usage: %s [OPTION]...\n"
92 " -e CONFIG-FILE configuration file\n"
94 " -h display this help and exit\n"
96 " -i install service `%s'\n"
97 " -I NAME install service NAME\n"
99 " -p threading debug mode, implies -d\n"
101 " -r remove service `%s'\n"
102 " -R NAME remove service NAME\n"
104 " -s enable stack checking\n"
105 " -v display version information and exit\n",
106 program_name, dflt_econfig
108 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
114 main(int argc, char **argv)
118 int install_service_set = 0;
119 char *program_name = NULL;
120 char *service_name = NULL;
121 int remove_service_set = 0;
123 char *config_file = NULL;
127 # define XOPTS "iI:rR:"
131 while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
134 flags |= EMPTH_PRINT;
141 config_file = optarg;
145 service_name = optarg;
148 install_service_set++;
151 service_name = optarg;
154 remove_service_set++;
158 flags |= EMPTH_STACKCHECK;
161 printf("%s\n\n%s", version, legal);
164 print_usage(argv[0]);
167 fprintf(stderr, "Try -h for help.\n");
173 if ((debug || flags || config_file != NULL) &&
174 remove_service_set) {
175 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
176 "-r or -R options\n");
179 if ((debug || flags) && install_service_set) {
180 fprintf(stderr, "Can't use -d, -p or -s with either "
181 "-i or -I options\n");
184 if (install_service_set && remove_service_set) {
185 fprintf(stderr, "Can't use both -r or -R and -i or -I "
190 if (remove_service_set)
191 return remove_service(service_name);
192 if (install_service_set) {
193 program_name = _fullpath(NULL, argv[0], 0);
194 if (config_file != NULL)
195 config_file = _fullpath(NULL, config_file, 0);
199 if (emp_config(config_file) < 0)
202 if (chdir(configdir)) {
203 fprintf(stderr, "Can't chdir to %s (%s)\n",
204 configdir, strerror(errno));
207 if (chdir(builtindir)) {
208 fprintf(stderr, "Can't chdir to %s (%s)\n",
209 builtindir, strerror(errno));
212 if (read_builtin_tables() < 0)
214 if (chdir(configdir)) {
215 fprintf(stderr, "Can't chdir to %s (%s)\n",
216 configdir, strerror(errno));
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();
286 * Initialize for serving, acquire resources.
295 update_policy_check();
301 if (opt_MOB_ACCESS) {
302 /* This fixes up mobility upon restart */
313 start_server(int flags)
318 create_pidfile(pidfname, pid);
319 logerror("------------------------------------------------------");
320 logerror("Empire server (pid %d) started", (int)pid);
322 empth_init((void **)&player, flags);
324 empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
325 "AcceptPlayers", "Accept network connections", 0);
326 empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
327 "KillIdle", "Kills idle player connections", 0);
328 empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
329 "DeleteItems", "Deletes old lost items", 0);
330 if (opt_MOB_ACCESS) {
331 /* Start the mobility access check thread */
332 empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
333 "MobilityCheck", "Writes the timestamp file", 0);
341 * Finish serving, release resources.
354 create_pidfile(char *fname, pid_t pid)
356 FILE *pidf = fopen(fname, "w");
358 || fprintf(pidf, "%d\n", (int)pid) < 0
360 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);
386 empth_sleep(now + 1);
388 for (p = player_next(0); p != 0; p = player_next(p)) {
391 empth_terminate(p->proc);
392 p = player_delete(p);
395 logerror("Server shutting down on signal %d", sig);
397 logerror("Server shutting down at deity's request");
412 WORD wVersionRequested;
415 wVersionRequested = MAKEWORD(2, 0);
416 rc = WSAStartup(wVersionRequested, &wsaData);
418 logerror("WSAStartup failed. %d", rc);