]> git.pond.sub.org Git - empserver/blob - src/server/main.c
Supply prototypes where possible. This uncovered type errors with
[empserver] / src / server / main.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  main.c: Thread and signal initialization for Empire Server
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996, 1998
33  *     Doug Hay, 1998
34  */
35
36 #if defined(aix) || defined(linux)
37 #include <unistd.h>
38 #endif /* aix or linux */
39
40 #include <signal.h>
41 #include <errno.h>
42 #if !defined(_WIN32)
43 #include <sys/ioctl.h>
44 #endif
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <string.h>
48
49 #if defined(_WIN32)
50 #include <winsock.h>
51 #endif
52
53 #include "misc.h"
54 #include "nat.h"
55 #include "file.h"
56 #include "player.h"
57 #include "empthread.h"
58 #include "plane.h"
59 #include "nuke.h"
60 #include "land.h"
61 #include "ship.h"
62 #include "sect.h"
63 #include "var.h"
64 #include "product.h"
65 #include "optlist.h"
66 #include "global.h"
67 #include "prototypes.h"
68
69 s_char program[] = "server";
70
71 extern void player_accept(void *);
72 extern void player_kill_idle(void *);
73 extern void update_sched(void *);
74 extern void delete_lostitems(void *);
75 void nullify_objects(void);
76 void init_files(void);
77 void close_files(void);
78
79 #if defined(_WIN32)
80 static void loc_NTInit(void);
81 static void loc_NTTerm(void);
82 #endif
83
84 extern void mobility_init(void);
85 extern void mobility_check(void *);
86 extern void market_update(void *);
87
88 #if !defined(_WIN32)
89 static int mainpid = 0;
90 #endif
91
92 int
93 main(int argc, char **argv)
94 {
95     time_t now;
96     int hour[2];
97     int flags = 0;
98     int debug = 0;
99     int op;
100     char *config_file = NULL;
101     extern s_char *datadir;
102     extern char *optarg;
103     s_char tbuf[256];
104 #ifdef POSIXSIGNALS
105     struct sigaction act;
106 #endif /* POSIXSIGNALS */
107
108 #if !defined(_WIN32)
109     mainpid = getpid();
110
111     while ((op = getopt(argc, argv, "D:de:psh")) != EOF) {
112         switch (op) {
113         case 'D':
114             datadir = optarg;
115             break;
116         case 'd':
117             debug++;
118             break;
119         case 'e':
120             config_file = optarg;
121             break;
122         case 'p':
123             flags |= EMPTH_PRINT;
124             break;
125         case 's':
126             flags |= EMPTH_PRINT | EMPTH_STACKCHECK;
127             break;
128         case 'h':
129         default:
130             printf("Usage: %s -d -p -s\n", argv[0]);
131             return 0;
132         }
133     }
134 #endif
135     if (config_file == NULL) {
136         sprintf(tbuf, "%s/econfig", datadir);
137         config_file = tbuf;
138     }
139
140     logerror("------------------------------------------------------");
141 #if !defined(_WIN32)
142     logerror("Empire server (pid %d) started", getpid());
143 #else
144     logerror("Empire server started");
145 #endif /* _WIN32 */
146
147 #if defined(_WIN32)
148     loc_NTInit();
149 #endif
150     emp_config(config_file);
151     update_policy_check();
152
153     nullify_objects();
154
155 #if !defined(_WIN32)
156     /* signal() should not be used with mit pthreads. Anyway if u
157        have a posix threads u definitly have posix signals -- Sasha */
158 #if defined (POSIXSIGNALS) || defined (_EMPTH_POSIX)
159 #ifdef SA_SIGINFO
160     act.sa_flags = SA_SIGINFO;
161 #endif
162     sigemptyset(&act.sa_mask);
163     if (debug == 0 && flags == 0) {
164         disassoc();
165     }
166     act.sa_handler = shutdwn;
167     /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
168        a Linux box running POSIX threads -- STM */
169 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
170     sigaction(SIGUSR1, &act, NULL);
171 #endif
172     sigaction(SIGTERM, &act, NULL);
173     sigaction(SIGINT, &act, NULL);
174     act.sa_handler = panic;
175     sigaction(SIGBUS, &act, NULL);
176     sigaction(SIGSEGV, &act, NULL);
177     sigaction(SIGILL, &act, NULL);
178     sigaction(SIGFPE, &act, NULL);
179     act.sa_handler = SIG_IGN;
180     sigaction(SIGPIPE, &act, NULL);
181 #else
182     if (debug == 0 && flags == 0) {
183         disassoc();
184         /* pthreads on Linux use SIGUSR1 (*shrug*) so only catch it if not on
185            a Linux box running POSIX threads -- STM */
186 #if !(defined(__linux__) && defined(_EMPTH_POSIX))
187         signal(SIGUSR1, shutdwn);
188 #endif
189         signal(SIGTERM, shutdwn);
190         signal(SIGBUS, panic);
191         signal(SIGSEGV, panic);
192         signal(SIGILL, panic);
193         signal(SIGFPE, panic);
194         signal(SIGINT, shutdwn);
195     }
196     signal(SIGPIPE, SIG_IGN);
197 #endif /* POSIXSIGNALS */
198 #endif /* _WIN32 */
199     empth_init((char **)&player, flags);
200     time(&now);
201 #if !defined(_WIN32)
202     srandom(now);
203 #else
204     srand(now);
205 #endif /* _WIN32 */
206     global_init();
207     shutdown_init();
208     player_init();
209     ef_init();
210     init_files();
211     io_init();
212     gamehours(now, hour);
213
214     if (opt_MOB_ACCESS) {
215         /* This fixes up mobility upon restart */
216         mobility_init();
217     }
218
219     empth_create(PP_ACCEPT, player_accept, (50 * 1024), flags,
220                  "AcceptPlayers", "Accept network connections", 0);
221     empth_create(PP_KILLIDLE, player_kill_idle, (50 * 1024), flags,
222                  "KillIdle", "Kills idle player connections", 0);
223     empth_create(PP_SCHED, update_sched, (50 * 1024), flags, "UpdateSched",
224                  "Schedules updates to occur", 0);
225     empth_create(PP_TIMESTAMP, delete_lostitems, (50 * 1024), flags,
226                  "DeleteItems", "Deletes old lost items", 0);
227     if (opt_MOB_ACCESS) {
228         /* Start the mobility access check thread */
229         empth_create(PP_TIMESTAMP, mobility_check, (50 * 1024), flags,
230                      "MobilityCheck", "Writes the timestamp file", 0);
231     }
232
233     if (opt_MARKET) {
234         empth_create(PP_TIMESTAMP, market_update, (50 * 1024), flags,
235                      "MarketUpdate", "Updates the market", 0);
236     }
237 #if defined(__linux__) && defined(_EMPTH_POSIX)
238     strcpy(tbuf, argv[0]);
239     for (op = 1; op < argc; op++) {
240         strcat(tbuf, " ");
241         strcat(tbuf, argv[op]);
242     }
243     sprintf(argv[0], "%s (main pid: %d)", tbuf, getpid());
244 #endif
245
246     empth_exit();
247
248 /* We should never get here.  But, just in case... */
249     close_files();
250
251 #if defined(_WIN32)
252     loc_NTTerm();
253 #endif
254     return 0;
255 }
256
257 void
258 init_files(void)
259 {
260     ef_open(EF_NATION, O_RDWR, EFF_MEM);
261     ef_open(EF_SECTOR, O_RDWR, EFF_MEM);
262     ef_open(EF_SHIP, O_RDWR, EFF_MEM);
263     ef_open(EF_PLANE, O_RDWR, EFF_MEM);
264     ef_open(EF_LAND, O_RDWR, EFF_MEM);
265     ef_open(EF_NEWS, O_RDWR, 0);
266     ef_open(EF_LOAN, O_RDWR, 0);
267     ef_open(EF_TREATY, O_RDWR, 0);
268     ef_open(EF_NUKE, O_RDWR, EFF_MEM);
269     ef_open(EF_POWER, O_RDWR, 0);
270     ef_open(EF_TRADE, O_RDWR, 0);
271     ef_open(EF_MAP, O_RDWR, EFF_MEM);
272     ef_open(EF_BMAP, O_RDWR, EFF_MEM);
273     ef_open(EF_COMM, O_RDWR, 0);
274     ef_open(EF_LOST, O_RDWR, 0);
275 }
276
277 void
278 close_files(void)
279 {
280     ef_close(EF_NATION);
281     ef_close(EF_SECTOR);
282     ef_close(EF_SHIP);
283     ef_close(EF_PLANE);
284     ef_close(EF_LAND);
285     ef_close(EF_NEWS);
286     ef_close(EF_LOAN);
287     ef_close(EF_TREATY);
288     ef_close(EF_NUKE);
289     ef_close(EF_POWER);
290     ef_close(EF_TRADE);
291     ef_close(EF_MAP);
292     ef_close(EF_COMM);
293     ef_close(EF_BMAP);
294     ef_close(EF_LOST);
295 }
296
297 /* we're going down.  try to close the files at least */
298 void
299 panic(int sig)
300 {
301 #if !defined(_WIN32)
302 #ifdef POSIXSIGNALS
303     struct sigaction act;
304
305     act.sa_flags = 0;
306     sigemptyset(&act.sa_mask);
307     act.sa_handler = SIG_DFL;
308     sigaction(SIGBUS, &act, NULL);
309     sigaction(SIGSEGV, &act, NULL);
310     sigaction(SIGILL, &act, NULL);
311     sigaction(SIGFPE, &act, NULL);
312 #else
313     signal(SIGBUS, SIG_DFL);
314     signal(SIGSEGV, SIG_DFL);
315     signal(SIGILL, SIG_DFL);
316     signal(SIGFPE, SIG_DFL);
317 #endif /* POSIXSIGNALS */
318 #endif /* _WIN32 */
319     logerror("server received fatal signal %d", sig);
320     log_last_commands();
321     close_files();
322     _exit(0);
323 }
324
325 void
326 shutdwn(int sig)
327 {
328     struct player *p;
329     time_t now;
330
331 #if defined(__linux__) && defined(_EMPTH_POSIX)
332 /* This is a hack to get around the way pthreads work on Linux.  This
333    may be useful on other platforms too where threads are turned into
334    processes. */
335     if (getpid() != mainpid) {
336         empth_t *me;
337
338         me = empth_self();
339         if (me && me->name) {
340             if (strlen(me->name) > 5) {
341                 /* Player threads are cleaned up below, so just have
342                    them return.  This should work. */
343                 if (!strncmp("Player", me->name, 6)) {
344                     return;
345                 }
346             }
347         }
348         /* Not a player thread - must be server thread, so exit */
349         empth_exit();
350         return;
351     }
352 #endif
353
354     logerror("Shutdown commencing (cleaning up threads.)");
355
356     for (p = player_next(0); p != 0; p = player_next(p)) {
357         if (p->state != PS_PLAYING)
358             continue;
359         pr_flash(p, "Server shutting down...\n");
360         p->state = PS_SHUTDOWN;
361         p->aborted++;
362         if (p->command) {
363             pr_flash(p, "Shutdown aborting command\n");
364         }
365         empth_wakeup(p->proc);
366     }
367
368     if (!sig) {
369         /* Sleep and let some cleanup happen - note this doesn't work
370            when called from a signal handler, since we may or may not
371            be in the right thread.  So we just pass by and kill 'em
372            all. */
373         time(&now);
374         empth_sleep(now + 1);
375     }
376
377     for (p = player_next(0); p != 0; p = player_next(p)) {
378         p->state = PS_KILL;
379         p->aborted++;
380         empth_terminate(p->proc);
381         p = player_delete(p);
382     }
383     if (sig)
384         logerror("Server shutting down on signal %d", sig);
385     else
386         logerror("Server shutting down at Deity's request");
387     close_files();
388     _exit(0);
389 }
390
391
392 void
393 nullify_objects(void)
394 {
395     int i, j;
396
397     if (opt_BIG_CITY) {
398         dchr[SCT_CAPIT].d_flg = bigcity_dchr.d_flg;
399         dchr[SCT_CAPIT].d_pkg = bigcity_dchr.d_pkg;
400         dchr[SCT_CAPIT].d_build = bigcity_dchr.d_build;
401         dchr[SCT_CAPIT].d_lcms = bigcity_dchr.d_lcms;
402         dchr[SCT_CAPIT].d_hcms = bigcity_dchr.d_hcms;
403         dchr[SCT_CAPIT].d_name = bigcity_dchr.d_name;
404     }
405     for (i = 0; i < pln_maxno; i++) {
406         if (opt_NO_HCMS)
407             plchr[i].pl_hcm = 0;
408         if (opt_NO_LCMS)
409             plchr[i].pl_lcm = 0;
410         if (opt_NO_OIL)
411             plchr[i].pl_fuel = 0;
412         if (opt_PLANENAMES)
413             plchr[i].pl_name = plchr[i].pl_planename;
414     }
415     for (i = 0; i < lnd_maxno; i++) {
416         if (opt_NO_HCMS)
417             lchr[i].l_hcm = 0;
418         if (opt_NO_LCMS)
419             lchr[i].l_lcm = 0;
420         /* Fix up the military values */
421         for (j = 0; j < lchr[i].l_nv; j++) {
422             if (lchr[i].l_vtype[j] == V_MILIT)
423                 lchr[i].l_mil = lchr[i].l_vamt[j];
424         }
425     }
426     for (i = 0; i < shp_maxno; i++) {
427         if (opt_NO_HCMS)
428             mchr[i].m_hcm = 0;
429         if (opt_NO_LCMS)
430             mchr[i].m_lcm = 0;
431         if (opt_NO_OIL) {
432             if (mchr[i].m_flags & M_OIL)
433                 mchr[i].m_name = 0;
434         }
435     }
436     for (i = 0; i < nuk_maxno; i++) {
437         if (opt_NO_HCMS)
438             nchr[i].n_hcm = 0;
439         if (opt_NO_LCMS)
440             nchr[i].n_lcm = 0;
441     }
442     for (i = 0; i < sct_maxno; i++) {
443         if (opt_NO_HCMS)
444             dchr[i].d_hcms = 0;
445         if (opt_NO_LCMS)
446             dchr[i].d_lcms = 0;
447     }
448     for (i = 0; i < prd_maxno; i++) {
449         for (j = 0; j < pchr[i].p_nv; j++) {
450             if (opt_NO_HCMS && pchr[i].p_vtype[j] == V_HCM)
451                 pchr[i].p_vamt[j] = 0;
452             if (opt_NO_LCMS && pchr[i].p_vtype[j] == V_LCM)
453                 pchr[i].p_vamt[j] = 0;
454             if (opt_NO_OIL && pchr[i].p_vtype[j] == V_OIL)
455                 pchr[i].p_vamt[j] = 0;
456         }
457     }
458 }
459
460 #if defined(_WIN32)
461 static void
462 loc_NTInit()
463 {
464     int rc;
465     WORD wVersionRequested;
466     WSADATA wsaData;
467
468     wVersionRequested = MAKEWORD(2, 0);
469     rc = WSAStartup(wVersionRequested, &wsaData);
470     if (rc != 0) {
471         logerror("WSAStartup failed.  %d", rc);
472         _exit(1);
473     }
474 }
475 #endif
476
477 #if defined(_WIN32)
478 static void
479 loc_NTTerm()
480 {
481     WSACleanup();
482 }
483 #endif