handle_signal.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:6k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #include "common/setup_before.h"
  19. #include "setup.h"
  20. #ifdef STDC_HEADERS
  21. # include <stdlib.h>
  22. #else
  23. # ifdef HAVE_MALLOC_H
  24. #  include <malloc.h>
  25. # endif
  26. #endif
  27. #include <errno.h>
  28. #ifdef HAVE_STRING_H
  29. # include <string.h>
  30. #else
  31. # ifdef HAVE_STRINGS_H
  32. #  include <strings.h>
  33. # endif
  34. # ifdef HAVE_MEMORY_H
  35. #  include <memory.h>
  36. # endif
  37. #endif
  38. #include "compat/strdup.h"
  39. #ifdef TIME_WITH_SYS_TIME
  40. # include <time.h>
  41. # include <sys/time.h>
  42. #else
  43. # ifdef HAVE_SYS_TIME_H
  44. #  include <sys/time.h>
  45. # else
  46. #  include <time.h>
  47. # endif
  48. #endif
  49. #ifdef DO_POSIXSIG
  50. # include <signal.h>
  51. # include "compat/signal.h"
  52. #endif
  53. #ifdef HAVE_FCNTL_H
  54. # include <fcntl.h>
  55. #else
  56. # ifdef HAVE_SYS_FILE_H
  57. #  include <sys/file.h>
  58. # endif
  59. #endif
  60. #ifdef HAVE_UNISTD_H
  61. # include <unistd.h>
  62. #endif
  63. #include "d2gs.h"
  64. #include "game.h"
  65. #include "prefs.h"
  66. #include "d2ladder.h"
  67. #include "cmdline_parse.h"
  68. #include "handle_signal.h"
  69. #include "d2gstrans.h"
  70. #include "common/eventlog.h"
  71. #include "common/setup_after.h"
  72. static void on_signal(int s);
  73. static volatile struct
  74. {
  75. unsigned char do_quit;
  76. unsigned char cancel_quit;
  77. unsigned char reload_config;
  78. unsigned char reload_ladder;
  79. unsigned char restart_d2gs;
  80. unsigned int exit_time;
  81. } signal_data ={ 0, 0, 0, 0, 0, 0 };
  82. extern int handle_signal(void)
  83. {
  84. time_t now;
  85.     char const * levels;
  86.     char *       temp;
  87.     char const * tok;
  88. if (signal_data.cancel_quit) {
  89. signal_data.cancel_quit=0;
  90. if (!signal_data.exit_time) {
  91. eventlog(eventlog_level_info,__FUNCTION__,"there is no previous shutdown to be canceled");
  92. } else {
  93. signal_data.exit_time=0;
  94. eventlog(eventlog_level_info,__FUNCTION__,"shutdown was canceled due to signal");
  95. }
  96. }
  97. if (signal_data.do_quit) {
  98. signal_data.do_quit=0;
  99. now=time(NULL);
  100. if (!signal_data.exit_time) {
  101. signal_data.exit_time=now+d2cs_prefs_get_shutdown_delay();
  102. } else {
  103. signal_data.exit_time-=d2cs_prefs_get_shutdown_decr();
  104. }
  105. if (now >= (signed)signal_data.exit_time) {
  106. eventlog(eventlog_level_info,__FUNCTION__,"shutdown server due to signal");
  107. return -1;
  108. }
  109. eventlog(eventlog_level_info,__FUNCTION__,"the server is going to shutdown in %lu minutes",(signal_data.exit_time-now)/60);
  110. }
  111. if (signal_data.reload_config) {
  112. signal_data.reload_config=0;
  113. eventlog(eventlog_level_info,__FUNCTION__,"reloading configuartion file due to signal");
  114. if (prefs_reload(cmdline_get_prefs_file())<0) {
  115. eventlog(eventlog_level_error,__FUNCTION__,"error reload configuration file,exitting");
  116. return -1;
  117. }
  118. if (d2gslist_reload(prefs_get_d2gs_list())<0) {
  119. eventlog(eventlog_level_error,__FUNCTION__,"error reloading game server list,exitting");
  120. return -1;
  121. }
  122. if (d2gstrans_reload(prefs_get_d2gstrans_file())<0) {
  123. eventlog(eventlog_level_error,__FUNCTION__,"error reloading d2gstrans list,exitting");
  124. return -1;
  125. }
  126.         eventlog_clear_level();
  127.         if ((levels = d2cs_prefs_get_loglevels()))
  128.         {
  129.             if (!(temp = strdup(levels)))
  130.             {
  131.                eventlog(eventlog_level_fatal,__FUNCTION__,"could not allocate memory for temp (exiting)");
  132.                return -1;
  133.             }
  134.             tok = strtok(temp,","); /* strtok modifies the string it is passed */
  135.             while (tok)
  136.             {
  137.               if (eventlog_add_level(tok)<0)
  138.               eventlog(eventlog_level_error,__FUNCTION__,"could not add log level "%s"",tok);
  139.               tok = strtok(NULL,",");
  140.             }
  141.             free(temp);
  142.         }
  143. if (!cmdline_get_debugmode()) eventlog_open(d2cs_prefs_get_logfile());
  144. }
  145. if (signal_data.reload_ladder) {
  146. signal_data.reload_ladder=0;
  147. eventlog(eventlog_level_info,__FUNCTION__,"reloading ladder data due to signal");
  148. d2ladder_refresh();
  149. }
  150. if (signal_data.restart_d2gs) {
  151. signal_data.restart_d2gs=0;
  152. eventlog(eventlog_level_info,__FUNCTION__,"restarting all game servers due to signal");
  153. d2gs_restart_all_gs();
  154. }
  155. return 0;
  156. }
  157. #ifdef WIN32
  158. extern void signal_quit_wrapper(void)
  159. {
  160.   signal_data.do_quit=1;
  161. }
  162. #else
  163. extern int handle_signal_init(void)
  164. {
  165. signal(SIGINT,on_signal);
  166. signal(SIGTERM,on_signal);
  167. signal(SIGABRT,on_signal);
  168. signal(SIGHUP,on_signal);
  169. signal(SIGUSR1,on_signal);
  170. signal(SIGUSR2,on_signal);
  171. signal(SIGPIPE,on_signal);
  172. return 0;
  173. }
  174. static void on_signal(int s)
  175. {
  176. switch (s) {
  177. case SIGINT:
  178. eventlog(eventlog_level_debug,__FUNCTION__,"sigint received");
  179. signal_data.do_quit=1;
  180. break;
  181. case SIGTERM:
  182. eventlog(eventlog_level_debug,__FUNCTION__,"sigint received");
  183. signal_data.do_quit=1;
  184. break;
  185. case SIGABRT:
  186. eventlog(eventlog_level_debug,__FUNCTION__,"sigabrt received");
  187. signal_data.cancel_quit=1;
  188. break;
  189. case SIGHUP:
  190. eventlog(eventlog_level_debug,__FUNCTION__,"sighup received");
  191. signal_data.reload_config=1;
  192. break;
  193. case SIGUSR1:
  194. eventlog(eventlog_level_debug,__FUNCTION__,"sigusr1 received");
  195. signal_data.reload_ladder=1;
  196. break;
  197. case SIGUSR2:
  198. eventlog(eventlog_level_debug,__FUNCTION__,"sigusr2 received");
  199. signal_data.restart_d2gs=1;
  200. break;
  201. case SIGPIPE:
  202. eventlog(eventlog_level_debug,__FUNCTION__,"sigpipe received");
  203. break;
  204. }
  205. signal(s,on_signal);
  206. }
  207. #endif