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-2007
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);
82 * Lock to synchronize player threads with update and shutdown.
83 * Update and shutdown takes it exclusive, commands take it shared.
85 empth_rwlock_t *play_lock;
88 * Is a thread attempting to take an exclusive play_lock?
89 * Threads holding a shared play_lock must not sleep while this is
92 int play_wrlock_wanted;
94 static char pidfname[] = "server.pid";
96 /* Run as daemon? If yes, detach from controlling terminal etc. */
97 static int daemonize = 1;
100 print_usage(char *program_name)
102 printf("Usage: %s [OPTION]...\n"
104 " -e CONFIG-FILE configuration file\n"
106 " -h display this help and exit\n"
108 " -i install service `%s'\n"
109 " -I NAME install service NAME\n"
111 " -p threading debug mode, implies -d\n"
113 " -r remove service `%s'\n"
114 " -R NAME remove service NAME\n"
116 " -s enable stack checking\n"
117 " -v display version information and exit\n",
118 program_name, dflt_econfig
120 , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
126 main(int argc, char **argv)
130 int install_service_set = 0;
131 char *program_name = NULL;
132 char *service_name = NULL;
133 int remove_service_set = 0;
135 char *config_file = NULL;
139 # define XOPTS "iI:rR:"
143 while ((op = getopt(argc, argv, "de:hpsv" 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 printf("%s\n\n%s", version, legal);
176 print_usage(argv[0]);
179 fprintf(stderr, "Try -h for help.\n");
185 if ((debug || flags || config_file != NULL) &&
186 remove_service_set) {
187 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
188 "-r or -R options\n");
191 if ((debug || flags) && install_service_set) {
192 fprintf(stderr, "Can't use -d, -p or -s with either "
193 "-i or -I options\n");
196 if (install_service_set && remove_service_set) {
197 fprintf(stderr, "Can't use both -r or -R and -i or -I "
202 if (remove_service_set)
203 return remove_service(service_name);
204 if (install_service_set) {
205 program_name = _fullpath(NULL, argv[0], 0);
206 if (config_file != NULL)
207 config_file = _fullpath(NULL, config_file, 0);
211 if (emp_config(config_file) < 0)
214 if (chdir(configdir)) {
215 fprintf(stderr, "Can't chdir to %s (%s)\n",
216 configdir, strerror(errno));
219 if (chdir(builtindir)) {
220 fprintf(stderr, "Can't chdir to %s (%s)\n",
221 builtindir, strerror(errno));
224 if (read_builtin_tables() < 0)
226 if (chdir(configdir)) {
227 fprintf(stderr, "Can't chdir to %s (%s)\n",
228 configdir, strerror(errno));
231 if (read_custom_tables() < 0)
233 if (chdir(gamedir)) {
234 fprintf(stderr, "Can't chdir to %s (%s)\n",
235 gamedir, strerror(errno));
240 if (install_service_set)
241 return install_service(program_name, service_name, config_file);
247 if (daemonize != 0) {
248 SERVICE_TABLE_ENTRY DispatchTable[]={
249 {"Empire Server", service_main},
252 if (StartServiceCtrlDispatcher(DispatchTable))
256 * If it is service startup error then exit otherwise
257 * start server in the foreground
259 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
260 logerror("Failed to dispatch service (%lu)",
270 if (disassoc() < 0) {
271 logerror("Can't become daemon (%s)", strerror(errno));
279 sig = empth_wait_for_signal();
282 /* if you make changes here, also update relo() */
299 * Initialize for serving, acquire resources.
320 start_server(int flags)
325 create_pidfile(pidfname, pid);
326 logerror("------------------------------------------------------");
327 logerror("Empire server (pid %d) started", (int)pid);
329 empth_init((void **)&player, flags);
331 if (journal_startup() < 0)
334 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
335 empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
336 empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
343 * Finish serving, release resources.
357 create_pidfile(char *fname, pid_t pid)
359 FILE *pidf = fopen(fname, "w");
361 || fprintf(pidf, "%d\n", (int)pid) < 0
363 logerror("Can't write PID file (%s)", strerror(errno));
373 logerror("Shutdown commencing (cleaning up threads.)");
375 play_wrlock_wanted = 1;
376 for (p = player_next(0); p != 0; p = player_next(p)) {
377 if (p->state != PS_PLAYING)
379 pr_flash(p, "Server shutting down...\n");
380 p->state = PS_SHUTDOWN;
383 pr_flash(p, "Shutdown aborting command\n");
385 empth_wakeup(p->proc);
387 empth_rwlock_wrlock(play_lock);
389 logerror("Server shutting down on signal %d", sig);
391 logerror("Server shutting down at deity's request");
406 WORD wVersionRequested;
409 wVersionRequested = MAKEWORD(2, 0);
410 rc = WSAStartup(wVersionRequested, &wsaData);
412 logerror("WSAStartup failed. %d", rc);