| 1 | /* |
| 2 | * Copyright (C) 1998 Mark Baysinger ([email protected]) |
| 3 | * Copyright (C) 1998,1999,2000 Ross Combs ([email protected]) |
| 4 | * Copyright (C) 2005 Dizzy |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | #include "common/setup_before.h" |
| 21 | |
| 22 | #include <exception> |
| 23 | #include <iostream> |
| 24 | #include <stdexcept> |
| 25 | |
| 26 | #include <cerrno> |
| 27 | #include <cstring> |
| 28 | |
| 29 | #ifdef HAVE_SYS_TYPES_H |
| 30 | # include <sys/types.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_UNISTD_H |
| 33 | # include <unistd.h> |
| 34 | #endif |
| 35 | #ifdef WIN32 |
| 36 | # include "win32/service.h" |
| 37 | #endif |
| 38 | #ifdef WIN32_GUI |
| 39 | # include "common/gui_printf.h" |
| 40 | #endif |
| 41 | |
| 42 | #include "compat/stdfileno.h" |
| 43 | #include "compat/psock.h" |
| 44 | #include "compat/uname.h" |
| 45 | #include "compat/pgetpid.h" |
| 46 | #include "common/eventlog.h" |
| 47 | #include "common/xalloc.h" |
| 48 | #include "common/fdwatch.h" |
| 49 | #include "common/trans.h" |
| 50 | #include "common/give_up_root_privileges.h" |
| 51 | #include "common/version.h" |
| 52 | #include "common/hexdump.h" |
| 53 | |
| 54 | #include "server.h" |
| 55 | #include "prefs.h" |
| 56 | #include "cmdline.h" |
| 57 | #include "storage.h" |
| 58 | #include "support.h" |
| 59 | #include "anongame_maplists.h" |
| 60 | #include "anongame.h" |
| 61 | #include "connection.h" |
| 62 | #include "game.h" |
| 63 | #include "timer.h" |
| 64 | #include "channel.h" |
| 65 | #include "helpfile.h" |
| 66 | #include "ipban.h" |
| 67 | #include "adbanner.h" |
| 68 | #include "autoupdate.h" |
| 69 | #include "versioncheck.h" |
| 70 | #include "news.h" |
| 71 | #include "watch.h" |
| 72 | #include "output.h" |
| 73 | #include "attrlayer.h" |
| 74 | #include "account.h" |
| 75 | #include "ladder.h" |
| 76 | #include "character.h" |
| 77 | #include "tracker.h" |
| 78 | #include "command_groups.h" |
| 79 | #include "alias_command.h" |
| 80 | #include "tournament.h" |
| 81 | #include "icons.h" |
| 82 | #include "anongame_infos.h" |
| 83 | #include "anongame_wol.h" |
| 84 | #include "clan.h" |
| 85 | #include "team.h" |
| 86 | #include "realm.h" |
| 87 | #include "topic.h" |
| 88 | #include "handle_apireg.h" |
| 89 | #include "i18n.h" |
| 90 | #include "userlog.h" |
| 91 | #ifdef WIN32 |
| 92 | #include "win32/windump.h" |
| 93 | #endif |
| 94 | #include "common/setup_after.h" |
| 95 | |
| 96 | #ifdef WITH_LUA |
| 97 | #include "luainterface.h" |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | /* out of memory safety */ |
| 102 | #define OOM_SAFE_MEM 1000000 /* 1 Mbyte of safety memory */ |
| 103 | |
| 104 | using namespace pvpgn::bnetd; |
| 105 | using namespace pvpgn; |
| 106 | |
| 107 | static int * emergency_mem = new int[1048576]; // 1 MiB |
| 108 | void new_oom_handler() |
| 109 | { |
| 110 | delete[] emergency_mem; |
| 111 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory, forcing immediate shutdown"); |
| 112 | |
| 113 | std::abort(); |
| 114 | } |
| 115 | |
| 116 | // FIXME: Use new instead of malloc everywhere |
| 117 | |
| 118 | void *oom_buffer = NULL; |
| 119 | |
| 120 | static int bnetd_oom_handler(void) |
| 121 | { |
| 122 | /* no safety buffer, sorry :( */ |
| 123 | if (!oom_buffer) return 0; |
| 124 | |
| 125 | /* free the safety buffer hoping next allocs will succeed */ |
| 126 | free(oom_buffer); |
| 127 | oom_buffer = NULL; |
| 128 | |
| 129 | eventlog(eventlog_level_fatal, __FUNCTION__, "out of memory, forcing immediate shutdown"); |
| 130 | |
| 131 | /* shutdown immediatly */ |
| 132 | server_quit_delay(-1); |
| 133 | |
| 134 | return 1; /* ask xalloc codes to retry the allocation request */ |
| 135 | } |
| 136 | |
| 137 | static int oom_setup(void) |
| 138 | { |
| 139 | /* use calloc so it initilizez the memory so it will make some lazy |
| 140 | * allocators really alocate it (ex. the linux kernel) |
| 141 | */ |
| 142 | oom_buffer = calloc(1, OOM_SAFE_MEM); |
| 143 | if (!oom_buffer) return -1; |
| 144 | |
| 145 | xalloc_setcb(bnetd_oom_handler); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void oom_free(void) |
| 150 | { |
| 151 | free(oom_buffer); |
| 152 | oom_buffer = NULL; |
| 153 | } |
| 154 | |
| 155 | FILE *hexstrm = NULL; |
| 156 | |
| 157 | char serviceLongName[] = PVPGN_SOFTWARE " service"; |
| 158 | char serviceName[] = "pvpgn"; |
| 159 | char serviceDescription[] = "Player vs. Player Gaming Network - Server"; |
| 160 | |
| 161 | /* |
| 162 | * by quetzal. indicates service status |
| 163 | * -1 - not in service mode |
| 164 | * 0 - stopped |
| 165 | * 1 - running |
| 166 | * 2 - paused |
| 167 | */ |
| 168 | int g_ServiceStatus = -1; |
| 169 | |
| 170 | /* added some more std::exit status --> put in "compat/exitstatus.h" ??? */ |
| 171 | #define STATUS_OOM_FAILURE 20 |
| 172 | #define STATUS_STORAGE_FAILURE 30 |
| 173 | #define STATUS_PSOCK_FAILURE 35 |
| 174 | #define STATUS_MAPLISTS_FAILURE 40 |
| 175 | #define STATUS_MATCHLISTS_FAILURE 50 |
| 176 | #define STATUS_LADDERLIST_FAILURE 60 |
| 177 | #define STATUS_WAR3XPTABLES_FAILURE 70 |
| 178 | #define STATUS_SUPPORT_FAILURE 80 |
| 179 | #define STATUS_FDWATCH_FAILURE 90 |
| 180 | |
| 181 | int pre_server_startup(void); |
| 182 | void post_server_shutdown(int status); |
| 183 | int eventlog_startup(void); |
| 184 | int fork_bnetd(int foreground); |
| 185 | char * write_to_pidfile(void); |
| 186 | void pvpgn_greeting(); |
| 187 | |
| 188 | int eventlog_startup(void) |
| 189 | { |
| 190 | char const * levels; |
| 191 | char * temp; |
| 192 | char const * tok; |
| 193 | |
| 194 | eventlog_clear_level(); |
| 195 | if ((levels = prefs_get_loglevels())) { |
| 196 | temp = xstrdup(levels); |
| 197 | tok = std::strtok(temp, ","); /* std::strtok modifies the string it is passed */ |
| 198 | while (tok) { |
| 199 | if (eventlog_add_level(tok) < 0) |
| 200 | eventlog(eventlog_level_error, __FUNCTION__, "could not add std::log level \"{}\"", tok); |
| 201 | tok = std::strtok(NULL, ","); |
| 202 | } |
| 203 | xfree(temp); |
| 204 | } |
| 205 | |
| 206 | #ifdef WIN32_GUI |
| 207 | if (cmdline_get_gui()){ |
| 208 | eventlog_add_level(eventlog_get_levelname_str(eventlog_level_gui)); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | if (eventlog_open(prefs_get_logfile()) < 0) { |
| 213 | if (prefs_get_logfile()) { |
| 214 | eventlog(eventlog_level_fatal, __FUNCTION__, "could not use file \"{}\" for the eventlog (exiting)", prefs_get_logfile()); |
| 215 | } |
| 216 | else { |
| 217 | eventlog(eventlog_level_fatal, __FUNCTION__, "no logfile specified in configuration file \"{}\" (exiting)", cmdline_get_preffile()); |
| 218 | } |
| 219 | return -1; |
| 220 | } |
| 221 | eventlog(eventlog_level_info, __FUNCTION__, "logging event levels: {}", prefs_get_loglevels()); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | int fork_bnetd(int foreground) |
| 226 | { |
| 227 | #ifdef DO_DAEMONIZE |
| 228 | int pid; |
| 229 | |
| 230 | if (!foreground) { |
| 231 | if (chdir("/") < 0) { |
| 232 | eventlog(eventlog_level_error, __FUNCTION__, "could not change working directory to / (chdir: {})", std::strerror(errno)); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | switch ((pid = fork())) { |
| 237 | case -1: |
| 238 | eventlog(eventlog_level_error, __FUNCTION__, "could not fork (fork: {})", std::strerror(errno)); |
| 239 | return -1; |
| 240 | case 0: /* child */ |
| 241 | break; |
| 242 | default: /* parent */ |
| 243 | return pid; |
| 244 | } |
| 245 | #ifndef WITH_D2 |
| 246 | close(STDINFD); |
| 247 | close(STDOUTFD); |
| 248 | close(STDERRFD); |
| 249 | #endif |
| 250 | # ifdef HAVE_SETPGID |
| 251 | if (setpgid(0, 0) < 0) { |
| 252 | eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgid: {})", std::strerror(errno)); |
| 253 | return -1; |
| 254 | } |
| 255 | # else |
| 256 | # ifdef HAVE_SETPGRP |
| 257 | # ifdef SETPGRP_VOID |
| 258 | if (setpgrp() < 0) { |
| 259 | eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgrp: {})", std::strerror(errno)); |
| 260 | return -1; |
| 261 | } |
| 262 | # else |
| 263 | if (setpgrp(0, 0) < 0) { |
| 264 | eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgrp: {})", std::strerror(errno)); |
| 265 | return -1; |
| 266 | } |
| 267 | # endif |
| 268 | # else |
| 269 | # ifdef HAVE_SETSID |
| 270 | if (setsid() < 0) { |
| 271 | eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setsid: {})", std::strerror(errno)); |
| 272 | return -1; |
| 273 | } |
| 274 | # else |
| 275 | # error "One of setpgid(), setpgrp(), or setsid() is required" |
| 276 | # endif |
| 277 | # endif |
| 278 | # endif |
| 279 | } |
| 280 | return 0; |
| 281 | #endif |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | char * write_to_pidfile(void) |
| 286 | { |
| 287 | char *pidfile = xstrdup(prefs_get_pidfile()); |
| 288 | |
| 289 | if (pidfile) |
| 290 | { |
| 291 | if (pidfile[0] == '\0') { |
| 292 | xfree((void *)pidfile); /* avoid warning */ |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | #ifdef HAVE_GETPID |
| 297 | std::FILE * fp; |
| 298 | |
| 299 | if (!(fp = std::fopen(pidfile, "w"))) { |
| 300 | eventlog(eventlog_level_error, __FUNCTION__, "unable to open pid file \"{}\" for writing (std::fopen: {})", pidfile, std::strerror(errno)); |
| 301 | xfree((void *)pidfile); /* avoid warning */ |
| 302 | return NULL; |
| 303 | } |
| 304 | else { |
| 305 | std::fprintf(fp, "%u", (unsigned int)getpid()); |
| 306 | if (std::fclose(fp) < 0) |
| 307 | eventlog(eventlog_level_error, __FUNCTION__, "could not close pid file \"{}\" after writing (std::fclose: {})", pidfile, std::strerror(errno)); |
| 308 | } |
| 309 | #else |
| 310 | eventlog(eventlog_level_warn, __FUNCTION__, "no getpid() std::system call, disable pid file in bnetd.conf"); |
| 311 | xfree((void *)pidfile); /* avoid warning */ |
| 312 | return NULL; |
| 313 | #endif |
| 314 | } |
| 315 | return pidfile; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* Initialize config files */ |
| 320 | int pre_server_startup(void) |
| 321 | { |
| 322 | pvpgn_greeting(); |
| 323 | if (oom_setup() < 0) { |
| 324 | eventlog(eventlog_level_error, __FUNCTION__, "OOM init failed"); |
| 325 | return STATUS_OOM_FAILURE; |
| 326 | } |
| 327 | |
| 328 | std::set_new_handler(new_oom_handler); |
| 329 | |
| 330 | if (storage_init(prefs_get_storage_path()) < 0) { |
| 331 | eventlog(eventlog_level_error, "pre_server_startup", "storage init failed"); |
| 332 | return STATUS_STORAGE_FAILURE; |
| 333 | } |
| 334 | if (psock_init() < 0) { |
| 335 | eventlog(eventlog_level_error, __FUNCTION__, "could not initialize socket functions"); |
| 336 | return STATUS_PSOCK_FAILURE; |
| 337 | } |
| 338 | if (support_check_files(prefs_get_supportfile()) < 0) { |
| 339 | eventlog(eventlog_level_error, "pre_server_startup", "some needed files are missing"); |
| 340 | eventlog(eventlog_level_error, "pre_server_startup", "please make sure you installed the supportfiles in {}", prefs_get_filedir()); |
| 341 | return STATUS_SUPPORT_FAILURE; |
| 342 | } |
| 343 | if (anongame_maplists_create() < 0) { |
| 344 | eventlog(eventlog_level_error, "pre_server_startup", "could not load maps"); |
| 345 | return STATUS_MAPLISTS_FAILURE; |
| 346 | } |
| 347 | if (anongame_matchlists_create() < 0) { |
| 348 | eventlog(eventlog_level_error, "pre_server_startup", "could not create matchlists"); |
| 349 | return STATUS_MATCHLISTS_FAILURE; |
| 350 | } |
| 351 | if (fdwatch_init(prefs_get_max_connections())) { |
| 352 | eventlog(eventlog_level_error, __FUNCTION__, "error initilizing fdwatch"); |
| 353 | return STATUS_FDWATCH_FAILURE; |
| 354 | } |
| 355 | |
| 356 | i18n_load(); |
| 357 | |
| 358 | connlist_create(); |
| 359 | gamelist_create(); |
| 360 | timerlist_create(); |
| 361 | server_set_hostname(); |
| 362 | channellist_create(); |
| 363 | apireglist_create(); |
| 364 | if (helpfile_init(prefs_get_helpfile()) < 0) |
| 365 | eventlog(eventlog_level_error, __FUNCTION__, "could not load helpfile"); |
| 366 | ipbanlist_create(); |
| 367 | if (ipbanlist_load(prefs_get_ipbanfile()) < 0) |
| 368 | eventlog(eventlog_level_error, __FUNCTION__, "could not load IP ban list"); |
| 369 | |
| 370 | try |
| 371 | { |
| 372 | if (AdBannerList.is_loaded()) |
| 373 | { |
| 374 | AdBannerList.unload(); |
| 375 | } |
| 376 | |
| 377 | AdBannerList.load(prefs_get_adfile()); |
| 378 | } |
| 379 | catch (const std::exception& e) |
| 380 | { |
| 381 | eventlog(eventlog_level_error, __FUNCTION__, "{}", e.what()); |
| 382 | } |
| 383 | |
| 384 | if (autoupdate_load(prefs_get_mpqfile()) < 0) |
| 385 | eventlog(eventlog_level_error, __FUNCTION__, "could not load autoupdate list"); |
| 386 | |
| 387 | if (!load_versioncheck_conf(prefs_get_versioncheck_file())) |
| 388 | { |
| 389 | eventlog(eventlog_level_error, __FUNCTION__, "could not load versioncheck list"); |
| 390 | } |
| 391 | |
| 392 | if (news_load(prefs_get_newsfile()) < 0) |
| 393 | eventlog(eventlog_level_error, __FUNCTION__, "could not load news list"); |
| 394 | watchlist.reset(new WatchComponent()); |
| 395 | output_init(); |
| 396 | attrlayer_init(); |
| 397 | accountlist_create(); |
| 398 | if (ladder_createxptable(prefs_get_xplevel_file(), prefs_get_xpcalc_file()) < 0) { |
| 399 | eventlog(eventlog_level_error, "pre_server_startup", "could not load WAR3 xp calc tables"); |
| 400 | return STATUS_WAR3XPTABLES_FAILURE; |
| 401 | } |
| 402 | ladders.load(); |
| 403 | ladders.update(); |
| 404 | if (characterlist_create("") < 0) |
| 405 | eventlog(eventlog_level_error, __FUNCTION__, "could not load character list"); |
| 406 | if (prefs_get_track()) /* setup the tracking mechanism */ |
| 407 | tracker_set_servers(prefs_get_trackserv_addrs()); |
| 408 | if (command_groups_load(prefs_get_command_groups_file()) < 0) |
| 409 | eventlog(eventlog_level_error, __FUNCTION__, "could not load command_groups list"); |
| 410 | aliasfile_load(prefs_get_aliasfile()); |
| 411 | if (trans_load(prefs_get_transfile(), TRANS_BNETD) < 0) |
| 412 | eventlog(eventlog_level_error, __FUNCTION__, "could not load trans list"); |
| 413 | tournament_init(prefs_get_tournament_file()); |
| 414 | customicons_load(prefs_get_customicons_file()); |
| 415 | anongame_infos_load(prefs_get_anongame_infos_file()); |
| 416 | anongame_wol_matchlist_create(); |
| 417 | clanlist_load(); |
| 418 | teamlist_load(); |
| 419 | if (realmlist_create(prefs_get_realmfile()) < 0) |
| 420 | eventlog(eventlog_level_error, __FUNCTION__, "could not load realm list"); |
| 421 | //topiclist_load(std::string(prefs_get_topicfile())); |
| 422 | userlog_init(); |
| 423 | |
| 424 | #ifdef WITH_LUA |
| 425 | lua_load(prefs_get_scriptdir()); |
| 426 | #endif |
| 427 | |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | void post_server_shutdown(int status) |
| 433 | { |
| 434 | switch (status) |
| 435 | { |
| 436 | case 0: |
| 437 | //topiclist_unload(); |
| 438 | realmlist_destroy(); |
| 439 | teamlist_unload(); |
| 440 | clanlist_unload(); |
| 441 | tournament_destroy(); |
| 442 | customicons_unload(); |
| 443 | anongame_infos_unload(); |
| 444 | anongame_wol_matchlist_destroy(); |
| 445 | trans_unload(); |
| 446 | aliasfile_unload(); |
| 447 | command_groups_unload(); |
| 448 | tracker_set_servers(NULL); |
| 449 | characterlist_destroy(); |
| 450 | ladder_destroyxptable(); |
| 451 | case STATUS_WAR3XPTABLES_FAILURE: |
| 452 | |
| 453 | case STATUS_LADDERLIST_FAILURE: |
| 454 | ladders.save(); |
| 455 | accountlist_destroy(); |
| 456 | attrlayer_cleanup(); |
| 457 | watchlist.reset(); |
| 458 | news_unload(); |
| 459 | unload_versioncheck_conf(); |
| 460 | autoupdate_unload(); |
| 461 | ipbanlist_save(prefs_get_ipbanfile()); |
| 462 | ipbanlist_destroy(); |
| 463 | helpfile_unload(); |
| 464 | apireglist_destroy(); |
| 465 | channellist_destroy(); |
| 466 | server_clear_hostname(); |
| 467 | timerlist_destroy(); |
| 468 | gamelist_destroy(); |
| 469 | connlist_destroy(); |
| 470 | fdwatch_close(); |
| 471 | case STATUS_FDWATCH_FAILURE: |
| 472 | anongame_matchlists_destroy(); |
| 473 | case STATUS_MATCHLISTS_FAILURE: |
| 474 | anongame_maplists_destroy(); |
| 475 | case STATUS_MAPLISTS_FAILURE: |
| 476 | case STATUS_SUPPORT_FAILURE: |
| 477 | if (psock_deinit()) |
| 478 | eventlog(eventlog_level_error, __FUNCTION__, "got error from psock_deinit()"); |
| 479 | case STATUS_PSOCK_FAILURE: |
| 480 | storage_close(); |
| 481 | case STATUS_STORAGE_FAILURE: |
| 482 | oom_free(); |
| 483 | delete[] emergency_mem; |
| 484 | case STATUS_OOM_FAILURE: |
| 485 | case -1: |
| 486 | break; |
| 487 | default: |
| 488 | eventlog(eventlog_level_error, __FUNCTION__, "got bad status \"{}\" during shutdown", status); |
| 489 | } |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | void pvpgn_greeting() |
| 494 | { |
| 495 | #ifdef HAVE_GETPID |
| 496 | eventlog(eventlog_level_info, __FUNCTION__, PVPGN_SOFTWARE " " PVPGN_VERSION " process {}", getpid()); |
| 497 | #else |
| 498 | eventlog(eventlog_level_info, __FUNCTION__, PVPGN_SOFTWARE" version "PVPGN_VERSION); |
| 499 | #endif |
| 500 | |
| 501 | struct utsname utsbuf = {}; |
| 502 | if (uname(&utsbuf) == 0) |
| 503 | { |
| 504 | eventlog(eventlog_level_info, __FUNCTION__, "running on {} ({} {}, {})", utsbuf.sysname, utsbuf.version, utsbuf.release, utsbuf.machine); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | eventlog(eventlog_level_info, __FUNCTION__, "uname() failed"); |
| 509 | } |
| 510 | |
| 511 | #ifdef WIN32_GUI |
| 512 | gui_lvprintf(eventlog_level_info, "You are currently running " PVPGN_SOFTWARE " " PVPGN_VERSION "\n"); |
| 513 | gui_lvprintf(eventlog_level_info, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); |
| 514 | gui_lvprintf(eventlog_level_info, "If you need support:\n"); |
| 515 | gui_lvprintf(eventlog_level_info, " * Create an issue at https://github.com/pvpgn/pvpgn-server\n"); |
| 516 | gui_lvprintf(eventlog_level_info, "\nServer is now running.\n"); |
| 517 | gui_lvprintf(eventlog_level_info, "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); |
| 518 | #else |
| 519 | std::printf("You are currently running " PVPGN_SOFTWARE " " PVPGN_VERSION "\n"); |
| 520 | std::printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); |
| 521 | std::printf("If you need support:\n"); |
| 522 | std::printf(" * Create an issue at https://github.com/pvpgn/pvpgn-server\n"); |
| 523 | std::printf("\nServer is now running.\n"); |
| 524 | std::printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); |
| 525 | #endif |
| 526 | |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | #ifdef WIN32_GUI |
| 531 | extern int app_main(int argc, char ** argv) |
| 532 | #else |
| 533 | extern int main(int argc, char ** argv) |
| 534 | #endif |
| 535 | { |
| 536 | #ifdef WIN32 |
| 537 | // create a dump file whenever the gateway crashes |
| 538 | SetUnhandledExceptionFilter(unhandled_handler); |
| 539 | #endif |
| 540 | |
| 541 | try { |
| 542 | int a; |
| 543 | char *pidfile; |
| 544 | |
| 545 | if ((a = cmdline_load(argc, argv)) != 1) |
| 546 | return a; |
| 547 | |
| 548 | #ifdef DO_DAEMONIZE |
| 549 | if ((a = fork_bnetd(cmdline_get_foreground())) != 0) |
| 550 | return a < 0 ? a : 0; /* dont return code != 0 when things are OK! */ |
| 551 | #endif |
| 552 | |
| 553 | eventlog_set(stderr); |
| 554 | /* errors to eventlog from here on... */ |
| 555 | |
| 556 | if (prefs_load(cmdline_get_preffile()) < 0) { |
| 557 | eventlog(eventlog_level_fatal, __FUNCTION__, "could not parse configuration file (exiting)"); |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | /* Start logging to std::log file */ |
| 562 | if (eventlog_startup() == -1) |
| 563 | return -1; |
| 564 | /* eventlog goes to std::log file from here on... */ |
| 565 | |
| 566 | /* Give up root privileges */ |
| 567 | /* Hakan: That's way too late to give up root privileges... Have to look for a better place */ |
| 568 | if (give_up_root_privileges(prefs_get_effective_user(), prefs_get_effective_group()) < 0) { |
| 569 | eventlog(eventlog_level_fatal, __FUNCTION__, "could not give up privileges (exiting)"); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | /* Write the pidfile */ |
| 574 | pidfile = write_to_pidfile(); |
| 575 | |
| 576 | if (cmdline_get_hexfile()) { |
| 577 | if (!(hexstrm = std::fopen(cmdline_get_hexfile(), "w"))) |
| 578 | eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for writing the hexdump (std::fopen: {})", cmdline_get_hexfile(), std::strerror(errno)); |
| 579 | else |
| 580 | std::fprintf(hexstrm, "# dump generated by " PVPGN_SOFTWARE " version " PVPGN_VERSION "\n"); |
| 581 | } |
| 582 | |
| 583 | /* Run the pre server stuff */ |
| 584 | a = pre_server_startup(); |
| 585 | |
| 586 | /* now process connections and network traffic */ |
| 587 | if (a == 0) { |
| 588 | if (server_process() < 0) |
| 589 | eventlog(eventlog_level_fatal, __FUNCTION__, "failed to initialize network (exiting)"); |
| 590 | } |
| 591 | |
| 592 | // run post server stuff and exit |
| 593 | post_server_shutdown(a); |
| 594 | |
| 595 | // Close hexfile |
| 596 | if (hexstrm) { |
| 597 | std::fprintf(hexstrm, "# end of dump\n"); |
| 598 | if (std::fclose(hexstrm) < 0) |
| 599 | eventlog(eventlog_level_error, __FUNCTION__, "could not close hexdump file \"{}\" after writing (std::fclose: {})", cmdline_get_hexfile(), std::strerror(errno)); |
| 600 | } |
| 601 | |
| 602 | // Delete pidfile |
| 603 | if (pidfile) { |
| 604 | if (std::remove(pidfile) < 0) |
| 605 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove pid file \"{}\" (std::remove: {})", pidfile, std::strerror(errno)); |
| 606 | xfree((void *)pidfile); /* avoid warning */ |
| 607 | } |
| 608 | |
| 609 | if (a == 0) |
| 610 | eventlog(eventlog_level_info, __FUNCTION__, "server has shut down"); |
| 611 | prefs_unload(); |
| 612 | cmdline_unload(); |
| 613 | //guiOnClose |
| 614 | #ifndef WIN32_GUI |
| 615 | eventlog_close(); |
| 616 | #endif |
| 617 | |
| 618 | if (a == 0) |
| 619 | return 0; |
| 620 | |
| 621 | return -1; |
| 622 | } |
| 623 | catch (const std::exception& ex) { |
| 624 | std::cerr << "FATAL ERROR: " << ex.what() << std::endl; |
| 625 | } |
| 626 | catch (...) { |
| 627 | std::cerr << "UNKNOWN EXCEPTION TYPE" << std::endl; |
| 628 | } |
| 629 | return -1; |
| 630 | } |
| 631 |