core.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // $Id: core.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $
  2. // original : core.c 2003/02/26 18:03:12 Rev 1.7
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #ifndef _WIN32
  6. #include <unistd.h>
  7. #endif
  8. #include <signal.h>
  9. #include <string.h>
  10. #ifdef DUMPSTACK
  11. #ifndef CYGWIN // HAVE_EXECINFO_H
  12. #include <execinfo.h>
  13. #endif
  14. #endif
  15. #include "../common/mmo.h"
  16. #include "malloc.h"
  17. #include "core.h"
  18. #include "socket.h"
  19. #include "timer.h"
  20. #include "version.h"
  21. #include "showmsg.h"
  22. #ifdef MEMWATCH
  23. #include "memwatch.h"
  24. #endif
  25. static void (*term_func)(void)=NULL;
  26. /*======================================
  27. * CORE : Set function
  28. *--------------------------------------
  29. */
  30. void set_termfunc(void (*termfunc)(void))
  31. {
  32. term_func = termfunc;
  33. }
  34. // Added by Gabuzomeu
  35. //
  36. // This is an implementation of signal() using sigaction() for portability.
  37. // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced
  38. // Programming in the UNIX Environment_.
  39. //
  40. #ifndef SIGPIPE
  41. #define SIGPIPE SIGINT
  42. #endif
  43. #ifndef POSIX
  44. #define compat_signal(signo, func) signal(signo, func)
  45. #else
  46. sigfunc *compat_signal(int signo, sigfunc *func)
  47. {
  48. struct sigaction sact, oact;
  49. sact.sa_handler = func;
  50. sigemptyset(&sact.sa_mask);
  51. sact.sa_flags = 0;
  52. #ifdef SA_INTERRUPT
  53. sact.sa_flags |= SA_INTERRUPT; /* SunOS */
  54. #endif
  55. if (sigaction(signo, &sact, &oact) < 0)
  56. return (SIG_ERR);
  57. return (oact.sa_handler);
  58. }
  59. #endif
  60. /*======================================
  61. * CORE : Signal Sub Function
  62. *--------------------------------------
  63. */
  64. static void sig_proc(int sn)
  65. {
  66. int i;
  67. static int is_called = 0;
  68. if(is_called++)
  69. return;
  70. switch(sn){
  71. case SIGINT:
  72. case SIGTERM:
  73. if(term_func)
  74. term_func();
  75. for(i=0;i<fd_max;i++){
  76. if(!session[i])
  77. continue;
  78. close(i);
  79. }
  80. exit(0);
  81. break;
  82. }
  83. }
  84. /*=========================================
  85. * Dumps the stack using glibc's backtrace
  86. *-----------------------------------------
  87. */
  88. #ifdef CYGWIN
  89. #define sig_dump SIG_DFL // allow cygwin's default dumper utility to handle this
  90. #else
  91. static void sig_dump(int sn)
  92. {
  93. #ifdef DUMPSTACK
  94. FILE *fp;
  95. void* array[20];
  96. char **stack;
  97. size_t size;
  98. int no = 0;
  99. char tmp[256];
  100. // search for a usable filename
  101. do {
  102. sprintf(tmp,"log/stackdump_%04d.txt", ++no);
  103. } while((fp = fopen(tmp,"r")) && (fclose(fp), no < 9999));
  104. // dump the trace into the file
  105. if ((fp = fopen (tmp,"w")) != NULL) {
  106. fprintf(fp,"Exception: %s\n", strsignal(sn));
  107. fprintf(fp,"Stack trace:\n");
  108. size = backtrace (array, 20);
  109. stack = backtrace_symbols (array, size);
  110. for (no = 0; no < size; no++) {
  111. fprintf(fp, "%s\n", stack[no]);
  112. }
  113. fprintf(fp,"End of stack trace\n");
  114. fclose(fp);
  115. aFree(stack);
  116. }
  117. #endif
  118. //cygwin_stackdump ();
  119. // When pass the signal to the system's default handler
  120. compat_signal(sn, SIG_DFL);
  121. raise(sn);
  122. }
  123. #endif
  124. int get_svn_revision(char *svnentry) { // Warning: minor syntax checking
  125. char line[1024];
  126. int rev = 0;
  127. FILE *fp;
  128. if ((fp = fopen(svnentry, "r")) == NULL) {
  129. return 0;
  130. } else {
  131. while (fgets(line,1023,fp)) if (strstr(line,"revision=")) break;
  132. fclose(fp);
  133. if (sscanf(line," %*[^\"]\"%d%*[^\n]",&rev)==1)
  134. return rev;
  135. else
  136. return 0;
  137. }
  138. // return 0;
  139. }
  140. /*======================================
  141. * CORE : Display title
  142. *--------------------------------------
  143. */
  144. static void display_title(void)
  145. {
  146. int revision;
  147. // for help with the console colors look here:
  148. // http://www.edoceo.com/liberum/?doc=printf-with-color
  149. // some code explanation (used here):
  150. // \033[2J : clear screen and go up/left (0, 0 position)
  151. // \033[K : clear line from actual position to end of the line
  152. // \033[0m : reset color parameter
  153. // \033[1m : use bold for font
  154. printf("\033[2J"); // clear screen and go up/left (0, 0 position in text)
  155. printf("\033[37;44m (=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)\033[K\033[0m\n"); // white writing (37) on blue background (44), \033[K clean until end of file
  156. printf("\033[0;44m (\033[1;33m (c)2005 eAthena Development Team presents \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  157. printf("\033[0;44m (\033[1m ______ __ __ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  158. printf("\033[0;44m (\033[1m /\\ _ \\/\\ \\__/\\ \\ v%2d.%02d.%02d \033[0;44m)\033[K\033[0m\n", ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION); // 1: bold char, 0: normal char
  159. printf("\033[0;44m (\033[1m __\\ \\ \\_\\ \\ \\ ,_\\ \\ \\___ __ ___ __ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  160. printf("\033[0;44m (\033[1m /'__`\\ \\ __ \\ \\ \\/\\ \\ _ `\\ /'__`\\/' _ `\\ /'__`\\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  161. printf("\033[0;44m (\033[1m /\\ __/\\ \\ \\/\\ \\ \\ \\_\\ \\ \\ \\ \\/\\ __//\\ \\/\\ \\/\\ \\_\\.\\_ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  162. printf("\033[0;44m (\033[1m \\ \\____\\\\ \\_\\ \\_\\ \\__\\\\ \\_\\ \\_\\ \\____\\ \\_\\ \\_\\ \\__/.\\_\\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  163. printf("\033[0;44m (\033[1m \\/____/ \\/_/\\/_/\\/__/ \\/_/\\/_/\\/____/\\/_/\\/_/\\/__/\\/_/ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  164. printf("\033[0;44m (\033[1m _ _ _ _ _ _ _ _ _ _ _ _ _ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  165. printf("\033[0;44m (\033[1m / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  166. printf("\033[0;44m (\033[1m ( e | n | g | l | i | s | h ) ( A | t | h | e | n | a ) \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  167. printf("\033[0;44m (\033[1m \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  168. printf("\033[0;44m (\033[1m \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  169. printf("\033[0;44m (\033[1;33m Advanced Fusion Maps (c) 2003-2005 The Fusion Project \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  170. printf("\033[37;44m (=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)\033[K\033[0m\n\n"); // reset color
  171. if ((revision = get_svn_revision(".svn\\entries"))>0) {
  172. snprintf(tmp_output,sizeof(tmp_output),"SVN Revision: '"CL_WHITE"%d"CL_RESET"'.\n",revision);
  173. ShowInfo(tmp_output);
  174. }
  175. }
  176. /*======================================
  177. * CORE : MAINROUTINE
  178. *--------------------------------------
  179. */
  180. int runflag = 1;
  181. unsigned long ticks = 0; // by MC Cameri
  182. char pid_file[256];
  183. char server_type[24];
  184. void pid_delete(void) {
  185. unlink(pid_file);
  186. }
  187. void pid_create(const char* file) {
  188. FILE *fp;
  189. int len = strlen(file);
  190. strcpy(pid_file,file);
  191. if(len > 4 && pid_file[len - 4] == '.') {
  192. pid_file[len - 4] = 0;
  193. }
  194. strcat(pid_file,".pid");
  195. fp = fopen(pid_file,"w");
  196. if(fp) {
  197. #ifdef _WIN32
  198. fprintf(fp,"%d",GetCurrentProcessId());
  199. #else
  200. fprintf(fp,"%d",getpid());
  201. #endif
  202. fclose(fp);
  203. atexit(pid_delete);
  204. }
  205. }
  206. #define LOG_UPTIME 0
  207. void log_uptime()
  208. {
  209. #if LOG_UPTIME
  210. time_t curtime;
  211. char curtime2[24];
  212. FILE *fp;
  213. long seconds = 0, day = 24*60*60, hour = 60*60,
  214. minute = 60, days = 0, hours = 0, minutes = 0;
  215. fp = fopen("log/uptime.log","a");
  216. if (fp) {
  217. time(&curtime);
  218. strftime(curtime2, 24, "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  219. seconds = (gettick()-ticks)/CLOCKS_PER_SEC;
  220. days = seconds/day;
  221. seconds -= (seconds/day>0)?(seconds/day)*day:0;
  222. hours = seconds/hour;
  223. seconds -= (seconds/hour>0)?(seconds/hour)*hour:0;
  224. minutes = seconds/minute;
  225. seconds -= (seconds/minute>0)?(seconds/minute)*minute:0;
  226. fprintf(fp, "%s: %s uptime - %ld days, %ld hours, %ld minutes, %ld seconds.\n",
  227. curtime2, server_type, days, hours, minutes, seconds);
  228. fclose(fp);
  229. }
  230. return;
  231. #endif
  232. }
  233. int main(int argc,char **argv)
  234. {
  235. int next;
  236. sscanf(argv[0], "./%24[^\n]", server_type); // map/char/login?
  237. atexit(log_uptime);
  238. pid_create(argv[0]);
  239. Net_Init();
  240. do_socket();
  241. compat_signal(SIGPIPE,SIG_IGN);
  242. compat_signal(SIGTERM,sig_proc);
  243. compat_signal(SIGINT,sig_proc);
  244. // Signal to create coredumps by system when necessary (crash)
  245. compat_signal(SIGSEGV, sig_dump);
  246. compat_signal(SIGFPE, sig_dump);
  247. compat_signal(SIGILL, sig_dump);
  248. #ifndef _WIN32
  249. compat_signal(SIGBUS, sig_dump);
  250. compat_signal(SIGTRAP, SIG_DFL);
  251. #endif
  252. display_title();
  253. do_init_memmgr(argv[0]); // 一番最初に実行する必要がある
  254. tick_ = time(0);
  255. ticks = gettick();
  256. do_init(argc,argv);
  257. while(runflag){
  258. next=do_timer(gettick_nocache());
  259. do_sendrecv(next);
  260. do_parsepacket();
  261. }
  262. return 0;
  263. }