]> 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-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  shutdown.c: Shuts down server.  Runs at low priority.
29  *
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Markus Armbruster, 2007-2008
33  */
34
35 #include <config.h>
36
37 #include <time.h>
38 #include "empthread.h"
39 #include "file.h"
40 #include "nat.h"
41 #include "prototypes.h"
42 #include "server.h"
43
44 int shutdown_pending;
45 static empth_t *shutdown_thread;
46
47 static void shutdown_sequence(void *unused);
48
49 /*
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.
54  */
55 int
56 shutdown_initiate(int mins_from_now)
57 {
58     int old_pending = shutdown_pending;
59
60     if (mins_from_now < 0) {
61         if (shutdown_pending) {
62             shutdown_pending = 0;
63             pr_wall("The server shutdown has been cancelled!\n");
64         }
65         return old_pending;
66     }
67
68     shutdown_pending = mins_from_now + 1;
69
70     if (shutdown_thread) {
71         if (old_pending)
72             pr_wall("The shutdown time has been changed to %d minutes!\n",
73                     mins_from_now);
74         empth_wakeup(shutdown_thread);
75     } else {
76         shutdown_thread = empth_create(shutdown_sequence, 50 * 1024, 0,
77                                        "shutdownSeq", NULL);
78         if (!shutdown_thread) {
79             shutdown_pending = 0;
80             return -1;
81         }
82     }
83
84     return old_pending;
85 }
86
87 static void
88 shutdown_sequence(void *unused)
89 {
90     time_t now;
91
92     pr_wall("The server will shut down in %d minutes!\n",
93             shutdown_pending - 1);
94
95     while (shutdown_pending > 0) {
96         --shutdown_pending;
97         time(&now);
98         if (shutdown_pending <= 1440) { /* one day */
99             if (shutdown_pending == 0) {
100                 shutdwn(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",
105                         shutdown_pending);
106             } else if (shutdown_pending <= 60
107                        && shutdown_pending % 10 == 0) {
108                 pr_wall("The server will be shutting down in %d minutes!\n",
109                         shutdown_pending);
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);
113             }
114         }
115         /* FIXME error due to late wakeup accumulates */
116         empth_sleep(now + 60);
117     }
118
119     shutdown_thread = NULL;
120 }