debug.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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][30];
  30. char cx_note[16][256];
  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, STR("Debug (%s) written %s\n"), ver, s);
  109. dprintf(-x, STR("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. {
  118. /* Use this lame method because shell_exec() or mail() may have caused another segfault :o */
  119. char buff[255] = "";
  120. 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);
  121. system(buff);
  122. egg_snprintf(buff, sizeof(buff), "cat %sbleh |mail wraith@shatow.net", tempdir);
  123. system(buff);
  124. unlink("bleh");
  125. }
  126. unlink(buf);
  127. exit(1); /* Dont even try & tell people about, that may
  128. have caused the fault last time. */
  129. } else {
  130. nested_debug = 1;
  131. }
  132. egg_snprintf(tmpout, sizeof tmpout, "* Last 3 contexts: %s/%d [%s], %s/%d [%s], %s/%d [%s]",
  133. cx_file[cx_ptr-2], cx_line[cx_ptr-2], cx_note[cx_ptr-2][0] ? cx_note[cx_ptr-2] : "",
  134. cx_file[cx_ptr-1], cx_line[cx_ptr-1], cx_note[cx_ptr-1][0] ? cx_note[cx_ptr-1] : "",
  135. cx_file[cx_ptr], cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
  136. putlog(LOG_MISC, "*", "%s", tmpout);
  137. printf("%s\n", tmpout);
  138. sprintf(buf, "%sDEBUG", tempdir);
  139. x = creat(buf, 0600);
  140. setsock(x, SOCK_NONSOCK);
  141. if (x < 0) {
  142. putlog(LOG_MISC, "*", "* Failed to write DEBUG");
  143. } else {
  144. char date[80] = "";
  145. strncpyz(s, ctime(&now), sizeof s);
  146. dprintf(-x, "Debug (%s) written %s\n", ver, s);
  147. egg_strftime(date, sizeof date, "%c %Z", gmtime(&buildts));
  148. dprintf(-x, "Build: %s (%lu)\n", date, buildts);
  149. dprintf(-x, "Context: ");
  150. cx_ptr = cx_ptr & 15;
  151. for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
  152. dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
  153. (cx_note[y][0]) ? cx_note[y] : "");
  154. dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
  155. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  156. tell_dcc(-x);
  157. dprintf(-x, "\n");
  158. tell_netdebug(-x);
  159. killsock(x);
  160. close(x);
  161. {
  162. char date[81] = "", *w = NULL, *who = NULL, *ps = NULL, *uname = NULL,
  163. *id = NULL, *ls = NULL, *debug = NULL, *msg = NULL, buf2[DIRMAX] = "";
  164. egg_strftime(date, sizeof date, "%c %Z", gmtime(&now));
  165. shell_exec("w", NULL, &w, NULL);
  166. shell_exec("who", NULL, &who, NULL);
  167. shell_exec("ps cux", NULL, &ps, NULL);
  168. shell_exec("uname -a", NULL, &uname, NULL);
  169. shell_exec("id", NULL, &id, NULL);
  170. shell_exec("ls -al", NULL, &ls, NULL);
  171. buf2[0] = 0;
  172. sprintf(buf2, "cat %s", buf);
  173. shell_exec(buf2, NULL, &debug, NULL);
  174. msg = malloc(strlen(date) + strlen(id) + strlen(uname) + strlen(w) + strlen(who) + strlen(ps) + strlen(ls) + strlen(debug) + 50);
  175. 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);
  176. email("Debug output", msg, EMAIL_TEAM);
  177. free(msg);
  178. }
  179. unlink(buf);
  180. putlog(LOG_MISC, "*", "* Emailed DEBUG to development team...");
  181. }
  182. }
  183. #endif
  184. static void got_bus(int z)
  185. {
  186. signal(SIGBUS, SIG_DFL);
  187. #ifdef DEBUG_CONTEXT
  188. write_debug();
  189. #endif
  190. fatal(STR("BUS ERROR -- CRASHING!"), 1);
  191. kill(getpid(), SIGBUS);
  192. }
  193. static void got_segv(int z)
  194. {
  195. signal(SIGSEGV, SIG_DFL);
  196. #ifdef DEBUG_CONTEXT
  197. write_debug();
  198. #endif
  199. fatal(STR("SEGMENT VIOLATION -- CRASHING!"), 1);
  200. kill(getpid(), SIGSEGV);
  201. }
  202. static void got_fpe(int z)
  203. {
  204. #ifdef DEBUG_CONTEXT
  205. write_debug();
  206. #endif
  207. fatal(STR("FLOATING POINT ERROR -- CRASHING!"), 0);
  208. }
  209. static void got_term(int z)
  210. {
  211. #ifdef HUB
  212. write_userfile(-1);
  213. #endif
  214. putlog(LOG_MISC, "*", STR("RECEIVED TERMINATE SIGNAL (IGNORING)"));
  215. }
  216. static void got_abort(int z)
  217. {
  218. signal(SIGABRT, SIG_DFL);
  219. #ifdef DEBUG_CONTEXT
  220. write_debug();
  221. #endif
  222. fatal(STR("GOT SIGABRT -- CRASHING!"), 1);
  223. kill(getpid(), SIGSEGV);
  224. }
  225. #ifdef S_HIJACKCHECK
  226. static void got_cont(int z)
  227. {
  228. detected(DETECT_SIGCONT, STR("POSSIBLE HIJACK DETECTED"));
  229. }
  230. #endif
  231. /* A call to resolver (gethostbyname, etc) timed out
  232. */
  233. static void got_alarm(int z)
  234. {
  235. longjmp(alarmret, 1);
  236. /* -Never reached- */
  237. }
  238. /* Got ILL signal -- log context and continue
  239. */
  240. static void got_ill(int z)
  241. {
  242. #ifdef DEBUG_CONTEXT
  243. putlog(LOG_MISC, "*", STR("* Context: %s/%d [%s]"), cx_file[cx_ptr], cx_line[cx_ptr],
  244. (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
  245. #endif
  246. }
  247. void init_signals()
  248. {
  249. signal(SIGBUS, got_bus);
  250. signal(SIGSEGV, got_segv);
  251. signal(SIGFPE, got_fpe);
  252. signal(SIGTERM, got_term);
  253. #ifdef S_HIJACKCHECK
  254. signal(SIGCONT, got_cont);
  255. #endif /* S_HIJACKCHECK */
  256. signal(SIGABRT, got_abort);
  257. signal(SIGPIPE, SIG_IGN);
  258. signal(SIGILL, got_ill);
  259. signal(SIGALRM, got_alarm);
  260. }
  261. #ifdef DEBUG_CONTEXT
  262. /* Context */
  263. void eggContext(const char *file, int line, const char *module)
  264. {
  265. char x[31] = "", *p = NULL;
  266. p = strrchr(file, '/');
  267. if (!module) {
  268. strncpyz(x, p ? p + 1 : file, sizeof x);
  269. } else
  270. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  271. cx_ptr = ((cx_ptr + 1) & 15);
  272. strcpy(cx_file[cx_ptr], x);
  273. cx_line[cx_ptr] = line;
  274. cx_note[cx_ptr][0] = 0;
  275. }
  276. /* Called from the ContextNote macro.
  277. */
  278. void eggContextNote(const char *file, int line, const char *module, const char *note)
  279. {
  280. char x[31] = "", *p = NULL;
  281. p = strrchr(file, '/');
  282. if (!module) {
  283. strncpyz(x, p ? p + 1 : file, sizeof x);
  284. } else {
  285. egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
  286. }
  287. cx_ptr = ((cx_ptr + 1) & 15);
  288. strcpy(cx_file[cx_ptr], x);
  289. cx_line[cx_ptr] = line;
  290. strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
  291. }
  292. #endif
  293. #ifdef DEBUG_ASSERT
  294. /* Called from the Assert macro.
  295. */
  296. void eggAssert(const char *file, int line, const char *module)
  297. {
  298. if (!module)
  299. putlog(LOG_MISC, "*", STR("* In file %s, line %u"), file, line);
  300. else
  301. putlog(LOG_MISC, "*", STR("* In file %s:%s, line %u"), module, file, line);
  302. #ifdef DEBUG_CONTEXT
  303. write_debug();
  304. #endif /* DEBUG_CONTEXT */
  305. fatal(STR("ASSERT FAILED -- CRASHING!"), 1);
  306. }
  307. #endif /* DEBUG_ASSERT */