Debian Codegraph @

src/d2cs/main.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"cxx . . $ `<file>/src/common/setup_before.h`/
19 #include "setup.h"cxx . . $ `<file>/src/d2cs/setup.h`/
20
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstring>
24
25 #ifdef HAVE_SYS_TYPES_Hcxx . . $ `build/config.h:16:9`!
26 # include <sys/types.h>
27 #endif
28 #ifdef HAVE_UNISTD_Hcxx . . $ `build/config.h:7:9`!
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_Hcxx . . $ `build/config.h:21:9`!
32 # include <sys/stat.h>
33 #endif
34 #ifdef WIN32
35 # include "win32/service.h"
36 #endif
37 #ifdef WIN32_GUI
38 # include "win32/winmain.h"
39 #endif
40 #ifdef WIN32
41 # include "win32/windump.h"
42 #endif
43
44 #include "compat/stdfileno.h"cxx . . $ `<file>/src/compat/stdfileno.h`/
45 #include "compat/pgetpid.h"cxx . . $ `<file>/src/compat/pgetpid.h`/
46 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
47 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
48 #include "common/trans.h"cxx . . $ `<file>/src/common/trans.h`/
49 #include "common/fdwatch.h"cxx . . $ `<file>/src/common/fdwatch.h`/
50 #include "prefs.h"cxx . . $ `<file>/src/d2cs/prefs.h`/
51 #include "connection.h"cxx . . $ `<file>/src/d2cs/connection.h`/
52 #include "d2gs.h"cxx . . $ `<file>/src/d2cs/d2gs.h`/
53 #include "serverqueue.h"cxx . . $ `<file>/src/d2cs/serverqueue.h`/
54 #include "d2ladder.h"cxx . . $ `<file>/src/d2cs/d2ladder.h`/
55 #include "cmdline.h"cxx . . $ `<file>/src/d2cs/cmdline.h`/
56 #include "game.h"cxx . . $ `<file>/src/d2cs/game.h`/
57 #include "server.h"cxx . . $ `<file>/src/d2cs/server.h`/
58 #include "version.h"cxx . . $ `<file>/src/d2cs/version.h`/
59 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
60
61 using namespace pvpgncxx . . $ pvpgn/::d2cs;
62 using namespace pvpgn;
63
64 #ifdef WIN32
65 char serviceLongName[] = "d2cs service";
66 char serviceName[] = "d2cs";
67 char serviceDescription[] = "Diablo 2 Character Server";
68
69 int g_ServiceStatus = -1;
70 #endif
71
72 static int initcxx . . $ init(b126dc7c1de90089).(void);
73 static int cleanupcxx . . $ cleanup(b126dc7c1de90089).(void);
74 static int config_initcxx . . $ config_init(afcd3168fc1188a4).(int argc, char * * argv);
75 static int config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).(void);
76 static int setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).(void);
77 static char * write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).(void);
78
79
80 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
81 static int setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).(void)
82 {
83 int pid;
84
85 if (chdir("/")<0) {
86 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"can not change working directory to root directory (chdir: {})",stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
87 return -1;
88 }
89 close(STDIN_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:210:9`!);
90 close(STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!);
91 if (!cmdline_get_foregroundcxx . . $ pvpgn/d2cs/cmdline_get_foreground(b126dc7c1de90089).()) {
92 close(STDERR_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:212:9`!);
93 }
94 switch ((pid = fork())) {
95 case 0:
96 break;
97 case -1:
98 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error create child process (fork: {})",stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
99 return -1;
100 default:
101 return pid;
102 }
103 umask(0);
104 setsid();
105 return 0;
106 }
107 #endif
108
109
110 static char * write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).(void)
111 {
112 char *pidfile = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(prefs_get_pidfile());
113
114 if (pidfile)
115 {
116 if (pidfile[0] == '\0') {
117 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
118 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
119 }
120 #ifdef HAVE_GETPIDcxx . . $ `build/config.h:49:9`!
121 stdcxx . . $ std/::FILEcxx . . $ std/FILE# * fp;
122
123 if (!(fp = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(pidfile,"w"))) {
124 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"unable to open pid file \"{}\" for writing (std::fopen: {})",pidfile,stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
125 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
126 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
127 } else {
128 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(fp,"%u",(unsigned int)getpid());
129 if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(fp)<0)
130 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"could not close pid file \"{}\" after writing (std::fclose: {})",pidfile,stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
131 }
132
133 #else
134 eventlog(eventlog_level_warn,__FUNCTION__,"no getpid() std::system call, disable pid file in d2cs.conf");
135 xfree((void *)pidfile); /* avoid warning */
136 return NULL;
137 #endif
138 }
139
140 return pidfile;
141 }
142
143
144 static int initcxx . . $ init(b126dc7c1de90089).(void)
145 {
146 d2cs_connlist_createcxx . . $ pvpgn/d2cs/d2cs_connlist_create(b126dc7c1de90089).();
147 d2cs_gamelist_createcxx . . $ pvpgn/d2cs/d2cs_gamelist_create(b126dc7c1de90089).();
148 sqlist_createcxx . . $ pvpgn/d2cs/sqlist_create(b126dc7c1de90089).();
149 d2gslist_createcxx . . $ pvpgn/d2cs/d2gslist_create(b126dc7c1de90089).();
150 gqlist_createcxx . . $ pvpgn/d2cs/gqlist_create(b126dc7c1de90089).();
151 d2ladder_initcxx . . $ pvpgn/d2cs/d2ladder_init(b126dc7c1de90089).();
152 if(trans_loadcxx . . $ pvpgn/trans_load(cb7559238bb26814).(d2cs_prefs_get_transfilecxx . . $ pvpgn/d2cs/d2cs_prefs_get_transfile(33a1f659e94feb42).(),TRANS_D2CScxx . . $ `src/common/trans.h:52:9`!)<0)
153 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"could not load trans list");
154 fdwatch_initcxx . . $ pvpgn/fdwatch_init(7864480464b09eea).(prefs_get_max_connectionscxx . . $ pvpgn/d2cs/prefs_get_max_connections(db52760e79bf9054).());
155 return 0;
156 }
157
158
159 static int cleanupcxx . . $ cleanup(b126dc7c1de90089).(void)
160 {
161 d2ladder_destroycxx . . $ pvpgn/d2cs/d2ladder_destroy(b126dc7c1de90089).();
162 d2cs_connlist_destroycxx . . $ pvpgn/d2cs/d2cs_connlist_destroy(b126dc7c1de90089).();
163 d2cs_gamelist_destroycxx . . $ pvpgn/d2cs/d2cs_gamelist_destroy(b126dc7c1de90089).();
164 sqlist_destroycxx . . $ pvpgn/d2cs/sqlist_destroy(b126dc7c1de90089).();
165 d2gslist_destroycxx . . $ pvpgn/d2cs/d2gslist_destroy(b126dc7c1de90089).();
166 gqlist_destroycxx . . $ pvpgn/d2cs/gqlist_destroy(b126dc7c1de90089).();
167 trans_unloadcxx . . $ pvpgn/trans_unload(b126dc7c1de90089).();
168 fdwatch_closecxx . . $ pvpgn/fdwatch_close(b126dc7c1de90089).();
169 return 0;
170 }
171
172
173 static int config_initcxx . . $ config_init(afcd3168fc1188a4).(int argc, char * * argv)
174 {
175 char const * levels;
176 char * temp;
177 char const * tok;
178
179 if (cmdline_loadcxx . . $ pvpgn/d2cs/cmdline_load(afcd3168fc1188a4).(argc, argv) != 1) {
180 return -1;
181 }
182
183 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
184 int pid;
185
186 if ((!cmdline_get_foregroundcxx . . $ pvpgn/d2cs/cmdline_get_foreground(b126dc7c1de90089).())) {
187 if (!((pid = setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).()) == 0)) {
188 return pid;
189 }
190 }
191 #endif
192
193 if (d2cs_prefs_loadcxx . . $ pvpgn/d2cs/d2cs_prefs_load(5c4306d8553b3696).(cmdline_get_preffilecxx . . $ pvpgn/d2cs/cmdline_get_preffile(33a1f659e94feb42).())<0) {
194 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error loading configuration file {}",cmdline_get_preffilecxx . . $ pvpgn/d2cs/cmdline_get_preffile(33a1f659e94feb42).());
195 return -1;
196 }
197
198 eventlog_clear_levelcxx . . $ pvpgn/eventlog_clear_level(49f6e7a06ebc5aa8).();
199 if ((levels = d2cs_prefs_get_loglevelscxx . . $ pvpgn/d2cs/d2cs_prefs_get_loglevels(33a1f659e94feb42).()))
200 {
201 temp = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(levels);
202 tok = stdcxx . . $ std/::strtok(temp,","); /* std::strtok modifies the string it is passed */
203
204 while (tok)
205 {
206 if (eventlog_add_levelcxx . . $ pvpgn/eventlog_add_level(5c4306d8553b3696).(tok)<0)
207 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"could not add std::log level \"{}\"",tok);
208 tok = stdcxx . . $ std/::strtok(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!,",");
209 }
210
211 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
212 }
213
214 #ifdef WIN32_GUI
215 if (cmdline_get_gui()){
216 eventlog_add_level(eventlog_get_levelname_str(eventlog_level_gui));
217 }
218 #endif
219
220 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
221 if (cmdline_get_foregroundcxx . . $ pvpgn/d2cs/cmdline_get_foreground(b126dc7c1de90089).()) {
222 eventlog_setcxx . . $ pvpgn/eventlog_set(8d4262d45fc644ab).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
223 }
224 else
225 #endif
226 {
227 if (cmdline_get_logfilecxx . . $ pvpgn/d2cs/cmdline_get_logfile(33a1f659e94feb42).()) {
228 if (eventlog_opencxx . . $ pvpgn/eventlog_open(5c4306d8553b3696).(cmdline_get_logfilecxx . . $ pvpgn/d2cs/cmdline_get_logfile(33a1f659e94feb42).())<0) {
229 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error open eventlog file {}",cmdline_get_logfilecxx . . $ pvpgn/d2cs/cmdline_get_logfile(33a1f659e94feb42).());
230 return -1;
231 }
232 } else {
233 if (eventlog_opencxx . . $ pvpgn/eventlog_open(5c4306d8553b3696).(d2cs_prefs_get_logfilecxx . . $ pvpgn/d2cs/d2cs_prefs_get_logfile(33a1f659e94feb42).())<0) {
234 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"error open eventlog file {}",d2cs_prefs_get_logfilecxx . . $ pvpgn/d2cs/d2cs_prefs_get_logfile(33a1f659e94feb42).());
235 return -1;
236 }
237 }
238 }
239 return 0;
240 }
241
242
243 static int config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).(void)
244 {
245 d2cs_prefs_unloadcxx . . $ pvpgn/d2cs/d2cs_prefs_unload(b126dc7c1de90089).();
246 cmdline_unloadcxx . . $ pvpgn/d2cs/cmdline_unload(49f6e7a06ebc5aa8).();
247 return 0;
248 }
249
250
251 #ifdef WIN32_GUI
252 extern int app_main(int argc, char ** argv)
253 #else
254 extern int maincxx . . $ main(afcd3168fc1188a4).(int argc, char ** argv)
255 #endif
256 {
257 int pid;
258
259 #ifdef WIN32
260 // create a dump file whenever the gateway crashes
261 SetUnhandledExceptionFilter(unhandled_handler);
262 #endif
263
264 eventlog_setcxx . . $ pvpgn/eventlog_set(8d4262d45fc644ab).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
265 if (!((pid = config_initcxx . . $ config_init(afcd3168fc1188a4).(argc, argv)) == 0)) {
266 // if (pid==1) pid=0;
267 return pid;
268 }
269 const char* const pidfile = write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).();
270 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info.,__FUNCTION__,D2CS_VERSIONcxx . . $ `src/d2cs/version.h:23:9`!);
271 if (initcxx . . $ init(b126dc7c1de90089).()<0) {
272 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"failed to init");
273 if (pidfile)
274 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void*)pidfile);
275 return -1;
276 } else {
277 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info.,__FUNCTION__,"server initialized");
278 }
279 if (d2cs_server_processcxx . . $ pvpgn/d2cs/d2cs_server_process(b126dc7c1de90089).()<0) {
280 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"failed to run server");
281 if (pidfile)
282 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void*)pidfile);
283 return -1;
284 }
285 cleanupcxx . . $ cleanup(b126dc7c1de90089).();
286 if (pidfile) {
287 if (stdcxx . . $ std/::removecxx . . $ std/remove(f5ecf3191ed0cd93).(pidfile)<0)
288 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error.,__FUNCTION__,"could not remove pid file \"{}\" (std::remove: {})",pidfile,stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
289 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
290 }
291 config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).();
292 eventlog_closecxx . . $ pvpgn/eventlog_close(b126dc7c1de90089).();
293 return 0;
294 }