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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Mon Jun 12 18:31:17 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
Changes since 1.1: +12 -9 lines
Content type: text/x-csrc
no message

1 sysadm 1.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 sysadm 1.2 #include "gs_scope.h"
59 sysadm 1.1 #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     if(trans_load(d2cs_prefs_get_transfile(),TRANS_D2CS)<0)
125     eventlog(eventlog_level_error,__FUNCTION__,"could not load trans list");
126     fdwatch_init(prefs_get_max_connections());
127 sysadm 1.2
128     /* Added by Leaflet at 2006-06-05 Begin */
129     if (serve_scope_list_load(cmdline_get_gsscope_file()))
130     eventlog(eventlog_level_error,__FUNCTION__,"error loading gsscope file %s");
131     /* End */
132    
133 sysadm 1.1 return 0;
134     }
135    
136    
137     static int cleanup(void)
138     {
139     d2ladder_destroy();
140     d2cs_connlist_destroy();
141     d2cs_gamelist_destroy();
142     sqlist_destroy();
143     d2gslist_destroy();
144     gqlist_destroy();
145     trans_unload();
146     fdwatch_close();
147 sysadm 1.2
148     /* Added by Leaflet at 2006-06-05 Begin */
149     serve_scope_list_unload();
150     /* End */
151    
152 sysadm 1.1 return 0;
153     }
154    
155    
156     static int config_init(int argc, char * * argv)
157     {
158     char const * levels;
159     char * temp;
160     char const * tok;
161     int pid;
162    
163     if (cmdline_parse(argc, argv)<0) {
164     return -1;
165     }
166     #ifdef WIN32
167     if (cmdline_get_run_as_service())
168     {
169     Win32_ServiceRun();
170     return 1;
171     }
172     #endif
173    
174     if (cmdline_get_version()) {
175     cmdline_show_version();
176     return -1;
177     }
178     if (cmdline_get_help()) {
179     cmdline_show_help();
180     return -1;
181     }
182     #ifdef DO_DAEMONIZE
183     if ((!cmdline_get_foreground())&&(!cmdline_get_debugmode())) {
184     if (!((pid = setup_daemon()) == 0)) {
185     return pid;
186     }
187     }
188     #endif
189    
190     #ifdef WIN32
191     if (cmdline_get_make_service())
192     {
193     if (strcmp(cmdline_get_make_service(), "install") == 0) {
194     fprintf(stderr, "Installing service\n");
195     Win32_ServiceInstall();
196     return 1;
197     }
198     if (strcmp(cmdline_get_make_service(), "uninstall") == 0) {
199     fprintf(stderr, "Uninstalling service\n");
200     Win32_ServiceUninstall();
201     return 1;
202     }
203     }
204     #endif
205    
206     if (d2cs_prefs_load(cmdline_get_prefs_file())<0) {
207     eventlog(eventlog_level_error,__FUNCTION__,"error loading configuration file %s",cmdline_get_prefs_file());
208     return -1;
209     }
210    
211     eventlog_clear_level();
212     if ((levels = d2cs_prefs_get_loglevels()))
213     {
214     temp = xstrdup(levels);
215     tok = strtok(temp,","); /* strtok modifies the string it is passed */
216    
217     while (tok)
218     {
219     if (eventlog_add_level(tok)<0)
220     eventlog(eventlog_level_error,__FUNCTION__,"could not add log level \"%s\"",tok);
221     tok = strtok(NULL,",");
222     }
223    
224     xfree(temp);
225     }
226    
227     if (cmdline_get_debugmode()) {
228     eventlog_set(stderr);
229     } else if (cmdline_get_logfile()) {
230     if (eventlog_open(cmdline_get_logfile())<0) {
231     eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",cmdline_get_logfile());
232     return -1;
233     }
234     } else {
235     if (eventlog_open(d2cs_prefs_get_logfile())<0) {
236     eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",d2cs_prefs_get_logfile());
237     return -1;
238     }
239     }
240     return 0;
241     }
242    
243    
244     static int config_cleanup(void)
245     {
246     d2cs_prefs_unload();
247     cmdline_cleanup();
248     return 0;
249     }
250    
251     #ifdef WIN32_GUI
252     extern int server_main(int argc, char * * argv)
253     #else
254     extern int main(int argc, char * * argv)
255     #endif
256     {
257     int pid;
258     eventlog_set(stderr);
259     if (!((pid = config_init(argc, argv)) == 0)) {
260     // if (pid==1) pid=0;
261     return pid;
262     }
263     eventlog(eventlog_level_info,__FUNCTION__,D2CS_VERSION);
264     if (init()<0) {
265     eventlog(eventlog_level_error,__FUNCTION__,"failed to init");
266     return -1;
267     } else {
268     eventlog(eventlog_level_info,__FUNCTION__,"server initialized");
269     }
270     if (d2cs_server_process()<0) {
271     eventlog(eventlog_level_error,__FUNCTION__,"failed to run server");
272     return -1;
273     }
274     cleanup();
275     config_cleanup();
276     eventlog_close();
277     return 0;
278     }

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