]> git.pond.sub.org Git - empserver/blob - src/server/main.c
Update known contributors comments
[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-2008
36  */
37
38 #include <config.h>
39
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <unistd.h>
44
45 #if defined(_WIN32)
46 #include <winsock2.h>
47 #undef NS_ALL
48 #include <process.h>
49 #include "service.h"
50 #endif
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     empfile_init();
215     if (emp_config(config_file) < 0)
216         exit(EXIT_FAILURE);
217     empfile_fixup();
218     if (read_builtin_tables() < 0)
219         exit(EXIT_FAILURE);
220     if (read_custom_tables() < 0)
221         exit(EXIT_FAILURE);
222     if (chdir(gamedir)) {
223         fprintf(stderr, "Can't chdir to %s (%s)\n",
224                 gamedir, strerror(errno));
225         exit(EXIT_FAILURE);
226     }
227
228 #if defined(_WIN32)
229     if (install_service_set)
230         return install_service(program_name, service_name, config_file);
231 #endif  /* _WIN32 */
232
233     init_server(seed);
234
235 #if defined(_WIN32)
236     if (daemonize != 0) {
237         SERVICE_TABLE_ENTRY DispatchTable[]={
238             {"Empire Server", service_main},
239             {NULL, NULL}
240         };
241         if (StartServiceCtrlDispatcher(DispatchTable))
242             return 0;
243         else {
244             /*
245              * If it is service startup error then exit otherwise
246              * start server in the foreground
247              */
248             if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
249                 logerror("Failed to dispatch service (%lu)",
250                          GetLastError());
251                 finish_server();
252                 exit(EXIT_FAILURE);
253             }
254         }
255     }
256     daemonize = 0;
257 #else  /* !_WIN32 */
258     if (daemonize) {
259         if (disassoc() < 0) {
260             logerror("Can't become daemon (%s)", strerror(errno));
261             exit(1);
262         }
263     }
264 #endif /* !_WIN32 */
265     start_server(flags);
266
267     for (;;) {
268         sig = empth_wait_for_signal();
269 #ifdef SIGHUP
270         if (sig == SIGHUP) {
271             /* if you make changes here, also update relo() */
272             journal_reopen();
273             update_reschedule();
274             logreopen();
275             continue;
276         }
277 #endif
278         break;
279     }
280
281     shutdwn(sig);
282     CANT_REACH();
283     finish_server();
284     return EXIT_SUCCESS;
285 }
286
287
288 /*
289  * Initialize for serving, acquire resources.
290  */
291 void
292 init_server(unsigned seed)
293 {
294     srandom(seed);
295 #if defined(_WIN32)
296     loc_NTInit();
297 #endif
298     player_init();
299     ef_init_srv();
300     io_init();
301     init_nreport();
302
303     loginit("server");
304 }
305
306 /*
307  * Start serving.
308  */
309 void
310 start_server(int flags)
311 {
312     pid_t pid;
313
314     pid = getpid();
315     create_pidfile(pidfname, pid);
316     logerror("------------------------------------------------------");
317     logerror("Empire server (pid %d) started", (int)pid);
318
319     empth_init((void **)&player, flags);
320
321     if (journal_startup() < 0)
322         exit(1);
323
324     empth_create(player_accept, 50 * 1024, flags, "AcceptPlayers", 0);
325     empth_create(player_kill_idle, 50 * 1024, flags, "KillIdle", 0);
326     empth_create(delete_lostitems, 50 * 1024, flags, "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     play_wrlock_wanted = 1;
366     for (p = player_next(0); p != 0; p = player_next(p)) {
367         if (p->state != PS_PLAYING)
368             continue;
369         pr_flash(p, "Server shutting down...\n");
370         p->state = PS_SHUTDOWN;
371         p->aborted++;
372         if (p->command) {
373             pr_flash(p, "Shutdown aborting command\n");
374         }
375         empth_wakeup(p->proc);
376     }
377     empth_rwlock_wrlock(play_lock);
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