core.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "mmo.h"
  4. #include "showmsg.h"
  5. #include "malloc.h"
  6. #include "core.h"
  7. #ifndef MINICORE
  8. #include "db.h"
  9. #include "socket.h"
  10. #include "timer.h"
  11. #include "thread.h"
  12. #include "mempool.h"
  13. #include "sql.h"
  14. #include "cbasetypes.h"
  15. #include "msg_conf.h"
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #ifndef _WIN32
  22. #include <unistd.h>
  23. #else
  24. #include "../common/winapi.h" // Console close event handling
  25. #endif
  26. /// Called when a terminate signal is received.
  27. void (*shutdown_callback)(void) = NULL;
  28. #if defined(BUILDBOT)
  29. int buildbotflag = 0;
  30. #endif
  31. int runflag = CORE_ST_RUN;
  32. int arg_c = 0;
  33. char **arg_v = NULL;
  34. char *SERVER_NAME = NULL;
  35. char SERVER_TYPE = ATHENA_SERVER_NONE;
  36. #ifndef MINICORE // minimalist Core
  37. // Added by Gabuzomeu
  38. //
  39. // This is an implementation of signal() using sigaction() for portability.
  40. // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced
  41. // Programming in the UNIX Environment_.
  42. //
  43. #ifdef WIN32 // windows don't have SIGPIPE
  44. #define SIGPIPE SIGINT
  45. #endif
  46. #ifndef POSIX
  47. #define compat_signal(signo, func) signal(signo, func)
  48. #else
  49. sigfunc *compat_signal(int signo, sigfunc *func)
  50. {
  51. struct sigaction sact, oact;
  52. sact.sa_handler = func;
  53. sigemptyset(&sact.sa_mask);
  54. sact.sa_flags = 0;
  55. #ifdef SA_INTERRUPT
  56. sact.sa_flags |= SA_INTERRUPT; /* SunOS */
  57. #endif
  58. if (sigaction(signo, &sact, &oact) < 0)
  59. return (SIG_ERR);
  60. return (oact.sa_handler);
  61. }
  62. #endif
  63. /*======================================
  64. * CORE : Console events for Windows
  65. *--------------------------------------*/
  66. #ifdef _WIN32
  67. static BOOL WINAPI console_handler(DWORD c_event)
  68. {
  69. switch(c_event)
  70. {
  71. case CTRL_CLOSE_EVENT:
  72. case CTRL_LOGOFF_EVENT:
  73. case CTRL_SHUTDOWN_EVENT:
  74. if( shutdown_callback != NULL )
  75. shutdown_callback();
  76. else
  77. runflag = CORE_ST_STOP;// auto-shutdown
  78. break;
  79. default:
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. static void cevents_init()
  85. {
  86. if (SetConsoleCtrlHandler(console_handler,TRUE)==FALSE)
  87. ShowWarning ("Unable to install the console handler!\n");
  88. }
  89. #endif
  90. /*======================================
  91. * CORE : Signal Sub Function
  92. *--------------------------------------*/
  93. static void sig_proc(int sn)
  94. {
  95. static int is_called = 0;
  96. switch (sn) {
  97. case SIGINT:
  98. case SIGTERM:
  99. if (++is_called > 3)
  100. exit(EXIT_SUCCESS);
  101. if( shutdown_callback != NULL )
  102. shutdown_callback();
  103. else
  104. runflag = CORE_ST_STOP;// auto-shutdown
  105. break;
  106. case SIGSEGV:
  107. case SIGFPE:
  108. do_abort();
  109. // Pass the signal to the system's default handler
  110. compat_signal(sn, SIG_DFL);
  111. raise(sn);
  112. break;
  113. #ifndef _WIN32
  114. case SIGXFSZ:
  115. // ignore and allow it to set errno to EFBIG
  116. ShowWarning ("Max file size reached!\n");
  117. //run_flag = 0; // should we quit?
  118. break;
  119. case SIGPIPE:
  120. //ShowInfo ("Broken pipe found... closing socket\n"); // set to eof in socket.c
  121. break; // does nothing here
  122. #endif
  123. }
  124. }
  125. void signals_init (void)
  126. {
  127. compat_signal(SIGTERM, sig_proc);
  128. compat_signal(SIGINT, sig_proc);
  129. #ifndef _DEBUG // need unhandled exceptions to debug on Windows
  130. compat_signal(SIGSEGV, sig_proc);
  131. compat_signal(SIGFPE, sig_proc);
  132. #endif
  133. #ifndef _WIN32
  134. compat_signal(SIGILL, SIG_DFL);
  135. compat_signal(SIGXFSZ, sig_proc);
  136. compat_signal(SIGPIPE, sig_proc);
  137. compat_signal(SIGBUS, SIG_DFL);
  138. compat_signal(SIGTRAP, SIG_DFL);
  139. #endif
  140. }
  141. #endif
  142. #ifdef SVNVERSION
  143. const char *get_svn_revision(void)
  144. {
  145. return EXPAND_AND_QUOTE(SVNVERSION);
  146. }
  147. #else// not SVNVERSION
  148. const char* get_svn_revision(void)
  149. {
  150. static char svn_version_buffer[16] = "";
  151. FILE *fp;
  152. if( svn_version_buffer[0] != '\0' )
  153. return svn_version_buffer;
  154. // subversion 1.7 uses a sqlite3 database
  155. // FIXME this is hackish at best...
  156. // - ignores database file structure
  157. // - assumes the data in NODES.dav_cache column ends with "!svn/ver/<revision>/<path>)"
  158. // - since it's a cache column, the data might not even exist
  159. if( (fp = fopen(".svn"PATHSEP_STR"wc.db", "rb")) != NULL || (fp = fopen(".."PATHSEP_STR".svn"PATHSEP_STR"wc.db", "rb")) != NULL )
  160. {
  161. #ifndef SVNNODEPATH
  162. //not sure how to handle branches, so i'll leave this overridable define until a better solution comes up
  163. #define SVNNODEPATH trunk
  164. #endif
  165. const char* prefix = "!svn/ver/";
  166. const char* postfix = "/"EXPAND_AND_QUOTE(SVNNODEPATH)")"; // there should exist only 1 entry like this
  167. size_t prefix_len = strlen(prefix);
  168. size_t postfix_len = strlen(postfix);
  169. size_t i,j,len;
  170. char* buffer;
  171. // read file to buffer
  172. fseek(fp, 0, SEEK_END);
  173. len = ftell(fp);
  174. buffer = (char*)aMalloc(len + 1);
  175. fseek(fp, 0, SEEK_SET);
  176. len = fread(buffer, 1, len, fp);
  177. buffer[len] = '\0';
  178. fclose(fp);
  179. // parse buffer
  180. for( i = prefix_len + 1; i + postfix_len <= len; ++i )
  181. {
  182. if( buffer[i] != postfix[0] || memcmp(buffer + i, postfix, postfix_len) != 0 )
  183. continue; // postfix missmatch
  184. for( j = i; j > 0; --j )
  185. {// skip digits
  186. if( !ISDIGIT(buffer[j - 1]) )
  187. break;
  188. }
  189. if( memcmp(buffer + j - prefix_len, prefix, prefix_len) != 0 )
  190. continue; // prefix missmatch
  191. // done
  192. snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", atoi(buffer + j));
  193. break;
  194. }
  195. aFree(buffer);
  196. if( svn_version_buffer[0] != '\0' )
  197. return svn_version_buffer;
  198. }
  199. // subversion 1.6 and older?
  200. if ((fp = fopen(".svn/entries", "r")) != NULL)
  201. {
  202. char line[1024];
  203. int rev;
  204. // Check the version
  205. if (fgets(line, sizeof(line), fp))
  206. {
  207. if(!ISDIGIT(line[0]))
  208. {
  209. // XML File format
  210. while (fgets(line,sizeof(line),fp))
  211. if (strstr(line,"revision=")) break;
  212. if (sscanf(line," %*[^\"]\"%d%*[^\n]", &rev) == 1) {
  213. snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", rev);
  214. }
  215. }
  216. else
  217. {
  218. // Bin File format
  219. if ( fgets(line, sizeof(line), fp) == NULL ) { printf("Can't get bin name\n"); } // Get the name
  220. if ( fgets(line, sizeof(line), fp) == NULL ) { printf("Can't get entries kind\n"); } // Get the entries kind
  221. if(fgets(line, sizeof(line), fp)) // Get the rev numver
  222. {
  223. snprintf(svn_version_buffer, sizeof(svn_version_buffer), "%d", atoi(line));
  224. }
  225. }
  226. }
  227. fclose(fp);
  228. if( svn_version_buffer[0] != '\0' )
  229. return svn_version_buffer;
  230. }
  231. // fallback
  232. snprintf(svn_version_buffer, sizeof(svn_version_buffer), "Unknown");
  233. return svn_version_buffer;
  234. }
  235. #endif
  236. /*======================================
  237. * CORE : Display title
  238. * ASCII By CalciumKid 1/12/2011
  239. *--------------------------------------*/
  240. static void display_title(void) {
  241. //ClearScreen(); // clear screen and go up/left (0, 0 position in text)
  242. ShowMessage("\n");
  243. ShowMessage(""CL_PASS" "CL_BOLD" "CL_PASS""CL_CLL""CL_NORMAL"\n");
  244. ShowMessage(""CL_PASS" "CL_BT_WHITE" rAthena Development Team presents "CL_PASS""CL_CLL""CL_NORMAL"\n");
  245. ShowMessage(""CL_PASS" "CL_BOLD" ___ __ __ "CL_PASS""CL_CLL""CL_NORMAL"\n");
  246. ShowMessage(""CL_PASS" "CL_BOLD" _____/ | / /_/ /_ ___ ____ ____ _ "CL_PASS""CL_CLL""CL_NORMAL"\n");
  247. ShowMessage(""CL_PASS" "CL_BOLD" / ___/ /| |/ __/ __ \\/ _ \\/ __ \\/ __ `/ "CL_PASS""CL_CLL""CL_NORMAL"\n");
  248. ShowMessage(""CL_PASS" "CL_BOLD" / / / ___ / /_/ / / / __/ / / / /_/ / "CL_PASS""CL_CLL""CL_NORMAL"\n");
  249. ShowMessage(""CL_PASS" "CL_BOLD" /_/ /_/ |_\\__/_/ /_/\\___/_/ /_/\\__,_/ "CL_PASS""CL_CLL""CL_NORMAL"\n");
  250. ShowMessage(""CL_PASS" "CL_BOLD" "CL_PASS""CL_CLL""CL_NORMAL"\n");
  251. ShowMessage(""CL_PASS" "CL_GREEN" http://rathena.org/board/ "CL_PASS""CL_CLL""CL_NORMAL"\n");
  252. ShowMessage(""CL_PASS" "CL_BOLD" "CL_PASS""CL_CLL""CL_NORMAL"\n");
  253. ShowInfo("SVN Revision: '"CL_WHITE"%s"CL_RESET"'.\n", get_svn_revision());
  254. }
  255. // Warning if executed as superuser (root)
  256. void usercheck(void)
  257. {
  258. #ifndef _WIN32
  259. if (geteuid() == 0) {
  260. ShowWarning ("You are running rAthena with root privileges, it is not necessary.\n");
  261. }
  262. #endif
  263. }
  264. /*======================================
  265. * CORE : MAINROUTINE
  266. *--------------------------------------*/
  267. int main (int argc, char **argv)
  268. {
  269. {// initialize program arguments
  270. char *p1 = SERVER_NAME = argv[0];
  271. char *p2 = p1;
  272. while ((p1 = strchr(p2, '/')) != NULL || (p1 = strchr(p2, '\\')) != NULL)
  273. {
  274. SERVER_NAME = ++p1;
  275. p2 = p1;
  276. }
  277. arg_c = argc;
  278. arg_v = argv;
  279. }
  280. malloc_init();// needed for Show* in display_title() [FlavioJS]
  281. #ifdef MINICORE // minimalist Core
  282. display_title();
  283. usercheck();
  284. do_init(argc,argv);
  285. do_final();
  286. #else// not MINICORE
  287. set_server_type();
  288. display_title();
  289. usercheck();
  290. Sql_Init();
  291. rathread_init();
  292. mempool_init();
  293. db_init();
  294. signals_init();
  295. #ifdef _WIN32
  296. cevents_init();
  297. #endif
  298. timer_init();
  299. socket_init();
  300. do_init(argc,argv);
  301. {// Main runtime cycle
  302. int next;
  303. while (runflag != CORE_ST_STOP) {
  304. next = do_timer(gettick_nocache());
  305. do_sockets(next);
  306. }
  307. }
  308. do_final();
  309. timer_final();
  310. socket_final();
  311. db_final();
  312. mempool_final();
  313. rathread_final();
  314. #endif
  315. malloc_final();
  316. return 0;
  317. }