sig.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // $Id: sig.c 1 2005-6-13 3:17:17 PM Celestia $
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include "../common/plugin.h"
  8. #include "../common/version.h"
  9. #include "../common/showmsg.h"
  10. PLUGIN_INFO = {
  11. "Signals",
  12. PLUGIN_CORE,
  13. "1.1",
  14. PLUGIN_VERSION,
  15. "Handles program signals"
  16. };
  17. PLUGIN_EVENTS_TABLE = {
  18. { "sig_init", "Plugin_Init" },
  19. { "sig_final", "Plugin_Final" },
  20. { NULL, NULL }
  21. };
  22. //////////////////////////////////////
  23. #if defined(_WIN32) || defined(MINGW)
  24. int sig_init() {
  25. ShowError("sig: This plugin is not supported - Enable 'exchndl' instead!");
  26. return 0;
  27. }
  28. int sig_final() { return 0; }
  29. #elif defined (__NETBSD__) || defined (__FREEBSD__)
  30. int sig_init() {
  31. ShowError("sig: This plugin is not supported!");
  32. return 0;
  33. }
  34. int sig_final() { return 0; }
  35. #else
  36. //////////////////////////////////////
  37. #if !defined(CYGWIN)
  38. #include <execinfo.h>
  39. #endif
  40. const char* (*getrevision)();
  41. unsigned long (*getuptime)();
  42. char *server_name;
  43. int crash_flag = 0;
  44. extern const char *strsignal(int);
  45. int sig_final ();
  46. // by Gabuzomeu
  47. // This is an implementation of signal() using sigaction() for portability.
  48. // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced
  49. // Programming in the UNIX Environment_.
  50. //
  51. #ifdef WIN32 // windows don't have SIGPIPE
  52. #define SIGPIPE SIGINT
  53. #endif
  54. #ifndef POSIX
  55. #define compat_signal(signo, func) signal(signo, func)
  56. #else
  57. sigfunc *compat_signal(int signo, sigfunc *func)
  58. {
  59. struct sigaction sact, oact;
  60. sact.sa_handler = func;
  61. sigemptyset(&sact.sa_mask);
  62. sact.sa_flags = 0;
  63. #ifdef SA_INTERRUPT
  64. sact.sa_flags |= SA_INTERRUPT; /* SunOS */
  65. #endif
  66. if (sigaction(signo, &sact, &oact) < 0)
  67. return (SIG_ERR);
  68. return (oact.sa_handler);
  69. }
  70. #endif
  71. /*=========================================
  72. * Dumps the stack using glibc's backtrace
  73. *-----------------------------------------
  74. */
  75. #ifdef CYGWIN
  76. #define FOPEN_ freopen
  77. extern void cygwin_stackdump();
  78. #else
  79. #define FOPEN_(fn,m,s) fopen(fn,m)
  80. #endif
  81. void sig_dump(int sn)
  82. {
  83. FILE *fp;
  84. char file[256];
  85. int no = 0;
  86. crash_flag = 1;
  87. // search for a usable filename
  88. do {
  89. sprintf (file, "log/%s%04d.stackdump", server_name, ++no);
  90. } while((fp = fopen(file,"r")) && (fclose(fp), no < 9999));
  91. // dump the trace into the file
  92. if ((fp = FOPEN_(file, "w", stderr)) != NULL) {
  93. const char *revision;
  94. #ifndef CYGWIN
  95. void* array[20];
  96. char **stack;
  97. size_t size;
  98. #endif
  99. ShowNotice ("Dumping stack to '"CL_WHITE"%s"CL_RESET"'...\n", file);
  100. if ((revision = getrevision()) != NULL)
  101. fprintf(fp, "Version: svn%s \n", revision);
  102. else
  103. fprintf(fp, "Version: %2d.%02d.%02d mod%02d \n", ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION, ATHENA_MOD_VERSION);
  104. fprintf(fp, "Exception: %s \n", strsignal(sn));
  105. fflush (fp);
  106. #ifdef CYGWIN
  107. cygwin_stackdump ();
  108. #else
  109. fprintf(fp, "Stack trace:\n");
  110. size = backtrace (array, 20);
  111. stack = backtrace_symbols (array, size);
  112. for (no = 0; no < size; no++) {
  113. fprintf(fp, "%s\n", stack[no]);
  114. }
  115. fprintf(fp,"End of stack trace\n");
  116. free(stack);
  117. #endif
  118. ShowNotice("%s Saved.\n", file);
  119. fflush(stdout);
  120. fclose(fp);
  121. }
  122. sig_final(); // Log our uptime
  123. // Pass the signal to the system's default handler
  124. compat_signal(sn, SIG_DFL);
  125. raise(sn);
  126. }
  127. /*=========================================
  128. * Shutting down (Program did not crash ^^)
  129. * - Log our current up time
  130. *-----------------------------------------
  131. */
  132. int sig_final ()
  133. {
  134. time_t curtime;
  135. char curtime2[24];
  136. FILE *fp;
  137. long seconds = 0, day = 24*60*60, hour = 60*60,
  138. minute = 60, days = 0, hours = 0, minutes = 0;
  139. fp = fopen("log/uptime.log","a");
  140. if (fp) {
  141. time(&curtime);
  142. strftime(curtime2, 24, "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  143. seconds = getuptime();
  144. days = seconds/day;
  145. seconds -= (seconds/day>0)?(seconds/day)*day:0;
  146. hours = seconds/hour;
  147. seconds -= (seconds/hour>0)?(seconds/hour)*hour:0;
  148. minutes = seconds/minute;
  149. seconds -= (seconds/minute>0)?(seconds/minute)*minute:0;
  150. fprintf(fp, "%s: %s %s - %ld days, %ld hours, %ld minutes, %ld seconds.\n",
  151. curtime2, server_name, (crash_flag ? "crashed" : "uptime"),
  152. days, hours, minutes, seconds);
  153. fclose(fp);
  154. }
  155. return 1;
  156. }
  157. /*=========================================
  158. * Register the signal handlers
  159. *-----------------------------------------
  160. */
  161. int sig_init ()
  162. {
  163. void (*func) = sig_dump;
  164. #ifdef CYGWIN // test if dumper is enabled
  165. char *buf = getenv ("CYGWIN");
  166. if (buf && strstr(buf, "error_start") != NULL)
  167. func = SIG_DFL;
  168. #endif
  169. IMPORT_SYMBOL(server_name, 1);
  170. IMPORT_SYMBOL(getrevision, 6);
  171. IMPORT_SYMBOL(getuptime, 11);
  172. compat_signal(SIGSEGV, func);
  173. compat_signal(SIGFPE, func);
  174. compat_signal(SIGILL, func);
  175. #ifndef __WIN32
  176. compat_signal(SIGBUS, func);
  177. #endif
  178. return 1;
  179. }
  180. #endif