]> git.pond.sub.org Git - empserver/blob - src/server/shutdown.c
Update known contributors comment.
[empserver] / src / server / shutdown.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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
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
46 static void shutdown_sequence(void *unused);
47
48 int
49 shutdown_initiate(int mins_from_now)
50 {
51     int old_pending = shutdown_pending;
52
53     if (mins_from_now < 0) {
54         if (shutdown_pending) {
55             shutdown_pending = 0;
56             pr_wall("The server shutdown has been cancelled!\n");
57         }
58         return old_pending;
59     }
60
61     shutdown_pending = mins_from_now + 1;
62
63     if (old_pending) {
64         pr_wall("The shutdown time has been changed to %d minutes!\n",
65                 mins_from_now);
66         /* FIXME wake up shutdown_sequence() */
67     } else {
68         if (!empth_create(PP_SHUTDOWN, shutdown_sequence, 50 * 1024,
69                           0, "shutdownSeq", "Counts down server shutdown",
70                           NULL))
71             return -1;
72     }
73
74     return old_pending;
75 }
76
77 static void
78 shutdown_sequence(void *unused)
79 {
80     time_t now;
81
82     pr_wall("The server will shut down in %d minutes!\n",
83             shutdown_pending - 1);
84
85     while (shutdown_pending > 0) {
86         --shutdown_pending;
87         time(&now);
88         if (shutdown_pending <= 1440) { /* one day */
89             if (shutdown_pending == 0) {
90                 shutdwn(0);
91             } else if (shutdown_pending == 1) {
92                 pr_wall("Server shutting down in 1 minute!\n");
93             } else if (shutdown_pending <= 5) {
94                 pr_wall("Server shutting down in %d minutes!\n",
95                         shutdown_pending);
96             } else if (shutdown_pending <= 60
97                        && shutdown_pending % 10 == 0) {
98                 pr_wall("The server will be shutting down in %d minutes!\n",
99                         shutdown_pending);
100             } else if (shutdown_pending % 60 == 0) {
101                 pr_wall("The server will be shutting down %d hours from now.\n",
102                         shutdown_pending / 60);
103             }
104         }
105         /* FIXME error due to late wakeup accumulates */
106         empth_sleep(now + 60);
107     }
108 }