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