Debian Codegraph @

src/d2dbs/main.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000,2001 Onlyer ([email protected])
3 * Copyright (C) 2005 Olaf Freyer ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
20 #include "setup.h"cxx . . $ `<file>/src/d2dbs/setup.h`/
21
22 #include <cerrno>
23 #include <cstdio>
24 #include <cstring>
25
26 #ifdef HAVE_UNISTD_Hcxx . . $ `build/config.h:7:9`!
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_TYPES_Hcxx . . $ `build/config.h:16:9`!
30 # include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_STAT_Hcxx . . $ `build/config.h:21:9`!
33 # include <sys/stat.h>
34 #endif
35 #ifdef WIN32
36 # include "win32/service.h"
37 #endif
38 #ifdef WIN32_GUI
39 # include "win32/winmain.h"
40 #endif
41 #ifdef WIN32
42 # include "win32/windump.h"
43 #endif
44
45 #include "compat/stdfileno.h"cxx . . $ `<file>/src/compat/stdfileno.h`/
46 #include "compat/pgetpid.h"cxx . . $ `<file>/src/compat/pgetpid.h`/
47 #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/
48 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
49 #include "cmdline.h"cxx . . $ `<file>/src/d2dbs/cmdline.h`/
50 #include "prefs.h"cxx . . $ `<file>/src/d2dbs/prefs.h`/
51 #include "version.h"cxx . . $ `<file>/src/d2dbs/version.h`/
52 #include "handle_signal.h"cxx . . $ `<file>/src/d2dbs/handle_signal.h`/
53 #include "dbserver.h"cxx . . $ `<file>/src/d2dbs/dbserver.h`/
54 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
55
56 using namespace pvpgncxx . . $ pvpgn/::d2dbs;
57 using namespace pvpgn;
58
59 static stdcxx . . $ std/::FILEcxx . . $ std/FILE# * eventlog_fpcxx . . $ eventlog_fp.;
60
61 char serviceLongNamecxx . . $ serviceLongName.[] = "d2dbs service";
62 char serviceNamecxx . . $ serviceName.[] = "d2dbs";
63 char serviceDescriptioncxx . . $ serviceDescription.[] = "Diablo 2 DataBase Server";
64
65 int g_ServiceStatuscxx . . $ g_ServiceStatus. = -1;
66
67 static int initcxx . . $ init(b126dc7c1de90089).(void);
68 static int cleanupcxx . . $ cleanup(b126dc7c1de90089).(void);
69 static int config_initcxx . . $ config_init(afcd3168fc1188a4).(int argc, char * * argv);
70 static int config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).(void);
71 static int setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).(void);
72 static char * write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).(void);
73
74
75 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
76 static int setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).(void)
77 {
78 int pid;
79
80 if (chdir("/") < 0) {
81 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`!));
82 return -1;
83 }
84
85 close(STDIN_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:210:9`!);
86 close(STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!);
87 if (!cmdline_get_foregroundcxx . . $ pvpgn/d2dbs/cmdline_get_foreground(b126dc7c1de90089).()) {
88 close(STDERR_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:212:9`!);
89 }
90
91 switch ((pid = fork())) {
92 case 0:
93 break;
94 case -1:
95 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`!));
96 return -1;
97 default:
98 return pid;
99 }
100 umask(0);
101 setsid();
102 return 0;
103 }
104 #endif
105
106 static char * write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).(void)
107 {
108 char *pidfile = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(d2dbs_prefs_get_pidfile());
109
110 if (pidfile)
111 {
112 if (pidfile[0] == '\0') {
113 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
114 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
115 }
116 #ifdef HAVE_GETPIDcxx . . $ `build/config.h:49:9`!
117 stdcxx . . $ std/::FILEcxx . . $ std/FILE# * fp;
118
119 if (!(fp = stdcxx . . $ std/::fopencxx . . $ std/fopen(82ec0787cd37e0a3).(pidfile, "w"))) {
120 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`!));
121 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
122 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
123 }
124 else {
125 stdcxx . . $ std/::fprintfcxx . . $ std/fprintf(bc216f8e5b1ebeac).(fp, "%u", (unsigned int)getpid());
126 if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(fp) < 0)
127 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`!));
128 }
129
130 #else
131 eventlog(eventlog_level_warn, __FUNCTION__, "no getpid() std::system call, disable pid file in d2dbs.conf");
132 xfree((void *)pidfile); /* avoid warning */
133 return NULL;
134 #endif
135 }
136
137 return pidfile;
138 }
139
140 static int initcxx . . $ init(b126dc7c1de90089).(void)
141 {
142 return 0;
143 }
144
145 static int cleanupcxx . . $ cleanup(b126dc7c1de90089).(void)
146 {
147 return 0;
148 }
149
150 static int config_initcxx . . $ config_init(afcd3168fc1188a4).(int argc, char * * argv)
151 {
152 char const * levels;
153 char * temp;
154 char const * tok;
155
156 if (cmdline_loadcxx . . $ pvpgn/d2dbs/cmdline_load(afcd3168fc1188a4).(argc, argv) < 0) {
157 return -1;
158 }
159
160 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
161 int pid;
162
163 if (!cmdline_get_foregroundcxx . . $ pvpgn/d2dbs/cmdline_get_foreground(b126dc7c1de90089).()) {
164 if (!((pid = setup_daemoncxx . . $ setup_daemon(b126dc7c1de90089).()) == 0)) {
165 return pid;
166 }
167 }
168 #endif
169
170 if (d2dbs_prefs_loadcxx . . $ pvpgn/d2dbs/d2dbs_prefs_load(5c4306d8553b3696).(cmdline_get_preffilecxx . . $ pvpgn/d2dbs/cmdline_get_preffile(33a1f659e94feb42).()) < 0) {
171 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "error loading configuration file {}", cmdline_get_preffilecxx . . $ pvpgn/d2dbs/cmdline_get_preffile(33a1f659e94feb42).());
172 return -1;
173 }
174
175 eventlog_clear_levelcxx . . $ pvpgn/eventlog_clear_level(49f6e7a06ebc5aa8).();
176 if ((levels = d2dbs_prefs_get_loglevelscxx . . $ pvpgn/d2dbs/d2dbs_prefs_get_loglevels(33a1f659e94feb42).()))
177 {
178 temp = xstrdupcxx . . $ `src/common/xalloc.h:49:9`!(levels);
179 tok = stdcxx . . $ std/::strtok(temp, ","); /* std::strtok modifies the string it is passed */
180
181 while (tok)
182 {
183 if (eventlog_add_levelcxx . . $ pvpgn/eventlog_add_level(5c4306d8553b3696).(tok) < 0)
184 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not add std::log level \"{}\"", tok);
185 tok = stdcxx . . $ std/::strtok(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, ",");
186 }
187
188 xfreecxx . . $ `src/common/xalloc.h:51:9`!(temp);
189 }
190
191 #ifdef WIN32_GUI
192 if (cmdline_get_gui()){
193 eventlog_add_level(eventlog_get_levelname_str(eventlog_level_gui));
194 }
195 #endif
196
197 #ifdef DO_DAEMONIZEcxx . . $ `src/common/setup_before.h:353:10`!
198 if (cmdline_get_foregroundcxx . . $ pvpgn/d2dbs/cmdline_get_foreground(b126dc7c1de90089).()) {
199 eventlog_setcxx . . $ pvpgn/eventlog_set(8d4262d45fc644ab).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
200 }
201 else
202 #endif
203 {
204 if (cmdline_get_logfilecxx . . $ pvpgn/d2dbs/cmdline_get_logfile(33a1f659e94feb42).()) {
205 if (eventlog_opencxx . . $ pvpgn/eventlog_open(5c4306d8553b3696).(cmdline_get_logfilecxx . . $ pvpgn/d2dbs/cmdline_get_logfile(33a1f659e94feb42).()) < 0) {
206 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "error open eventlog file {}", cmdline_get_logfilecxx . . $ pvpgn/d2dbs/cmdline_get_logfile(33a1f659e94feb42).());
207 return -1;
208 }
209 }
210 else {
211 if (eventlog_opencxx . . $ pvpgn/eventlog_open(5c4306d8553b3696).(d2dbs_prefs_get_logfilecxx . . $ pvpgn/d2dbs/d2dbs_prefs_get_logfile(33a1f659e94feb42).()) < 0) {
212 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "error open eventlog file {}", d2dbs_prefs_get_logfilecxx . . $ pvpgn/d2dbs/d2dbs_prefs_get_logfile(33a1f659e94feb42).());
213 return -1;
214 }
215 }
216 }
217 return 0;
218 }
219
220 static int config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).(void)
221 {
222 d2dbs_prefs_unloadcxx . . $ pvpgn/d2dbs/d2dbs_prefs_unload(b126dc7c1de90089).();
223 cmdline_unloadcxx . . $ pvpgn/d2dbs/cmdline_unload(49f6e7a06ebc5aa8).();
224 eventlog_closecxx . . $ pvpgn/eventlog_close(b126dc7c1de90089).();
225 if (eventlog_fpcxx . . $ eventlog_fp.) stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(eventlog_fpcxx . . $ eventlog_fp.);
226 return 0;
227 }
228
229
230 #ifdef WIN32_GUI
231 extern int app_main(int argc, char ** argv)
232 #else
233 extern int maincxx . . $ main(afcd3168fc1188a4).(int argc, char ** argv)
234 #endif
235 {
236 int pid;
237 char * pidfile;
238
239 #ifdef WIN32
240 // create a dump file whenever the gateway crashes
241 SetUnhandledExceptionFilter(unhandled_handler);
242 #endif
243
244 eventlog_setcxx . . $ pvpgn/eventlog_set(8d4262d45fc644ab).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
245 pid = config_initcxx . . $ config_init(afcd3168fc1188a4).(argc, argv);
246 if (!(pid == 0)) {
247 // if (pid==1) pid=0;
248 return pid;
249 }
250 pidfile = write_to_pidfilecxx . . $ write_to_pidfile(41208ce6ca65dd58).();
251 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info., __FUNCTION__, D2DBS_VERSIONcxx . . $ `src/d2dbs/version.h:22:9`!);
252 if (initcxx . . $ init(b126dc7c1de90089).() < 0) {
253 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "failed to init");
254 return -1;
255 }
256 else {
257 eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_infocxx . . $ pvpgn/eventlog_level_info., __FUNCTION__, "server initialized");
258 }
259 #ifndef WIN32
260 d2dbs_handle_signal_initcxx . . $ pvpgn/d2dbs/d2dbs_handle_signal_init(b126dc7c1de90089).();
261 #endif
262 dbs_server_maincxx . . $ pvpgn/d2dbs/dbs_server_main(b126dc7c1de90089).();
263 cleanupcxx . . $ cleanup(b126dc7c1de90089).();
264 if (pidfile) {
265 if (stdcxx . . $ std/::removecxx . . $ std/remove(f5ecf3191ed0cd93).(pidfile) < 0)
266 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`!));
267 xfreecxx . . $ `src/common/xalloc.h:51:9`!((void *)pidfile); /* avoid warning */
268 }
269 config_cleanupcxx . . $ config_cleanup(b126dc7c1de90089).();
270 return 0;
271 }