2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * shutdown.c: Shuts down server. Runs at low priority.
29 * Known contributors to this file:
31 * Markus Armbruster, 2007-2013
37 #include "empthread.h"
38 #include "prototypes.h"
42 static empth_t *shutdown_thread;
44 static void shutdown_sequence(void *unused);
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.
53 shutdown_initiate(int mins_from_now)
55 int old_pending = shutdown_pending;
57 if (mins_from_now < 0) {
58 if (shutdown_pending) {
60 pr_wall("The server shutdown has been cancelled!\n");
65 shutdown_pending = mins_from_now + 1;
67 if (shutdown_thread) {
69 pr_wall("The shutdown time has been changed to %d minutes!\n",
71 empth_wakeup(shutdown_thread);
73 shutdown_thread = empth_create(shutdown_sequence, 65536, 0,
75 if (!shutdown_thread) {
85 shutdown_sequence(void *unused)
89 pr_wall("The server will shut down in %d minutes!\n",
90 shutdown_pending - 1);
92 while (shutdown_pending > 0) {
95 if (shutdown_pending <= 1440) { /* one day */
96 if (shutdown_pending == 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",
103 } else if (shutdown_pending <= 60
104 && shutdown_pending % 10 == 0) {
105 pr_wall("The server will be shutting down in %d minutes!\n",
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);
112 /* FIXME error due to late wakeup accumulates */
113 empth_sleep(now + 60);
116 shutdown_thread = NULL;