]> git.pond.sub.org Git - empserver/blob - src/server/shutdown.c
Update copyright notice
[empserver] / src / server / shutdown.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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-2013
32  */
33
34 #include <config.h>
35
36 #include <time.h>
37 #include "empthread.h"
38 #include "prototypes.h"
39 #include "server.h"
40
41 int shutdown_pending;
42 static empth_t *shutdown_thread;
43
44 static void shutdown_sequence(void *unused);
45
46 /*
47  * Initiate shutdown in @mins_from_now minutes.
48  * If @mins_from_now is negative, cancel any pending shutdown instead.
49  * Return -1 on error, zero when no shutdown was pending, positive
50  * number when a pending shutdown was modified.
51  */
52 int
53 shutdown_initiate(int mins_from_now)
54 {
55     int old_pending = shutdown_pending;
56
57     if (mins_from_now < 0) {
58         if (shutdown_pending) {
59             shutdown_pending = 0;
60             pr_wall("The server shutdown has been cancelled!\n");
61         }
62         return old_pending;
63     }
64
65     shutdown_pending = mins_from_now + 1;
66
67     if (shutdown_thread) {
68         if (old_pending)
69             pr_wall("The shutdown time has been changed to %d minutes!\n",
70                     mins_from_now);
71         empth_wakeup(shutdown_thread);
72     } else {
73         shutdown_thread = empth_create(shutdown_sequence, 65536, 0,
74                                        "shutdownSeq", NULL);
75         if (!shutdown_thread) {
76             shutdown_pending = 0;
77             return -1;
78         }
79     }
80
81     return old_pending;
82 }
83
84 static void
85 shutdown_sequence(void *unused)
86 {
87     time_t now;
88
89     pr_wall("The server will shut down in %d minutes!\n",
90             shutdown_pending - 1);
91
92     while (shutdown_pending > 0) {
93         --shutdown_pending;
94         time(&now);
95         if (shutdown_pending <= 1440) { /* one day */
96             if (shutdown_pending == 0) {
97                 shutdwn(0);
98             } else if (shutdown_pending == 1) {
99                 pr_wall("Server shutting down in 1 minute!\n");
100             } else if (shutdown_pending <= 5) {
101                 pr_wall("Server shutting down in %d minutes!\n",
102                         shutdown_pending);
103             } else if (shutdown_pending <= 60
104                        && shutdown_pending % 10 == 0) {
105                 pr_wall("The server will be shutting down in %d minutes!\n",
106                         shutdown_pending);
107             } else if (shutdown_pending % 60 == 0) {
108                 pr_wall("The server will be shutting down %d hours from now.\n",
109                         shutdown_pending / 60);
110             }
111         }
112         /* FIXME error due to late wakeup accumulates */
113         empth_sleep(now + 60);
114     }
115
116     shutdown_thread = NULL;
117 }