2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2014, 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"
39 #include "prototypes.h"
43 static empth_t *shutdown_thread;
45 static void shutdown_sequence(void *unused);
48 * Initiate shutdown in MINS_FROM_NOW minutes.
49 * If MINS_FROM_NOW is negative, cancel any pending shutdown instead.
50 * Return -1 on error, zero when no shutdown was pending, positive
51 * number when a pending shutdown was modified.
54 shutdown_initiate(int mins_from_now)
56 int old_pending = shutdown_pending;
58 if (mins_from_now < 0) {
59 if (shutdown_pending) {
61 pr_wall("The server shutdown has been cancelled!\n");
66 shutdown_pending = mins_from_now + 1;
68 if (shutdown_thread) {
70 pr_wall("The shutdown time has been changed to %d minutes!\n",
72 empth_wakeup(shutdown_thread);
74 shutdown_thread = empth_create(shutdown_sequence, 65536, 0,
76 if (!shutdown_thread) {
86 shutdown_sequence(void *unused)
90 pr_wall("The server will shut down in %d minutes!\n",
91 shutdown_pending - 1);
93 while (shutdown_pending > 0) {
96 if (shutdown_pending <= 1440) { /* one day */
97 if (shutdown_pending == 0) {
99 } else if (shutdown_pending == 1) {
100 pr_wall("Server shutting down in 1 minute!\n");
101 } else if (shutdown_pending <= 5) {
102 pr_wall("Server shutting down in %d minutes!\n",
104 } else if (shutdown_pending <= 60
105 && shutdown_pending % 10 == 0) {
106 pr_wall("The server will be shutting down in %d minutes!\n",
108 } else if (shutdown_pending % 60 == 0) {
109 pr_wall("The server will be shutting down %d hours from now.\n",
110 shutdown_pending / 60);
113 /* FIXME error due to late wakeup accumulates */
114 empth_sleep(now + 60);
117 shutdown_thread = NULL;