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
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);
137 # define XOPTS "iI:uU:"
141 while ((op = getopt(argc, argv, "de:hpsR:v" XOPTS)) != EOF) {
144 flags |= EMPTH_PRINT;
151 config_file = optarg;
155 service_name = optarg;
158 install_service_set++;
161 service_name = optarg;
164 remove_service_set++;
168 flags |= EMPTH_STACKCHECK;
171 seed = strtoul(optarg, NULL, 10);
174 printf("%s\n\n%s", version, legal);
177 print_usage(argv[0]);
180 fprintf(stderr, "Try -h for help.\n");
186 if ((debug || flags || config_file != NULL) &&
187 remove_service_set) {
188 fprintf(stderr, "Can't use -p, -s, -d or -e with either "
189 "-u or -U options\n");
192 if ((debug || flags) && install_service_set) {
193 fprintf(stderr, "Can't use -d, -p or -s with either "
194 "-i or -I options\n");
197 if (install_service_set && remove_service_set) {
198 fprintf(stderr, "Can't use both -u or -U and -i or -I "
203 if (remove_service_set)
204 return remove_service(service_name);
205 if (install_service_set) {
206 program_name = _fullpath(NULL, argv[0], 0);
207 if (config_file != NULL)
208 config_file = _fullpath(NULL, config_file, 0);
212 if (emp_config(config_file) < 0)
215 if (chdir(configdir)) {
216 fprintf(stderr, "Can't chdir to %s (%s)\n",
217 configdir, strerror(errno));
220 if (chdir(builtindir)) {
221 fprintf(stderr, "Can't chdir to %s (%s)\n",
222 builtindir, strerror(errno));
225 if (read_builtin_tables() < 0)
227 if (chdir(configdir)) {
228 fprintf(stderr, "Can't chdir to %s (%s)\n",
229 configdir, strerror(errno));
232 if (read_custom_tables() < 0)
234 if (chdir(gamedir)) {
235 fprintf(stderr, "Can't chdir to %s (%s)\n",
236 gamedir, strerror(errno));
241 if (install_service_set)
242 return install_service(program_name, service_name, config_file);
248 if (daemonize != 0) {
249 SERVICE_TABLE_ENTRY DispatchTable[]={
250 {"Empire Server", service_main},
253 if (StartServiceCtrlDispatcher(DispatchTable))
257 * If it is service startup error then exit otherwise
258 * start server in the foreground
260 if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
261 logerror("Failed to dispatch service (%lu)",
271 if (disassoc() < 0) {
272 logerror("Can't become daemon (%s)", strerror(errno));
280 sig = empth_wait_for_signal();
283 /* if you make changes here, also update relo() */
300 * Initialize for serving, acquire resources.
303 init_server(unsigned seed)
321 start_server(int flags)
326 create_pidfile(pidfname, pid);
327 logerror("------------------------------------------------------");
328 logerror("Empire server (pid %d) started", (int)pid);
330 empth_init((void **)&player, flags);
332 if (journal_startup() < 0)
335 empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
336 empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
337 empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
344 * Finish serving, release resources.
358 create_pidfile(char *fname, pid_t pid)
360 FILE *pidf = fopen(fname, "w");
362 || fprintf(pidf, "%d\n", (int)pid) < 0
364 logerror("Can't write PID file (%s)", strerror(errno));
374 logerror("Shutdown commencing (cleaning up threads.)");
376 play_wrlock_wanted = 1;
377 for (p = player_next(0); p != 0; p = player_next(p)) {
378 if (p->state != PS_PLAYING)
380 pr_flash(p, "Server shutting down...\n");
381 p->state = PS_SHUTDOWN;
384 pr_flash(p, "Shutdown aborting command\n");
386 empth_wakeup(p->proc);
388 empth_rwlock_wrlock(play_lock);
390 logerror("Server shutting down on signal %d", sig);
392 logerror("Server shutting down at deity's request");
407 WORD wVersionRequested;
410 wVersionRequested = MAKEWORD(2, 0);
411 rc = WSAStartup(wVersionRequested, &wsaData);
413 logerror("WSAStartup failed. %d", rc);