torerr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file torerr.c
  8. *
  9. * \brief Handling code for unrecoverable emergencies, at a lower level
  10. * than the logging code.
  11. *
  12. * There are plenty of places that things can go wrong in Tor's backend
  13. * libraries: the allocator can fail, the locking subsystem can fail, and so
  14. * on. But since these subsystems are used themselves by the logging module,
  15. * they can't use the logging code directly to report their errors.
  16. *
  17. * As a workaround, the logging code provides this module with a set of raw
  18. * fds to be used for reporting errors in the lowest-level Tor code.
  19. */
  20. #include "orconfig.h"
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #ifdef HAVE_SYS_TIME_H
  26. #include <sys/time.h>
  27. #endif
  28. #ifdef HAVE_TIME_H
  29. #include <time.h>
  30. #endif
  31. #ifdef HAVE_UNISTD_H
  32. #include <unistd.h>
  33. #endif
  34. #ifdef HAVE_SYS_TYPES_H
  35. #include <sys/types.h>
  36. #endif
  37. #include "lib/err/torerr.h"
  38. #include "lib/err/backtrace.h"
  39. /** Array of fds to log crash-style warnings to. */
  40. static int sigsafe_log_fds[TOR_SIGSAFE_LOG_MAX_FDS] = { STDERR_FILENO };
  41. /** The number of elements used in sigsafe_log_fds */
  42. static int n_sigsafe_log_fds = 1;
  43. /** Log granularity in milliseconds. */
  44. static int log_granularity = 1000;
  45. /** Write <b>s</b> to each element of sigsafe_log_fds. Return 0 on success, -1
  46. * on failure. */
  47. static int
  48. tor_log_err_sigsafe_write(const char *s)
  49. {
  50. int i;
  51. ssize_t r;
  52. size_t len = strlen(s);
  53. int err = 0;
  54. for (i=0; i < n_sigsafe_log_fds; ++i) {
  55. r = write(sigsafe_log_fds[i], s, len);
  56. err += (r != (ssize_t)len);
  57. }
  58. return err ? -1 : 0;
  59. }
  60. /** Given a list of string arguments ending with a NULL, writes them
  61. * to our logs and to stderr (if possible). This function is safe to call
  62. * from within a signal handler. */
  63. void
  64. tor_log_err_sigsafe(const char *m, ...)
  65. {
  66. va_list ap;
  67. const char *x;
  68. char timebuf[33];
  69. time_t now = time(NULL);
  70. if (!m)
  71. return;
  72. if (log_granularity >= 2000) {
  73. int g = log_granularity / 1000;
  74. now -= now % g;
  75. }
  76. timebuf[0] = now < 0 ? '-' : ' ';
  77. if (now < 0) now = -now;
  78. timebuf[1] = '\0';
  79. format_dec_number_sigsafe(now, timebuf+1, sizeof(timebuf)-1);
  80. tor_log_err_sigsafe_write("\n=========================================="
  81. "================== T=");
  82. tor_log_err_sigsafe_write(timebuf);
  83. tor_log_err_sigsafe_write("\n");
  84. tor_log_err_sigsafe_write(m);
  85. va_start(ap, m);
  86. while ((x = va_arg(ap, const char*))) {
  87. tor_log_err_sigsafe_write(x);
  88. }
  89. va_end(ap);
  90. }
  91. /** Set *<b>out</b> to a pointer to an array of the fds to log errors to from
  92. * inside a signal handler or other emergency condition. Return the number of
  93. * elements in the array. */
  94. int
  95. tor_log_get_sigsafe_err_fds(const int **out)
  96. {
  97. *out = sigsafe_log_fds;
  98. return n_sigsafe_log_fds;
  99. }
  100. /**
  101. * Update the list of fds that get errors from inside a signal handler or
  102. * other emergency condition. Ignore any beyond the first
  103. * TOR_SIGSAFE_LOG_MAX_FDS.
  104. */
  105. void
  106. tor_log_set_sigsafe_err_fds(const int *fds, int n)
  107. {
  108. if (n > TOR_SIGSAFE_LOG_MAX_FDS) {
  109. n = TOR_SIGSAFE_LOG_MAX_FDS;
  110. }
  111. memcpy(sigsafe_log_fds, fds, n * sizeof(int));
  112. n_sigsafe_log_fds = n;
  113. }
  114. /**
  115. * Reset the list of emergency error fds to its default.
  116. */
  117. void
  118. tor_log_reset_sigsafe_err_fds(void)
  119. {
  120. int fds[] = { STDERR_FILENO };
  121. tor_log_set_sigsafe_err_fds(fds, 1);
  122. }
  123. /**
  124. * Set the granularity (in ms) to use when reporting fatal errors outside
  125. * the logging system.
  126. */
  127. void
  128. tor_log_sigsafe_err_set_granularity(int ms)
  129. {
  130. log_granularity = ms;
  131. }
  132. /**
  133. * Log an emergency assertion failure message.
  134. *
  135. * This kind of message is safe to send from within a log handler,
  136. * a signal handler, or other emergency situation.
  137. */
  138. void
  139. tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr,
  140. const char *msg)
  141. {
  142. char linebuf[16];
  143. format_dec_number_sigsafe(line, linebuf, sizeof(linebuf));
  144. tor_log_err_sigsafe("INTERNAL ERROR: Raw assertion failed at ",
  145. file, ":", linebuf, ": ", expr, NULL);
  146. if (msg) {
  147. tor_log_err_sigsafe_write(msg);
  148. tor_log_err_sigsafe_write("\n");
  149. }
  150. dump_stack_symbols_to_error_fds();
  151. }
  152. /* As format_{hex,dex}_number_sigsafe, but takes a <b>radix</b> argument
  153. * in range 2..16 inclusive. */
  154. static int
  155. format_number_sigsafe(unsigned long x, char *buf, int buf_len,
  156. unsigned int radix)
  157. {
  158. unsigned long tmp;
  159. int len;
  160. char *cp;
  161. /* NOT tor_assert. This needs to be safe to run from within a signal
  162. * handler, and from within the 'tor_assert() has failed' code. Not even
  163. * raw_assert(), since raw_assert() calls this function on failure. */
  164. if (radix < 2 || radix > 16)
  165. return 0;
  166. /* Count how many digits we need. */
  167. tmp = x;
  168. len = 1;
  169. while (tmp >= radix) {
  170. tmp /= radix;
  171. ++len;
  172. }
  173. /* Not long enough */
  174. if (!buf || len >= buf_len)
  175. return 0;
  176. cp = buf + len;
  177. *cp = '\0';
  178. do {
  179. unsigned digit = (unsigned) (x % radix);
  180. if (cp <= buf) {
  181. /* Not tor_assert(); see above. */
  182. abort();
  183. }
  184. --cp;
  185. *cp = "0123456789ABCDEF"[digit];
  186. x /= radix;
  187. } while (x);
  188. /* NOT tor_assert; see above. */
  189. if (cp != buf) {
  190. abort(); // LCOV_EXCL_LINE
  191. }
  192. return len;
  193. }
  194. /**
  195. * Helper function to output hex numbers from within a signal handler.
  196. *
  197. * Writes the nul-terminated hexadecimal digits of <b>x</b> into a buffer
  198. * <b>buf</b> of size <b>buf_len</b>, and return the actual number of digits
  199. * written, not counting the terminal NUL.
  200. *
  201. * If there is insufficient space, write nothing and return 0.
  202. *
  203. * This accepts an unsigned int because format_helper_exit_status() needs to
  204. * call it with a signed int and an unsigned char, and since the C standard
  205. * does not guarantee that an int is wider than a char (an int must be at
  206. * least 16 bits but it is permitted for a char to be that wide as well), we
  207. * can't assume a signed int is sufficient to accommodate an unsigned char.
  208. * Thus, format_helper_exit_status() will still need to emit any require '-'
  209. * on its own.
  210. *
  211. * For most purposes, you'd want to use tor_snprintf("%x") instead of this
  212. * function; it's designed to be used in code paths where you can't call
  213. * arbitrary C functions.
  214. */
  215. int
  216. format_hex_number_sigsafe(unsigned long x, char *buf, int buf_len)
  217. {
  218. return format_number_sigsafe(x, buf, buf_len, 16);
  219. }
  220. /** As format_hex_number_sigsafe, but format the number in base 10. */
  221. int
  222. format_dec_number_sigsafe(unsigned long x, char *buf, int buf_len)
  223. {
  224. return format_number_sigsafe(x, buf, buf_len, 10);
  225. }