debug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * debug.c -- handles:
  22. * signal handling
  23. * Context handling
  24. * debug funtions
  25. *
  26. */
  27. #include "common.h"
  28. #include "debug.h"
  29. #include "chanprog.h"
  30. #include "net.h"
  31. #include "shell.h"
  32. #include "color.h"
  33. #include "binary.h"
  34. #include "userrec.h"
  35. #include "main.h"
  36. #include "dccutil.h"
  37. #include <setjmp.h>
  38. #include <signal.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #include <unistd.h>
  45. #include <stdarg.h>
  46. #include <errno.h>
  47. #include <sys/mman.h>
  48. bool sdebug = 0; /* enable debug output? */
  49. bool segfaulted = 0;
  50. char get_buf[GET_BUFS][SGRAB + 5];
  51. size_t current_get_buf = 0;
  52. void setlimits()
  53. {
  54. struct rlimit plim, fdlim, corelim;
  55. #ifndef DEBUG
  56. /* struct rsslim, stacklim;
  57. rsslim.rlim_cur = 30720;
  58. rsslim.rlim_max = 30720;
  59. setrlimit(RLIMIT_RSS, &rsslim);
  60. stacklim.rlim_cur = 30720;
  61. stacklim.rlim_max = 30720;
  62. setrlimit(RLIMIT_STACK, &stacklim);
  63. */
  64. /* do NOT dump a core. */
  65. plim.rlim_cur = 60;
  66. plim.rlim_max = 60;
  67. corelim.rlim_cur = 0;
  68. corelim.rlim_max = 0;
  69. #else /* DEBUG */
  70. plim.rlim_cur = 500;
  71. plim.rlim_max = 500;
  72. corelim.rlim_cur = RLIM_INFINITY;
  73. corelim.rlim_max = RLIM_INFINITY;
  74. #endif /* !DEBUG */
  75. setrlimit(RLIMIT_CORE, &corelim);
  76. #ifndef __sun__
  77. setrlimit(RLIMIT_NPROC, &plim);
  78. #endif
  79. fdlim.rlim_cur = MAX_SOCKETS;
  80. fdlim.rlim_max = MAX_SOCKETS;
  81. setrlimit(RLIMIT_NOFILE, &fdlim);
  82. }
  83. void init_debug()
  84. {
  85. }
  86. void sdprintf (const char *format, ...)
  87. {
  88. char s[2001] = "";
  89. va_list va;
  90. va_start(va, format);
  91. egg_vsnprintf(s, sizeof(s), format, va);
  92. va_end(va);
  93. remove_crlf(s);
  94. ContextNote("dbg", s);
  95. if (sdebug) {
  96. if (!backgrd)
  97. dprintf(DP_STDOUT, "[D:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  98. else
  99. printf("[D:%lu] %s%s%s\n", (unsigned long) mypid, BOLD(-1), s, BOLD_END(-1));
  100. }
  101. #ifdef DEBUG
  102. logfile(LOG_DEBUG, s);
  103. #endif
  104. }
  105. static void write_debug(bool fatal = 1)
  106. {
  107. if (fatal) {
  108. segfaulted = 1;
  109. alarm(0); /* dont let anything jump out of this signal! */
  110. }
  111. putlog(LOG_MISC, "*", "** Paste to bryan:");
  112. const size_t cur_buf = (current_get_buf == 0) ? GET_BUFS - 1 : current_get_buf - 1;
  113. for (size_t i = 0; i < GET_BUFS; i++)
  114. putlog(LOG_MISC, "*", "%c %02zu: %s", i == cur_buf ? '*' : '_', i, get_buf[i]);
  115. putlog(LOG_MISC, "*", "** end");
  116. #ifdef DEBUG
  117. if (fatal) {
  118. /* Write GDB backtrace */
  119. char gdb[1024] = "", btfile[256] = "", std_in[101] = "", *out = NULL;
  120. unsigned int core = 0;
  121. simple_snprintf(btfile, sizeof(btfile), ".gdb-backtrace-%d", getpid());
  122. FILE *f = fopen(btfile, "w");
  123. if (f) {
  124. strlcpy(std_in, "bt 100\nbt 100 full\ndetach\nquit\n", sizeof(std_in));
  125. //simple_snprintf(stdin, sizeof(stdin), "detach\n");
  126. //simple_snprintf(stdin, sizeof(stdin), "q\n");
  127. simple_snprintf(gdb, sizeof(gdb), "gdb --pid=%d %s", getpid(), binname);
  128. shell_exec(gdb, std_in, &out, NULL);
  129. fprintf(f, "%s\n", out);
  130. fclose(f);
  131. free(out);
  132. }
  133. //enabling core dumps
  134. struct rlimit limit;
  135. if (!getrlimit(RLIMIT_CORE, &limit)) {
  136. limit.rlim_cur = limit.rlim_max;
  137. if(!setrlimit(RLIMIT_CORE, &limit)) core = limit.rlim_cur;
  138. }
  139. }
  140. #endif
  141. }
  142. #ifndef DEBUG
  143. static void got_bus(int) __attribute__ ((noreturn));
  144. #endif /* DEBUG */
  145. static void got_bus(int z)
  146. {
  147. signal(SIGBUS, SIG_DFL);
  148. write_debug();
  149. fatal("BUS ERROR -- CRASHING!", 1);
  150. #ifdef DEBUG
  151. raise(SIGBUS);
  152. #else
  153. exit(1);
  154. #endif /* DEBUG */
  155. }
  156. #ifndef DEBUG
  157. static void got_segv(int) __attribute__ ((noreturn));
  158. #endif /* DEBUG */
  159. static void got_segv(int z)
  160. {
  161. signal(SIGSEGV, SIG_DFL);
  162. write_debug();
  163. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  164. #ifdef DEBUG
  165. raise(SIGSEGV);
  166. #else
  167. exit(1);
  168. #endif /* DEBUG */
  169. }
  170. #ifndef DEBUG
  171. static void got_trap(int) __attribute__ ((noreturn));
  172. #endif /* DEBUG */
  173. static void got_trap(int z)
  174. {
  175. signal(SIGTRAP, SIG_DFL);
  176. write_debug();
  177. fatal("SIGTRAP Received!", 1);
  178. #ifdef DEBUG
  179. raise(SIGTRAP);
  180. #else
  181. exit(1);
  182. #endif /* DEBUG */
  183. }
  184. #ifndef DEBUG
  185. static void got_fpe(int) __attribute__ ((noreturn));
  186. #endif /* DEBUG */
  187. static void got_fpe(int z)
  188. {
  189. signal(SIGFPE, SIG_DFL);
  190. write_debug();
  191. fatal("FLOATING POINT ERROR -- CRASHING!", 1);
  192. #ifdef DEBUG
  193. raise(SIGFPE);
  194. #else
  195. exit(1);
  196. #endif /* DEBUG */
  197. }
  198. static void got_term(int) __attribute__ ((noreturn));
  199. static void got_term(int z)
  200. {
  201. write_userfile(-1);
  202. fatal("Received SIGTERM", 0);
  203. exit(1); /* for GCC noreturn */
  204. }
  205. #ifndef DEBUG
  206. static void got_abort(int) __attribute__ ((noreturn));
  207. #endif /* DEBUG */
  208. static void got_abort(int z)
  209. {
  210. signal(SIGABRT, SIG_DFL);
  211. write_debug();
  212. fatal("GOT SIGABRT -- CRASHING!", 1);
  213. #ifdef DEBUG
  214. raise(SIGABRT);
  215. #else
  216. exit(1);
  217. #endif /* DEBUG */
  218. }
  219. static void got_cont(int z)
  220. {
  221. detected(DETECT_HIJACK, "POSSIBLE HIJACK DETECTED (!! MAY BE BOX REBOOT !!)");
  222. }
  223. static void got_alarm(int) __attribute__((noreturn));
  224. static void got_alarm(int z)
  225. {
  226. sdprintf("SIGALARM");
  227. longjmp(alarmret, 1);
  228. /* -Never reached- */
  229. }
  230. /* Got ILL signal -- log context and continue
  231. */
  232. static void got_ill(int z)
  233. {
  234. write_debug(0);
  235. }
  236. static void
  237. got_hup(int z)
  238. {
  239. signal(SIGHUP, got_hup);
  240. putlog(LOG_MISC, "*", "GOT SIGHUP -- RESTARTING");
  241. do_restart = 1;
  242. // restart(-1);
  243. }
  244. static void
  245. got_usr1(int z)
  246. {
  247. signal(SIGUSR1, got_usr1);
  248. putlog(LOG_DEBUG, "*", "GOT SIGUSR1 -- RECHECKING BINARY");
  249. do_restart = 2;
  250. // reload_bin_data();
  251. }
  252. void got_int(int z)
  253. {
  254. signal(SIGINT, SIG_DFL);
  255. putlog(LOG_DEBUG, "*", "GOT SIGINT -- QUITTING");
  256. fatal("Received SIGINT", 0);
  257. sdprintf("HERE");
  258. exit(1);
  259. #ifdef DEBUG
  260. raise(SIGINT);
  261. #else
  262. exit(1);
  263. #endif /* DEBUG */
  264. }
  265. void init_signals()
  266. {
  267. signal(SIGBUS, got_bus);
  268. signal(SIGTRAP, got_trap);
  269. signal(SIGSEGV, got_segv);
  270. signal(SIGFPE, got_fpe);
  271. signal(SIGTERM, got_term);
  272. signal(SIGCONT, got_cont);
  273. signal(SIGABRT, got_abort);
  274. signal(SIGPIPE, SIG_IGN);
  275. signal(SIGILL, got_ill);
  276. signal(SIGALRM, got_alarm);
  277. signal(SIGHUP, got_hup);
  278. signal(SIGUSR1, got_usr1);
  279. signal(SIGINT, got_int);
  280. }