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