diff options
Diffstat (limited to 'src/common/core.c')
-rw-r--r-- | src/common/core.c | 248 |
1 files changed, 78 insertions, 170 deletions
diff --git a/src/common/core.c b/src/common/core.c index c53d2243b..99dbc36ec 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -2,32 +2,41 @@ // See the LICENSE file // Portions Copyright (c) Athena Dev Teams +#define HERCULES_CORE + +#include "../config/core.h" +#include "core.h" + +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../common/cbasetypes.h" +#include "../common/console.h" +#include "../common/malloc.h" #include "../common/mmo.h" +#include "../common/random.h" #include "../common/showmsg.h" -#include "../common/malloc.h" #include "../common/strlib.h" -#include "core.h" -#include "../common/console.h" +#include "../common/sysinfo.h" #ifndef MINICORE - #include "../common/db.h" - #include "../common/socket.h" - #include "../common/timer.h" - #include "../common/thread.h" - #include "../common/mempool.h" - #include "../common/sql.h" - #include "../config/core.h" - #include "../common/HPM.h" +# include "../common/HPM.h" +# include "../common/conf.h" +# include "../common/db.h" +# include "../common/ers.h" +# include "../common/socket.h" +# include "../common/sql.h" +# include "../common/thread.h" +# include "../common/timer.h" +# include "../common/utils.h" #endif -#include <stdio.h> -#include <stdlib.h> -#include <signal.h> -#include <string.h> #ifndef _WIN32 -#include <unistd.h> +# include <unistd.h> #else -#include "../common/winapi.h" // Console close event handling +# include "../common/winapi.h" // Console close event handling #endif /// Called when a terminate signal is received. @@ -51,7 +60,7 @@ char *SERVER_NAME = NULL; #endif #ifndef POSIX -#define compat_signal(signo, func) signal(signo, func) +#define compat_signal(signo, func) signal((signo), (func)) #else sigfunc *compat_signal(int signo, sigfunc *func) { struct sigaction sact, oact; @@ -75,7 +84,7 @@ sigfunc *compat_signal(int signo, sigfunc *func) { *--------------------------------------*/ #ifdef _WIN32 static BOOL WINAPI console_handler(DWORD c_event) { - switch(c_event) { + switch(c_event) { case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: @@ -86,11 +95,11 @@ static BOOL WINAPI console_handler(DWORD c_event) { break; default: return FALSE; - } - return TRUE; + } + return TRUE; } -static void cevents_init() { +static void cevents_init(void) { if (SetConsoleCtrlHandler(console_handler,TRUE)==FALSE) ShowWarning ("Unable to install the console handler!\n"); } @@ -149,152 +158,37 @@ void signals_init (void) { } #endif -#ifdef SVNVERSION -const char *get_svn_revision(void) { - return EXPAND_AND_QUOTE(SVNVERSION); -} -#else// not SVNVERSION -const char* get_svn_revision(void) { - static char svn_version_buffer[16] = ""; - FILE *fp; - - if( svn_version_buffer[0] != '\0' ) - return svn_version_buffer; - - // subversion 1.7 uses a sqlite3 database - // FIXME this is hackish at best... - // - ignores database file structure - // - assumes the data in NODES.dav_cache column ends with "!svn/ver/<revision>/<path>)" - // - since it's a cache column, the data might not even exist - if( (fp = fopen(".svn"PATHSEP_STR"wc.db", "rb")) != NULL || (fp = fopen(".."PATHSEP_STR".svn"PATHSEP_STR"wc.db", "rb")) != NULL ) - { - #ifndef SVNNODEPATH - //not sure how to handle branches, so i'll leave this overridable define until a better solution comes up - #define SVNNODEPATH trunk - #endif - const char* prefix = "!svn/ver/"; - const char* postfix = "/"EXPAND_AND_QUOTE(SVNNODEPATH)")"; // there should exist only 1 entry like this - size_t prefix_len = strlen(prefix); - size_t postfix_len = strlen(postfix); - size_t i,j,len; - char* buffer; - - // read file to buffer - fseek(fp, 0, SEEK_END); - len = ftell(fp); - buffer = (char*)aMalloc(len + 1); - fseek(fp, 0, SEEK_SET); - len = fread(buffer, 1, len, fp); - buffer[len] = '\0'; - fclose(fp); - - // parse buffer - for( i = prefix_len + 1; i + postfix_len <= len; ++i ) { - if( buffer[i] != postfix[0] || memcmp(buffer + i, postfix, postfix_len) != 0 ) - continue; // postfix missmatch - for( j = i; j > 0; --j ) {// skip digits - if( !ISDIGIT(buffer[j - 1]) ) - break; - } - if( memcmp(buffer + j - prefix_len, prefix, prefix_len) != 0 ) - continue; // prefix missmatch - // done - snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", atoi(buffer + j)); - break; - } - aFree(buffer); - - if( svn_version_buffer[0] != '\0' ) - return svn_version_buffer; - } - - // subversion 1.6 and older? - if ((fp = fopen(".svn/entries", "r")) != NULL) { - char line[1024]; - int rev; - // Check the version - if (fgets(line, sizeof(line), fp)) { - if(!ISDIGIT(line[0])) { - // XML File format - while (fgets(line,sizeof(line),fp)) - if (strstr(line,"revision=")) break; - if (sscanf(line," %*[^\"]\"%d%*[^\n]", &rev) == 1) { - snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", rev); - } - } else { - // Bin File format - if ( fgets(line, sizeof(line), fp) == NULL ) { printf("Can't get bin name\n"); } // Get the name - if ( fgets(line, sizeof(line), fp) == NULL ) { printf("Can't get entries kind\n"); } // Get the entries kind - if(fgets(line, sizeof(line), fp)) { // Get the rev numver - snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", atoi(line)); - } - } - } - fclose(fp); - - if( svn_version_buffer[0] != '\0' ) - return svn_version_buffer; - } - - // fallback - svn_version_buffer[0] = HERC_UNKNOWN_VER; - return svn_version_buffer; -} -#endif -/* whats our origin */ -#define GIT_ORIGIN "refs/remotes/origin/master" -/* Grabs the hash from the last time the user updated his working copy (last pull) */ -const char *get_git_hash (void) { - static char HerculesGitHash[41] = "";//Sha(40) + 1 - FILE *fp; - - if( HerculesGitHash[0] != '\0' ) - return HerculesGitHash; - - if ( (fp = fopen (".git/"GIT_ORIGIN, "r")) != NULL) { - char line[64]; - char *rev = malloc (sizeof (char) * 50); - - if (fgets (line, sizeof (line), fp) && sscanf (line, "%50s", rev)) - snprintf (HerculesGitHash, sizeof (HerculesGitHash), "%s", rev); - - free (rev); - fclose (fp); - } else { - HerculesGitHash[0] = HERC_UNKNOWN_VER; - } - - if (! (*HerculesGitHash)) { - HerculesGitHash[0] = HERC_UNKNOWN_VER; - } - - return HerculesGitHash; -} -// Warning if executed as superuser (root) +/** + * Warns the user if executed as superuser (root) + */ void usercheck(void) { -#ifndef _WIN32 - if (geteuid() == 0) { - ShowWarning ("You are running Hercules with root privileges, it is not necessary.\n"); - } -#endif + if (sysinfo->is_superuser()) { + ShowWarning("You are running Hercules with root privileges, it is not necessary.\n"); + } } + void core_defaults(void) { #ifndef MINICORE hpm_defaults(); + HCache_defaults(); #endif + sysinfo_defaults(); console_defaults(); strlib_defaults(); malloc_defaults(); #ifndef MINICORE + libconfig_defaults(); sql_defaults(); timer_defaults(); db_defaults(); + socket_defaults(); #endif } /*====================================== * CORE : MAINROUTINE *--------------------------------------*/ int main (int argc, char **argv) { + int retval = EXIT_SUCCESS; {// initialize program arguments char *p1 = SERVER_NAME = argv[0]; char *p2 = p1; @@ -306,22 +200,33 @@ int main (int argc, char **argv) { arg_v = argv; } core_defaults(); + + { + int i; + for(i = 0; i < argc; i++) { + if( strcmp(argv[i], "--script-check") == 0 ) { + msg_silent = 0x7; // silence information and status messages + } + } + } iMalloc->init();// needed for Show* in display_title() [FlavioJS] - - console->display_title(); - -#ifdef MINICORE // minimalist Core + + sysinfo->init(); + + if (!(msg_silent&0x1)) + console->display_title(); + usercheck(); + +#ifdef MINICORE // minimalist Core do_init(argc,argv); do_final(); #else// not MINICORE set_server_type(); - usercheck(); Sql_Init(); rathread_init(); - mempool_init(); DB->init(); signals_init(); @@ -329,39 +234,42 @@ int main (int argc, char **argv) { cevents_init(); #endif - iTimer->init(); + timer->init(); + /* timer first */ + rnd_init(); + srand((unsigned int)timer->gettick()); + console->init(); - -#ifndef MINICORE + + HCache->init(); + HPM->init(); -#endif - - socket_init(); + + sockt->init(); do_init(argc,argv); {// Main runtime cycle int next; while (runflag != CORE_ST_STOP) { - next = iTimer->do_timer(iTimer->gettick_nocache()); - do_sockets(next); + next = timer->perform(timer->gettick_nocache()); + sockt->perform(next); } } console->final(); - do_final(); -#ifndef MINICORE + retval = do_final(); HPM->final(); -#endif - iTimer->final(); - socket_final(); + timer->final(); + sockt->final(); DB->final(); - mempool_final(); rathread_final(); + ers_final(); #endif + //sysinfo->final(); Called by iMalloc->final() iMalloc->final(); - return 0; + return retval; } |