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"
19 #include "setup.h"
20
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstring>
24
25 #ifdef HAVE_SYS_TYPES_H
26 # include <sys/types.h>
27 #endif
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
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"
45 #include "compat/pgetpid.h"
46 #include "common/eventlog.h"
47 #include "common/xalloc.h"
48 #include "common/trans.h"
49 #include "common/fdwatch.h"
50 #include "prefs.h"
51 #include "connection.h"
52 #include "d2gs.h"
53 #include "serverqueue.h"
54 #include "d2ladder.h"
55 #include "cmdline.h"
56 #include "game.h"
57 #include "server.h"
58 #include "version.h"
59 #include "common/setup_after.h"
60
61 using namespace 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 init(void);
73 static int cleanup(void);
74 static int config_init(int argc, char * * argv);
75 static int config_cleanup(void);
76 static int setup_daemon(void);
77 static char * write_to_pidfile(void);
78
79
80 #ifdef DO_DAEMONIZE
81 static int setup_daemon(void)
82 {
83 int pid;
84
85 if (chdir("/")<0) {
86 eventlog(eventlog_level_error,__FUNCTION__,"can not change working directory to root directory (chdir: {})",std::strerror(errno));
87 return -1;
88 }
89 close(STDIN_FILENO);
90 close(STDOUT_FILENO);
91 if (!cmdline_get_foreground()) {
92 close(STDERR_FILENO);
93 }
94 switch ((pid = fork())) {
95 case 0:
96 break;
97 case -1:
98 eventlog(eventlog_level_error,__FUNCTION__,"error create child process (fork: {})",std::strerror(errno));
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_pidfile(void)
111 {
112 char *pidfile = xstrdup(prefs_get_pidfile());
113
114 if (pidfile)
115 {
116 if (pidfile[0] == '\0') {
117 xfree((void *)pidfile); /* avoid warning */
118 return NULL;
119 }
120 #ifdef HAVE_GETPID
121 std::FILE * fp;
122
123 if (!(fp = std::fopen(pidfile,"w"))) {
124 eventlog(eventlog_level_error,__FUNCTION__,"unable to open pid file \"{}\" for writing (std::fopen: {})",pidfile,std::strerror(errno));
125 xfree((void *)pidfile); /* avoid warning */
126 return NULL;
127 } else {
128 std::fprintf(fp,"%u",(unsigned int)getpid());
129 if (std::fclose(fp)<0)
130 eventlog(eventlog_level_error,__FUNCTION__,"could not close pid file \"{}\" after writing (std::fclose: {})",pidfile,std::strerror(errno));
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 init(void)
145 {
146 d2cs_connlist_create();
147 d2cs_gamelist_create();
148 sqlist_create();
149 d2gslist_create();
150 gqlist_create();
151 d2ladder_init();
152 if(trans_load(d2cs_prefs_get_transfile(),TRANS_D2CS)<0)
153 eventlog(eventlog_level_error,__FUNCTION__,"could not load trans list");
154 fdwatch_init(prefs_get_max_connections());
155 return 0;
156 }
157
158
159 static int cleanup(void)
160 {
161 d2ladder_destroy();
162 d2cs_connlist_destroy();
163 d2cs_gamelist_destroy();
164 sqlist_destroy();
165 d2gslist_destroy();
166 gqlist_destroy();
167 trans_unload();
168 fdwatch_close();
169 return 0;
170 }
171
172
173 static int config_init(int argc, char * * argv)
174 {
175 char const * levels;
176 char * temp;
177 char const * tok;
178
179 if (cmdline_load(argc, argv) != 1) {
180 return -1;
181 }
182
183 #ifdef DO_DAEMONIZE
184 int pid;
185
186 if ((!cmdline_get_foreground())) {
187 if (!((pid = setup_daemon()) == 0)) {
188 return pid;
189 }
190 }
191 #endif
192
193 if (d2cs_prefs_load(cmdline_get_preffile())<0) {
194 eventlog(eventlog_level_error,__FUNCTION__,"error loading configuration file {}",cmdline_get_preffile());
195 return -1;
196 }
197
198 eventlog_clear_level();
199 if ((levels = d2cs_prefs_get_loglevels()))
200 {
201 temp = xstrdup(levels);
202 tok = std::strtok(temp,","); /* std::strtok modifies the string it is passed */
203
204 while (tok)
205 {
206 if (eventlog_add_level(tok)<0)
207 eventlog(eventlog_level_error,__FUNCTION__,"could not add std::log level \"{}\"",tok);
208 tok = std::strtok(NULL,",");
209 }
210
211 xfree(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_DAEMONIZE
221 if (cmdline_get_foreground()) {
222 eventlog_set(stderr);
223 }
224 else
225 #endif
226 {
227 if (cmdline_get_logfile()) {
228 if (eventlog_open(cmdline_get_logfile())<0) {
229 eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file {}",cmdline_get_logfile());
230 return -1;
231 }
232 } else {
233 if (eventlog_open(d2cs_prefs_get_logfile())<0) {
234 eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file {}",d2cs_prefs_get_logfile());
235 return -1;
236 }
237 }
238 }
239 return 0;
240 }
241
242
243 static int config_cleanup(void)
244 {
245 d2cs_prefs_unload();
246 cmdline_unload();
247 return 0;
248 }
249
250
251 #ifdef WIN32_GUI
252 extern int app_main(int argc, char ** argv)
253 #else
254 extern int main(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_set(stderr);
265 if (!((pid = config_init(argc, argv)) == 0)) {
266 // if (pid==1) pid=0;
267 return pid;
268 }
269 const char* const pidfile = write_to_pidfile();
270 eventlog(eventlog_level_info,__FUNCTION__,D2CS_VERSION);
271 if (init()<0) {
272 eventlog(eventlog_level_error,__FUNCTION__,"failed to init");
273 if (pidfile)
274 xfree((void*)pidfile);
275 return -1;
276 } else {
277 eventlog(eventlog_level_info,__FUNCTION__,"server initialized");
278 }
279 if (d2cs_server_process()<0) {
280 eventlog(eventlog_level_error,__FUNCTION__,"failed to run server");
281 if (pidfile)
282 xfree((void*)pidfile);
283 return -1;
284 }
285 cleanup();
286 if (pidfile) {
287 if (std::remove(pidfile)<0)
288 eventlog(eventlog_level_error,__FUNCTION__,"could not remove pid file \"{}\" (std::remove: {})",pidfile,std::strerror(errno));
289 xfree((void *)pidfile); /* avoid warning */
290 }
291 config_cleanup();
292 eventlog_close();
293 return 0;
294 }