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"
20 #include "setup.h"
21
22 #include <cerrno>
23 #include <cstdio>
24 #include <cstring>
25
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_STAT_H
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"
46 #include "compat/pgetpid.h"
47 #include "common/eventlog.h"
48 #include "common/xalloc.h"
49 #include "cmdline.h"
50 #include "prefs.h"
51 #include "version.h"
52 #include "handle_signal.h"
53 #include "dbserver.h"
54 #include "common/setup_after.h"
55
56 using namespace pvpgn::d2dbs;
57 using namespace pvpgn;
58
59 static std::FILE * eventlog_fp;
60
61 char serviceLongName[] = "d2dbs service";
62 char serviceName[] = "d2dbs";
63 char serviceDescription[] = "Diablo 2 DataBase Server";
64
65 int g_ServiceStatus = -1;
66
67 static int init(void);
68 static int cleanup(void);
69 static int config_init(int argc, char * * argv);
70 static int config_cleanup(void);
71 static int setup_daemon(void);
72 static char * write_to_pidfile(void);
73
74
75 #ifdef DO_DAEMONIZE
76 static int setup_daemon(void)
77 {
78 int pid;
79
80 if (chdir("/") < 0) {
81 eventlog(eventlog_level_error, __FUNCTION__, "can not change working directory to root directory (chdir: {})", std::strerror(errno));
82 return -1;
83 }
84
85 close(STDIN_FILENO);
86 close(STDOUT_FILENO);
87 if (!cmdline_get_foreground()) {
88 close(STDERR_FILENO);
89 }
90
91 switch ((pid = fork())) {
92 case 0:
93 break;
94 case -1:
95 eventlog(eventlog_level_error, __FUNCTION__, "error create child process (fork: {})", std::strerror(errno));
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_pidfile(void)
107 {
108 char *pidfile = xstrdup(d2dbs_prefs_get_pidfile());
109
110 if (pidfile)
111 {
112 if (pidfile[0] == '\0') {
113 xfree((void *)pidfile); /* avoid warning */
114 return NULL;
115 }
116 #ifdef HAVE_GETPID
117 std::FILE * fp;
118
119 if (!(fp = std::fopen(pidfile, "w"))) {
120 eventlog(eventlog_level_error, __FUNCTION__, "unable to open pid file \"{}\" for writing (std::fopen: {})", pidfile, std::strerror(errno));
121 xfree((void *)pidfile); /* avoid warning */
122 return NULL;
123 }
124 else {
125 std::fprintf(fp, "%u", (unsigned int)getpid());
126 if (std::fclose(fp) < 0)
127 eventlog(eventlog_level_error, __FUNCTION__, "could not close pid file \"{}\" after writing (std::fclose: {})", pidfile, std::strerror(errno));
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 init(void)
141 {
142 return 0;
143 }
144
145 static int cleanup(void)
146 {
147 return 0;
148 }
149
150 static int config_init(int argc, char * * argv)
151 {
152 char const * levels;
153 char * temp;
154 char const * tok;
155
156 if (cmdline_load(argc, argv) < 0) {
157 return -1;
158 }
159
160 #ifdef DO_DAEMONIZE
161 int pid;
162
163 if (!cmdline_get_foreground()) {
164 if (!((pid = setup_daemon()) == 0)) {
165 return pid;
166 }
167 }
168 #endif
169
170 if (d2dbs_prefs_load(cmdline_get_preffile()) < 0) {
171 eventlog(eventlog_level_error, __FUNCTION__, "error loading configuration file {}", cmdline_get_preffile());
172 return -1;
173 }
174
175 eventlog_clear_level();
176 if ((levels = d2dbs_prefs_get_loglevels()))
177 {
178 temp = xstrdup(levels);
179 tok = std::strtok(temp, ","); /* std::strtok modifies the string it is passed */
180
181 while (tok)
182 {
183 if (eventlog_add_level(tok) < 0)
184 eventlog(eventlog_level_error, __FUNCTION__, "could not add std::log level \"{}\"", tok);
185 tok = std::strtok(NULL, ",");
186 }
187
188 xfree(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_DAEMONIZE
198 if (cmdline_get_foreground()) {
199 eventlog_set(stderr);
200 }
201 else
202 #endif
203 {
204 if (cmdline_get_logfile()) {
205 if (eventlog_open(cmdline_get_logfile()) < 0) {
206 eventlog(eventlog_level_error, __FUNCTION__, "error open eventlog file {}", cmdline_get_logfile());
207 return -1;
208 }
209 }
210 else {
211 if (eventlog_open(d2dbs_prefs_get_logfile()) < 0) {
212 eventlog(eventlog_level_error, __FUNCTION__, "error open eventlog file {}", d2dbs_prefs_get_logfile());
213 return -1;
214 }
215 }
216 }
217 return 0;
218 }
219
220 static int config_cleanup(void)
221 {
222 d2dbs_prefs_unload();
223 cmdline_unload();
224 eventlog_close();
225 if (eventlog_fp) std::fclose(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 main(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_set(stderr);
245 pid = config_init(argc, argv);
246 if (!(pid == 0)) {
247 // if (pid==1) pid=0;
248 return pid;
249 }
250 pidfile = write_to_pidfile();
251 eventlog(eventlog_level_info, __FUNCTION__, D2DBS_VERSION);
252 if (init() < 0) {
253 eventlog(eventlog_level_error, __FUNCTION__, "failed to init");
254 return -1;
255 }
256 else {
257 eventlog(eventlog_level_info, __FUNCTION__, "server initialized");
258 }
259 #ifndef WIN32
260 d2dbs_handle_signal_init();
261 #endif
262 dbs_server_main();
263 cleanup();
264 if (pidfile) {
265 if (std::remove(pidfile) < 0)
266 eventlog(eventlog_level_error, __FUNCTION__, "could not remove pid file \"{}\" (std::remove: {})", pidfile, std::strerror(errno));
267 xfree((void *)pidfile); /* avoid warning */
268 }
269 config_cleanup();
270 return 0;
271 }