// $Id: core.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $ // original : core.c 2003/02/26 18:03:12 Rev 1.7 #include #include #ifndef _WIN32 #include #endif #include #include #ifdef DUMPSTACK #ifndef CYGWIN // HAVE_EXECINFO_H #include #endif #endif #include "../common/mmo.h" #include "malloc.h" #include "core.h" #include "socket.h" #include "timer.h" #include "version.h" #include "showmsg.h" #ifdef MEMWATCH #include "memwatch.h" #endif char server_type[24]; int runflag = 1; unsigned long ticks = 0; // by MC Cameri char pid_file[256]; static void (*term_func)(void)=NULL; /*====================================== * CORE : Set function *-------------------------------------- */ void set_termfunc(void (*termfunc)(void)) { term_func = termfunc; } // Added by Gabuzomeu // // This is an implementation of signal() using sigaction() for portability. // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced // Programming in the UNIX Environment_. // #ifndef SIGPIPE #define SIGPIPE SIGINT #endif #ifndef POSIX #define compat_signal(signo, func) signal(signo, func) #else sigfunc *compat_signal(int signo, sigfunc *func) { struct sigaction sact, oact; sact.sa_handler = func; sigemptyset(&sact.sa_mask); sact.sa_flags = 0; #ifdef SA_INTERRUPT sact.sa_flags |= SA_INTERRUPT; /* SunOS */ #endif if (sigaction(signo, &sact, &oact) < 0) return (SIG_ERR); return (oact.sa_handler); } #endif /*====================================== * CORE : Signal Sub Function *-------------------------------------- */ // for handling certain signals ourselves, like SIGPIPE static void sig_ignore(int sn) { printf ("Broken pipe found... closing socket\n"); // set to eof in socket.c return; // does nothing here } static void sig_proc(int sn) { int i; static int is_called = 0; if(is_called++) return; switch(sn){ case SIGINT: case SIGTERM: if(term_func) term_func(); for(i=0;i0) { snprintf(tmp_output,sizeof(tmp_output),"SVN Revision: '"CL_WHITE"%d"CL_RESET"'.\n",revision); ShowInfo(tmp_output); } } /*====================================== * CORE : MAINROUTINE *-------------------------------------- */ void pid_delete(void) { unlink(pid_file); } void pid_create(const char* file) { FILE *fp; int len = strlen(file); strcpy(pid_file,file); if(len > 4 && pid_file[len - 4] == '.') { pid_file[len - 4] = 0; } strcat(pid_file,".pid"); fp = fopen(pid_file,"w"); if(fp) { #ifdef _WIN32 fprintf(fp,"%d",GetCurrentProcessId()); #else fprintf(fp,"%d",getpid()); #endif fclose(fp); atexit(pid_delete); } } #define LOG_UPTIME 0 void log_uptime() { #if LOG_UPTIME time_t curtime; char curtime2[24]; FILE *fp; long seconds = 0, day = 24*60*60, hour = 60*60, minute = 60, days = 0, hours = 0, minutes = 0; fp = fopen("log/uptime.log","a"); if (fp) { time(&curtime); strftime(curtime2, 24, "%m/%d/%Y %H:%M:%S", localtime(&curtime)); seconds = (gettick()-ticks)/CLOCKS_PER_SEC; days = seconds/day; seconds -= (seconds/day>0)?(seconds/day)*day:0; hours = seconds/hour; seconds -= (seconds/hour>0)?(seconds/hour)*hour:0; minutes = seconds/minute; seconds -= (seconds/minute>0)?(seconds/minute)*minute:0; fprintf(fp, "%s: %s uptime - %ld days, %ld hours, %ld minutes, %ld seconds.\n", curtime2, server_type, days, hours, minutes, seconds); fclose(fp); } return; #endif } int main(int argc,char **argv) { int next; display_title(); // call this first so it'll be finalised last do_init_memmgr(argv[0]); // 一番最初に実行する必要がある sscanf(argv[0], "./%24[^\n]", server_type); // map/char/login? atexit(log_uptime); pid_create(argv[0]); Net_Init(); do_socket(); compat_signal(SIGPIPE, sig_ignore); compat_signal(SIGTERM,sig_proc); compat_signal(SIGINT,sig_proc); // Signal to create coredumps by system when necessary (crash) compat_signal(SIGSEGV, sig_dump); compat_signal(SIGFPE, sig_dump); compat_signal(SIGILL, sig_dump); #ifndef _WIN32 compat_signal(SIGBUS, sig_dump); compat_signal(SIGTRAP, SIG_DFL); #endif tick_ = time(0); ticks = gettick(); do_init(argc,argv); while(runflag){ next=do_timer(gettick_nocache()); do_sendrecv(next); do_parsepacket(); } return 0; }