00001 /* 00002 * Empire - A multi-player, client/server Internet based war game. 00003 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak, 00004 * Ken Stevens, Steve McClure 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 * --- 00021 * 00022 * See files README, COPYING and CREDITS in the root of the source 00023 * tree for related information and legal notices. It is expected 00024 * that future projects/authors will amend these files as needed. 00025 * 00026 * --- 00027 * 00028 * shutdown.c: Shuts down server. Runs at low priority. 00029 * 00030 * Known contributors to this file: 00031 * Ken Stevens, 1995 00032 * Markus Armbruster, 2007 00033 */ 00034 00035 #include <config.h> 00036 00037 #include <time.h> 00038 #include "empthread.h" 00039 #include "file.h" 00040 #include "nat.h" 00041 #include "prototypes.h" 00042 #include "server.h" 00043 00044 int shutdown_pending; 00045 00046 static void shutdown_sequence(void *unused); 00047 00048 int 00049 shutdown_initiate(int mins_from_now) 00050 { 00051 int old_pending = shutdown_pending; 00052 00053 if (mins_from_now < 0) { 00054 if (shutdown_pending) { 00055 shutdown_pending = 0; 00056 pr_wall("The server shutdown has been cancelled!\n"); 00057 } 00058 return old_pending; 00059 } 00060 00061 shutdown_pending = mins_from_now + 1; 00062 00063 if (old_pending) { 00064 pr_wall("The shutdown time has been changed to %d minutes!\n", 00065 mins_from_now); 00066 /* FIXME wake up shutdown_sequence() */ 00067 } else { 00068 if (!empth_create(shutdown_sequence, 50 * 1024, 0, 00069 "shutdownSeq", NULL)) 00070 return -1; 00071 } 00072 00073 return old_pending; 00074 } 00075 00076 static void 00077 shutdown_sequence(void *unused) 00078 { 00079 time_t now; 00080 00081 pr_wall("The server will shut down in %d minutes!\n", 00082 shutdown_pending - 1); 00083 00084 while (shutdown_pending > 0) { 00085 --shutdown_pending; 00086 time(&now); 00087 if (shutdown_pending <= 1440) { /* one day */ 00088 if (shutdown_pending == 0) { 00089 shutdwn(0); 00090 } else if (shutdown_pending == 1) { 00091 pr_wall("Server shutting down in 1 minute!\n"); 00092 } else if (shutdown_pending <= 5) { 00093 pr_wall("Server shutting down in %d minutes!\n", 00094 shutdown_pending); 00095 } else if (shutdown_pending <= 60 00096 && shutdown_pending % 10 == 0) { 00097 pr_wall("The server will be shutting down in %d minutes!\n", 00098 shutdown_pending); 00099 } else if (shutdown_pending % 60 == 0) { 00100 pr_wall("The server will be shutting down %d hours from now.\n", 00101 shutdown_pending / 60); 00102 } 00103 } 00104 /* FIXME error due to late wakeup accumulates */ 00105 empth_sleep(now + 60); 00106 } 00107 }
1.5.2