]> git.pond.sub.org Git - empserver/blob - src/server/main.c
(player_kill_idle): Don't kill hung player threads. That code was
[empserver] / src / server / main.c
1 /*
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
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 <direct.h>
49 #include "service.h"
50 #include "../lib/gen/getopt.h"
51 #else
52 #include <sys/types.h>
53 #include <unistd.h>
54 #endif
55
56 #include "empio.h"
57 #include "empthread.h"
58 #include "file.h"
59 #include "journal.h"
60 #include "land.h"
61 #include "misc.h"
62 #include "nat.h"
63 #include "nuke.h"
64 #include "optlist.h"
65 #include "plane.h"
66 #include "player.h"
67 #include "product.h"
68 #include "prototypes.h"
69 #include "sect.h"
70 #include "server.h"
71 #include "ship.h"
72 #include "version.h"
73
74 static void create_pidfile(char *, pid_t);
75
76 #if defined(_WIN32)
77 static void loc_NTInit(void);
78 static void loc_NTTerm(void);
79 #endif
80
81 /*
82  * Lock to synchronize player threads with update and shutdown.
83  * Update and shutdown takes it exclusive, commands take it shared.
84  */
85 empth_rwlock_t *play_lock;
86
87 /*
88  * Is a thread attempting to take an exclusive play_lock?
89  * Threads holding a shared play_lock must not sleep while this is
90  * true.
91  */
92 int play_wrlock_wanted;
93
94 static char pidfname[] = "server.pid";
95
96 /* Run as daemon?  If yes, detach from controlling terminal etc. */
97 static int daemonize = 1;
98
99 static void
100 print_usage(char *program_name)
101 {
102     printf("Usage: %s [OPTION]...\n"
103            "  -d              debug mode\n"
104            "  -e CONFIG-FILE  configuration file\n"
105            "                  (default %s)\n"
106            "  -h              display this help and exit\n"
107 #ifdef _WIN32
108            "  -i              install service `%s'\n"
109            "  -I NAME         install service NAME\n"
110 #endif
111            "  -p              threading debug mode, implies -d\n"
112 #ifdef _WIN32
113            "  -r              remove service `%s'\n"
114            "  -R NAME         remove service NAME\n"
115 #endif
116            "  -s              enable stack checking\n"
117            "  -v              display version information and exit\n",
118            program_name, dflt_econfig
119 #ifdef _WIN32
120            , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
121 #endif
122         );
123 }
124
125 int
126 main(int argc, char **argv)
127 {
128     int flags = 0;
129 #if defined(_WIN32)
130     int install_service_set = 0;
131     char *program_name = NULL;
132     char *service_name = NULL;
133     int remove_service_set = 0;
134 #endif
135     char *config_file = NULL;
136     int op, sig;
137
138 #ifdef _WIN32
139 # define XOPTS "iI:rR:"
140 #else
141 # define XOPTS
142 #endif
143     while ((op = getopt(argc, argv, "de:hpsv" 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 'R':
163             service_name = optarg;
164             /* fall through */
165         case 'r':
166             remove_service_set++;
167             break;
168 #endif  /* _WIN32 */
169         case 's':
170             flags |= EMPTH_STACKCHECK;
171             break;
172         case 'v':
173             printf("%s\n\n%s", version, legal);
174             return EXIT_SUCCESS;
175         case 'h':
176             print_usage(argv[0]);
177             return EXIT_SUCCESS;
178         default:
179             fprintf(stderr, "Try -h for help.\n");
180             return EXIT_FAILURE;
181         }
182     }
183
184 #if defined(_WIN32)
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");
189         exit(EXIT_FAILURE);
190     }
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");
194         exit(EXIT_FAILURE);
195     }
196     if (install_service_set && remove_service_set) {
197         fprintf(stderr, "Can't use both -r or -R and -i or -I "
198             "options\n");
199         exit(EXIT_FAILURE);
200     }
201
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);
208     }
209 #endif  /* _WIN32 */
210
211     if (emp_config(config_file) < 0)
212         exit(EXIT_FAILURE);
213     ef_init();
214     if (chdir(configdir)) {
215         fprintf(stderr, "Can't chdir to %s (%s)\n",
216                 configdir, strerror(errno));
217         exit(EXIT_FAILURE);
218     }
219     if (chdir(builtindir)) {
220         fprintf(stderr, "Can't chdir to %s (%s)\n",
221                 builtindir, strerror(errno));
222         exit(EXIT_FAILURE);
223     }
224     if (read_builtin_tables() < 0)
225         exit(EXIT_FAILURE);
226     if (chdir(configdir)) {
227         fprintf(stderr, "Can't chdir to %s (%s)\n",
228                 configdir, strerror(errno));
229         exit(EXIT_FAILURE);
230     }
231     if (read_custom_tables() < 0)
232         exit(EXIT_FAILURE);
233     if (chdir(gamedir)) {
234         fprintf(stderr, "Can't chdir to %s (%s)\n",
235                 gamedir, strerror(errno));
236         exit(EXIT_FAILURE);
237     }
238
239 #if defined(_WIN32)
240     if (install_service_set)
241         return install_service(program_name, service_name, config_file);
242 #endif  /* _WIN32 */
243
244     init_server();
245
246 #if defined(_WIN32)
247     if (daemonize != 0) {
248         SERVICE_TABLE_ENTRY DispatchTable[]={
249             {"Empire Server", service_main},
250             {NULL, NULL}
251         };
252         if (StartServiceCtrlDispatcher(DispatchTable))
253             return 0;
254         else {
255             /*
256              * If it is service startup error then exit otherwise
257              * start server in the foreground
258              */
259             if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
260                 logerror("Failed to dispatch service (%lu)",
261                          GetLastError());
262                 finish_server();
263                 exit(EXIT_FAILURE);
264             }
265         }
266     }
267     daemonize = 0;
268 #else  /* !_WIN32 */
269     if (daemonize) {
270         if (disassoc() < 0) {
271             logerror("Can't become daemon (%s)", strerror(errno));
272             exit(1);
273         }
274     }
275 #endif /* !_WIN32 */
276     start_server(flags);
277
278     for (;;) {
279         sig = empth_wait_for_signal();
280 #ifdef SIGHUP
281         if (sig == SIGHUP) {
282             /* if you make changes here, also update relo() */
283             journal_reopen();
284             update_reschedule();
285             continue;
286         }
287 #endif
288         break;
289     }
290
291     shutdwn(sig);
292     CANT_REACH();
293     finish_server();
294     return EXIT_SUCCESS;
295 }
296
297
298 /*
299  * Initialize for serving, acquire resources.
300  */
301 void
302 init_server(void)
303 {
304     srandom(time(NULL));
305 #if defined(_WIN32)
306     loc_NTInit();
307 #endif
308     player_init();
309     ef_init_srv();
310     io_init();
311     init_nreport();
312
313     loginit("server");
314 }
315
316 /*
317  * Start serving.
318  */
319 void
320 start_server(int flags)
321 {
322     pid_t pid;
323
324     pid = getpid();
325     create_pidfile(pidfname, pid);
326     logerror("------------------------------------------------------");
327     logerror("Empire server (pid %d) started", (int)pid);
328
329     empth_init((void **)&player, flags);
330
331     if (journal_startup() < 0)
332         exit(1);
333
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);
337
338     market_init();
339     update_init();
340 }
341
342 /*
343  * Finish serving, release resources.
344  */
345 void
346 finish_server(void)
347 {
348     ef_fin_srv();
349 #if defined(_WIN32)
350     loc_NTTerm();
351 #endif
352     journal_shutdown();
353     remove(pidfname);
354 }
355
356 static void
357 create_pidfile(char *fname, pid_t pid)
358 {
359     FILE *pidf = fopen(fname, "w");
360     if (!pidf
361         || fprintf(pidf, "%d\n", (int)pid) < 0
362         || fclose(pidf)) {
363         logerror("Can't write PID file (%s)", strerror(errno));
364         exit(1);
365     }
366 }
367
368 void
369 shutdwn(int sig)
370 {
371     struct player *p;
372
373     logerror("Shutdown commencing (cleaning up threads.)");
374
375     play_wrlock_wanted = 1;
376     for (p = player_next(0); p != 0; p = player_next(p)) {
377         if (p->state != PS_PLAYING)
378             continue;
379         pr_flash(p, "Server shutting down...\n");
380         p->state = PS_SHUTDOWN;
381         p->aborted++;
382         if (p->command) {
383             pr_flash(p, "Shutdown aborting command\n");
384         }
385         empth_wakeup(p->proc);
386     }
387     empth_rwlock_wrlock(play_lock);
388     if (sig)
389         logerror("Server shutting down on signal %d", sig);
390     else
391         logerror("Server shutting down at deity's request");
392     finish_server();
393
394 #if defined(_WIN32)
395     if (daemonize)
396         stop_service();
397 #endif
398     exit(0);
399 }
400
401 #if defined(_WIN32)
402 static void
403 loc_NTInit(void)
404 {
405     int rc;
406     WORD wVersionRequested;
407     WSADATA wsaData;
408
409     wVersionRequested = MAKEWORD(2, 0);
410     rc = WSAStartup(wVersionRequested, &wsaData);
411     if (rc != 0) {
412         logerror("WSAStartup failed.  %d", rc);
413         exit(1);
414     }
415 }
416
417 static void
418 loc_NTTerm(void)
419 {
420     WSACleanup();
421 }
422 #endif