]> git.pond.sub.org Git - empserver/blob - src/server/shutdown.c
License upgrade to GPL version 3 or later
[empserver] / src / server / shutdown.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  shutdown.c: Shuts down server.  Runs at low priority.
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Markus Armbruster, 2007-2008
32  */
33
34 #include <config.h>
35
36 #include <time.h>
37 #include "empthread.h"
38 #include "file.h"
39 #include "nat.h"
40 #include "prototypes.h"
41 #include "server.h"
42
43 int shutdown_pending;
44 static empth_t *shutdown_thread;
45
46 static void shutdown_sequence(void *unused);
47
48 /*
49  * Initiate shutdown in MINS_FROM_NOW minutes.
50  * If MINS_FROM_NOW is negative, cancel any pending shutdown instead.
51  * Return -1 on error, zero when no shutdown was pending, positive
52  * number when a pending shutdown was modified.
53  */
54 int
55 shutdown_initiate(int mins_from_now)
56 {
57     int old_pending = shutdown_pending;
58
59     if (mins_from_now < 0) {
60         if (shutdown_pending) {
61             shutdown_pending = 0;
62             pr_wall("The server shutdown has been cancelled!\n");
63         }
64         return old_pending;
65     }
66
67     shutdown_pending = mins_from_now + 1;
68
69     if (shutdown_thread) {
70         if (old_pending)
71             pr_wall("The shutdown time has been changed to %d minutes!\n",
72                     mins_from_now);
73         empth_wakeup(shutdown_thread);
74     } else {
75         shutdown_thread = empth_create(shutdown_sequence, 50 * 1024, 0,
76                                        "shutdownSeq", NULL);
77         if (!shutdown_thread) {
78             shutdown_pending = 0;
79             return -1;
80         }
81     }
82
83     return old_pending;
84 }
85
86 static void
87 shutdown_sequence(void *unused)
88 {
89     time_t now;
90
91     pr_wall("The server will shut down in %d minutes!\n",
92             shutdown_pending - 1);
93
94     while (shutdown_pending > 0) {
95         --shutdown_pending;
96         time(&now);
97         if (shutdown_pending <= 1440) { /* one day */
98             if (shutdown_pending == 0) {
99                 shutdwn(0);
100             } else if (shutdown_pending == 1) {
101                 pr_wall("Server shutting down in 1 minute!\n");
102             } else if (shutdown_pending <= 5) {
103                 pr_wall("Server shutting down in %d minutes!\n",
104                         shutdown_pending);
105             } else if (shutdown_pending <= 60
106                        && shutdown_pending % 10 == 0) {
107                 pr_wall("The server will be shutting down in %d minutes!\n",
108                         shutdown_pending);
109             } else if (shutdown_pending % 60 == 0) {
110                 pr_wall("The server will be shutting down %d hours from now.\n",
111                         shutdown_pending / 60);
112             }
113         }
114         /* FIXME error due to late wakeup accumulates */
115         empth_sleep(now + 60);
116     }
117
118     shutdown_thread = NULL;
119 }