| 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 |
|
|
#include <errno.h>
|
| 33 |
|
|
#ifdef HAVE_STRING_H
|
| 34 |
|
|
# include <string.h>
|
| 35 |
|
|
#else
|
| 36 |
|
|
# ifdef HAVE_STRINGS_H
|
| 37 |
|
|
# include <strings.h>
|
| 38 |
|
|
# endif
|
| 39 |
|
|
# ifdef HAVE_MEMORY_H
|
| 40 |
|
|
# include <memory.h>
|
| 41 |
|
|
# endif
|
| 42 |
|
|
#endif
|
| 43 |
|
|
#include "compat/strdup.h"
|
| 44 |
|
|
#ifdef HAVE_SYS_STAT_H
|
| 45 |
|
|
# include <sys/stat.h>
|
| 46 |
|
|
#endif
|
| 47 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 48 |
|
|
# include <sys/types.h>
|
| 49 |
|
|
#endif
|
| 50 |
|
|
|
| 51 |
|
|
#include "prefs.h"
|
| 52 |
|
|
#include "cmdline_parse.h"
|
| 53 |
|
|
#include "version.h"
|
| 54 |
|
|
#include "common/eventlog.h"
|
| 55 |
|
|
#ifdef WIN32
|
| 56 |
|
|
# include "win32/service.h"
|
| 57 |
|
|
#endif
|
| 58 |
|
|
#include "handle_signal.h"
|
| 59 |
|
|
#include "dbserver.h"
|
| 60 |
|
|
#include "common/xalloc.h"
|
| 61 |
|
|
#include "compat/strerror.h"
|
| 62 |
|
|
#ifdef WIN32_GUI
|
| 63 |
|
|
# include "win32/winmain.h"
|
| 64 |
|
|
#endif
|
| 65 |
|
|
#include "common/setup_after.h"
|
| 66 |
|
|
|
| 67 |
|
|
static FILE * eventlog_fp;
|
| 68 |
|
|
|
| 69 |
|
|
char serviceLongName[] = "d2dbs service";
|
| 70 |
|
|
char serviceName[] = "d2dbs";
|
| 71 |
|
|
char serviceDescription[] = "Diablo 2 DataBase Server";
|
| 72 |
|
|
|
| 73 |
|
|
int g_ServiceStatus = -1;
|
| 74 |
|
|
|
| 75 |
|
|
static int init(void);
|
| 76 |
|
|
static int cleanup(void);
|
| 77 |
|
|
static int config_init(int argc, char * * argv);
|
| 78 |
|
|
static int config_cleanup(void);
|
| 79 |
|
|
static int setup_daemon(void);
|
| 80 |
|
|
|
| 81 |
|
|
|
| 82 |
|
|
#ifdef DO_DAEMONIZE
|
| 83 |
|
|
static int setup_daemon(void)
|
| 84 |
|
|
{
|
| 85 |
|
|
int pid;
|
| 86 |
|
|
|
| 87 |
|
|
if (chdir("/")<0) {
|
| 88 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"can not change working directory to root directory (chdir: %s)",pstrerror(errno));
|
| 89 |
|
|
return -1;
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
close(STDIN_FILENO);
|
| 93 |
|
|
close(STDOUT_FILENO);
|
| 94 |
|
|
if (!d2dbs_cmdline_get_debugmode()) {
|
| 95 |
|
|
close(STDERR_FILENO);
|
| 96 |
|
|
}
|
| 97 |
|
|
|
| 98 |
|
|
switch ((pid=fork())) {
|
| 99 |
|
|
case 0:
|
| 100 |
|
|
break;
|
| 101 |
|
|
case -1:
|
| 102 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error create child process (fork: %s)",pstrerror(errno));
|
| 103 |
|
|
return -1;
|
| 104 |
|
|
default:
|
| 105 |
|
|
return pid;
|
| 106 |
|
|
}
|
| 107 |
|
|
umask(0);
|
| 108 |
|
|
setsid();
|
| 109 |
|
|
return 0;
|
| 110 |
|
|
}
|
| 111 |
|
|
#endif
|
| 112 |
|
|
|
| 113 |
|
|
static int init(void)
|
| 114 |
|
|
{
|
| 115 |
|
|
return 0;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
static int cleanup(void)
|
| 119 |
|
|
{
|
| 120 |
|
|
return 0;
|
| 121 |
|
|
}
|
| 122 |
|
|
|
| 123 |
|
|
static int config_init(int argc, char * * argv)
|
| 124 |
|
|
{
|
| 125 |
|
|
char const * levels;
|
| 126 |
|
|
char * temp;
|
| 127 |
|
|
char const * tok;
|
| 128 |
|
|
int pid;
|
| 129 |
|
|
|
| 130 |
|
|
if (d2dbs_cmdline_parse(argc, argv)<0) {
|
| 131 |
|
|
return -1;
|
| 132 |
|
|
}
|
| 133 |
|
|
|
| 134 |
|
|
#ifdef WIN32
|
| 135 |
|
|
if (d2dbs_cmdline_get_run_as_service())
|
| 136 |
|
|
{
|
| 137 |
|
|
Win32_ServiceRun();
|
| 138 |
|
|
return 1;
|
| 139 |
|
|
}
|
| 140 |
|
|
#endif
|
| 141 |
|
|
|
| 142 |
|
|
if (d2dbs_cmdline_get_version()) {
|
| 143 |
|
|
d2dbs_cmdline_show_version();
|
| 144 |
|
|
return -1;
|
| 145 |
|
|
}
|
| 146 |
|
|
if (d2dbs_cmdline_get_help()) {
|
| 147 |
|
|
d2dbs_cmdline_show_help();
|
| 148 |
|
|
return -1;
|
| 149 |
|
|
}
|
| 150 |
|
|
#ifdef DO_DAEMONIZE
|
| 151 |
|
|
if ((!d2dbs_cmdline_get_foreground()) && (!d2dbs_cmdline_get_debugmode())) {
|
| 152 |
|
|
if (!((pid = setup_daemon()) == 0)) {
|
| 153 |
|
|
return pid;
|
| 154 |
|
|
}
|
| 155 |
|
|
}
|
| 156 |
|
|
#endif
|
| 157 |
|
|
|
| 158 |
|
|
#ifdef WIN32
|
| 159 |
|
|
if (d2dbs_cmdline_get_make_service())
|
| 160 |
|
|
{
|
| 161 |
|
|
if (strcmp(d2dbs_cmdline_get_make_service(), "install") == 0) {
|
| 162 |
|
|
fprintf(stderr, "Installing service\n");
|
| 163 |
|
|
Win32_ServiceInstall();
|
| 164 |
|
|
return 1;
|
| 165 |
|
|
}
|
| 166 |
|
|
if (strcmp(d2dbs_cmdline_get_make_service(), "uninstall") == 0) {
|
| 167 |
|
|
fprintf(stderr, "Uninstalling service\n");
|
| 168 |
|
|
Win32_ServiceUninstall();
|
| 169 |
|
|
return 1;
|
| 170 |
|
|
}
|
| 171 |
|
|
}
|
| 172 |
|
|
#endif
|
| 173 |
|
|
|
| 174 |
|
|
if (d2dbs_prefs_load(d2dbs_cmdline_get_prefs_file())<0) {
|
| 175 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error loading configuration file %s",d2dbs_cmdline_get_prefs_file());
|
| 176 |
|
|
return -1;
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
eventlog_clear_level();
|
| 180 |
|
|
if ((levels = d2dbs_prefs_get_loglevels()))
|
| 181 |
|
|
{
|
| 182 |
|
|
temp = xstrdup(levels);
|
| 183 |
|
|
tok = strtok(temp,","); /* strtok modifies the string it is passed */
|
| 184 |
|
|
|
| 185 |
|
|
while (tok)
|
| 186 |
|
|
{
|
| 187 |
|
|
if (eventlog_add_level(tok)<0)
|
| 188 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"could not add log level \"%s\"",tok);
|
| 189 |
|
|
tok = strtok(NULL,",");
|
| 190 |
|
|
}
|
| 191 |
|
|
|
| 192 |
|
|
xfree(temp);
|
| 193 |
|
|
}
|
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
if (d2dbs_cmdline_get_debugmode()) {
|
| 197 |
|
|
eventlog_set(stderr);
|
| 198 |
|
|
} else if (d2dbs_cmdline_get_logfile()) {
|
| 199 |
|
|
if (eventlog_open(d2dbs_cmdline_get_logfile())<0) {
|
| 200 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",d2dbs_cmdline_get_logfile());
|
| 201 |
|
|
return -1;
|
| 202 |
|
|
}
|
| 203 |
|
|
} else {
|
| 204 |
|
|
if (eventlog_open(d2dbs_prefs_get_logfile())<0) {
|
| 205 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error open eventlog file %s",d2dbs_prefs_get_logfile());
|
| 206 |
|
|
return -1;
|
| 207 |
|
|
}
|
| 208 |
|
|
}
|
| 209 |
|
|
return 0;
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
static int config_cleanup(void)
|
| 213 |
|
|
{
|
| 214 |
|
|
d2dbs_prefs_unload();
|
| 215 |
|
|
d2dbs_cmdline_cleanup();
|
| 216 |
|
|
eventlog_close();
|
| 217 |
|
|
if (eventlog_fp) fclose(eventlog_fp);
|
| 218 |
|
|
return 0;
|
| 219 |
|
|
}
|
| 220 |
|
|
|
| 221 |
|
|
#ifdef WIN32_GUI
|
| 222 |
|
|
extern int server_main(int argc, char * * argv)
|
| 223 |
|
|
#else
|
| 224 |
|
|
extern int main(int argc, char * * argv)
|
| 225 |
|
|
#endif
|
| 226 |
|
|
{
|
| 227 |
|
|
int pid;
|
| 228 |
|
|
|
| 229 |
|
|
eventlog_set(stderr);
|
| 230 |
|
|
pid = config_init(argc, argv);
|
| 231 |
|
|
if (!(pid == 0)) {
|
| 232 |
|
|
// if (pid==1) pid=0;
|
| 233 |
|
|
return pid;
|
| 234 |
|
|
}
|
| 235 |
|
|
eventlog(eventlog_level_info,__FUNCTION__,D2DBS_VERSION);
|
| 236 |
|
|
if (init()<0) {
|
| 237 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"failed to init");
|
| 238 |
|
|
return -1;
|
| 239 |
|
|
} else {
|
| 240 |
|
|
eventlog(eventlog_level_info,__FUNCTION__,"server initialized");
|
| 241 |
|
|
}
|
| 242 |
|
|
#ifndef WIN32
|
| 243 |
|
|
d2dbs_handle_signal_init();
|
| 244 |
|
|
#endif
|
| 245 |
|
|
dbs_server_main();
|
| 246 |
|
|
cleanup();
|
| 247 |
|
|
config_cleanup();
|
| 248 |
|
|
return 0;
|
| 249 |
|
|
}
|