log.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. /** Return true iff <b>lf</b> would like to receive a message with the specified
  318. * <b>severity</b> in the specified <b>domain</b>.
  319. */
  320. static INLINE int
  321. logfile_wants_message(const logfile_t *lf, int severity,
  322. log_domain_mask_t domain)
  323. {
  324. if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  325. return 0;
  326. }
  327. if (! (lf->fd >= 0 || lf->is_syslog || lf->callback)) {
  328. return 0;
  329. }
  330. if (lf->seems_dead) {
  331. return 0;
  332. }
  333. return 1;
  334. }
  335. /** Helper: sends a message to the appropriate logfiles, at loglevel
  336. * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
  337. * message. The actual message is derived as from tor_snprintf(format,ap).
  338. */
  339. MOCK_IMPL(STATIC void,
  340. logv,(int severity, log_domain_mask_t domain, const char *funcname,
  341. const char *suffix, const char *format, va_list ap))
  342. {
  343. char buf[10024];
  344. size_t msg_len = 0;
  345. int formatted = 0;
  346. logfile_t *lf;
  347. char *end_of_prefix=NULL;
  348. int callbacks_deferred = 0;
  349. /* Call assert, not tor_assert, since tor_assert calls log on failure. */
  350. assert(format);
  351. /* check that severity is sane. Overrunning the masks array leads to
  352. * interesting and hard to diagnose effects */
  353. assert(severity >= LOG_ERR && severity <= LOG_DEBUG);
  354. LOCK_LOGS();
  355. if ((! (domain & LD_NOCB)) && smartlist_len(pending_cb_messages))
  356. flush_pending_log_callbacks();
  357. for (lf = logfiles; lf; lf = lf->next) {
  358. if (! logfile_wants_message(lf, severity, domain))
  359. continue;
  360. if (!formatted) {
  361. end_of_prefix =
  362. format_msg(buf, sizeof(buf), domain, severity, funcname, suffix,
  363. format, ap, &msg_len);
  364. formatted = 1;
  365. }
  366. if (lf->is_syslog) {
  367. #ifdef HAVE_SYSLOG_H
  368. char *m = end_of_prefix;
  369. #ifdef MAXLINE
  370. /* Some syslog implementations have limits on the length of what you can
  371. * pass them, and some very old ones do not detect overflow so well.
  372. * Regrettably, they call their maximum line length MAXLINE. */
  373. #if MAXLINE < 64
  374. #warn "MAXLINE is a very low number; it might not be from syslog.h after all"
  375. #endif
  376. if (msg_len >= MAXLINE)
  377. m = tor_strndup(end_of_prefix, MAXLINE-1);
  378. #endif
  379. syslog(severity, "%s", m);
  380. #ifdef MAXLINE
  381. if (m != end_of_prefix) {
  382. tor_free(m);
  383. }
  384. #endif
  385. #endif
  386. continue;
  387. } else if (lf->callback) {
  388. if (domain & LD_NOCB) {
  389. if (!callbacks_deferred && pending_cb_messages) {
  390. smartlist_add(pending_cb_messages,
  391. pending_log_message_new(severity,domain,end_of_prefix));
  392. callbacks_deferred = 1;
  393. }
  394. } else {
  395. lf->callback(severity, domain, end_of_prefix);
  396. }
  397. continue;
  398. }
  399. if (write_all(lf->fd, buf, msg_len, 0) < 0) { /* error */
  400. /* don't log the error! mark this log entry to be blown away, and
  401. * continue. */
  402. lf->seems_dead = 1;
  403. }
  404. }
  405. UNLOCK_LOGS();
  406. }
  407. /** Output a message to the log. It gets logged to all logfiles that
  408. * care about messages with <b>severity</b> in <b>domain</b>. The content
  409. * is formatted printf-style based on <b>format</b> and extra arguments.
  410. * */
  411. void
  412. tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  413. {
  414. va_list ap;
  415. if (severity > log_global_min_severity_)
  416. return;
  417. va_start(ap,format);
  418. logv(severity, domain, NULL, NULL, format, ap);
  419. va_end(ap);
  420. }
  421. /** Maximum number of fds that will get notifications if we crash */
  422. #define MAX_SIGSAFE_FDS 8
  423. /** Array of fds to log crash-style warnings to. */
  424. static int sigsafe_log_fds[MAX_SIGSAFE_FDS] = { STDERR_FILENO };
  425. /** The number of elements used in sigsafe_log_fds */
  426. static int n_sigsafe_log_fds = 1;
  427. /** Write <b>s</b> to each element of sigsafe_log_fds. Return 0 on success, -1
  428. * on failure. */
  429. static int
  430. tor_log_err_sigsafe_write(const char *s)
  431. {
  432. int i;
  433. ssize_t r;
  434. size_t len = strlen(s);
  435. int err = 0;
  436. for (i=0; i < n_sigsafe_log_fds; ++i) {
  437. r = write(sigsafe_log_fds[i], s, len);
  438. err += (r != (ssize_t)len);
  439. }
  440. return err ? -1 : 0;
  441. }
  442. /** Given a list of string arguments ending with a NULL, writes them
  443. * to our logs and to stderr (if possible). This function is safe to call
  444. * from within a signal handler. */
  445. void
  446. tor_log_err_sigsafe(const char *m, ...)
  447. {
  448. va_list ap;
  449. const char *x;
  450. char timebuf[33];
  451. time_t now = time(NULL);
  452. if (!m)
  453. return;
  454. if (log_time_granularity >= 2000) {
  455. int g = log_time_granularity / 1000;
  456. now -= now % g;
  457. }
  458. timebuf[0] = now < 0 ? '-' : ' ';
  459. if (now < 0) now = -now;
  460. timebuf[1] = '\0';
  461. format_dec_number_sigsafe(now, timebuf+1, sizeof(timebuf)-1);
  462. tor_log_err_sigsafe_write("\n=========================================="
  463. "================== T=");
  464. tor_log_err_sigsafe_write(timebuf);
  465. tor_log_err_sigsafe_write("\n");
  466. tor_log_err_sigsafe_write(m);
  467. va_start(ap, m);
  468. while ((x = va_arg(ap, const char*))) {
  469. tor_log_err_sigsafe_write(x);
  470. }
  471. va_end(ap);
  472. }
  473. /** Set *<b>out</b> to a pointer to an array of the fds to log errors to from
  474. * inside a signal handler. Return the number of elements in the array. */
  475. int
  476. tor_log_get_sigsafe_err_fds(const int **out)
  477. {
  478. *out = sigsafe_log_fds;
  479. return n_sigsafe_log_fds;
  480. }
  481. /** Helper function; return true iff the <b>n</b>-element array <b>array</b>
  482. * contains <b>item</b>. */
  483. static int
  484. int_array_contains(const int *array, int n, int item)
  485. {
  486. int j;
  487. for (j = 0; j < n; ++j) {
  488. if (array[j] == item)
  489. return 1;
  490. }
  491. return 0;
  492. }
  493. /** Function to call whenever the list of logs changes to get ready to log
  494. * from signal handlers. */
  495. void
  496. tor_log_update_sigsafe_err_fds(void)
  497. {
  498. const logfile_t *lf;
  499. int found_real_stderr = 0;
  500. LOCK_LOGS();
  501. /* Reserve the first one for stderr. This is safe because when we daemonize,
  502. * we dup2 /dev/null to stderr, */
  503. sigsafe_log_fds[0] = STDERR_FILENO;
  504. n_sigsafe_log_fds = 1;
  505. for (lf = logfiles; lf; lf = lf->next) {
  506. /* Don't try callback to the control port, or syslogs: We can't
  507. * do them from a signal handler. Don't try stdout: we always do stderr.
  508. */
  509. if (lf->is_temporary || lf->is_syslog ||
  510. lf->callback || lf->seems_dead || lf->fd < 0)
  511. continue;
  512. if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] &
  513. (LD_BUG|LD_GENERAL)) {
  514. if (lf->fd == STDERR_FILENO)
  515. found_real_stderr = 1;
  516. /* Avoid duplicates */
  517. if (int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, lf->fd))
  518. continue;
  519. sigsafe_log_fds[n_sigsafe_log_fds++] = lf->fd;
  520. if (n_sigsafe_log_fds == MAX_SIGSAFE_FDS)
  521. break;
  522. }
  523. }
  524. if (!found_real_stderr &&
  525. int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, STDOUT_FILENO)) {
  526. /* Don't use a virtual stderr when we're also logging to stdout. */
  527. assert(n_sigsafe_log_fds >= 2); /* Don't use assert inside log functions*/
  528. sigsafe_log_fds[0] = sigsafe_log_fds[--n_sigsafe_log_fds];
  529. }
  530. UNLOCK_LOGS();
  531. }
  532. /** Add to <b>out</b> a copy of every currently configured log file name. Used
  533. * to enable access to these filenames with the sandbox code. */
  534. void
  535. tor_log_get_logfile_names(smartlist_t *out)
  536. {
  537. logfile_t *lf;
  538. tor_assert(out);
  539. LOCK_LOGS();
  540. for (lf = logfiles; lf; lf = lf->next) {
  541. if (lf->is_temporary || lf->is_syslog || lf->callback)
  542. continue;
  543. if (lf->filename == NULL)
  544. continue;
  545. smartlist_add(out, tor_strdup(lf->filename));
  546. }
  547. UNLOCK_LOGS();
  548. }
  549. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  550. #ifdef __GNUC__
  551. /** GCC-based implementation of the log_fn backend, used when we have
  552. * variadic macros. All arguments are as for log_fn, except for
  553. * <b>fn</b>, which is the name of the calling functions. */
  554. void
  555. log_fn_(int severity, log_domain_mask_t domain, const char *fn,
  556. const char *format, ...)
  557. {
  558. va_list ap;
  559. if (severity > log_global_min_severity_)
  560. return;
  561. va_start(ap,format);
  562. logv(severity, domain, fn, NULL, format, ap);
  563. va_end(ap);
  564. }
  565. void
  566. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  567. const char *fn, const char *format, ...)
  568. {
  569. va_list ap;
  570. char *m;
  571. if (severity > log_global_min_severity_)
  572. return;
  573. m = rate_limit_log(ratelim, approx_time());
  574. if (m == NULL)
  575. return;
  576. va_start(ap, format);
  577. logv(severity, domain, fn, m, format, ap);
  578. va_end(ap);
  579. tor_free(m);
  580. }
  581. #else
  582. /** @{ */
  583. /** Variant implementation of log_fn, log_debug, log_info,... for C compilers
  584. * without variadic macros. In this case, the calling function sets
  585. * log_fn_function_name_ to the name of the function, then invokes the
  586. * appropriate log_fn_, log_debug_, etc. */
  587. const char *log_fn_function_name_=NULL;
  588. void
  589. log_fn_(int severity, log_domain_mask_t domain, const char *format, ...)
  590. {
  591. va_list ap;
  592. if (severity > log_global_min_severity_)
  593. return;
  594. va_start(ap,format);
  595. logv(severity, domain, log_fn_function_name_, NULL, format, ap);
  596. va_end(ap);
  597. log_fn_function_name_ = NULL;
  598. }
  599. void
  600. log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
  601. const char *format, ...)
  602. {
  603. va_list ap;
  604. char *m;
  605. if (severity > log_global_min_severity_)
  606. return;
  607. m = rate_limit_log(ratelim, approx_time());
  608. if (m == NULL)
  609. return;
  610. va_start(ap, format);
  611. logv(severity, domain, log_fn_function_name_, m, format, ap);
  612. va_end(ap);
  613. tor_free(m);
  614. }
  615. void
  616. log_debug_(log_domain_mask_t domain, const char *format, ...)
  617. {
  618. va_list ap;
  619. /* For GCC we do this check in the macro. */
  620. if (PREDICT_LIKELY(LOG_DEBUG > log_global_min_severity_))
  621. return;
  622. va_start(ap,format);
  623. logv(LOG_DEBUG, domain, log_fn_function_name_, NULL, format, ap);
  624. va_end(ap);
  625. log_fn_function_name_ = NULL;
  626. }
  627. void
  628. log_info_(log_domain_mask_t domain, const char *format, ...)
  629. {
  630. va_list ap;
  631. if (LOG_INFO > log_global_min_severity_)
  632. return;
  633. va_start(ap,format);
  634. logv(LOG_INFO, domain, log_fn_function_name_, NULL, format, ap);
  635. va_end(ap);
  636. log_fn_function_name_ = NULL;
  637. }
  638. void
  639. log_notice_(log_domain_mask_t domain, const char *format, ...)
  640. {
  641. va_list ap;
  642. if (LOG_NOTICE > log_global_min_severity_)
  643. return;
  644. va_start(ap,format);
  645. logv(LOG_NOTICE, domain, log_fn_function_name_, NULL, format, ap);
  646. va_end(ap);
  647. log_fn_function_name_ = NULL;
  648. }
  649. void
  650. log_warn_(log_domain_mask_t domain, const char *format, ...)
  651. {
  652. va_list ap;
  653. if (LOG_WARN > log_global_min_severity_)
  654. return;
  655. va_start(ap,format);
  656. logv(LOG_WARN, domain, log_fn_function_name_, NULL, format, ap);
  657. va_end(ap);
  658. log_fn_function_name_ = NULL;
  659. }
  660. void
  661. log_err_(log_domain_mask_t domain, const char *format, ...)
  662. {
  663. va_list ap;
  664. if (LOG_ERR > log_global_min_severity_)
  665. return;
  666. va_start(ap,format);
  667. logv(LOG_ERR, domain, log_fn_function_name_, NULL, format, ap);
  668. va_end(ap);
  669. log_fn_function_name_ = NULL;
  670. }
  671. /** @} */
  672. #endif
  673. /** Free all storage held by <b>victim</b>. */
  674. static void
  675. log_free(logfile_t *victim)
  676. {
  677. if (!victim)
  678. return;
  679. tor_free(victim->severities);
  680. tor_free(victim->filename);
  681. tor_free(victim);
  682. }
  683. /** Close all open log files, and free other static memory. */
  684. void
  685. logs_free_all(void)
  686. {
  687. logfile_t *victim, *next;
  688. smartlist_t *messages;
  689. LOCK_LOGS();
  690. next = logfiles;
  691. logfiles = NULL;
  692. messages = pending_cb_messages;
  693. pending_cb_messages = NULL;
  694. UNLOCK_LOGS();
  695. while (next) {
  696. victim = next;
  697. next = next->next;
  698. close_log(victim);
  699. log_free(victim);
  700. }
  701. tor_free(appname);
  702. SMARTLIST_FOREACH(messages, pending_log_message_t *, msg, {
  703. pending_log_message_free(msg);
  704. });
  705. smartlist_free(messages);
  706. /* We _could_ destroy the log mutex here, but that would screw up any logs
  707. * that happened between here and the end of execution. */
  708. }
  709. /** Remove and free the log entry <b>victim</b> from the linked-list
  710. * logfiles (it is probably present, but it might not be due to thread
  711. * racing issues). After this function is called, the caller shouldn't
  712. * refer to <b>victim</b> anymore.
  713. *
  714. * Long-term, we need to do something about races in the log subsystem
  715. * in general. See bug 222 for more details.
  716. */
  717. static void
  718. delete_log(logfile_t *victim)
  719. {
  720. logfile_t *tmpl;
  721. if (victim == logfiles)
  722. logfiles = victim->next;
  723. else {
  724. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  725. // tor_assert(tmpl);
  726. // tor_assert(tmpl->next == victim);
  727. if (!tmpl)
  728. return;
  729. tmpl->next = victim->next;
  730. }
  731. log_free(victim);
  732. }
  733. /** Helper: release system resources (but not memory) held by a single
  734. * logfile_t. */
  735. static void
  736. close_log(logfile_t *victim)
  737. {
  738. if (victim->needs_close && victim->fd >= 0) {
  739. close(victim->fd);
  740. victim->fd = -1;
  741. } else if (victim->is_syslog) {
  742. #ifdef HAVE_SYSLOG_H
  743. if (--syslog_count == 0) {
  744. /* There are no other syslogs; close the logging facility. */
  745. closelog();
  746. }
  747. #endif
  748. }
  749. }
  750. /** Adjust a log severity configuration in <b>severity_out</b> to contain
  751. * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
  752. */
  753. void
  754. set_log_severity_config(int loglevelMin, int loglevelMax,
  755. log_severity_list_t *severity_out)
  756. {
  757. int i;
  758. tor_assert(loglevelMin >= loglevelMax);
  759. tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
  760. tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
  761. memset(severity_out, 0, sizeof(log_severity_list_t));
  762. for (i = loglevelMin; i >= loglevelMax; --i) {
  763. severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  764. }
  765. }
  766. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  767. * to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */
  768. static void
  769. add_stream_log_impl(const log_severity_list_t *severity,
  770. const char *name, int fd)
  771. {
  772. logfile_t *lf;
  773. lf = tor_malloc_zero(sizeof(logfile_t));
  774. lf->fd = fd;
  775. lf->filename = tor_strdup(name);
  776. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  777. lf->next = logfiles;
  778. logfiles = lf;
  779. log_global_min_severity_ = get_min_log_level();
  780. }
  781. /** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
  782. * to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must
  783. * not use it after calling this function. */
  784. void
  785. add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
  786. {
  787. LOCK_LOGS();
  788. add_stream_log_impl(severity, name, fd);
  789. UNLOCK_LOGS();
  790. }
  791. /** Initialize the global logging facility */
  792. void
  793. init_logging(void)
  794. {
  795. if (!log_mutex_initialized) {
  796. tor_mutex_init(&log_mutex);
  797. log_mutex_initialized = 1;
  798. }
  799. if (pending_cb_messages == NULL)
  800. pending_cb_messages = smartlist_new();
  801. if (pending_startup_messages == NULL)
  802. pending_startup_messages = smartlist_new();
  803. }
  804. /** Set whether we report logging domains as a part of our log messages.
  805. */
  806. void
  807. logs_set_domain_logging(int enabled)
  808. {
  809. LOCK_LOGS();
  810. log_domains_are_logged = enabled;
  811. UNLOCK_LOGS();
  812. }
  813. /** Add a log handler to receive messages during startup (before the real
  814. * logs are initialized).
  815. */
  816. void
  817. add_temp_log(int min_severity)
  818. {
  819. log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
  820. set_log_severity_config(min_severity, LOG_ERR, s);
  821. LOCK_LOGS();
  822. add_stream_log_impl(s, "<temp>", fileno(stdout));
  823. tor_free(s);
  824. logfiles->is_temporary = 1;
  825. UNLOCK_LOGS();
  826. }
  827. /**
  828. * Add a log handler to send messages in <b>severity</b>
  829. * to the function <b>cb</b>.
  830. */
  831. int
  832. add_callback_log(const log_severity_list_t *severity, log_callback cb)
  833. {
  834. logfile_t *lf;
  835. lf = tor_malloc_zero(sizeof(logfile_t));
  836. lf->fd = -1;
  837. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  838. lf->filename = tor_strdup("<callback>");
  839. lf->callback = cb;
  840. lf->next = logfiles;
  841. LOCK_LOGS();
  842. logfiles = lf;
  843. log_global_min_severity_ = get_min_log_level();
  844. UNLOCK_LOGS();
  845. return 0;
  846. }
  847. /** Adjust the configured severity of any logs whose callback function is
  848. * <b>cb</b>. */
  849. void
  850. change_callback_log_severity(int loglevelMin, int loglevelMax,
  851. log_callback cb)
  852. {
  853. logfile_t *lf;
  854. log_severity_list_t severities;
  855. set_log_severity_config(loglevelMin, loglevelMax, &severities);
  856. LOCK_LOGS();
  857. for (lf = logfiles; lf; lf = lf->next) {
  858. if (lf->callback == cb) {
  859. memcpy(lf->severities, &severities, sizeof(severities));
  860. }
  861. }
  862. log_global_min_severity_ = get_min_log_level();
  863. UNLOCK_LOGS();
  864. }
  865. /** If there are any log messages that were generated with LD_NOCB waiting to
  866. * be sent to callback-based loggers, send them now. */
  867. void
  868. flush_pending_log_callbacks(void)
  869. {
  870. logfile_t *lf;
  871. smartlist_t *messages, *messages_tmp;
  872. LOCK_LOGS();
  873. if (0 == smartlist_len(pending_cb_messages)) {
  874. UNLOCK_LOGS();
  875. return;
  876. }
  877. messages = pending_cb_messages;
  878. pending_cb_messages = smartlist_new();
  879. do {
  880. SMARTLIST_FOREACH_BEGIN(messages, pending_log_message_t *, msg) {
  881. const int severity = msg->severity;
  882. const int domain = msg->domain;
  883. for (lf = logfiles; lf; lf = lf->next) {
  884. if (! lf->callback || lf->seems_dead ||
  885. ! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
  886. continue;
  887. }
  888. lf->callback(severity, domain, msg->msg);
  889. }
  890. pending_log_message_free(msg);
  891. } SMARTLIST_FOREACH_END(msg);
  892. smartlist_clear(messages);
  893. messages_tmp = pending_cb_messages;
  894. pending_cb_messages = messages;
  895. messages = messages_tmp;
  896. } while (smartlist_len(messages));
  897. smartlist_free(messages);
  898. UNLOCK_LOGS();
  899. }
  900. /** Close any log handlers added by add_temp_log() or marked by
  901. * mark_logs_temp(). */
  902. void
  903. close_temp_logs(void)
  904. {
  905. logfile_t *lf, **p;
  906. LOCK_LOGS();
  907. for (p = &logfiles; *p; ) {
  908. if ((*p)->is_temporary) {
  909. lf = *p;
  910. /* we use *p here to handle the edge case of the head of the list */
  911. *p = (*p)->next;
  912. close_log(lf);
  913. log_free(lf);
  914. } else {
  915. p = &((*p)->next);
  916. }
  917. }
  918. log_global_min_severity_ = get_min_log_level();
  919. UNLOCK_LOGS();
  920. }
  921. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  922. * live again, and close all non-temporary logs. */
  923. void
  924. rollback_log_changes(void)
  925. {
  926. logfile_t *lf;
  927. LOCK_LOGS();
  928. for (lf = logfiles; lf; lf = lf->next)
  929. lf->is_temporary = ! lf->is_temporary;
  930. UNLOCK_LOGS();
  931. close_temp_logs();
  932. }
  933. /** Configure all log handles to be closed by close_temp_logs(). */
  934. void
  935. mark_logs_temp(void)
  936. {
  937. logfile_t *lf;
  938. LOCK_LOGS();
  939. for (lf = logfiles; lf; lf = lf->next)
  940. lf->is_temporary = 1;
  941. UNLOCK_LOGS();
  942. }
  943. /**
  944. * Add a log handler to send messages to <b>filename</b>. If opening the
  945. * logfile fails, -1 is returned and errno is set appropriately (by open(2)).
  946. */
  947. int
  948. add_file_log(const log_severity_list_t *severity, const char *filename,
  949. const int truncate)
  950. {
  951. int fd;
  952. logfile_t *lf;
  953. int open_flags = O_WRONLY|O_CREAT;
  954. open_flags |= truncate ? O_TRUNC : O_APPEND;
  955. fd = tor_open_cloexec(filename, open_flags, 0644);
  956. if (fd<0)
  957. return -1;
  958. if (tor_fd_seekend(fd)<0) {
  959. close(fd);
  960. return -1;
  961. }
  962. LOCK_LOGS();
  963. add_stream_log_impl(severity, filename, fd);
  964. logfiles->needs_close = 1;
  965. lf = logfiles;
  966. log_global_min_severity_ = get_min_log_level();
  967. if (log_tor_version(lf, 0) < 0) {
  968. delete_log(lf);
  969. }
  970. UNLOCK_LOGS();
  971. return 0;
  972. }
  973. #ifdef HAVE_SYSLOG_H
  974. /**
  975. * Add a log handler to send messages to they system log facility.
  976. */
  977. int
  978. add_syslog_log(const log_severity_list_t *severity)
  979. {
  980. logfile_t *lf;
  981. if (syslog_count++ == 0)
  982. /* This is the first syslog. */
  983. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  984. lf = tor_malloc_zero(sizeof(logfile_t));
  985. lf->fd = -1;
  986. lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
  987. lf->filename = tor_strdup("<syslog>");
  988. lf->is_syslog = 1;
  989. LOCK_LOGS();
  990. lf->next = logfiles;
  991. logfiles = lf;
  992. log_global_min_severity_ = get_min_log_level();
  993. UNLOCK_LOGS();
  994. return 0;
  995. }
  996. #endif
  997. /** If <b>level</b> is a valid log severity, return the corresponding
  998. * numeric value. Otherwise, return -1. */
  999. int
  1000. parse_log_level(const char *level)
  1001. {
  1002. if (!strcasecmp(level, "err"))
  1003. return LOG_ERR;
  1004. if (!strcasecmp(level, "warn"))
  1005. return LOG_WARN;
  1006. if (!strcasecmp(level, "notice"))
  1007. return LOG_NOTICE;
  1008. if (!strcasecmp(level, "info"))
  1009. return LOG_INFO;
  1010. if (!strcasecmp(level, "debug"))
  1011. return LOG_DEBUG;
  1012. return -1;
  1013. }
  1014. /** Return the string equivalent of a given log level. */
  1015. const char *
  1016. log_level_to_string(int level)
  1017. {
  1018. return sev_to_string(level);
  1019. }
  1020. /** NULL-terminated array of names for log domains such that domain_list[dom]
  1021. * is a description of <b>dom</b>. */
  1022. static const char *domain_list[] = {
  1023. "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
  1024. "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
  1025. "OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL", NULL
  1026. };
  1027. /** Return a bitmask for the log domain for which <b>domain</b> is the name,
  1028. * or 0 if there is no such name. */
  1029. static log_domain_mask_t
  1030. parse_log_domain(const char *domain)
  1031. {
  1032. int i;
  1033. for (i=0; domain_list[i]; ++i) {
  1034. if (!strcasecmp(domain, domain_list[i]))
  1035. return (1u<<i);
  1036. }
  1037. return 0;
  1038. }
  1039. /** Translate a bitmask of log domains to a string. */
  1040. static char *
  1041. domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen)
  1042. {
  1043. char *cp = buf;
  1044. char *eos = buf+buflen;
  1045. buf[0] = '\0';
  1046. if (! domain)
  1047. return buf;
  1048. while (1) {
  1049. const char *d;
  1050. int bit = tor_log2(domain);
  1051. size_t n;
  1052. if (bit >= N_LOGGING_DOMAINS) {
  1053. tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain);
  1054. return buf+strlen(buf);
  1055. }
  1056. d = domain_list[bit];
  1057. n = strlcpy(cp, d, eos-cp);
  1058. if (n >= buflen) {
  1059. tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain);
  1060. return buf+strlen(buf);
  1061. }
  1062. cp += n;
  1063. domain &= ~(1<<bit);
  1064. if (domain == 0 || (eos-cp) < 2)
  1065. return cp;
  1066. memcpy(cp, ",", 2); /*Nul-terminated ,"*/
  1067. cp++;
  1068. }
  1069. }
  1070. /** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after
  1071. * the end of the severityPattern. Set the value of <b>severity_out</b> to
  1072. * the parsed pattern. Return 0 on success, -1 on failure.
  1073. *
  1074. * The syntax for a SeverityPattern is:
  1075. * <pre>
  1076. * SeverityPattern = *(DomainSeverity SP)* DomainSeverity
  1077. * DomainSeverity = (DomainList SP)? SeverityRange
  1078. * SeverityRange = MinSeverity ("-" MaxSeverity )?
  1079. * DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
  1080. * DomainSpec = "*" | Domain | "~" Domain
  1081. * </pre>
  1082. * A missing MaxSeverity defaults to ERR. Severities and domains are
  1083. * case-insensitive. "~" indicates negation for a domain; negation happens
  1084. * last inside a DomainList. Only one SeverityRange without a DomainList is
  1085. * allowed per line.
  1086. */
  1087. int
  1088. parse_log_severity_config(const char **cfg_ptr,
  1089. log_severity_list_t *severity_out)
  1090. {
  1091. const char *cfg = *cfg_ptr;
  1092. int got_anything = 0;
  1093. int got_an_unqualified_range = 0;
  1094. memset(severity_out, 0, sizeof(*severity_out));
  1095. cfg = eat_whitespace(cfg);
  1096. while (*cfg) {
  1097. const char *dash, *space;
  1098. char *sev_lo, *sev_hi;
  1099. int low, high, i;
  1100. log_domain_mask_t domains = ~0u;
  1101. if (*cfg == '[') {
  1102. int err = 0;
  1103. char *domains_str;
  1104. smartlist_t *domains_list;
  1105. log_domain_mask_t neg_domains = 0;
  1106. const char *closebracket = strchr(cfg, ']');
  1107. if (!closebracket)
  1108. return -1;
  1109. domains = 0;
  1110. domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
  1111. domains_list = smartlist_new();
  1112. smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
  1113. -1);
  1114. tor_free(domains_str);
  1115. SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) {
  1116. if (!strcmp(domain, "*")) {
  1117. domains = ~0u;
  1118. } else {
  1119. int d;
  1120. int negate=0;
  1121. if (*domain == '~') {
  1122. negate = 1;
  1123. ++domain;
  1124. }
  1125. d = parse_log_domain(domain);
  1126. if (!d) {
  1127. log_warn(LD_CONFIG, "No such logging domain as %s", domain);
  1128. err = 1;
  1129. } else {
  1130. if (negate)
  1131. neg_domains |= d;
  1132. else
  1133. domains |= d;
  1134. }
  1135. }
  1136. } SMARTLIST_FOREACH_END(domain);
  1137. SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
  1138. smartlist_free(domains_list);
  1139. if (err)
  1140. return -1;
  1141. if (domains == 0 && neg_domains)
  1142. domains = ~neg_domains;
  1143. else
  1144. domains &= ~neg_domains;
  1145. cfg = eat_whitespace(closebracket+1);
  1146. } else {
  1147. ++got_an_unqualified_range;
  1148. }
  1149. if (!strcasecmpstart(cfg, "file") ||
  1150. !strcasecmpstart(cfg, "stderr") ||
  1151. !strcasecmpstart(cfg, "stdout") ||
  1152. !strcasecmpstart(cfg, "syslog")) {
  1153. goto done;
  1154. }
  1155. if (got_an_unqualified_range > 1)
  1156. return -1;
  1157. space = strchr(cfg, ' ');
  1158. dash = strchr(cfg, '-');
  1159. if (!space)
  1160. space = strchr(cfg, '\0');
  1161. if (dash && dash < space) {
  1162. sev_lo = tor_strndup(cfg, dash-cfg);
  1163. sev_hi = tor_strndup(dash+1, space-(dash+1));
  1164. } else {
  1165. sev_lo = tor_strndup(cfg, space-cfg);
  1166. sev_hi = tor_strdup("ERR");
  1167. }
  1168. low = parse_log_level(sev_lo);
  1169. high = parse_log_level(sev_hi);
  1170. tor_free(sev_lo);
  1171. tor_free(sev_hi);
  1172. if (low == -1)
  1173. return -1;
  1174. if (high == -1)
  1175. return -1;
  1176. got_anything = 1;
  1177. for (i=low; i >= high; --i)
  1178. severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
  1179. cfg = eat_whitespace(space);
  1180. }
  1181. done:
  1182. *cfg_ptr = cfg;
  1183. return got_anything ? 0 : -1;
  1184. }
  1185. /** Return the least severe log level that any current log is interested in. */
  1186. int
  1187. get_min_log_level(void)
  1188. {
  1189. logfile_t *lf;
  1190. int i;
  1191. int min = LOG_ERR;
  1192. for (lf = logfiles; lf; lf = lf->next) {
  1193. for (i = LOG_DEBUG; i > min; --i)
  1194. if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
  1195. min = i;
  1196. }
  1197. return min;
  1198. }
  1199. /** Switch all logs to output at most verbose level. */
  1200. void
  1201. switch_logs_debug(void)
  1202. {
  1203. logfile_t *lf;
  1204. int i;
  1205. LOCK_LOGS();
  1206. for (lf = logfiles; lf; lf=lf->next) {
  1207. for (i = LOG_DEBUG; i >= LOG_ERR; --i)
  1208. lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u;
  1209. }
  1210. log_global_min_severity_ = get_min_log_level();
  1211. UNLOCK_LOGS();
  1212. }
  1213. /** Truncate all the log files. */
  1214. void
  1215. truncate_logs(void)
  1216. {
  1217. logfile_t *lf;
  1218. for (lf = logfiles; lf; lf = lf->next) {
  1219. if (lf->fd >= 0) {
  1220. tor_ftruncate(lf->fd);
  1221. }
  1222. }
  1223. }