/[LeafOK_CVS]/pvpgn-1.7.4/src/d2cs/main.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/d2cs/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Tue Jun 13 12:19:11 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, HEAD
Changes since 1.2: +2 -0 lines
Content type: text/x-csrc
Antibot (for non-password protected game)

1 /*
2 * Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
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 <stdio.h>
22 #ifdef STDC_HEADERS
23 # include <stdlib.h>
24 #else
25 # ifdef HAVE_MALLOC_H
26 # include <malloc.h>
27 # endif
28 #endif
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #else
35 # ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 # endif
38 # ifdef HAVE_MEMORY_H
39 # include <memory.h>
40 # endif
41 #endif
42 #include "compat/strdup.h"
43 #include <errno.h>
44 #include "compat/strerror.h"
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
47 #endif
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
50 #endif
51
52 #include "server.h"
53 #include "game.h"
54 #include "connection.h"
55 #include "serverqueue.h"
56 #include "d2gs.h"
57 #include "prefs.h"
58 #include "gs_scope.h"
59 #include "cmdline_parse.h"
60 #include "d2ladder.h"
61 #include "version.h"
62 #include "common/trans.h"
63 #include "common/fdwatch.h"
64 #include "common/eventlog.h"
65 #ifdef WIN32
66 # include "win32/service.h"
67 #endif
68 #include "common/xalloc.h"
69 #ifdef WIN32_GUI
70 # include "win32/winmain.h"
71 #endif
72 #include "common/setup_after.h"
73
74 char serviceLongName[] = "d2cs service";
75 char serviceName[] = "d2cs";
76 char serviceDescription[] = "Diablo 2 Character Server";
77
78 int g_ServiceStatus = -1;
79
80 static int init(void);
81 static int cleanup(void);
82 static int config_init(int argc, char * * argv);
83 static int config_cleanup(void);
84 static int setup_daemon(void);
85
86
87 #ifdef DO_DAEMONIZE
88 static int setup_daemon(void)
89 {
90 int pid;
91
92 if (chdir("/")<0) {
93 eventlog(eventlog_level_error,__FUNCTION__,"can not change working directory to root directory (chdir: %s)",pstrerror(errno));
94 return -1;
95 }
96 close(STDIN_FILENO);
97 close(STDOUT_FILENO);
98 if (!cmdline_get_debugmode()) {
99 close(STDERR_FILENO);
100 }
101 switch ((pid = fork())) {
102 case 0:
103 break;
104 case -1:
105 eventlog(eventlog_level_error,__FUNCTION__,"error create child process (fork: %s)",pstrerror(errno));
106 return -1;
107 default:
108 return pid;
109 }
110 umask(0);
111 setsid();
112 return 0;
113 }
114 #endif
115
116 static int init(void)
117 {
118 d2cs_connlist_create();
119 d2cs_gamelist_create();
120 sqlist_create();
121 d2gslist_create();
122 gqlist_create();
123 d2ladder_init();
124 charstatus_list_create();
125 if(trans_load(d2cs_prefs_get_transfile(),TRANS_D2CS)<0)
126 eventlog(eventlog_level_error,__FUNCTION__,"could not load trans list");
127 fdwatch_init(prefs_get_max_connections());
128
129 /* Added by Leaflet at 2006-06-05 Begin */
130 if (serve_scope_list_load(cmdline_get_gsscope_file()))
131 eventlog(eventlog_level_error,__FUNCTION__,"error loading gsscope file %s");
132 /* End */
133
134 return 0;
135 }
136
137
138 static int cleanup(void)
139 {
140 charstatus_list_destroy();
141 d2ladder_destroy();
142 d2cs_connlist_destroy();
143 d2cs_gamelist_destroy();
144 sqlist_destroy();
145 d2gslist_destroy();
146 gqlist_destroy();
147 trans_unload();
148 fdwatch_close();
149
150 /* Added by Leaflet at 2006-06-05 Begin */
151 serve_scope_list_unload();
152 /* End */
153
154 return 0;
155 }
156
157
158 static int config_init(int argc, char * * argv)
159 {
160 char const * levels;
161 char * temp;
162 char const * tok;
163 int pid;
164
165 if (cmdline_parse(argc, argv)<0) {
166 return -1;
167 }
168 #ifdef WIN32
169 if (cmdline_get_run_as_service())
170 {
171 Win32_ServiceRun();
172 return 1;
173 }
174 #endif
175
176 if (cmdline_get_version()) {
177 cmdline_show_version();
178 return -1;
179 }
180 if (cmdline_get_help()) {
181 cmdline_show_help();
182 return -1;
183 }
184 #ifdef DO_DAEMONIZE
185 if ((!cmdline_get_foreground())&&(!cmdline_get_debugmode())) {
186 if (!((pid = setup_daemon()) == 0)) {
187 return pid;
188 }
189 }
190 #endif
191
192 #ifdef WIN32
193 if (cmdline_get_make_service())
194 {
195 if (strcmp(cmdline_get_make_service(), "install") == 0) {
196 fprintf(stderr, "Installing service\n");
197 Win32_ServiceInstall();
198 return 1;
199 }
200 if (strcmp(cmdline_get_make_service(), "uninstall") == 0) {
201 fprintf(stderr, "Uninstalling service\n");
202 Win32_ServiceUninstall();
203 return 1;
204 }
205 }
206 #endif
207
208 if (d2cs_prefs_load(cmdline_get_prefs_file())<0) {
209 eventlog(eventlog_level_error,__FUNCTION__,"error loading configuration file %s",cmdline_get_prefs_file());
210 return -1;
211 }
212
213 eventlog_clear_level();
214 if ((levels = d2cs_prefs_get_loglevels()))
215 {
216 temp = xstrdup(levels);
217 tok = strtok(temp,","); /* strtok modifies the string it is passed */
218
219 while (tok)
220 {
221 if (eventlog_add_level(tok)<0)
222 eventlog(eventlog_level_error,__FUNCTION__,"could not add log level \"%s\"",tok);
223 tok = strtok(NULL,",");
224 }
225
226 xfree(temp);
227 }
228
229 if (cmdline_get_debugmode()) {
230 eventlog_set(stderr);
231 } else if (cmdline_get_logfile()) {
232 if (eventlog_open(cmdline_get_logfile())<0) {
233 eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",cmdline_get_logfile());
234 return -1;
235 }
236 } else {
237 if (eventlog_open(d2cs_prefs_get_logfile())<0) {
238 eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",d2cs_prefs_get_logfile());
239 return -1;
240 }
241 }
242 return 0;
243 }
244
245
246 static int config_cleanup(void)
247 {
248 d2cs_prefs_unload();
249 cmdline_cleanup();
250 return 0;
251 }
252
253 #ifdef WIN32_GUI
254 extern int server_main(int argc, char * * argv)
255 #else
256 extern int main(int argc, char * * argv)
257 #endif
258 {
259 int pid;
260 eventlog_set(stderr);
261 if (!((pid = config_init(argc, argv)) == 0)) {
262 // if (pid==1) pid=0;
263 return pid;
264 }
265 eventlog(eventlog_level_info,__FUNCTION__,D2CS_VERSION);
266 if (init()<0) {
267 eventlog(eventlog_level_error,__FUNCTION__,"failed to init");
268 return -1;
269 } else {
270 eventlog(eventlog_level_info,__FUNCTION__,"server initialized");
271 }
272 if (d2cs_server_process()<0) {
273 eventlog(eventlog_level_error,__FUNCTION__,"failed to run server");
274 return -1;
275 }
276 cleanup();
277 config_cleanup();
278 eventlog_close();
279 return 0;
280 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1