torerr.c 5.8 KB

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