log.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file log.c
  8. * \brief Functions to send messages to log files or the console.
  9. **/
  10. #include "orconfig.h"
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. // #include <stdio.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. #ifdef HAVE_FCNTL_H
  29. #include <fcntl.h>
  30. #endif
  31. #include "compat.h"
  32. #include "util.h"
  33. #define LOG_PRIVATE
  34. #include "torlog.h"
  35. #include "container.h"
  36. /** Given a severity, yields an index into log_severity_list_t.masks to use
  37. * for that severity. */
  38. #define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
  39. /** @{ */
  40. /** The string we stick at the end of a log message when it is too long,
  41. * and its length. */
  42. #define TRUNCATED_STR "[...truncated]"
  43. #define TRUNCATED_STR_LEN 14
  44. /** @} */
  45. /** Information for a single logfile; only used in log.c */
  46. typedef struct logfile_t {
  47. struct logfile_t *next; /**< Next logfile_t in the linked list. */
  48. char *filename; /**< Filename to open. */
  49. int fd; /**< fd to receive log messages, or -1 for none. */
  50. int seems_dead; /**< Boolean: true if the stream seems to be kaput. */
  51. int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
  52. int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
  53. int is_syslog; /**< Boolean: send messages to syslog. */
  54. log_callback callback; /**< If not NULL, send messages to this function. */
  55. log_severity_list_t *severities; /**< Which severity of messages should we
  56. * log for each log domain? */
  57. } logfile_t;
  58. static void log_free(logfile_t *victim);
  59. /** Helper: map a log severity to descriptive string. */
  60. static INLINE const char *
  61. sev_to_string(int severity)
  62. {
  63. switch (severity) {
  64. case LOG_DEBUG: return "debug";
  65. case LOG_INFO: return "info";
  66. case LOG_NOTICE: return "notice";
  67. case LOG_WARN: return "warn";
  68. case LOG_ERR: return "err";
  69. default: /* Call assert, not tor_assert, since tor_assert
  70. * calls log on failure. */
  71. assert(0); return "UNKNOWN";
  72. }
  73. }
  74. /** Helper: decide whether to include the function name in the log message. */
  75. static INLINE int
  76. should_log_function_name(log_domain_mask_t domain, int severity)
  77. {
  78. switch (severity) {
  79. case LOG_DEBUG:
  80. case LOG_INFO:
  81. /* All debugging messages occur in interesting places. */
  82. return (domain & LD_NOFUNCNAME) == 0;
  83. case LOG_NOTICE:
  84. case LOG_WARN:
  85. case LOG_ERR:
  86. /* We care about places where bugs occur. */
  87. return (domain & (LD_BUG|LD_NOFUNCNAME)) == LD_BUG;
  88. default:
  89. /* Call assert, not tor_assert, since tor_assert calls log on failure. */
  90. assert(0); return 0;
  91. }
  92. }
  93. /** A mutex to guard changes to logfiles and logging. */
  94. static tor_mutex_t log_mutex;
  95. /** True iff we have initialized log_mutex */
  96. static int log_mutex_initialized = 0;
  97. /** Linked list of logfile_t. */
  98. static logfile_t *logfiles = NULL;
  99. /** Boolean: do we report logging domains? */
  100. static int log_domains_are_logged = 0;
  101. #ifdef HAVE_SYSLOG_H
  102. /** The number of open syslog log handlers that we have. When this reaches 0,
  103. * we can close our connection to the syslog facility. */
  104. static int syslog_count = 0;
  105. #endif
  106. /** Represents a log message that we are going to send to callback-driven
  107. * loggers once we can do so in a non-reentrant way. */
  108. typedef struct pending_log_message_t {
  109. int severity; /**< The severity of the message */
  110. log_domain_mask_t domain; /**< The domain of the message */
  111. char *msg; /**< The content of the message */
  112. } pending_log_message_t;
  113. /** Log messages waiting to be replayed onto callback-based logs */
  114. static smartlist_t *pending_cb_messages = NULL;
  115. /** Log messages waiting to be replayed once the logging system is initialized.
  116. */
  117. static smartlist_t *pending_startup_messages = NULL;
  118. /** Lock the log_mutex to prevent others from changing the logfile_t list */
  119. #define LOCK_LOGS() STMT_BEGIN \
  120. tor_mutex_acquire(&log_mutex); \
  121. STMT_END
  122. /** Unlock the log_mutex */
  123. #define UNLOCK_LOGS() STMT_BEGIN tor_mutex_release(&log_mutex); STMT_END
  124. /** What's the lowest log level anybody cares about? Checking this lets us
  125. * bail out early from log_debug if we aren't debugging. */
  126. int log_global_min_severity_ = LOG_NOTICE;
  127. static void delete_log(logfile_t *victim);
  128. static void close_log(logfile_t *victim);
  129. static char *domain_to_string(log_domain_mask_t domain,
  130. char *buf, size_t buflen);
  131. static INLINE char *format_msg(char *buf, size_t buf_len,
  132. log_domain_mask_t domain, int severity, const char *funcname,
  133. const char *suffix,
  134. const char *format, va_list ap, size_t *msg_len_out)
  135. CHECK_PRINTF(7,0);
  136. /** Name of the application: used to generate the message we write at the
  137. * start of each new log. */
  138. static char *appname = NULL;
  139. /** Set the "application name" for the logs to <b>name</b>: we'll use this
  140. * name in the message we write when starting up, and at the start of each new
  141. * log.
  142. *
  143. * Tor uses this string to write the version number to the log file. */
  144. void
  145. log_set_application_name(const char *name)
  146. {
  147. tor_free(appname);
  148. appname = name ? tor_strdup(name) : NULL;
  149. }
  150. /** Log time granularity in milliseconds. */
  151. static int log_time_granularity = 1;
  152. /** Define log time granularity for all logs to be <b>granularity_msec</b>
  153. * milliseconds. */
  154. void
  155. set_log_time_granularity(int granularity_msec)
  156. {
  157. log_time_granularity = granularity_msec;
  158. }
  159. /** Helper: Write the standard prefix for log lines to a
  160. * <b>buf_len</b> character buffer in <b>buf</b>.
  161. */
  162. static INLINE size_t
  163. log_prefix_(char *buf, size_t buf_len, int severity)
  164. {
  165. time_t t;
  166. struct timeval now;
  167. struct tm tm;
  168. size_t n;
  169. int r, ms;
  170. tor_gettimeofday(&now);
  171. t = (time_t)now.tv_sec;
  172. ms = (int)now.tv_usec / 1000;
  173. if (log_time_granularity >= 1000) {
  174. t -= t % (log_time_granularity / 1000);
  175. ms = 0;
  176. } else {
  177. ms -= ((int)now.tv_usec / 1000) % log_time_granularity;
  178. }
  179. n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
  180. r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms,
  181. sev_to_string(severity));
  182. if (r<0)
  183. return buf_len-1;
  184. else
  185. return n+r;
  186. }
  187. /** If lf refers to an actual file that we have just opened, and the file
  188. * contains no data, log an "opening new logfile" message at the top.
  189. *
  190. * Return -1 if the log is broken and needs to be deleted, else return 0.
  191. */
  192. static int
  193. log_tor_version(logfile_t *lf, int reset)
  194. {
  195. char buf[256];
  196. size_t n;
  197. int is_new;
  198. if (!lf->needs_close)
  199. /* If it doesn't get closed, it isn't really a file. */
  200. return 0;
  201. if (lf->is_temporary)
  202. /* If it's temporary, it isn't really a file. */
  203. return 0;
  204. is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0;
  205. if (reset && !is_new)
  206. /* We are resetting, but we aren't at the start of the file; no
  207. * need to log again. */
  208. return 0;
  209. n = log_prefix_(buf, sizeof(buf), LOG_NOTICE);
  210. if (appname) {
  211. tor_snprintf(buf+n, sizeof(buf)-n,
  212. "%s opening %slog file.\n", appname, is_new?"new ":"");
  213. } else {
  214. tor_snprintf(buf+n, sizeof(buf)-n,
  215. "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
  216. }
  217. if (write_all(lf->fd, buf, strlen(buf), 0) < 0) /* error */
  218. return -1; /* failed */
  219. return 0;
  220. }
  221. /** Helper: Format a log message into a fixed-sized buffer. (This is
  222. * factored out of <b>logv</b> so that we never format a message more
  223. * than once.) Return a pointer to the first character of the message
  224. * portion of the formatted string.
  225. */
  226. static INLINE char *
  227. format_msg(char *buf, size_t buf_len,
  228. log_domain_mask_t domain, int severity, const char *funcname,
  229. const char *suffix,
  230. const char *format, va_list ap, size_t *msg_len_out)
  231. {
  232. size_t n;
  233. int r;
  234. char *end_of_prefix;
  235. char *buf_end;
  236. assert(buf_len >= 16); /* prevent integer underflow and general stupidity */
  237. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  238. buf_end = buf+buf_len; /* point *after* the last char we can write to */
  239. n = log_prefix_(buf, buf_len, severity);
  240. end_of_prefix = buf+n;
  241. if (log_domains_are_logged) {
  242. char *cp = buf+n;
  243. if (cp == buf_end) goto format_msg_no_room_for_domains;
  244. *cp++ = '{';
  245. if (cp == buf_end) goto format_msg_no_room_for_domains;
  246. cp = domain_to_string(domain, cp, (buf+buf_len-cp));
  247. if (cp == buf_end) goto format_msg_no_room_for_domains;
  248. *cp++ = '}';
  249. if (cp == buf_end) goto format_msg_no_room_for_domains;
  250. *cp++ = ' ';
  251. if (cp == buf_end) goto format_msg_no_room_for_domains;
  252. end_of_prefix = cp;
  253. n = cp-buf;
  254. format_msg_no_room_for_domains:
  255. /* This will leave end_of_prefix and n unchanged, and thus cause
  256. * whatever log domain string we had written to be clobbered. */
  257. ;
  258. }
  259. if (funcname && should_log_function_name(domain, severity)) {
  260. r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  261. if (r<0)
  262. n = strlen(buf);
  263. else
  264. n += r;
  265. }
  266. if (domain == LD_BUG && buf_len-n > 6) {
  267. memcpy(buf+n, "Bug: ", 6);
  268. n += 5;
  269. }
  270. r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
  271. if (r < 0) {
  272. /* The message was too long; overwrite the end of the buffer with
  273. * "[...truncated]" */
  274. if (buf_len >= TRUNCATED_STR_LEN) {
  275. size_t offset = buf_len-TRUNCATED_STR_LEN;
  276. /* We have an extra 2 characters after buf_len to hold the \n\0,
  277. * so it's safe to add 1 to the size here. */
  278. strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
  279. }
  280. /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
  281. * Since we already subtracted 2 from buf_len, this is safe.*/
  282. n = buf_len;
  283. } else {
  284. n += r;
  285. if (suffix) {
  286. size_t suffix_len = strlen(suffix);
  287. if (buf_len-n >= suffix_len) {
  288. memcpy(buf+n, suffix, suffix_len);
  289. n += suffix_len;
  290. }
  291. }
  292. }
  293. buf[n]='\n';
  294. buf[n+1]='\0';
  295. *msg_len_out = n+1;
  296. return end_of_prefix;
  297. }
  298. /* Create a new pending_log_message_t with appropriate values */
  299. static pending_log_message_t *
  300. pending_log_message_new(int severity, log_domain_mask_t domain, const char *msg)
  301. {
  302. pending_log_message_t *m = tor_malloc(sizeof(pending_log_message_t));
  303. m->severity = severity;
  304. m->domain = domain;
  305. m->msg = tor_strdup(msg);
  306. return m;
  307. }
  308. /** Release all storage held by <b>msg</b>. */
  309. static void
  310. pending_log_message_free(pending_log_message_t *msg)
  311. {
  312. if (!msg)
  313. return;
  314. tor_free(msg->msg);
  315. tor_free(msg);
  316. }
  317. /** Helper: sends a message to the appropriate logfiles, at loglevel
  318. * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
  319. * message. The actual message is derived as from tor_snprintf(format,ap).
  320. */
  321. MOCK_IMPL(STATIC void,
  322. logv,(int severity, log_domain_mask_t domain, const char *funcname,
  323. const char *suffix, const char *format, va_list ap))
  324. {
  325. char buf[10024];
  326. size_t msg_len = 0;
  327. int formatted = 0;
  328. logfile_t *lf;
  329. char *end_of_prefix=NULL;
  330. int callbacks_deferred = 0;
  331. /* Call assert, not tor_assert, since tor_assert calls log on failure. */
  332. assert(format);
  333. /* check that severity is sane. Overrunning the masks array leads to
  334. * interesting and hard to diagnose effects */
  335. assert(severity >= LOG_ERR && severity <= LOG_DEBUG);
  336. LOCK_LOGS();
  337. if ((! (domain & LD_NOCB)) && smartlist_len(pending_cb_messages))
  338. flush_pending_log_callbacks();
  339. lf = logfiles;
  340. while (lf) {
  341. if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  342. lf = lf->next;
  343. continue;
  344. }
  345. if (! (lf->fd >= 0 || lf->is_syslog || lf->callback)) {
  346. lf = lf->next;
  347. continue;
  348. }
  349. if (lf->seems_dead) {
  350. lf = lf->next;
  351. continue;
  352. }
  353. if (!formatted) {
  354. end_of_prefix =
  355. format_msg(buf, sizeof(buf), domain, severity, funcname, suffix,
  356. format, ap, &msg_len);
  357. formatted = 1;
  358. }
  359. if (lf->is_syslog) {
  360. #ifdef HAVE_SYSLOG_H
  361. char *m = end_of_prefix;
  362. #ifdef MAXLINE
  363. /* Some syslog implementations have limits on the length of what you can
  364. * pass them, and some very old ones do not detect overflow so well.
  365. * Regrettably, they call their maximum line length MAXLINE. */
  366. #if MAXLINE < 64
  367. #warn "MAXLINE is a very low number; it might not be from syslog.h after all"
  368. #endif
  369. if (msg_len >= MAXLINE)
  370. m = tor_strndup(end_of_prefix, MAXLINE-1);
  371. #endif
  372. syslog(severity, "%s", m);
  373. #ifdef MAXLINE
  374. if (m != end_of_prefix) {
  375. tor_free(m);
  376. }
  377. #endif
  378. #endif
  379. lf = lf->next;
  380. continue;
  381. } else if (lf->callback) {
  382. if (domain & LD_NOCB) {
  383. if (!callbacks_deferred && pending_cb_messages) {
  384. smartlist_add(pending_cb_messages,
  385. pending_log_message_new(severity,domain,end_of_prefix));
  386. callbacks_deferred = 1;
  387. }
  388. } else {
  389. lf->callback(severity, domain, end_of_prefix);
  390. }
  391. lf = lf->next;
  392. continue;
  393. }
  394. if (write_all(lf->fd, buf, msg_len, 0) < 0) { /* error */
  395. /* don't log the error! mark this log entry to be blown away, and
  396. * continue. */
  397. lf->seems_dead = 1;
  398. }
  399. lf = lf->next;
  400. }
  401. UNLOCK_LOGS();
  402. }
  403. /** Output a message to the log. It gets logged to all logfiles that
  404. * care about messages with <b>severity</b> in <b>domain</b>. The content
  405. * is formatted printf-style based on <b>format</b> and extra arguments.
  406. * */
  407. void
  408. tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  409. {
  410. va_list ap;
  411. if (severity > log_global_min_severity_)
  412. return;
  413. va_start(ap,format);
  414. logv(severity, domain, NULL, NULL, format, ap);
  415. va_end(ap);
  416. }
  417. /** Maximum number of fds that will get notifications if we crash */
  418. #define MAX_SIGSAFE_FDS 8
  419. /** Array of fds to log crash-style warnings to. */
  420. static int sigsafe_log_fds[MAX_SIGSAFE_FDS] = { STDERR_FILENO };
  421. /** The number of elements used in sigsafe_log_fds */
  422. static int n_sigsafe_log_fds = 1;
  423. /** Write <b>s</b> to each element of sigsafe_log_fds. Return 0 on success, -1
  424. * on failure. */
  425. static int
  426. tor_log_err_sigsafe_write(const char *s)
  427. {
  428. int i;
  429. ssize_t r;
  430. size_t len = strlen(s);
  431. int err = 0;
  432. for (i=0; i < n_sigsafe_log_fds; ++i) {
  433. r = write(sigsafe_log_fds[i], s, len);
  434. err += (r != (ssize_t)len);
  435. }
  436. return err ? -1 : 0;
  437. }
  438. /** Given a list of string arguments ending with a NULL, writes them
  439. * to our logs and to stderr (if possible). This function is safe to call
  440. * from within a signal handler. */
  441. void
  442. tor_log_err_sigsafe(const char *m, ...)
  443. {
  444. va_list ap;
  445. const char *x;
  446. char timebuf[33];
  447. time_t now = time(NULL);
  448. if (!m)
  449. return;
  450. if (log_time_granularity >= 2000) {
  451. int g = log_time_granularity / 1000;
  452. now -= now % g;
  453. }
  454. timebuf[0] = now < 0 ? '-' : ' ';
  455. if (now < 0) now = -now;
  456. timebuf[1] = '\0';
  457. format_dec_number_sigsafe(now, timebuf+1, sizeof(timebuf)-1);
  458. tor_log_err_sigsafe_write("\n=========================================="
  459. "================== T=");
  460. tor_log_err_sigsafe_write(timebuf);
  461. tor_log_err_sigsafe_write("\n");
  462. tor_log_err_sigsafe_write(m);
  463. va_start(ap, m);
  464. while ((x = va_arg(ap, const char*))) {
  465. tor_log_err_sigsafe_write(x);
  466. }
  467. va_end(ap);
  468. }
  469. /** Set *<b>out</b> to a pointer to an array of the fds to log errors to from
  470. * inside a signal handler. Return the number of elements in the array. */
  471. int
  472. tor_log_get_sigsafe_err_fds(const int **out)
  473. {
  474. *out = sigsafe_log_fds;
  475. return n_sigsafe_log_fds;
  476. }
  477. /** Helper function; return true iff the <b>n</b>-element array <b>array</b>
  478. * contains <b>item</b>. */
  479. static int
  480. int_array_contains(const int *array, int n, int item)
  481. {
  482. int j;
  483. for (j = 0; j < n; ++j) {
  484. if (array[j] == item)
  485. return 1;
  486. }
  487. return 0;
  488. }
  489. /** Function to call whenever the list of logs changes to get ready to log
  490. * from signal handlers. */
  491. void
  492. tor_log_update_sigsafe_err_fds(void)
  493. {
  494. const logfile_t *lf;
  495. int found_real_stderr = 0;
  496. LOCK_LOGS();
  497. /* Reserve the first one for stderr. This is safe because when we daemonize,
  498. * we dup2 /dev/null to stderr, */
  499. sigsafe_log_fds[0] = STDERR_FILENO;
  500. n_sigsafe_log_fds = 1;
  501. for (lf = logfiles; lf; lf = lf->next) {
  502. /* Don't try callback to the control port, or syslogs: We can't
  503. * do them from a signal handler. Don't try stdout: we always do stderr.
  504. */
  505. if (lf->is_temporary || lf->is_syslog ||
  506. lf->callback || lf->seems_dead || lf->fd < 0)
  507. continue;
  508. if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] &
  509. (LD_BUG|LD_GENERAL)) {
  510. if (lf->fd == STDERR_FILENO)
  511. found_real_stderr = 1;
  512. /* Avoid duplicates */
  513. if (int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, lf->fd))
  514. continue;
  515. sigsafe_log_fds[n_sigsafe_log_fds++] = lf->fd;
  516. if (n_sigsafe_log_fds == MAX_SIGSAFE_FDS)
  517. break;
  518. }
  519. }
  520. if (!found_real_stderr &&
  521. int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, STDOUT_FILENO)) {
  522. /* Don't use a virtual stderr when we're also logging to stdout. */
  523. assert(n_sigsafe_log_fds >= 2); /* Don't use assert inside log functions*/
  524. sigsafe_log_fds[0] = sigsafe_log_fds[--n_sigsafe_log_fds];
  525. }
  526. UNLOCK_LOGS();
  527. }
  528. /** Add to <b>out</b> a copy of every currently configured log file name. Used
  529. * to enable access to these filenames with the sandbox code. */
  530. void
  531. tor_log_get_logfile_names(smartlist_t *out)
  532. {
  533. logfile_t *lf;
  534. tor_assert(out);
  535. LOCK_LOGS();
  536. for (lf = logfiles; lf; lf = lf->next) {
  537. if (lf->is_temporary || lf->is_syslog || lf->callback)
  538. continue;
  539. if (lf->filename == NULL)
  540. continue;
  541. smartlist_add(out, tor_strdup(lf->filename));
  542. }
  543. UNLOCK_LOGS();
  544. }
  545. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  546. #ifdef __GNUC__
  547. /** GCC-based implementation of the log_fn backend, used when we have
  548. * variadic macros. All arguments are as for log_fn, except for
  549. * <b>fn</b>, which is the name of the calling functions. */
  550. void
  551. log_fn_(int severity, log_domain_mask_t domain, const char *fn,
  552. const char *format, ...)
  553. {
  554. va_list ap;
  555. if (severity > log_global_min_severity_)
  556. return;
  557. va_start(ap,format);
  558. logv(severity, domain, fn, NULL, format, ap);
  559. va_end(ap);
  560. }
  561. void
  562. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  563. const char *fn, const char *format, ...)
  564. {
  565. va_list ap;
  566. char *m;
  567. if (severity > log_global_min_severity_)
  568. return;
  569. m = rate_limit_log(ratelim, approx_time());
  570. if (m == NULL)
  571. return;
  572. va_start(ap, format);
  573. logv(severity, domain, fn, m, format, ap);
  574. va_end(ap);
  575. tor_free(m);
  576. }
  577. #else
  578. /** @{ */
  579. /** Variant implementation of log_fn, log_debug, log_info,... for C compilers
  580. * without variadic macros. In this case, the calling function sets
  581. * log_fn_function_name_ to the name of the function, then invokes the
  582. * appropriate log_fn_, log_debug_, etc. */
  583. const char *log_fn_function_name_=NULL;
  584. void
  585. log_fn_(int severity, log_domain_mask_t domain, const char *format, ...)
  586. {
  587. va_list ap;
  588. if (severity > log_global_min_severity_)
  589. return;
  590. va_start(ap,format);
  591. logv(severity, domain, log_fn_function_name_, NULL, format, ap);
  592. va_end(ap);
  593. log_fn_function_name_ = NULL;
  594. }
  595. void
  596. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  597. const char *format, ...)
  598. {
  599. va_list ap;
  600. char *m;
  601. if (severity > log_global_min_severity_)
  602. return;
  603. m = rate_limit_log(ratelim, approx_time());
  604. if (m == NULL)
  605. return;
  606. va_start(ap, format);
  607. logv(severity, domain, log_fn_function_name_, m, format, ap);
  608. va_end(ap);
  609. tor_free(m);
  610. }
  611. void
  612. log_debug_(log_domain_mask_t domain, const char *format, ...)
  613. {
  614. va_list ap;
  615. /* For GCC we do this check in the macro. */
  616. if (PREDICT_LIKELY(LOG_DEBUG > log_global_min_severity_))
  617. return;
  618. va_start(ap,format);
  619. logv(LOG_DEBUG, domain, log_fn_function_name_, NULL, format, ap);
  620. va_end(ap);
  621. log_fn_function_name_ = NULL;
  622. }
  623. void
  624. log_info_(log_domain_mask_t domain, const char *format, ...)
  625. {
  626. va_list ap;
  627. if (LOG_INFO > log_global_min_severity_)
  628. return;
  629. va_start(ap,format);
  630. logv(LOG_INFO, domain, log_fn_function_name_, NULL, format, ap);
  631. va_end(ap);
  632. log_fn_function_name_ = NULL;
  633. }
  634. void
  635. log_notice_(log_domain_mask_t domain, const char *format, ...)
  636. {
  637. va_list ap;
  638. if (LOG_NOTICE > log_global_min_severity_)
  639. return;
  640. va_start(ap,format);
  641. logv(LOG_NOTICE, domain, log_fn_function_name_, NULL, format, ap);
  642. va_end(ap);
  643. log_fn_function_name_ = NULL;
  644. }
  645. void
  646. log_warn_(log_domain_mask_t domain, const char *format, ...)
  647. {
  648. va_list ap;
  649. if (LOG_WARN > log_global_min_severity_)
  650. return;
  651. va_start(ap,format);
  652. logv(LOG_WARN, domain, log_fn_function_name_, NULL, format, ap);
  653. va_end(ap);
  654. log_fn_function_name_ = NULL;
  655. }
  656. void
  657. log_err_(log_domain_mask_t domain, const char *format, ...)
  658. {
  659. va_list ap;
  660. if (LOG_ERR > log_global_min_severity_)
  661. return;
  662. va_start(ap,format);
  663. logv(LOG_ERR, domain, log_fn_function_name_, NULL, format, ap);
  664. va_end(ap);
  665. log_fn_function_name_ = NULL;
  666. }
  667. /** @} */
  668. #endif
  669. /** Free all storage held by <b>victim</b>. */
  670. static void
  671. log_free(logfile_t *victim)
  672. {
  673. if (!victim)
  674. return;
  675. tor_free(victim->severities);
  676. tor_free(victim->filename);
  677. tor_free(victim);
  678. }
  679. /** Close all open log files, and free other static memory. */
  680. void
  681. logs_free_all(void)
  682. {
  683. logfile_t *victim, *next;
  684. smartlist_t *messages;
  685. LOCK_LOGS();
  686. next = logfiles;
  687. logfiles = NULL;
  688. messages = pending_cb_messages;
  689. pending_cb_messages = NULL;
  690. UNLOCK_LOGS();
  691. while (next) {
  692. victim = next;
  693. next = next->next;
  694. close_log(victim);
  695. log_free(victim);
  696. }
  697. tor_free(appname);
  698. SMARTLIST_FOREACH(messages, pending_log_message_t *, msg, {
  699. pending_log_message_free(msg);
  700. });
  701. smartlist_free(messages);
  702. /* We _could_ destroy the log mutex here, but that would screw up any logs
  703. * that happened between here and the end of execution. */
  704. }
  705. /** Remove and free the log entry <b>victim</b> from the linked-list
  706. * logfiles (it is probably present, but it might not be due to thread
  707. * racing issues). After this function is called, the caller shouldn't
  708. * refer to <b>victim</b> anymore.
  709. *
  710. * Long-term, we need to do something about races in the log subsystem
  711. * in general. See bug 222 for more details.
  712. */
  713. static void
  714. delete_log(logfile_t *victim)
  715. {
  716. logfile_t *tmpl;
  717. if (victim == logfiles)
  718. logfiles = victim->next;
  719. else {
  720. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  721. // tor_assert(tmpl);
  722. // tor_assert(tmpl->next == victim);
  723. if (!tmpl)
  724. return;
  725. tmpl->next = victim->next;
  726. }
  727. log_free(victim);
  728. }
  729. /** Helper: release system resources (but not memory) held by a single
  730. * logfile_t. */
  731. static void
  732. close_log(logfile_t *victim)
  733. {
  734. if (victim->needs_close && victim->fd >= 0) {
  735. close(victim->fd);
  736. victim->fd = -1;
  737. } else if (victim->is_syslog) {
  738. #ifdef HAVE_SYSLOG_H
  739. if (--syslog_count == 0) {
  740. /* There are no other syslogs; close the logging facility. */
  741. closelog();
  742. }
  743. #endif
  744. }
  745. }
  746. /** Adjust a log severity configuration in <b>severity_out</b> to contain
  747. * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
  748. */
  749. void
  750. set_log_severity_config(int loglevelMin, int loglevelMax,
  751. log_severity_list_t *severity_out)
  752. {
  753. int i;
  754. tor_assert(loglevelMin >= loglevelMax);
  755. tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
  756. tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
  757. memset(severity_out, 0, sizeof(log_severity_list_t));
  758. for (i = loglevelMin; i >= loglevelMax; --i) {
  759. severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  760. }
  761. }
  762. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  763. * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */
  764. static void
  765. add_stream_log_impl(const log_severity_list_t *severity,
  766. const char *name, int fd)
  767. {
  768. logfile_t *lf;
  769. lf = tor_malloc_zero(sizeof(logfile_t));
  770. lf->fd = fd;
  771. lf->filename = tor_strdup(name);
  772. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  773. lf->next = logfiles;
  774. logfiles = lf;
  775. log_global_min_severity_ = get_min_log_level();
  776. }
  777. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  778. * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must
  779. * not use it after calling this function. */
  780. void
  781. add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
  782. {
  783. LOCK_LOGS();
  784. add_stream_log_impl(severity, name, fd);
  785. UNLOCK_LOGS();
  786. }
  787. /** Initialize the global logging facility */
  788. void
  789. init_logging(void)
  790. {
  791. if (!log_mutex_initialized) {
  792. tor_mutex_init(&log_mutex);
  793. log_mutex_initialized = 1;
  794. }
  795. if (pending_cb_messages == NULL)
  796. pending_cb_messages = smartlist_new();
  797. if (pending_startup_messages == NULL)
  798. pending_startup_messages = smartlist_new();
  799. }
  800. /** Set whether we report logging domains as a part of our log messages.
  801. */
  802. void
  803. logs_set_domain_logging(int enabled)
  804. {
  805. LOCK_LOGS();
  806. log_domains_are_logged = enabled;
  807. UNLOCK_LOGS();
  808. }
  809. /** Add a log handler to receive messages during startup (before the real
  810. * logs are initialized).
  811. */
  812. void
  813. add_temp_log(int min_severity)
  814. {
  815. log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
  816. set_log_severity_config(min_severity, LOG_ERR, s);
  817. LOCK_LOGS();
  818. add_stream_log_impl(s, "<temp>", fileno(stdout));
  819. tor_free(s);
  820. logfiles->is_temporary = 1;
  821. UNLOCK_LOGS();
  822. }
  823. /**
  824. * Add a log handler to send messages in <b>severity</b>
  825. * to the function <b>cb</b>.
  826. */
  827. int
  828. add_callback_log(const log_severity_list_t *severity, log_callback cb)
  829. {
  830. logfile_t *lf;
  831. lf = tor_malloc_zero(sizeof(logfile_t));
  832. lf->fd = -1;
  833. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  834. lf->filename = tor_strdup("<callback>");
  835. lf->callback = cb;
  836. lf->next = logfiles;
  837. LOCK_LOGS();
  838. logfiles = lf;
  839. log_global_min_severity_ = get_min_log_level();
  840. UNLOCK_LOGS();
  841. return 0;
  842. }
  843. /** Adjust the configured severity of any logs whose callback function is
  844. * <b>cb</b>. */
  845. void
  846. change_callback_log_severity(int loglevelMin, int loglevelMax,
  847. log_callback cb)
  848. {
  849. logfile_t *lf;
  850. log_severity_list_t severities;
  851. set_log_severity_config(loglevelMin, loglevelMax, &severities);
  852. LOCK_LOGS();
  853. for (lf = logfiles; lf; lf = lf->next) {
  854. if (lf->callback == cb) {
  855. memcpy(lf->severities, &severities, sizeof(severities));
  856. }
  857. }
  858. log_global_min_severity_ = get_min_log_level();
  859. UNLOCK_LOGS();
  860. }
  861. /** If there are any log messages that were generated with LD_NOCB waiting to
  862. * be sent to callback-based loggers, send them now. */
  863. void
  864. flush_pending_log_callbacks(void)
  865. {
  866. logfile_t *lf;
  867. smartlist_t *messages, *messages_tmp;
  868. LOCK_LOGS();
  869. if (0 == smartlist_len(pending_cb_messages)) {
  870. UNLOCK_LOGS();
  871. return;
  872. }
  873. messages = pending_cb_messages;
  874. pending_cb_messages = smartlist_new();
  875. do {
  876. SMARTLIST_FOREACH_BEGIN(messages, pending_log_message_t *, msg) {
  877. const int severity = msg->severity;
  878. const int domain = msg->domain;
  879. for (lf = logfiles; lf; lf = lf->next) {
  880. if (! lf->callback || lf->seems_dead ||
  881. ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  882. continue;
  883. }
  884. lf->callback(severity, domain, msg->msg);
  885. }
  886. pending_log_message_free(msg);
  887. } SMARTLIST_FOREACH_END(msg);
  888. smartlist_clear(messages);
  889. messages_tmp = pending_cb_messages;
  890. pending_cb_messages = messages;
  891. messages = messages_tmp;
  892. } while (smartlist_len(messages));
  893. smartlist_free(messages);
  894. UNLOCK_LOGS();
  895. }
  896. /** Close any log handlers added by add_temp_log() or marked by
  897. * mark_logs_temp(). */
  898. void
  899. close_temp_logs(void)
  900. {
  901. logfile_t *lf, **p;
  902. LOCK_LOGS();
  903. for (p = &logfiles; *p; ) {
  904. if ((*p)->is_temporary) {
  905. lf = *p;
  906. /* we use *p here to handle the edge case of the head of the list */
  907. *p = (*p)->next;
  908. close_log(lf);
  909. log_free(lf);
  910. } else {
  911. p = &((*p)->next);
  912. }
  913. }
  914. log_global_min_severity_ = get_min_log_level();
  915. UNLOCK_LOGS();
  916. }
  917. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  918. * live again, and close all non-temporary logs. */
  919. void
  920. rollback_log_changes(void)
  921. {
  922. logfile_t *lf;
  923. LOCK_LOGS();
  924. for (lf = logfiles; lf; lf = lf->next)
  925. lf->is_temporary = ! lf->is_temporary;
  926. UNLOCK_LOGS();
  927. close_temp_logs();
  928. }
  929. /** Configure all log handles to be closed by close_temp_logs(). */
  930. void
  931. mark_logs_temp(void)
  932. {
  933. logfile_t *lf;
  934. LOCK_LOGS();
  935. for (lf = logfiles; lf; lf = lf->next)
  936. lf->is_temporary = 1;
  937. UNLOCK_LOGS();
  938. }
  939. /**
  940. * Add a log handler to send messages to <b>filename</b>. If opening the
  941. * logfile fails, -1 is returned and errno is set appropriately (by open(2)).
  942. */
  943. int
  944. add_file_log(const log_severity_list_t *severity, const char *filename,
  945. const int truncate)
  946. {
  947. int fd;
  948. logfile_t *lf;
  949. int open_flags = O_WRONLY|O_CREAT;
  950. open_flags |= truncate ? O_TRUNC : O_APPEND;
  951. fd = tor_open_cloexec(filename, open_flags, 0644);
  952. if (fd<0)
  953. return -1;
  954. if (tor_fd_seekend(fd)<0) {
  955. close(fd);
  956. return -1;
  957. }
  958. LOCK_LOGS();
  959. add_stream_log_impl(severity, filename, fd);
  960. logfiles->needs_close = 1;
  961. lf = logfiles;
  962. log_global_min_severity_ = get_min_log_level();
  963. if (log_tor_version(lf, 0) < 0) {
  964. delete_log(lf);
  965. }
  966. UNLOCK_LOGS();
  967. return 0;
  968. }
  969. #ifdef HAVE_SYSLOG_H
  970. /**
  971. * Add a log handler to send messages to they system log facility.
  972. */
  973. int
  974. add_syslog_log(const log_severity_list_t *severity)
  975. {
  976. logfile_t *lf;
  977. if (syslog_count++ == 0)
  978. /* This is the first syslog. */
  979. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  980. lf = tor_malloc_zero(sizeof(logfile_t));
  981. lf->fd = -1;
  982. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  983. lf->filename = tor_strdup("<syslog>");
  984. lf->is_syslog = 1;
  985. LOCK_LOGS();
  986. lf->next = logfiles;
  987. logfiles = lf;
  988. log_global_min_severity_ = get_min_log_level();
  989. UNLOCK_LOGS();
  990. return 0;
  991. }
  992. #endif
  993. /** If <b>level</b> is a valid log severity, return the corresponding
  994. * numeric value. Otherwise, return -1. */
  995. int
  996. parse_log_level(const char *level)
  997. {
  998. if (!strcasecmp(level, "err"))
  999. return LOG_ERR;
  1000. if (!strcasecmp(level, "warn"))
  1001. return LOG_WARN;
  1002. if (!strcasecmp(level, "notice"))
  1003. return LOG_NOTICE;
  1004. if (!strcasecmp(level, "info"))
  1005. return LOG_INFO;
  1006. if (!strcasecmp(level, "debug"))
  1007. return LOG_DEBUG;
  1008. return -1;
  1009. }
  1010. /** Return the string equivalent of a given log level. */
  1011. const char *
  1012. log_level_to_string(int level)
  1013. {
  1014. return sev_to_string(level);
  1015. }
  1016. /** NULL-terminated array of names for log domains such that domain_list[dom]
  1017. * is a description of <b>dom</b>. */
  1018. static const char *domain_list[] = {
  1019. "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
  1020. "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
  1021. "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", NULL
  1022. };
  1023. /** Return a bitmask for the log domain for which <b>domain</b> is the name,
  1024. * or 0 if there is no such name. */
  1025. static log_domain_mask_t
  1026. parse_log_domain(const char *domain)
  1027. {
  1028. int i;
  1029. for (i=0; domain_list[i]; ++i) {
  1030. if (!strcasecmp(domain, domain_list[i]))
  1031. return (1u<<i);
  1032. }
  1033. return 0;
  1034. }
  1035. /** Translate a bitmask of log domains to a string. */
  1036. static char *
  1037. domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen)
  1038. {
  1039. char *cp = buf;
  1040. char *eos = buf+buflen;
  1041. buf[0] = '\0';
  1042. if (! domain)
  1043. return buf;
  1044. while (1) {
  1045. const char *d;
  1046. int bit = tor_log2(domain);
  1047. size_t n;
  1048. if (bit >= N_LOGGING_DOMAINS) {
  1049. tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain);
  1050. return buf+strlen(buf);
  1051. }
  1052. d = domain_list[bit];
  1053. n = strlcpy(cp, d, eos-cp);
  1054. if (n >= buflen) {
  1055. tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain);
  1056. return buf+strlen(buf);
  1057. }
  1058. cp += n;
  1059. domain &= ~(1<<bit);
  1060. if (domain == 0 || (eos-cp) < 2)
  1061. return cp;
  1062. memcpy(cp, ",", 2); /*Nul-terminated ,"*/
  1063. cp++;
  1064. }
  1065. }
  1066. /** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after
  1067. * the end of the severityPattern. Set the value of <b>severity_out</b> to
  1068. * the parsed pattern. Return 0 on success, -1 on failure.
  1069. *
  1070. * The syntax for a SeverityPattern is:
  1071. * <pre>
  1072. * SeverityPattern = *(DomainSeverity SP)* DomainSeverity
  1073. * DomainSeverity = (DomainList SP)? SeverityRange
  1074. * SeverityRange = MinSeverity ("-" MaxSeverity )?
  1075. * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
  1076. * DomainSpec = "*" | Domain | "~" Domain
  1077. * </pre>
  1078. * A missing MaxSeverity defaults to ERR. Severities and domains are
  1079. * case-insensitive. "~" indicates negation for a domain; negation happens
  1080. * last inside a DomainList. Only one SeverityRange without a DomainList is
  1081. * allowed per line.
  1082. */
  1083. int
  1084. parse_log_severity_config(const char **cfg_ptr,
  1085. log_severity_list_t *severity_out)
  1086. {
  1087. const char *cfg = *cfg_ptr;
  1088. int got_anything = 0;
  1089. int got_an_unqualified_range = 0;
  1090. memset(severity_out, 0, sizeof(*severity_out));
  1091. cfg = eat_whitespace(cfg);
  1092. while (*cfg) {
  1093. const char *dash, *space;
  1094. char *sev_lo, *sev_hi;
  1095. int low, high, i;
  1096. log_domain_mask_t domains = ~0u;
  1097. if (*cfg == '[') {
  1098. int err = 0;
  1099. char *domains_str;
  1100. smartlist_t *domains_list;
  1101. log_domain_mask_t neg_domains = 0;
  1102. const char *closebracket = strchr(cfg, ']');
  1103. if (!closebracket)
  1104. return -1;
  1105. domains = 0;
  1106. domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
  1107. domains_list = smartlist_new();
  1108. smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
  1109. -1);
  1110. tor_free(domains_str);
  1111. SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) {
  1112. if (!strcmp(domain, "*")) {
  1113. domains = ~0u;
  1114. } else {
  1115. int d;
  1116. int negate=0;
  1117. if (*domain == '~') {
  1118. negate = 1;
  1119. ++domain;
  1120. }
  1121. d = parse_log_domain(domain);
  1122. if (!d) {
  1123. log_warn(LD_CONFIG, "No such logging domain as %s", domain);
  1124. err = 1;
  1125. } else {
  1126. if (negate)
  1127. neg_domains |= d;
  1128. else
  1129. domains |= d;
  1130. }
  1131. }
  1132. } SMARTLIST_FOREACH_END(domain);
  1133. SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
  1134. smartlist_free(domains_list);
  1135. if (err)
  1136. return -1;
  1137. if (domains == 0 && neg_domains)
  1138. domains = ~neg_domains;
  1139. else
  1140. domains &= ~neg_domains;
  1141. cfg = eat_whitespace(closebracket+1);
  1142. } else {
  1143. ++got_an_unqualified_range;
  1144. }
  1145. if (!strcasecmpstart(cfg, "file") ||
  1146. !strcasecmpstart(cfg, "stderr") ||
  1147. !strcasecmpstart(cfg, "stdout") ||
  1148. !strcasecmpstart(cfg, "syslog")) {
  1149. goto done;
  1150. }
  1151. if (got_an_unqualified_range > 1)
  1152. return -1;
  1153. space = strchr(cfg, ' ');
  1154. dash = strchr(cfg, '-');
  1155. if (!space)
  1156. space = strchr(cfg, '\0');
  1157. if (dash && dash < space) {
  1158. sev_lo = tor_strndup(cfg, dash-cfg);
  1159. sev_hi = tor_strndup(dash+1, space-(dash+1));
  1160. } else {
  1161. sev_lo = tor_strndup(cfg, space-cfg);
  1162. sev_hi = tor_strdup("ERR");
  1163. }
  1164. low = parse_log_level(sev_lo);
  1165. high = parse_log_level(sev_hi);
  1166. tor_free(sev_lo);
  1167. tor_free(sev_hi);
  1168. if (low == -1)
  1169. return -1;
  1170. if (high == -1)
  1171. return -1;
  1172. got_anything = 1;
  1173. for (i=low; i >= high; --i)
  1174. severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
  1175. cfg = eat_whitespace(space);
  1176. }
  1177. done:
  1178. *cfg_ptr = cfg;
  1179. return got_anything ? 0 : -1;
  1180. }
  1181. /** Return the least severe log level that any current log is interested in. */
  1182. int
  1183. get_min_log_level(void)
  1184. {
  1185. logfile_t *lf;
  1186. int i;
  1187. int min = LOG_ERR;
  1188. for (lf = logfiles; lf; lf = lf->next) {
  1189. for (i = LOG_DEBUG; i > min; --i)
  1190. if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
  1191. min = i;
  1192. }
  1193. return min;
  1194. }
  1195. /** Switch all logs to output at most verbose level. */
  1196. void
  1197. switch_logs_debug(void)
  1198. {
  1199. logfile_t *lf;
  1200. int i;
  1201. LOCK_LOGS();
  1202. for (lf = logfiles; lf; lf=lf->next) {
  1203. for (i = LOG_DEBUG; i >= LOG_ERR; --i)
  1204. lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  1205. }
  1206. log_global_min_severity_ = get_min_log_level();
  1207. UNLOCK_LOGS();
  1208. }
  1209. /** Truncate all the log files. */
  1210. void
  1211. truncate_logs(void)
  1212. {
  1213. logfile_t *lf;
  1214. for (lf = logfiles; lf; lf = lf->next) {
  1215. if (lf->fd >= 0) {
  1216. tor_ftruncate(lf->fd);
  1217. }
  1218. }
  1219. }