torerr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Set the granularity (in ms) to use when reporting fatal errors outside
  116. * the logging system.
  117. */
  118. void
  119. tor_log_sigsafe_err_set_granularity(int ms)
  120. {
  121. log_granularity = ms;
  122. }
  123. /**
  124. * Log an emergency assertion failure message.
  125. *
  126. * This kind of message is safe to send from within a log handler,
  127. * a signal handler, or other emergency situation.
  128. */
  129. void
  130. tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr,
  131. const char *msg)
  132. {
  133. char linebuf[16];
  134. format_dec_number_sigsafe(line, linebuf, sizeof(linebuf));
  135. tor_log_err_sigsafe("INTERNAL ERROR: Raw assertion failed at ",
  136. file, ":", linebuf, ": ", expr, NULL);
  137. if (msg) {
  138. tor_log_err_sigsafe_write(msg);
  139. tor_log_err_sigsafe_write("\n");
  140. }
  141. dump_stack_symbols_to_error_fds();
  142. }
  143. /* As format_{hex,dex}_number_sigsafe, but takes a <b>radix</b> argument
  144. * in range 2..16 inclusive. */
  145. static int
  146. format_number_sigsafe(unsigned long x, char *buf, int buf_len,
  147. unsigned int radix)
  148. {
  149. unsigned long tmp;
  150. int len;
  151. char *cp;
  152. /* NOT tor_assert. This needs to be safe to run from within a signal
  153. * handler, and from within the 'tor_assert() has failed' code. Not even
  154. * raw_assert(), since raw_assert() calls this function on failure. */
  155. if (radix < 2 || radix > 16)
  156. return 0;
  157. /* Count how many digits we need. */
  158. tmp = x;
  159. len = 1;
  160. while (tmp >= radix) {
  161. tmp /= radix;
  162. ++len;
  163. }
  164. /* Not long enough */
  165. if (!buf || len >= buf_len)
  166. return 0;
  167. cp = buf + len;
  168. *cp = '\0';
  169. do {
  170. unsigned digit = (unsigned) (x % radix);
  171. if (cp <= buf) {
  172. /* Not tor_assert(); see above. */
  173. abort();
  174. }
  175. --cp;
  176. *cp = "0123456789ABCDEF"[digit];
  177. x /= radix;
  178. } while (x);
  179. /* NOT tor_assert; see above. */
  180. if (cp != buf) {
  181. abort(); // LCOV_EXCL_LINE
  182. }
  183. return len;
  184. }
  185. /**
  186. * Helper function to output hex numbers from within a signal handler.
  187. *
  188. * Writes the nul-terminated hexadecimal digits of <b>x</b> into a buffer
  189. * <b>buf</b> of size <b>buf_len</b>, and return the actual number of digits
  190. * written, not counting the terminal NUL.
  191. *
  192. * If there is insufficient space, write nothing and return 0.
  193. *
  194. * This accepts an unsigned int because format_helper_exit_status() needs to
  195. * call it with a signed int and an unsigned char, and since the C standard
  196. * does not guarantee that an int is wider than a char (an int must be at
  197. * least 16 bits but it is permitted for a char to be that wide as well), we
  198. * can't assume a signed int is sufficient to accommodate an unsigned char.
  199. * Thus, format_helper_exit_status() will still need to emit any require '-'
  200. * on its own.
  201. *
  202. * For most purposes, you'd want to use tor_snprintf("%x") instead of this
  203. * function; it's designed to be used in code paths where you can't call
  204. * arbitrary C functions.
  205. */
  206. int
  207. format_hex_number_sigsafe(unsigned long x, char *buf, int buf_len)
  208. {
  209. return format_number_sigsafe(x, buf, buf_len, 16);
  210. }
  211. /** As format_hex_number_sigsafe, but format the number in base 10. */
  212. int
  213. format_dec_number_sigsafe(unsigned long x, char *buf, int buf_len)
  214. {
  215. return format_number_sigsafe(x, buf, buf_len, 10);
  216. }