]> git.pond.sub.org Git - empserver/blob - src/server/main.c
(update_running): New.
[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 static char pidfname[] = "server.pid";
82
83 /* Run as daemon?  If yes, detach from controlling terminal etc. */
84 static int daemonize = 1;
85
86 static void
87 print_usage(char *program_name)
88 {
89     printf("Usage: %s [OPTION]...\n"
90            "  -d              debug mode\n"
91            "  -e CONFIG-FILE  configuration file\n"
92            "                  (default %s)\n"
93            "  -h              display this help and exit\n"
94 #ifdef _WIN32
95            "  -i              install service `%s'\n"
96            "  -I NAME         install service NAME\n"
97 #endif
98            "  -p              threading debug mode, implies -d\n"
99 #ifdef _WIN32
100            "  -r              remove service `%s'\n"
101            "  -R NAME         remove service NAME\n"
102 #endif
103            "  -s              enable stack checking\n"
104            "  -v              display version information and exit\n",
105            program_name, dflt_econfig
106 #ifdef _WIN32
107            , DEFAULT_SERVICE_NAME, DEFAULT_SERVICE_NAME
108 #endif
109         );
110 }
111
112 int
113 main(int argc, char **argv)
114 {
115     int flags = 0;
116 #if defined(_WIN32)
117     int install_service_set = 0;
118     char *program_name = NULL;
119     char *service_name = NULL;
120     int remove_service_set = 0;
121 #endif
122     char *config_file = NULL;
123     int op, sig;
124
125 #ifdef _WIN32
126 # define XOPTS "iI:rR:"
127 #else
128 # define XOPTS
129 #endif
130     while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
131         switch (op) {
132         case 'p':
133             flags |= EMPTH_PRINT;
134             /* fall through */
135         case 'd':
136             debug = 1;
137             daemonize = 0;
138             break;
139         case 'e':
140             config_file = optarg;
141             break;
142 #if defined(_WIN32)
143         case 'I':
144             service_name = optarg;
145             /* fall through */
146         case 'i':
147             install_service_set++;
148             break;
149         case 'R':
150             service_name = optarg;
151             /* fall through */
152         case 'r':
153             remove_service_set++;
154             break;
155 #endif  /* _WIN32 */
156         case 's':
157             flags |= EMPTH_STACKCHECK;
158             break;
159         case 'v':
160             printf("%s\n\n%s", version, legal);
161             return EXIT_SUCCESS;
162         case 'h':
163             print_usage(argv[0]);
164             return EXIT_SUCCESS;
165         default:
166             fprintf(stderr, "Try -h for help.\n");
167             return EXIT_FAILURE;
168         }
169     }
170
171 #if defined(_WIN32)
172     if ((debug || flags || config_file != NULL) &&
173         remove_service_set) {
174         fprintf(stderr, "Can't use -p, -s, -d or -e with either "
175             "-r or -R options\n");
176         exit(EXIT_FAILURE);
177     }
178     if ((debug || flags) && install_service_set) {
179         fprintf(stderr, "Can't use -d, -p or -s with either "
180             "-i or -I options\n");
181         exit(EXIT_FAILURE);
182     }
183     if (install_service_set && remove_service_set) {
184         fprintf(stderr, "Can't use both -r or -R and -i or -I "
185             "options\n");
186         exit(EXIT_FAILURE);
187     }
188
189     if (remove_service_set)
190         return remove_service(service_name);
191     if (install_service_set) {
192         program_name = _fullpath(NULL, argv[0], 0);
193         if (config_file != NULL)
194             config_file = _fullpath(NULL, config_file, 0);
195     }
196 #endif  /* _WIN32 */
197
198     if (emp_config(config_file) < 0)
199         exit(EXIT_FAILURE);
200     ef_init();
201     if (chdir(configdir)) {
202         fprintf(stderr, "Can't chdir to %s (%s)\n",
203                 configdir, strerror(errno));
204         exit(EXIT_FAILURE);
205     }
206     if (chdir(builtindir)) {
207         fprintf(stderr, "Can't chdir to %s (%s)\n",
208                 builtindir, strerror(errno));
209         exit(EXIT_FAILURE);
210     }
211     if (read_builtin_tables() < 0)
212         exit(EXIT_FAILURE);
213     if (chdir(configdir)) {
214         fprintf(stderr, "Can't chdir to %s (%s)\n",
215                 configdir, strerror(errno));
216         exit(EXIT_FAILURE);
217     }
218     if (read_custom_tables() < 0)
219         exit(EXIT_FAILURE);
220     if (chdir(gamedir)) {
221         fprintf(stderr, "Can't chdir to %s (%s)\n",
222                 gamedir, strerror(errno));
223         exit(EXIT_FAILURE);
224     }
225
226 #if defined(_WIN32)
227     if (install_service_set)
228         return install_service(program_name, service_name, config_file);
229 #endif  /* _WIN32 */
230
231     init_server();
232
233 #if defined(_WIN32)
234     if (daemonize != 0) {
235         SERVICE_TABLE_ENTRY DispatchTable[]={
236             {"Empire Server", service_main},
237             {NULL, NULL}
238         };
239         if (StartServiceCtrlDispatcher(DispatchTable))
240             return 0;
241         else {
242             /*
243              * If it is service startup error then exit otherwise
244              * start server in the foreground
245              */
246             if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
247                 logerror("Failed to dispatch service (%lu)",
248                          GetLastError());
249                 finish_server();
250                 exit(EXIT_FAILURE);
251             }
252         }
253     }
254     daemonize = 0;
255 #else  /* !_WIN32 */
256     if (daemonize) {
257         if (disassoc() < 0) {
258             logerror("Can't become daemon (%s)", strerror(errno));
259             exit(1);
260         }
261     }
262 #endif /* !_WIN32 */
263     start_server(flags);
264
265     for (;;) {
266         sig = empth_wait_for_signal();
267 #ifdef SIGHUP
268         if (sig == SIGHUP) {
269             /* if you make changes here, also update relo() */
270             journal_reopen();
271             update_reschedule();
272             continue;
273         }
274 #endif
275         break;
276     }
277
278     shutdwn(sig);
279     CANT_REACH();
280     finish_server();
281     return EXIT_SUCCESS;
282 }
283
284
285 /*
286  * Initialize for serving, acquire resources.
287  */
288 void
289 init_server(void)
290 {
291     srandom(time(NULL));
292 #if defined(_WIN32)
293     loc_NTInit();
294 #endif
295     player_init();
296     ef_init_srv();
297     io_init();
298     init_nreport();
299
300     loginit("server");
301 }
302
303 /*
304  * Start serving.
305  */
306 void
307 start_server(int flags)
308 {
309     pid_t pid;
310
311     pid = getpid();
312     create_pidfile(pidfname, pid);
313     logerror("------------------------------------------------------");
314     logerror("Empire server (pid %d) started", (int)pid);
315
316     empth_init((void **)&player, flags);
317
318     if (journal_startup() < 0)
319         exit(1);
320
321     empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
322                  "AcceptPlayers", 0);
323     empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
324                  "KillIdle", 0);
325     empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
326                  "DeleteItems", 0);
327
328     market_init();
329     update_init();
330 }
331
332 /*
333  * Finish serving, release resources.
334  */
335 void
336 finish_server(void)
337 {
338     ef_fin_srv();
339 #if defined(_WIN32)
340     loc_NTTerm();
341 #endif
342     journal_shutdown();
343     remove(pidfname);
344 }
345
346 static void
347 create_pidfile(char *fname, pid_t pid)
348 {
349     FILE *pidf = fopen(fname, "w");
350     if (!pidf
351         || fprintf(pidf, "%d\n", (int)pid) < 0
352         || fclose(pidf)) {
353         logerror("Can't write PID file (%s)", strerror(errno));
354         exit(1);
355     }
356 }
357
358 void
359 shutdwn(int sig)
360 {
361     struct player *p;
362
363     logerror("Shutdown commencing (cleaning up threads.)");
364
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(update_lock);
377     /* rely on player_kill_idle() for killing hung player threads */
378     if (sig)
379         logerror("Server shutting down on signal %d", sig);
380     else
381         logerror("Server shutting down at deity's request");
382     finish_server();
383
384 #if defined(_WIN32)
385     if (daemonize)
386         stop_service();
387 #endif
388     exit(0);
389 }
390
391 #if defined(_WIN32)
392 static void
393 loc_NTInit(void)
394 {
395     int rc;
396     WORD wVersionRequested;
397     WSADATA wsaData;
398
399     wVersionRequested = MAKEWORD(2, 0);
400     rc = WSAStartup(wVersionRequested, &wsaData);
401     if (rc != 0) {
402         logerror("WSAStartup failed.  %d", rc);
403         exit(1);
404     }
405 }
406
407 static void
408 loc_NTTerm(void)
409 {
410     WSACleanup();
411 }
412 #endif