]> git.pond.sub.org Git - empserver/blob - src/server/main.c
Make conftab.c independent of the current directory
[empserver] / src / server / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  main.c: Empire Server main, startup and shutdown
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996, 1998
33  *     Doug Hay, 1998
34  *     Ron Koenderink, 2004-2005
35  *     Markus Armbruster, 2005-2007
36  */
37
38 #include <config.h>
39
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdio.h>
43
44 #if defined(_WIN32)
45 #include <winsock2.h>
46 #undef NS_ALL
47 #include <process.h>
48 #include "service.h"
49 #endif
50 #include <unistd.h>
51
52 #include "empio.h"
53 #include "empthread.h"
54 #include "file.h"
55 #include "journal.h"
56 #include "land.h"
57 #include "misc.h"
58 #include "nat.h"
59 #include "nuke.h"
60 #include "optlist.h"
61 #include "plane.h"
62 #include "player.h"
63 #include "product.h"
64 #include "prototypes.h"
65 #include "sect.h"
66 #include "server.h"
67 #include "ship.h"
68 #include "version.h"
69
70 static void create_pidfile(char *, pid_t);
71
72 #if defined(_WIN32)
73 static void loc_NTInit(void);
74 static void loc_NTTerm(void);
75 #endif
76
77 /*
78  * Lock to synchronize player threads with update and shutdown.
79  * Update and shutdown takes it exclusive, commands take it shared.
80  */
81 empth_rwlock_t *play_lock;
82
83 /*
84  * Is a thread attempting to take an exclusive play_lock?
85  * Threads holding a shared play_lock must not sleep while this is
86  * true.
87  */
88 int play_wrlock_wanted;
89
90 static char pidfname[] = "server.pid";
91
92 /* Run as daemon?  If yes, detach from controlling terminal etc. */
93 static int daemonize = 1;
94
95 static void
96 print_usage(char *program_name)
97 {
98     printf("Usage: %s [OPTION]...\n"
99            "  -d              debug mode\n"
100            "  -e CONFIG-FILE  configuration file\n"
101            "                  (default %s)\n"
102            "  -h              display this help and exit\n"
103 #ifdef _WIN32
104            "  -i              install service `%s'\n"
105            "  -I NAME         install service NAME\n"
106 #endif
107            "  -p              threading debug mode, implies -d\n"
108 #ifdef _WIN32
109            "  -u              uninstall service `%s'\n"
110            "  -U NAME         uninstall service NAME\n"
111 #endif
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
116 #ifdef _WIN32
117            , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
118 #endif
119         );
120 }
121
122 int
123 main(int argc, char **argv)
124 {
125     int flags = 0;
126 #if defined(_WIN32)
127     int install_service_set = 0;
128     char *program_name = NULL;
129     char *service_name = NULL;
130     int remove_service_set = 0;
131 #endif
132     char *config_file = NULL;
133     int op, sig;
134     unsigned seed = time(NULL);
135
136     debug = 0;
137
138 #ifdef _WIN32
139 # define XOPTS "iI:uU:"
140 #else
141 # define XOPTS
142 #endif
143     while ((op = getopt(argc, argv, "de:hpsR:v" XOPTS)) != EOF) {
144         switch (op) {
145         case 'p':
146             flags |= EMPTH_PRINT;
147             /* fall through */
148         case 'd':
149             debug = 1;
150             daemonize = 0;
151             break;
152         case 'e':
153             config_file = optarg;
154             break;
155 #if defined(_WIN32)
156         case 'I':
157             service_name = optarg;
158             /* fall through */
159         case 'i':
160             install_service_set++;
161             break;
162         case 'U':
163             service_name = optarg;
164             /* fall through */
165         case 'u':
166             remove_service_set++;
167             break;
168 #endif  /* _WIN32 */
169         case 's':
170             flags |= EMPTH_STACKCHECK;
171             break;
172         case 'R':
173             seed = strtoul(optarg, NULL, 10);
174             break;
175         case 'v':
176             printf("%s\n\n%s", version, legal);
177             return EXIT_SUCCESS;
178         case 'h':
179             print_usage(argv[0]);
180             return EXIT_SUCCESS;
181         default:
182             fprintf(stderr, "Try -h for help.\n");
183             return EXIT_FAILURE;
184         }
185     }
186
187 #if defined(_WIN32)
188     if ((debug || flags || config_file != NULL) &&
189         remove_service_set) {
190         fprintf(stderr, "Can't use -p, -s, -d or -e with either "
191             "-u or -U options\n");
192         exit(EXIT_FAILURE);
193     }
194     if ((debug || flags) && install_service_set) {
195         fprintf(stderr, "Can't use -d, -p or -s with either "
196             "-i or -I options\n");
197         exit(EXIT_FAILURE);
198     }
199     if (install_service_set && remove_service_set) {
200         fprintf(stderr, "Can't use both -u or -U and -i or -I "
201             "options\n");
202         exit(EXIT_FAILURE);
203     }
204
205     if (remove_service_set)
206         return remove_service(service_name);
207     if (install_service_set) {
208         program_name = _fullpath(NULL, argv[0], 0);
209         if (config_file != NULL)
210             config_file = _fullpath(NULL, config_file, 0);
211     }
212 #endif  /* _WIN32 */
213
214     if (emp_config(config_file) < 0)
215         exit(EXIT_FAILURE);
216     ef_init();
217     if (read_builtin_tables() < 0)
218         exit(EXIT_FAILURE);
219     if (read_custom_tables() < 0)
220         exit(EXIT_FAILURE);
221     if (chdir(gamedir)) {
222         fprintf(stderr, "Can't chdir to %s (%s)\n",
223                 gamedir, strerror(errno));
224         exit(EXIT_FAILURE);
225     }
226
227 #if defined(_WIN32)
228     if (install_service_set)
229         return install_service(program_name, service_name, config_file);
230 #endif  /* _WIN32 */
231
232     init_server(seed);
233
234 #if defined(_WIN32)
235     if (daemonize != 0) {
236         SERVICE_TABLE_ENTRY DispatchTable[]={
237             {"Empire Server", service_main},
238             {NULL, NULL}
239         };
240         if (StartServiceCtrlDispatcher(DispatchTable))
241             return 0;
242         else {
243             /*
244              * If it is service startup error then exit otherwise
245              * start server in the foreground
246              */
247             if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
248                 logerror("Failed to dispatch service (%lu)",
249                          GetLastError());
250                 finish_server();
251                 exit(EXIT_FAILURE);
252             }
253         }
254     }
255     daemonize = 0;
256 #else  /* !_WIN32 */
257     if (daemonize) {
258         if (disassoc() < 0) {
259             logerror("Can't become daemon (%s)", strerror(errno));
260             exit(1);
261         }
262     }
263 #endif /* !_WIN32 */
264     start_server(flags);
265
266     for (;;) {
267         sig = empth_wait_for_signal();
268 #ifdef SIGHUP
269         if (sig == SIGHUP) {
270             /* if you make changes here, also update relo() */
271             journal_reopen();
272             update_reschedule();
273             logreopen();
274             continue;
275         }
276 #endif
277         break;
278     }
279
280     shutdwn(sig);
281     CANT_REACH();
282     finish_server();
283     return EXIT_SUCCESS;
284 }
285
286
287 /*
288  * Initialize for serving, acquire resources.
289  */
290 void
291 init_server(unsigned seed)
292 {
293     srandom(seed);
294 #if defined(_WIN32)
295     loc_NTInit();
296 #endif
297     player_init();
298     ef_init_srv();
299     io_init();
300     init_nreport();
301
302     loginit("server");
303 }
304
305 /*
306  * Start serving.
307  */
308 void
309 start_server(int flags)
310 {
311     pid_t pid;
312
313     pid = getpid();
314     create_pidfile(pidfname, pid);
315     logerror("------------------------------------------------------");
316     logerror("Empire server (pid %d) started", (int)pid);
317
318     empth_init((void **)&player, flags);
319
320     if (journal_startup() < 0)
321         exit(1);
322
323     empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
324     empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
325     empth_create(delete_lostitems, 50 * 1024, flags, "DeleteItems", 0);
326
327     market_init();
328     update_init();
329 }
330
331 /*
332  * Finish serving, release resources.
333  */
334 void
335 finish_server(void)
336 {
337     ef_fin_srv();
338 #if defined(_WIN32)
339     loc_NTTerm();
340 #endif
341     journal_shutdown();
342     remove(pidfname);
343 }
344
345 static void
346 create_pidfile(char *fname, pid_t pid)
347 {
348     FILE *pidf = fopen(fname, "w");
349     if (!pidf
350         || fprintf(pidf, "%d\n", (int)pid) < 0
351         || fclose(pidf)) {
352         logerror("Can't write PID file (%s)", strerror(errno));
353         exit(1);
354     }
355 }
356
357 void
358 shutdwn(int sig)
359 {
360     struct player *p;
361
362     logerror("Shutdown commencing (cleaning up threads.)");
363
364     play_wrlock_wanted = 1;
365     for (p = player_next(0); p != 0; p = player_next(p)) {
366         if (p->state != PS_PLAYING)
367             continue;
368         pr_flash(p, "Server shutting down...\n");
369         p->state = PS_SHUTDOWN;
370         p->aborted++;
371         if (p->command) {
372             pr_flash(p, "Shutdown aborting command\n");
373         }
374         empth_wakeup(p->proc);
375     }
376     empth_rwlock_wrlock(play_lock);
377     if (sig)
378         logerror("Server shutting down on signal %d", sig);
379     else
380         logerror("Server shutting down at deity's request");
381     finish_server();
382
383 #if defined(_WIN32)
384     if (daemonize)
385         stop_service();
386 #endif
387     exit(0);
388 }
389
390 #if defined(_WIN32)
391 static void
392 loc_NTInit(void)
393 {
394     int rc;
395     WORD wVersionRequested;
396     WSADATA wsaData;
397
398     wVersionRequested = MAKEWORD(2, 0);
399     rc = WSAStartup(wVersionRequested, &wsaData);
400     if (rc != 0) {
401         logerror("WSAStartup failed.  %d", rc);
402         exit(1);
403     }
404 }
405
406 static void
407 loc_NTTerm(void)
408 {
409     WSACleanup();
410 }
411 #endif