]> git.pond.sub.org Git - empserver/blob - src/server/shutdown.c
Detect and log errors in crash_dump()
[empserver] / src / server / shutdown.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  *  shutdown.c: Shuts down server.  Runs at low priority.
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Markus Armbruster, 2007
33  */
34
35 #include <config.h>
36
37 #include <time.h>
38 #include "empthread.h"
39 #include "file.h"
40 #include "nat.h"
41 #include "prototypes.h"
42 #include "server.h"
43
44 int shutdown_pending;
45
46 static void shutdown_sequence(void *unused);
47
48 int
49 shutdown_initiate(int mins_from_now)
50 {
51     int old_pending = shutdown_pending;
52
53     if (mins_from_now < 0) {
54         if (shutdown_pending) {
55             shutdown_pending = 0;
56             pr_wall("The server shutdown has been cancelled!\n");
57         }
58         return old_pending;
59     }
60
61     shutdown_pending = mins_from_now + 1;
62
63     if (old_pending) {
64         pr_wall("The shutdown time has been changed to %d minutes!\n",
65                 mins_from_now);
66         /* FIXME wake up shutdown_sequence() */
67     } else {
68         if (!empth_create(shutdown_sequence, 50 * 1024, 0,
69                           "shutdownSeq", NULL))
70             return -1;
71     }
72
73     return old_pending;
74 }
75
76 static void
77 shutdown_sequence(void *unused)
78 {
79     time_t now;
80
81     pr_wall("The server will shut down in %d minutes!\n",
82             shutdown_pending - 1);
83
84     while (shutdown_pending > 0) {
85         --shutdown_pending;
86         time(&now);
87         if (shutdown_pending <= 1440) { /* one day */
88             if (shutdown_pending == 0) {
89                 shutdwn(0);
90             } else if (shutdown_pending == 1) {
91                 pr_wall("Server shutting down in 1 minute!\n");
92             } else if (shutdown_pending <= 5) {
93                 pr_wall("Server shutting down in %d minutes!\n",
94                         shutdown_pending);
95             } else if (shutdown_pending <= 60
96                        && shutdown_pending % 10 == 0) {
97                 pr_wall("The server will be shutting down in %d minutes!\n",
98                         shutdown_pending);
99             } else if (shutdown_pending % 60 == 0) {
100                 pr_wall("The server will be shutting down %d hours from now.\n",
101                         shutdown_pending / 60);
102             }
103         }
104         /* FIXME error due to late wakeup accumulates */
105         empth_sleep(now + 60);
106     }
107 }