]> 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-2014, 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 "nat.h"
39 #include "prototypes.h"
40 #include "server.h"
41
42 int shutdown_pending;
43 static empth_t *shutdown_thread;
44
45 static void shutdown_sequence(void *unused);
46
47 /*
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.
52  */
53 int
54 shutdown_initiate(int mins_from_now)
55 {
56     int old_pending = shutdown_pending;
57
58     if (mins_from_now < 0) {
59         if (shutdown_pending) {
60             shutdown_pending = 0;
61             pr_wall("The server shutdown has been cancelled!\n");
62         }
63         return old_pending;
64     }
65
66     shutdown_pending = mins_from_now + 1;
67
68     if (shutdown_thread) {
69         if (old_pending)
70             pr_wall("The shutdown time has been changed to %d minutes!\n",
71                     mins_from_now);
72         empth_wakeup(shutdown_thread);
73     } else {
74         shutdown_thread = empth_create(shutdown_sequence, 65536, 0,
75                                        "shutdownSeq", NULL);
76         if (!shutdown_thread) {
77             shutdown_pending = 0;
78             return -1;
79         }
80     }
81
82     return old_pending;
83 }
84
85 static void
86 shutdown_sequence(void *unused)
87 {
88     time_t now;
89
90     pr_wall("The server will shut down in %d minutes!\n",
91             shutdown_pending - 1);
92
93     while (shutdown_pending > 0) {
94         --shutdown_pending;
95         time(&now);
96         if (shutdown_pending <= 1440) { /* one day */
97             if (shutdown_pending == 0) {
98                 shutdwn(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",
103                         shutdown_pending);
104             } else if (shutdown_pending <= 60
105                        && shutdown_pending % 10 == 0) {
106                 pr_wall("The server will be shutting down in %d minutes!\n",
107                         shutdown_pending);
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);
111             }
112         }
113         /* FIXME error due to late wakeup accumulates */
114         empth_sleep(now + 60);
115     }
116
117     shutdown_thread = NULL;
118 }