core.c 9.1 KB

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