torerr.c 6.0 KB

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