Debian Codegraph @

src/d2dbs/handle_signal.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000,2001 Onlyer ([email protected])
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 #include "handle_signal.h"
21
22 #include <cstring>
23 #include <ctime>
24
25 #include <csignal>
26
27 #include "common/eventlog.h"
28 #include "common/xalloc.h"
29 #include "prefs.h"
30 #include "d2ladder.h"
31 #include "cmdline.h"
32 #include "common/setup_after.h"
33
34 namespace pvpgn
35 {
36
37 namespace d2dbs
38 {
39
40 static void on_signal(int s);
41
42 static volatile struct
43 {
44 unsigned char do_quit;
45 unsigned char cancel_quit;
46 unsigned char reload_config;
47 unsigned char save_ladder;
48 time_t exit_time;
49 } signal_data = { 0, 0, 0, 0, 0 };
50
51 extern int d2dbs_handle_signal(void)
52 {
53 std::time_t now;
54 char const * levels;
55 char * temp;
56 char const * tok;
57
58
59 if (signal_data.cancel_quit) {
60 signal_data.cancel_quit = 0;
61 if (!signal_data.exit_time) {
62 eventlog(eventlog_level_info, __FUNCTION__, "there is no previous shutdown to be canceled");
63 }
64 else {
65 signal_data.exit_time = 0;
66 eventlog(eventlog_level_info, __FUNCTION__, "shutdown was canceled due to std::signal");
67 }
68 }
69 if (signal_data.do_quit) {
70 signal_data.do_quit = 0;
71 now = std::time(NULL);
72 if (!signal_data.exit_time) {
73 signal_data.exit_time = now + d2dbs_prefs_get_shutdown_delay();
74 }
75 else {
76 signal_data.exit_time -= d2dbs_prefs_get_shutdown_decr();
77 }
78 eventlog(eventlog_level_info, __FUNCTION__, "the server is going to shutdown in {} minutes", (signal_data.exit_time - now) / 60);
79 }
80 if (signal_data.exit_time) {
81 now = std::time(NULL);
82 if (now >= (signed)signal_data.exit_time) {
83 signal_data.exit_time = 0;
84 eventlog(eventlog_level_info, __FUNCTION__, "shutdown server due to std::signal");
85 return -1;
86 }
87 }
88 if (signal_data.reload_config) {
89 signal_data.reload_config = 0;
90 eventlog(eventlog_level_info, __FUNCTION__, "reloading configuartion file due to std::signal");
91 if (d2dbs_prefs_reload(cmdline_get_preffile()) < 0) {
92 eventlog(eventlog_level_error, __FUNCTION__, "error reload configuration file,exitting");
93 return -1;
94 }
95 eventlog_clear_level();
96 if ((levels = d2dbs_prefs_get_loglevels()))
97 {
98 temp = xstrdup(levels);
99 tok = std::strtok(temp, ","); /* std::strtok modifies the string it is passed */
100
101 while (tok)
102 {
103 if (eventlog_add_level(tok) < 0)
104 eventlog(eventlog_level_error, __FUNCTION__, "could not add std::log level \"{}\"", tok);
105 tok = std::strtok(NULL, ",");
106 }
107 xfree(temp);
108 }
109 #ifdef DO_DAEMONIZE
110 if (!cmdline_get_foreground())
111 #endif
112 eventlog_open(d2dbs_prefs_get_logfile());
113 }
114 if (signal_data.save_ladder) {
115 signal_data.save_ladder = 0;
116 eventlog(eventlog_level_info, __FUNCTION__, "save ladder data due to std::signal");
117 d2ladder_saveladder();
118 }
119 return 0;
120 }
121
122 #ifdef WIN32
123 extern void d2dbs_signal_quit_wrapper(void)
124 {
125 signal_data.do_quit = 1;
126 }
127
128 extern void d2dbs_signal_reload_config_wrapper(void)
129 {
130 signal_data.reload_config = 1;
131 }
132
133 extern void d2dbs_signal_save_ladder_wrapper(void)
134 {
135 signal_data.save_ladder = 1;
136 }
137
138 extern void d2dbs_signal_exit_wrapper(void)
139 {
140 signal_data.exit_time = 1;
141 }
142 #else
143 extern int d2dbs_handle_signal_init(void)
144 {
145 std::signal(SIGINT, on_signal);
146 std::signal(SIGTERM, on_signal);
147 std::signal(SIGABRT, on_signal);
148 std::signal(SIGHUP, on_signal);
149 std::signal(SIGUSR1, on_signal);
150 std::signal(SIGPIPE, on_signal);
151 return 0;
152 }
153
154 static void on_signal(int s)
155 {
156 switch (s) {
157 case SIGINT:
158 eventlog(eventlog_level_debug, __FUNCTION__, "sigint received");
159 signal_data.do_quit = 1;
160 break;
161 case SIGTERM:
162 eventlog(eventlog_level_debug, __FUNCTION__, "sigint received");
163 signal_data.do_quit = 1;
164 break;
165 case SIGABRT:
166 eventlog(eventlog_level_debug, __FUNCTION__, "sigabrt received");
167 signal_data.cancel_quit = 1;
168 break;
169 case SIGHUP:
170 eventlog(eventlog_level_debug, __FUNCTION__, "sighup received");
171 signal_data.reload_config = 1;
172 break;
173 case SIGUSR1:
174 eventlog(eventlog_level_debug, __FUNCTION__, "sigusr1 received");
175 signal_data.save_ladder = 1;
176 break;
177 case SIGPIPE:
178 eventlog(eventlog_level_debug, __FUNCTION__, "sigpipe received");
179 break;
180 }
181 std::signal(s, on_signal);
182 }
183 #endif
184
185 }
186
187 }