2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * shutdown.c: Shuts down server. Runs at low priority.
30 * Known contributors to this file:
32 * Markus Armbruster, 2007-2008
38 #include "empthread.h"
41 #include "prototypes.h"
45 static empth_t *shutdown_thread;
47 static void shutdown_sequence(void *unused);
50 * Initiate shutdown in MINS_FROM_NOW minutes.
51 * If MINS_FROM_NOW is negative, cancel any pending shutdown instead.
52 * Return -1 on error, zero when no shutdown was pending, positive
53 * number when a pending shutdown was modified.
56 shutdown_initiate(int mins_from_now)
58 int old_pending = shutdown_pending;
60 if (mins_from_now < 0) {
61 if (shutdown_pending) {
63 pr_wall("The server shutdown has been cancelled!\n");
68 shutdown_pending = mins_from_now + 1;
70 if (shutdown_thread) {
72 pr_wall("The shutdown time has been changed to %d minutes!\n",
74 empth_wakeup(shutdown_thread);
76 shutdown_thread = empth_create(shutdown_sequence, 50 * 1024, 0,
78 if (!shutdown_thread) {
88 shutdown_sequence(void *unused)
92 pr_wall("The server will shut down in %d minutes!\n",
93 shutdown_pending - 1);
95 while (shutdown_pending > 0) {
98 if (shutdown_pending <= 1440) { /* one day */
99 if (shutdown_pending == 0) {
101 } else if (shutdown_pending == 1) {
102 pr_wall("Server shutting down in 1 minute!\n");
103 } else if (shutdown_pending <= 5) {
104 pr_wall("Server shutting down in %d minutes!\n",
106 } else if (shutdown_pending <= 60
107 && shutdown_pending % 10 == 0) {
108 pr_wall("The server will be shutting down in %d minutes!\n",
110 } else if (shutdown_pending % 60 == 0) {
111 pr_wall("The server will be shutting down %d hours from now.\n",
112 shutdown_pending / 60);
115 /* FIXME error due to late wakeup accumulates */
116 empth_sleep(now + 60);
119 shutdown_thread = NULL;