/[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.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
Branch point for: GNU
Content type: text/x-csrc
Initial revision

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

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