debug.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * debug.c -- handles:
  3. * signal handling
  4. * Context handling
  5. * debug funtions
  6. *
  7. */
  8. #include "common.h"
  9. #include "debug.h"
  10. #include "chanprog.h"
  11. #include "net.h"
  12. #include "shell.h"
  13. #include "color.h"
  14. #include "userrec.h"
  15. #include "main.h"
  16. #include "dccutil.h"
  17. #include <setjmp.h>
  18. #include <signal.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <sys/time.h>
  23. #include <sys/resource.h>
  24. #include <unistd.h>
  25. #include <stdarg.h>
  26. int sdebug = 0; /* enable debug output? */
  27. #ifdef DEBUG_CONTEXT
  28. /* Context storage for fatal crashes */
  29. char cx_file[16][15];
  30. char cx_note[16][21];
  31. int cx_line[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  32. int cx_ptr = 0;
  33. #endif /* DEBUG_CONTEXT */
  34. void setlimits()
  35. {
  36. #ifndef CYGWIN_HACKS
  37. #ifndef DEBUG_MEM
  38. struct rlimit plim, fdlim, corelim;
  39. /* struct rsslim, stacklim;
  40. rsslim.rlim_cur = 30720;
  41. rsslim.rlim_max = 30720;
  42. setrlimit(RLIMIT_RSS, &rsslim);
  43. stacklim.rlim_cur = 30720;
  44. stacklim.rlim_max = 30720;
  45. setrlimit(RLIMIT_STACK, &stacklim);
  46. */
  47. /* do NOT dump a core. */
  48. corelim.rlim_cur = 0;
  49. corelim.rlim_max = 0;
  50. setrlimit(RLIMIT_CORE, &corelim);
  51. plim.rlim_cur = 50;
  52. plim.rlim_max = 50;
  53. setrlimit(RLIMIT_NPROC, &plim);
  54. fdlim.rlim_cur = 200;
  55. fdlim.rlim_max = 200;
  56. setrlimit(RLIMIT_NOFILE, &fdlim);
  57. #else /* DEBUG_MEM */
  58. struct rlimit cdlim;
  59. cdlim.rlim_cur = RLIM_INFINITY;
  60. cdlim.rlim_max = RLIM_INFINITY;
  61. setrlimit(RLIMIT_CORE, &cdlim);
  62. #endif /* !DEBUG_MEM */
  63. #endif /* !CYGWIN_HACKS */
  64. }
  65. void init_debug()
  66. {
  67. int i = 0;
  68. for (i = 0; i < 16; i ++)
  69. Context;
  70. #ifdef DEBUG_CONTEXT
  71. egg_bzero(&cx_file, sizeof cx_file);
  72. egg_bzero(&cx_note, sizeof cx_note);
  73. #endif /* DEBUG_CONTEXT */
  74. }
  75. void sdprintf (char *format, ...)
  76. {
  77. if (sdebug) {
  78. char s[2001] = "";
  79. va_list va;
  80. va_start(va, format);
  81. egg_vsnprintf(s, 2000, format, va);
  82. va_end(va);
  83. if (!backgrd)
  84. dprintf(DP_STDOUT, "[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  85. else
  86. printf("[D:%d] %s%s%s\n", getpid(), BOLD(-1), s, BOLD_END(-1));
  87. }
  88. }
  89. #ifdef DEBUG_CONTEXT
  90. static int nested_debug = 0;
  91. void write_debug()
  92. {
  93. int x;
  94. char s[25] = "", tmpout[150] = "", buf[DIRMAX] = "";
  95. int y;
  96. if (nested_debug) {
  97. /* Yoicks, if we have this there's serious trouble!
  98. * All of these are pretty reliable, so we'll try these.
  99. *
  100. * NOTE: dont try and display context-notes in here, it's
  101. * _not_ safe <cybah>
  102. */
  103. sprintf(buf, "%sDEBUG.DEBUG", tempdir);
  104. x = creat(buf, 0600);
  105. setsock(x, SOCK_NONSOCK);
  106. if (x >= 0) {
  107. strncpyz(s, ctime(&now), sizeof s);
  108. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  109. dprintf(-x, "Context: ");
  110. cx_ptr = cx_ptr & 15;
  111. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  112. dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
  113. dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
  114. killsock(x);
  115. close(x);
  116. }
  117. #ifndef CYGWIN_HACKS
  118. {
  119. /* Use this lame method because shell_exec() or mail() may have caused another segfault :o */
  120. char buff[255] = "";
  121. egg_snprintf(buff, sizeof(buff), "cat << EOFF >> %sbleh\nDEBUG from: %s\n`date`\n`w`\n---\n`who`\n---\n`ls -al`\n---\n`ps ux`\n---\n`uname -a`\n---\n`id`\n---\n`cat %s`\nEOFF", tempdir, origbotname, buf);
  122. system(buff);
  123. egg_snprintf(buff, sizeof(buff), "cat %sbleh |mail wraith@shatow.net", tempdir);
  124. system(buff);
  125. unlink("bleh");
  126. }
  127. #endif /* !CYGWIN_HACKS */
  128. unlink(buf);
  129. exit(1); /* Dont even try & tell people about, that may
  130. have caused the fault last time. */
  131. } else {
  132. nested_debug = 1;
  133. }
  134. egg_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  135. cx_file[cx_ptr-2], cx_line[cx_ptr-2], cx_note[cx_ptr-2][0] ? cx_note[cx_ptr-2] : "",
  136. cx_file[cx_ptr-1], cx_line[cx_ptr-1], cx_note[cx_ptr-1][0] ? cx_note[cx_ptr-1] : "",
  137. cx_file[cx_ptr], cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
  138. putlog(LOG_MISC, "*", "%s", tmpout);
  139. printf("%s\n", tmpout);
  140. sprintf(buf, "%sDEBUG", tempdir);
  141. x = creat(buf, 0600);
  142. setsock(x, SOCK_NONSOCK);
  143. if (x < 0) {
  144. putlog(LOG_MISC, "*", "* Failed to write DEBUG");
  145. } else {
  146. char date[80] = "";
  147. strncpyz(s, ctime(&now), sizeof s);
  148. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  149. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  150. dprintf(-x, "Build: %s (%lu)\n", date, buildts);
  151. dprintf(-x, "Context: ");
  152. cx_ptr = cx_ptr & 15;
  153. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  154. dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
  155. (cx_note[y][0]) ? cx_note[y] : "");
  156. dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
  157. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  158. tell_dcc(-x);
  159. dprintf(-x, "\n");
  160. tell_netdebug(-x);
  161. killsock(x);
  162. close(x);
  163. #ifndef CYGWIN_HACKS
  164. {
  165. char *w = NULL, *who = NULL, *ps = NULL, *uname = NULL,
  166. *id = NULL, *ls = NULL, *debug = NULL, *msg = NULL, buf2[DIRMAX] = "";
  167. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  168. shell_exec("w", NULL, &w, NULL);
  169. shell_exec("who", NULL, &who, NULL);
  170. shell_exec("ps cux", NULL, &ps, NULL);
  171. shell_exec("uname -a", NULL, &uname, NULL);
  172. shell_exec("id", NULL, &id, NULL);
  173. shell_exec("ls -al", NULL, &ls, NULL);
  174. buf2[0] = 0;
  175. sprintf(buf2, "cat %s", buf);
  176. shell_exec(buf2, NULL, &debug, NULL);
  177. msg = calloc(1, strlen(date) + strlen(id) + strlen(uname) + strlen(w) + strlen(who) + strlen(ps) + strlen(ls) + strlen(debug) + 50);
  178. sprintf(msg, "%s\n%s\n%s\n\n%s\n%s\n\n%s\n\n-----%s-----\n\n\n\n%s", date, id, uname, w, who, ps, ls, debug);
  179. email("Debug output", msg, EMAIL_TEAM);
  180. free(msg);
  181. }
  182. putlog(LOG_MISC, "*", "* Emailed DEBUG to development team...");
  183. #endif /* !CYGWIN_HACKS */
  184. unlink(buf);
  185. }
  186. }
  187. #endif /* DEBUG_CONTEXT */
  188. static void got_bus(int z)
  189. {
  190. signal(SIGBUS, SIG_DFL);
  191. #ifdef DEBUG_CONTEXT
  192. write_debug();
  193. #endif
  194. fatal("BUS ERROR -- CRASHING!", 1);
  195. #ifdef DEBUG_MEM
  196. kill(getpid(), SIGBUS);
  197. #else
  198. exit(1);
  199. #endif /* DEBUG_MEM */
  200. }
  201. static void got_segv(int z)
  202. {
  203. signal(SIGSEGV, SIG_DFL);
  204. #ifdef DEBUG_CONTEXT
  205. write_debug();
  206. #endif
  207. fatal("SEGMENT VIOLATION -- CRASHING!", 1);
  208. #ifdef DEBUG_MEM
  209. kill(getpid(), SIGSEGV);
  210. #else
  211. exit(1);
  212. #endif /* DEBUG_MEM */
  213. }
  214. static void got_fpe(int z)
  215. {
  216. #ifdef DEBUG_CONTEXT
  217. write_debug();
  218. #endif
  219. fatal("FLOATING POINT ERROR -- CRASHING!", 0);
  220. }
  221. static void got_term(int z)
  222. {
  223. #ifdef HUB
  224. write_userfile(-1);
  225. #endif
  226. fatal("Received SIGTERM", 0);
  227. }
  228. static void got_abort(int z)
  229. {
  230. signal(SIGABRT, SIG_DFL);
  231. #ifdef DEBUG_CONTEXT
  232. write_debug();
  233. #endif
  234. fatal("GOT SIGABRT -- CRASHING!", 1);
  235. #ifdef DEBUG_MEM
  236. kill(getpid(), SIGSEGV);
  237. #else
  238. exit(1);
  239. #endif /* DEBUG_MEM */
  240. }
  241. #ifdef S_HIJACKCHECK
  242. static void got_cont(int z)
  243. {
  244. detected(DETECT_SIGCONT, "POSSIBLE HIJACK DETECTED");
  245. }
  246. #endif /* S_HIJACKCHECK */
  247. /* A call to resolver (gethostbyname, etc) timed out
  248. */
  249. static void got_alarm(int) __attribute__((noreturn));
  250. static void got_alarm(int z)
  251. {
  252. longjmp(alarmret, 1);
  253. /* -Never reached- */
  254. }
  255. /* Got ILL signal -- log context and continue
  256. */
  257. static void got_ill(int z)
  258. {
  259. #ifdef DEBUG_CONTEXT
  260. putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr], cx_line[cx_ptr],
  261. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  262. #endif /* DEBUG_CONTEXT */
  263. }
  264. void init_signals()
  265. {
  266. signal(SIGBUS, got_bus);
  267. signal(SIGSEGV, got_segv);
  268. signal(SIGFPE, got_fpe);
  269. signal(SIGTERM, got_term);
  270. #ifdef S_HIJACKCHECK
  271. signal(SIGCONT, got_cont);
  272. #endif /* S_HIJACKCHECK */
  273. signal(SIGABRT, got_abort);
  274. signal(SIGPIPE, SIG_IGN);
  275. signal(SIGILL, got_ill);
  276. signal(SIGALRM, got_alarm);
  277. }
  278. #ifdef DEBUG_CONTEXT
  279. /* Context */
  280. void eggContext(const char *file, int line, const char *module)
  281. {
  282. char x[31] = "", *p = NULL;
  283. p = strrchr(file, '/');
  284. if (!module) {
  285. strncpyz(x, p ? p + 1 : file, sizeof x);
  286. } else
  287. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  288. cx_ptr = ((cx_ptr + 1) & 15);
  289. strcpy(cx_file[cx_ptr], x);
  290. cx_line[cx_ptr] = line;
  291. cx_note[cx_ptr][0] = 0;
  292. }
  293. /* Called from the ContextNote macro.
  294. */
  295. void eggContextNote(const char *file, int line, const char *module, const char *note)
  296. {
  297. char x[31] = "", *p = NULL;
  298. p = strrchr(file, '/');
  299. if (!module) {
  300. strncpyz(x, p ? p + 1 : file, sizeof x);
  301. } else {
  302. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  303. }
  304. cx_ptr = ((cx_ptr + 1) & 15);
  305. strcpy(cx_file[cx_ptr], x);
  306. cx_line[cx_ptr] = line;
  307. strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  308. }
  309. #endif
  310. #ifdef DEBUG_ASSERT
  311. /* Called from the Assert macro.
  312. */
  313. void eggAssert(const char *file, int line, const char *module)
  314. {
  315. if (!module)
  316. putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
  317. else
  318. putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
  319. #ifdef DEBUG_CONTEXT
  320. write_debug();
  321. #endif /* DEBUG_CONTEXT */
  322. fatal("ASSERT FAILED -- CRASHING!", 1);
  323. }
  324. #endif /* DEBUG_ASSERT */