2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001, Matej Pfajfar.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2016-02-27 18:48:19 +01:00
|
|
|
* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2002-11-24 09:45:13 +01:00
|
|
|
/* See LICENSE for licensing information */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/**
|
|
|
|
* \file log.c
|
|
|
|
* \brief Functions to send messages to log files or the console.
|
2005-06-11 07:31:17 +02:00
|
|
|
**/
|
2004-05-10 05:53:24 +02:00
|
|
|
|
2004-11-14 18:21:32 +01:00
|
|
|
#include "orconfig.h"
|
2004-04-03 04:40:30 +02:00
|
|
|
#include <stdarg.h>
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <assert.h>
|
2008-12-03 00:36:58 +01:00
|
|
|
// #include <stdio.h>
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <stdlib.h>
|
2004-05-19 22:07:08 +02:00
|
|
|
#include <string.h>
|
2005-08-12 19:24:53 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2008-12-03 00:36:58 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#include "compat.h"
|
2008-03-05 23:31:39 +01:00
|
|
|
#include "util.h"
|
2008-03-13 23:18:38 +01:00
|
|
|
#define LOG_PRIVATE
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "torlog.h"
|
2008-03-05 23:31:39 +01:00
|
|
|
#include "container.h"
|
2004-04-03 06:05:12 +02:00
|
|
|
|
2013-06-06 23:58:28 +02:00
|
|
|
/** Given a severity, yields an index into log_severity_list_t.masks to use
|
|
|
|
* for that severity. */
|
|
|
|
#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @{ */
|
|
|
|
/** The string we stick at the end of a log message when it is too long,
|
|
|
|
* and its length. */
|
2004-08-18 13:21:50 +02:00
|
|
|
#define TRUNCATED_STR "[...truncated]"
|
|
|
|
#define TRUNCATED_STR_LEN 14
|
2011-03-16 22:05:37 +01:00
|
|
|
/** @} */
|
2004-08-15 22:13:07 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Information for a single logfile; only used in log.c */
|
2003-09-16 19:58:36 +02:00
|
|
|
typedef struct logfile_t {
|
2004-05-10 12:27:54 +02:00
|
|
|
struct logfile_t *next; /**< Next logfile_t in the linked list. */
|
2004-05-20 21:47:28 +02:00
|
|
|
char *filename; /**< Filename to open. */
|
2008-12-03 00:36:58 +01:00
|
|
|
int fd; /**< fd to receive log messages, or -1 for none. */
|
2007-05-29 20:21:00 +02:00
|
|
|
int seems_dead; /**< Boolean: true if the stream seems to be kaput. */
|
2004-05-10 05:53:24 +02:00
|
|
|
int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
|
2004-05-20 21:47:28 +02:00
|
|
|
int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
|
2004-10-26 23:48:41 +02:00
|
|
|
int is_syslog; /**< Boolean: send messages to syslog. */
|
2004-11-03 22:53:12 +01:00
|
|
|
log_callback callback; /**< If not NULL, send messages to this function. */
|
2008-12-17 23:58:20 +01:00
|
|
|
log_severity_list_t *severities; /**< Which severity of messages should we
|
|
|
|
* log for each log domain? */
|
2003-09-16 19:58:36 +02:00
|
|
|
} logfile_t;
|
|
|
|
|
2008-03-05 23:31:39 +01:00
|
|
|
static void log_free(logfile_t *victim);
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Helper: map a log severity to descriptive string. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline const char *
|
2005-09-30 03:09:52 +02:00
|
|
|
sev_to_string(int severity)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (severity) {
|
2003-06-25 06:47:54 +02:00
|
|
|
case LOG_DEBUG: return "debug";
|
|
|
|
case LOG_INFO: return "info";
|
2004-03-30 05:15:23 +02:00
|
|
|
case LOG_NOTICE: return "notice";
|
2003-10-10 03:48:03 +02:00
|
|
|
case LOG_WARN: return "warn";
|
2003-06-25 06:47:54 +02:00
|
|
|
case LOG_ERR: return "err";
|
2008-03-11 19:56:41 +01:00
|
|
|
default: /* Call assert, not tor_assert, since tor_assert
|
|
|
|
* calls log on failure. */
|
2016-06-15 23:25:53 +02:00
|
|
|
assert(0); return "UNKNOWN"; // LCOV_EXCL_LINE
|
2002-09-04 02:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Helper: decide whether to include the function name in the log message. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2008-03-05 23:31:39 +01:00
|
|
|
should_log_function_name(log_domain_mask_t domain, int severity)
|
2006-09-29 20:13:29 +02:00
|
|
|
{
|
|
|
|
switch (severity) {
|
|
|
|
case LOG_DEBUG:
|
|
|
|
case LOG_INFO:
|
|
|
|
/* All debugging messages occur in interesting places. */
|
2013-07-25 12:12:35 +02:00
|
|
|
return (domain & LD_NOFUNCNAME) == 0;
|
2006-09-29 20:13:29 +02:00
|
|
|
case LOG_NOTICE:
|
2009-11-22 13:15:30 +01:00
|
|
|
case LOG_WARN:
|
2006-09-29 20:13:29 +02:00
|
|
|
case LOG_ERR:
|
|
|
|
/* We care about places where bugs occur. */
|
2013-07-25 12:12:35 +02:00
|
|
|
return (domain & (LD_BUG|LD_NOFUNCNAME)) == LD_BUG;
|
2006-09-29 20:13:29 +02:00
|
|
|
default:
|
2008-03-11 19:56:41 +01:00
|
|
|
/* Call assert, not tor_assert, since tor_assert calls log on failure. */
|
2016-06-15 23:25:53 +02:00
|
|
|
assert(0); return 0; // LCOV_EXCL_LINE
|
2006-09-29 20:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-02 06:38:53 +01:00
|
|
|
/** A mutex to guard changes to logfiles and logging. */
|
2009-08-20 17:51:34 +02:00
|
|
|
static tor_mutex_t log_mutex;
|
2012-06-05 06:17:54 +02:00
|
|
|
/** True iff we have initialized log_mutex */
|
2009-08-20 17:51:34 +02:00
|
|
|
static int log_mutex_initialized = 0;
|
2008-12-30 05:16:49 +01:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Linked list of logfile_t. */
|
2003-09-16 19:58:36 +02:00
|
|
|
static logfile_t *logfiles = NULL;
|
2011-01-25 21:53:15 +01:00
|
|
|
/** Boolean: do we report logging domains? */
|
|
|
|
static int log_domains_are_logged = 0;
|
|
|
|
|
2004-10-26 23:48:41 +02:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2008-12-22 20:14:08 +01:00
|
|
|
/** The number of open syslog log handlers that we have. When this reaches 0,
|
|
|
|
* we can close our connection to the syslog facility. */
|
2004-10-26 23:48:41 +02:00
|
|
|
static int syslog_count = 0;
|
|
|
|
#endif
|
2003-06-21 21:03:22 +02:00
|
|
|
|
2010-11-20 04:52:32 +01:00
|
|
|
/** Represents a log message that we are going to send to callback-driven
|
|
|
|
* loggers once we can do so in a non-reentrant way. */
|
2014-09-11 04:35:16 +02:00
|
|
|
typedef struct pending_log_message_t {
|
2011-03-16 22:05:37 +01:00
|
|
|
int severity; /**< The severity of the message */
|
|
|
|
log_domain_mask_t domain; /**< The domain of the message */
|
2014-09-11 05:30:37 +02:00
|
|
|
char *fullmsg; /**< The message, with all decorations */
|
2011-03-16 22:05:37 +01:00
|
|
|
char *msg; /**< The content of the message */
|
2014-09-11 04:35:16 +02:00
|
|
|
} pending_log_message_t;
|
2010-11-20 04:52:32 +01:00
|
|
|
|
|
|
|
/** Log messages waiting to be replayed onto callback-based logs */
|
|
|
|
static smartlist_t *pending_cb_messages = NULL;
|
|
|
|
|
2014-09-11 04:35:16 +02:00
|
|
|
/** Log messages waiting to be replayed once the logging system is initialized.
|
|
|
|
*/
|
|
|
|
static smartlist_t *pending_startup_messages = NULL;
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
/** Number of bytes of messages queued in pending_startup_messages. (This is
|
|
|
|
* the length of the messages, not the number of bytes used to store
|
|
|
|
* them.) */
|
|
|
|
static size_t pending_startup_messages_len;
|
|
|
|
|
|
|
|
/** True iff we should store messages while waiting for the logs to get
|
|
|
|
* configured. */
|
|
|
|
static int queue_startup_messages = 1;
|
|
|
|
|
2015-03-14 19:12:03 +01:00
|
|
|
/** True iff __PRETTY_FUNCTION__ includes parenthesized arguments. */
|
|
|
|
static int pretty_fn_has_parens = 0;
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
/** Don't store more than this many bytes of messages while waiting for the
|
|
|
|
* logs to get configured. */
|
|
|
|
#define MAX_STARTUP_MSG_LEN (1<<16)
|
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Lock the log_mutex to prevent others from changing the logfile_t list */
|
2008-01-02 06:38:53 +01:00
|
|
|
#define LOCK_LOGS() STMT_BEGIN \
|
2016-02-05 04:08:58 +01:00
|
|
|
tor_assert(log_mutex_initialized); \
|
2009-08-20 17:51:34 +02:00
|
|
|
tor_mutex_acquire(&log_mutex); \
|
2008-01-02 06:38:53 +01:00
|
|
|
STMT_END
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Unlock the log_mutex */
|
2016-02-05 04:08:58 +01:00
|
|
|
#define UNLOCK_LOGS() STMT_BEGIN \
|
|
|
|
tor_assert(log_mutex_initialized); \
|
|
|
|
tor_mutex_release(&log_mutex); \
|
|
|
|
STMT_END
|
2008-01-02 06:38:53 +01:00
|
|
|
|
2008-12-22 20:14:08 +01:00
|
|
|
/** What's the lowest log level anybody cares about? Checking this lets us
|
|
|
|
* bail out early from log_debug if we aren't debugging. */
|
2012-10-12 18:22:13 +02:00
|
|
|
int log_global_min_severity_ = LOG_NOTICE;
|
2007-02-21 06:57:12 +01:00
|
|
|
|
2004-09-23 06:59:02 +02:00
|
|
|
static void delete_log(logfile_t *victim);
|
2004-10-26 23:48:41 +02:00
|
|
|
static void close_log(logfile_t *victim);
|
2004-09-23 06:59:02 +02:00
|
|
|
|
2011-01-25 21:53:15 +01:00
|
|
|
static char *domain_to_string(log_domain_mask_t domain,
|
|
|
|
char *buf, size_t buflen);
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline char *format_msg(char *buf, size_t buf_len,
|
2012-05-31 01:57:02 +02:00
|
|
|
log_domain_mask_t domain, int severity, const char *funcname,
|
2012-12-26 16:51:58 +01:00
|
|
|
const char *suffix,
|
2012-05-31 01:57:02 +02:00
|
|
|
const char *format, va_list ap, size_t *msg_len_out)
|
2012-12-26 16:51:58 +01:00
|
|
|
CHECK_PRINTF(7,0);
|
2011-01-25 21:53:15 +01:00
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Name of the application: used to generate the message we write at the
|
|
|
|
* start of each new log. */
|
2007-05-22 17:48:46 +02:00
|
|
|
static char *appname = NULL;
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Set the "application name" for the logs to <b>name</b>: we'll use this
|
|
|
|
* name in the message we write when starting up, and at the start of each new
|
|
|
|
* log.
|
|
|
|
*
|
|
|
|
* Tor uses this string to write the version number to the log file. */
|
2007-05-22 17:48:46 +02:00
|
|
|
void
|
|
|
|
log_set_application_name(const char *name)
|
|
|
|
{
|
|
|
|
tor_free(appname);
|
|
|
|
appname = name ? tor_strdup(name) : NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-27 08:13:54 +02:00
|
|
|
/** Log time granularity in milliseconds. */
|
|
|
|
static int log_time_granularity = 1;
|
|
|
|
|
|
|
|
/** Define log time granularity for all logs to be <b>granularity_msec</b>
|
|
|
|
* milliseconds. */
|
|
|
|
void
|
|
|
|
set_log_time_granularity(int granularity_msec)
|
|
|
|
{
|
|
|
|
log_time_granularity = granularity_msec;
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Helper: Write the standard prefix for log lines to a
|
|
|
|
* <b>buf_len</b> character buffer in <b>buf</b>.
|
|
|
|
*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline size_t
|
2012-10-12 18:22:13 +02:00
|
|
|
log_prefix_(char *buf, size_t buf_len, int severity)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2002-09-04 02:39:33 +02:00
|
|
|
time_t t;
|
2002-11-24 09:45:13 +01:00
|
|
|
struct timeval now;
|
2005-02-22 08:03:03 +01:00
|
|
|
struct tm tm;
|
2004-03-09 23:01:17 +01:00
|
|
|
size_t n;
|
2010-08-27 08:13:54 +02:00
|
|
|
int r, ms;
|
2003-09-16 19:58:36 +02:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
tor_gettimeofday(&now);
|
2003-09-16 19:58:36 +02:00
|
|
|
t = (time_t)now.tv_sec;
|
2010-08-27 08:13:54 +02:00
|
|
|
ms = (int)now.tv_usec / 1000;
|
|
|
|
if (log_time_granularity >= 1000) {
|
|
|
|
t -= t % (log_time_granularity / 1000);
|
|
|
|
ms = 0;
|
|
|
|
} else {
|
|
|
|
ms -= ((int)now.tv_usec / 1000) % log_time_granularity;
|
|
|
|
}
|
2003-12-14 10:58:43 +01:00
|
|
|
|
2005-02-22 08:03:03 +01:00
|
|
|
n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
|
2010-08-27 08:13:54 +02:00
|
|
|
r = tor_snprintf(buf+n, buf_len-n, ".%.3i [%s] ", ms,
|
|
|
|
sev_to_string(severity));
|
|
|
|
|
2004-10-27 08:37:34 +02:00
|
|
|
if (r<0)
|
|
|
|
return buf_len-1;
|
|
|
|
else
|
|
|
|
return n+r;
|
2004-06-21 06:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** If lf refers to an actual file that we have just opened, and the file
|
2004-11-20 07:52:13 +01:00
|
|
|
* contains no data, log an "opening new logfile" message at the top.
|
|
|
|
*
|
|
|
|
* Return -1 if the log is broken and needs to be deleted, else return 0.
|
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
static int
|
|
|
|
log_tor_version(logfile_t *lf, int reset)
|
2004-06-21 06:37:27 +02:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
size_t n;
|
2004-06-30 18:35:11 +02:00
|
|
|
int is_new;
|
2004-06-21 06:37:27 +02:00
|
|
|
|
|
|
|
if (!lf->needs_close)
|
|
|
|
/* If it doesn't get closed, it isn't really a file. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2004-06-21 06:37:27 +02:00
|
|
|
if (lf->is_temporary)
|
|
|
|
/* If it's temporary, it isn't really a file. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2008-12-03 00:36:58 +01:00
|
|
|
|
|
|
|
is_new = lf->fd >= 0 && tor_fd_getpos(lf->fd) == 0;
|
|
|
|
|
2004-06-30 18:35:11 +02:00
|
|
|
if (reset && !is_new)
|
|
|
|
/* We are resetting, but we aren't at the start of the file; no
|
|
|
|
* need to log again. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2012-10-12 18:22:13 +02:00
|
|
|
n = log_prefix_(buf, sizeof(buf), LOG_NOTICE);
|
2007-05-22 17:48:46 +02:00
|
|
|
if (appname) {
|
|
|
|
tor_snprintf(buf+n, sizeof(buf)-n,
|
|
|
|
"%s opening %slog file.\n", appname, is_new?"new ":"");
|
|
|
|
} else {
|
|
|
|
tor_snprintf(buf+n, sizeof(buf)-n,
|
|
|
|
"Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
|
|
|
|
}
|
2008-12-03 00:36:58 +01:00
|
|
|
if (write_all(lf->fd, buf, strlen(buf), 0) < 0) /* error */
|
2004-11-20 07:52:13 +01:00
|
|
|
return -1; /* failed */
|
|
|
|
return 0;
|
2004-06-21 06:37:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:46:12 +02:00
|
|
|
static const char bug_suffix[] = " (on Tor " VERSION
|
2015-02-25 16:29:52 +01:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
" "
|
|
|
|
#include "micro-revision.i"
|
|
|
|
#endif
|
|
|
|
")";
|
|
|
|
|
2004-06-21 06:37:27 +02:00
|
|
|
/** Helper: Format a log message into a fixed-sized buffer. (This is
|
|
|
|
* factored out of <b>logv</b> so that we never format a message more
|
2004-10-26 23:48:41 +02:00
|
|
|
* than once.) Return a pointer to the first character of the message
|
|
|
|
* portion of the formatted string.
|
2004-06-21 06:37:27 +02:00
|
|
|
*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline char *
|
2005-09-30 03:09:52 +02:00
|
|
|
format_msg(char *buf, size_t buf_len,
|
2008-03-05 23:31:39 +01:00
|
|
|
log_domain_mask_t domain, int severity, const char *funcname,
|
2012-12-26 16:51:58 +01:00
|
|
|
const char *suffix,
|
2008-12-03 00:36:58 +01:00
|
|
|
const char *format, va_list ap, size_t *msg_len_out)
|
2004-06-21 06:37:27 +02:00
|
|
|
{
|
|
|
|
size_t n;
|
2004-10-27 08:37:34 +02:00
|
|
|
int r;
|
2004-10-26 23:48:41 +02:00
|
|
|
char *end_of_prefix;
|
2011-02-04 14:50:44 +01:00
|
|
|
char *buf_end;
|
2004-11-29 09:40:24 +01:00
|
|
|
|
2011-01-25 21:53:15 +01:00
|
|
|
assert(buf_len >= 16); /* prevent integer underflow and general stupidity */
|
2004-06-21 06:37:27 +02:00
|
|
|
buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
|
2011-02-04 14:50:44 +01:00
|
|
|
buf_end = buf+buf_len; /* point *after* the last char we can write to */
|
2004-06-21 06:37:27 +02:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
n = log_prefix_(buf, buf_len, severity);
|
2004-10-26 23:48:41 +02:00
|
|
|
end_of_prefix = buf+n;
|
2003-09-25 12:42:07 +02:00
|
|
|
|
2011-01-25 21:53:15 +01:00
|
|
|
if (log_domains_are_logged) {
|
|
|
|
char *cp = buf+n;
|
2011-02-04 14:50:44 +01:00
|
|
|
if (cp == buf_end) goto format_msg_no_room_for_domains;
|
2011-01-25 21:53:15 +01:00
|
|
|
*cp++ = '{';
|
2011-02-04 14:50:44 +01:00
|
|
|
if (cp == buf_end) goto format_msg_no_room_for_domains;
|
2011-01-25 21:53:15 +01:00
|
|
|
cp = domain_to_string(domain, cp, (buf+buf_len-cp));
|
2011-02-04 14:50:44 +01:00
|
|
|
if (cp == buf_end) goto format_msg_no_room_for_domains;
|
2011-01-25 21:53:15 +01:00
|
|
|
*cp++ = '}';
|
2011-02-04 14:50:44 +01:00
|
|
|
if (cp == buf_end) goto format_msg_no_room_for_domains;
|
2011-01-25 21:53:15 +01:00
|
|
|
*cp++ = ' ';
|
2011-02-04 14:50:44 +01:00
|
|
|
if (cp == buf_end) goto format_msg_no_room_for_domains;
|
2011-01-25 21:53:15 +01:00
|
|
|
end_of_prefix = cp;
|
|
|
|
n = cp-buf;
|
2011-02-04 14:50:44 +01:00
|
|
|
format_msg_no_room_for_domains:
|
|
|
|
/* This will leave end_of_prefix and n unchanged, and thus cause
|
|
|
|
* whatever log domain string we had written to be clobbered. */
|
2011-02-22 18:52:52 +01:00
|
|
|
;
|
2011-01-25 21:53:15 +01:00
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:29 +02:00
|
|
|
if (funcname && should_log_function_name(domain, severity)) {
|
2015-03-14 19:12:03 +01:00
|
|
|
r = tor_snprintf(buf+n, buf_len-n,
|
|
|
|
pretty_fn_has_parens ? "%s: " : "%s(): ",
|
|
|
|
funcname);
|
2004-10-27 08:37:34 +02:00
|
|
|
if (r<0)
|
|
|
|
n = strlen(buf);
|
|
|
|
else
|
|
|
|
n += r;
|
2003-09-19 11:30:34 +02:00
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
|
2007-03-04 21:11:46 +01:00
|
|
|
if (domain == LD_BUG && buf_len-n > 6) {
|
|
|
|
memcpy(buf+n, "Bug: ", 6);
|
|
|
|
n += 5;
|
|
|
|
}
|
|
|
|
|
2004-10-27 23:14:11 +02:00
|
|
|
r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (r < 0) {
|
2004-11-29 21:39:55 +01:00
|
|
|
/* The message was too long; overwrite the end of the buffer with
|
|
|
|
* "[...truncated]" */
|
|
|
|
if (buf_len >= TRUNCATED_STR_LEN) {
|
2008-02-21 22:57:42 +01:00
|
|
|
size_t offset = buf_len-TRUNCATED_STR_LEN;
|
2004-11-30 07:15:06 +01:00
|
|
|
/* We have an extra 2 characters after buf_len to hold the \n\0,
|
|
|
|
* so it's safe to add 1 to the size here. */
|
|
|
|
strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
|
2004-11-29 21:39:55 +01:00
|
|
|
}
|
|
|
|
/* Set 'n' to the end of the buffer, where we'll be writing \n\0.
|
|
|
|
* Since we already subtracted 2 from buf_len, this is safe.*/
|
|
|
|
n = buf_len;
|
2004-10-27 23:14:11 +02:00
|
|
|
} else {
|
|
|
|
n += r;
|
2012-12-26 16:51:58 +01:00
|
|
|
if (suffix) {
|
|
|
|
size_t suffix_len = strlen(suffix);
|
|
|
|
if (buf_len-n >= suffix_len) {
|
|
|
|
memcpy(buf+n, suffix, suffix_len);
|
|
|
|
n += suffix_len;
|
|
|
|
}
|
|
|
|
}
|
2004-08-15 22:13:07 +02:00
|
|
|
}
|
2015-02-25 16:29:52 +01:00
|
|
|
|
|
|
|
if (domain == LD_BUG &&
|
|
|
|
buf_len - n > strlen(bug_suffix)+1) {
|
|
|
|
memcpy(buf+n, bug_suffix, strlen(bug_suffix));
|
|
|
|
n += strlen(bug_suffix);
|
|
|
|
}
|
|
|
|
|
2003-09-16 19:58:36 +02:00
|
|
|
buf[n]='\n';
|
|
|
|
buf[n+1]='\0';
|
2008-12-03 00:36:58 +01:00
|
|
|
*msg_len_out = n+1;
|
2004-10-26 23:48:41 +02:00
|
|
|
return end_of_prefix;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:35:16 +02:00
|
|
|
/* Create a new pending_log_message_t with appropriate values */
|
|
|
|
static pending_log_message_t *
|
2014-09-11 05:30:37 +02:00
|
|
|
pending_log_message_new(int severity, log_domain_mask_t domain,
|
|
|
|
const char *fullmsg, const char *shortmsg)
|
2014-09-11 04:35:16 +02:00
|
|
|
{
|
|
|
|
pending_log_message_t *m = tor_malloc(sizeof(pending_log_message_t));
|
|
|
|
m->severity = severity;
|
|
|
|
m->domain = domain;
|
2014-09-11 05:30:37 +02:00
|
|
|
m->fullmsg = fullmsg ? tor_strdup(fullmsg) : NULL;
|
|
|
|
m->msg = tor_strdup(shortmsg);
|
2014-09-11 04:35:16 +02:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release all storage held by <b>msg</b>. */
|
|
|
|
static void
|
|
|
|
pending_log_message_free(pending_log_message_t *msg)
|
|
|
|
{
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
tor_free(msg->msg);
|
2014-09-11 05:30:37 +02:00
|
|
|
tor_free(msg->fullmsg);
|
2014-09-11 04:35:16 +02:00
|
|
|
tor_free(msg);
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:35:46 +02:00
|
|
|
/** Return true iff <b>lf</b> would like to receive a message with the
|
|
|
|
* specified <b>severity</b> in the specified <b>domain</b>.
|
2014-09-11 04:39:55 +02:00
|
|
|
*/
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2014-09-11 04:39:55 +02:00
|
|
|
logfile_wants_message(const logfile_t *lf, int severity,
|
|
|
|
log_domain_mask_t domain)
|
|
|
|
{
|
|
|
|
if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (! (lf->fd >= 0 || lf->is_syslog || lf->callback)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (lf->seems_dead) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:58:36 +02:00
|
|
|
/** Send a message to <b>lf</b>. The full message, with time prefix and
|
|
|
|
* severity, is in <b>buf</b>. The message itself is in
|
|
|
|
* <b>msg_after_prefix</b>. If <b>callbacks_deferred</b> points to true, then
|
|
|
|
* we already deferred this message for pending callbacks and don't need to do
|
|
|
|
* it again. Otherwise, if we need to do it, do it, and set
|
|
|
|
* <b>callbacks_deferred</b> to 1. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline void
|
2014-09-11 04:58:36 +02:00
|
|
|
logfile_deliver(logfile_t *lf, const char *buf, size_t msg_len,
|
|
|
|
const char *msg_after_prefix, log_domain_mask_t domain,
|
|
|
|
int severity, int *callbacks_deferred)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (lf->is_syslog) {
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
#ifdef MAXLINE
|
|
|
|
/* Some syslog implementations have limits on the length of what you can
|
|
|
|
* pass them, and some very old ones do not detect overflow so well.
|
|
|
|
* Regrettably, they call their maximum line length MAXLINE. */
|
|
|
|
#if MAXLINE < 64
|
|
|
|
#warn "MAXLINE is a very low number; it might not be from syslog.h after all"
|
|
|
|
#endif
|
|
|
|
char *m = msg_after_prefix;
|
|
|
|
if (msg_len >= MAXLINE)
|
|
|
|
m = tor_strndup(msg_after_prefix, MAXLINE-1);
|
|
|
|
syslog(severity, "%s", m);
|
|
|
|
if (m != msg_after_prefix) {
|
|
|
|
tor_free(m);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* We have syslog but not MAXLINE. That's promising! */
|
|
|
|
syslog(severity, "%s", msg_after_prefix);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} else if (lf->callback) {
|
|
|
|
if (domain & LD_NOCB) {
|
|
|
|
if (!*callbacks_deferred && pending_cb_messages) {
|
|
|
|
smartlist_add(pending_cb_messages,
|
2014-09-11 05:30:37 +02:00
|
|
|
pending_log_message_new(severity,domain,NULL,msg_after_prefix));
|
2014-09-11 04:58:36 +02:00
|
|
|
*callbacks_deferred = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lf->callback(severity, domain, msg_after_prefix);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (write_all(lf->fd, buf, msg_len, 0) < 0) { /* error */
|
|
|
|
/* don't log the error! mark this log entry to be blown away, and
|
|
|
|
* continue. */
|
|
|
|
lf->seems_dead = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Helper: sends a message to the appropriate logfiles, at loglevel
|
|
|
|
* <b>severity</b>. If provided, <b>funcname</b> is prepended to the
|
2004-10-27 08:37:34 +02:00
|
|
|
* message. The actual message is derived as from tor_snprintf(format,ap).
|
2004-05-10 05:53:24 +02:00
|
|
|
*/
|
2014-04-15 14:20:34 +02:00
|
|
|
MOCK_IMPL(STATIC void,
|
|
|
|
logv,(int severity, log_domain_mask_t domain, const char *funcname,
|
|
|
|
const char *suffix, const char *format, va_list ap))
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
2014-12-21 19:36:06 +01:00
|
|
|
char buf[10240];
|
2008-12-03 00:36:58 +01:00
|
|
|
size_t msg_len = 0;
|
2003-09-16 19:58:36 +02:00
|
|
|
int formatted = 0;
|
|
|
|
logfile_t *lf;
|
2004-10-26 23:48:41 +02:00
|
|
|
char *end_of_prefix=NULL;
|
2010-12-11 14:26:36 +01:00
|
|
|
int callbacks_deferred = 0;
|
2003-12-14 10:58:43 +01:00
|
|
|
|
2008-03-11 19:56:41 +01:00
|
|
|
/* Call assert, not tor_assert, since tor_assert calls log on failure. */
|
2003-06-21 21:03:22 +02:00
|
|
|
assert(format);
|
2008-03-13 17:56:14 +01:00
|
|
|
/* check that severity is sane. Overrunning the masks array leads to
|
|
|
|
* interesting and hard to diagnose effects */
|
|
|
|
assert(severity >= LOG_ERR && severity <= LOG_DEBUG);
|
2016-02-05 04:08:58 +01:00
|
|
|
/* check that we've initialised the log mutex before we try to lock it */
|
|
|
|
assert(log_mutex_initialized);
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2010-11-20 04:52:32 +01:00
|
|
|
|
2016-02-05 04:14:17 +01:00
|
|
|
if ((! (domain & LD_NOCB)) && pending_cb_messages
|
|
|
|
&& smartlist_len(pending_cb_messages))
|
2010-11-20 04:52:32 +01:00
|
|
|
flush_pending_log_callbacks();
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
if (queue_startup_messages &&
|
|
|
|
pending_startup_messages_len < MAX_STARTUP_MSG_LEN) {
|
|
|
|
end_of_prefix =
|
|
|
|
format_msg(buf, sizeof(buf), domain, severity, funcname, suffix,
|
|
|
|
format, ap, &msg_len);
|
|
|
|
formatted = 1;
|
|
|
|
|
|
|
|
smartlist_add(pending_startup_messages,
|
|
|
|
pending_log_message_new(severity,domain,buf,end_of_prefix));
|
|
|
|
pending_startup_messages_len += msg_len;
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:39:55 +02:00
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (! logfile_wants_message(lf, severity, domain))
|
2007-05-29 20:21:00 +02:00
|
|
|
continue;
|
2002-07-12 20:14:17 +02:00
|
|
|
|
2003-09-16 19:58:36 +02:00
|
|
|
if (!formatted) {
|
2004-10-26 23:48:41 +02:00
|
|
|
end_of_prefix =
|
2012-12-26 16:51:58 +01:00
|
|
|
format_msg(buf, sizeof(buf), domain, severity, funcname, suffix,
|
|
|
|
format, ap, &msg_len);
|
2003-09-16 19:58:36 +02:00
|
|
|
formatted = 1;
|
|
|
|
}
|
2011-01-25 21:53:15 +01:00
|
|
|
|
2014-09-11 04:58:36 +02:00
|
|
|
logfile_deliver(lf, buf, msg_len, end_of_prefix, domain, severity,
|
|
|
|
&callbacks_deferred);
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2003-06-21 21:03:22 +02:00
|
|
|
}
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2011-03-16 22:05:37 +01:00
|
|
|
/** Output a message to the log. It gets logged to all logfiles that
|
|
|
|
* care about messages with <b>severity</b> in <b>domain</b>. The content
|
2011-05-15 17:23:29 +02:00
|
|
|
* is formatted printf-style based on <b>format</b> and extra arguments.
|
2011-03-16 22:05:37 +01:00
|
|
|
* */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2009-12-15 20:32:55 +01:00
|
|
|
tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
|
2003-06-17 23:36:44 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (severity > log_global_min_severity_)
|
2008-01-02 06:38:53 +01:00
|
|
|
return;
|
2003-06-17 23:36:44 +02:00
|
|
|
va_start(ap,format);
|
2012-12-26 16:51:58 +01:00
|
|
|
logv(severity, domain, NULL, NULL, format, ap);
|
2003-06-17 23:36:44 +02:00
|
|
|
va_end(ap);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2002-09-04 02:39:33 +02:00
|
|
|
|
2013-07-20 04:47:49 +02:00
|
|
|
/** Maximum number of fds that will get notifications if we crash */
|
|
|
|
#define MAX_SIGSAFE_FDS 8
|
|
|
|
/** Array of fds to log crash-style warnings to. */
|
|
|
|
static int sigsafe_log_fds[MAX_SIGSAFE_FDS] = { STDERR_FILENO };
|
|
|
|
/** The number of elements used in sigsafe_log_fds */
|
|
|
|
static int n_sigsafe_log_fds = 1;
|
|
|
|
|
|
|
|
/** Write <b>s</b> to each element of sigsafe_log_fds. Return 0 on success, -1
|
|
|
|
* on failure. */
|
|
|
|
static int
|
|
|
|
tor_log_err_sigsafe_write(const char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ssize_t r;
|
|
|
|
size_t len = strlen(s);
|
|
|
|
int err = 0;
|
|
|
|
for (i=0; i < n_sigsafe_log_fds; ++i) {
|
|
|
|
r = write(sigsafe_log_fds[i], s, len);
|
|
|
|
err += (r != (ssize_t)len);
|
|
|
|
}
|
|
|
|
return err ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a list of string arguments ending with a NULL, writes them
|
|
|
|
* to our logs and to stderr (if possible). This function is safe to call
|
|
|
|
* from within a signal handler. */
|
|
|
|
void
|
|
|
|
tor_log_err_sigsafe(const char *m, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *x;
|
2013-11-22 18:14:11 +01:00
|
|
|
char timebuf[33];
|
2013-07-20 04:47:49 +02:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
if (log_time_granularity >= 2000) {
|
|
|
|
int g = log_time_granularity / 1000;
|
|
|
|
now -= now % g;
|
|
|
|
}
|
2013-11-22 18:14:11 +01:00
|
|
|
timebuf[0] = now < 0 ? '-' : ' ';
|
|
|
|
if (now < 0) now = -now;
|
|
|
|
timebuf[1] = '\0';
|
|
|
|
format_dec_number_sigsafe(now, timebuf+1, sizeof(timebuf)-1);
|
2013-07-20 04:47:49 +02:00
|
|
|
tor_log_err_sigsafe_write("\n=========================================="
|
|
|
|
"================== T=");
|
|
|
|
tor_log_err_sigsafe_write(timebuf);
|
|
|
|
tor_log_err_sigsafe_write("\n");
|
|
|
|
tor_log_err_sigsafe_write(m);
|
|
|
|
va_start(ap, m);
|
|
|
|
while ((x = va_arg(ap, const char*))) {
|
|
|
|
tor_log_err_sigsafe_write(x);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set *<b>out</b> to a pointer to an array of the fds to log errors to from
|
|
|
|
* inside a signal handler. Return the number of elements in the array. */
|
|
|
|
int
|
|
|
|
tor_log_get_sigsafe_err_fds(const int **out)
|
|
|
|
{
|
|
|
|
*out = sigsafe_log_fds;
|
|
|
|
return n_sigsafe_log_fds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper function; return true iff the <b>n</b>-element array <b>array</b>
|
|
|
|
* contains <b>item</b>. */
|
|
|
|
static int
|
|
|
|
int_array_contains(const int *array, int n, int item)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
if (array[j] == item)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Function to call whenever the list of logs changes to get ready to log
|
|
|
|
* from signal handlers. */
|
|
|
|
void
|
|
|
|
tor_log_update_sigsafe_err_fds(void)
|
|
|
|
{
|
|
|
|
const logfile_t *lf;
|
2013-07-25 12:12:35 +02:00
|
|
|
int found_real_stderr = 0;
|
2013-07-20 04:47:49 +02:00
|
|
|
|
|
|
|
LOCK_LOGS();
|
2013-07-25 12:12:35 +02:00
|
|
|
/* Reserve the first one for stderr. This is safe because when we daemonize,
|
|
|
|
* we dup2 /dev/null to stderr, */
|
2013-07-20 04:47:49 +02:00
|
|
|
sigsafe_log_fds[0] = STDERR_FILENO;
|
|
|
|
n_sigsafe_log_fds = 1;
|
|
|
|
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
/* Don't try callback to the control port, or syslogs: We can't
|
|
|
|
* do them from a signal handler. Don't try stdout: we always do stderr.
|
|
|
|
*/
|
|
|
|
if (lf->is_temporary || lf->is_syslog ||
|
|
|
|
lf->callback || lf->seems_dead || lf->fd < 0)
|
|
|
|
continue;
|
|
|
|
if (lf->severities->masks[SEVERITY_MASK_IDX(LOG_ERR)] &
|
|
|
|
(LD_BUG|LD_GENERAL)) {
|
2013-07-25 12:12:35 +02:00
|
|
|
if (lf->fd == STDERR_FILENO)
|
|
|
|
found_real_stderr = 1;
|
2013-07-20 04:47:49 +02:00
|
|
|
/* Avoid duplicates */
|
|
|
|
if (int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, lf->fd))
|
|
|
|
continue;
|
|
|
|
sigsafe_log_fds[n_sigsafe_log_fds++] = lf->fd;
|
|
|
|
if (n_sigsafe_log_fds == MAX_SIGSAFE_FDS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 12:12:35 +02:00
|
|
|
|
|
|
|
if (!found_real_stderr &&
|
|
|
|
int_array_contains(sigsafe_log_fds, n_sigsafe_log_fds, STDOUT_FILENO)) {
|
|
|
|
/* Don't use a virtual stderr when we're also logging to stdout. */
|
|
|
|
assert(n_sigsafe_log_fds >= 2); /* Don't use assert inside log functions*/
|
|
|
|
sigsafe_log_fds[0] = sigsafe_log_fds[--n_sigsafe_log_fds];
|
|
|
|
}
|
|
|
|
|
2013-07-20 04:47:49 +02:00
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
2014-05-20 21:21:27 +02:00
|
|
|
/** Add to <b>out</b> a copy of every currently configured log file name. Used
|
|
|
|
* to enable access to these filenames with the sandbox code. */
|
|
|
|
void
|
|
|
|
tor_log_get_logfile_names(smartlist_t *out)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
tor_assert(out);
|
|
|
|
|
|
|
|
LOCK_LOGS();
|
|
|
|
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (lf->is_temporary || lf->is_syslog || lf->callback)
|
|
|
|
continue;
|
|
|
|
if (lf->filename == NULL)
|
|
|
|
continue;
|
|
|
|
smartlist_add(out, tor_strdup(lf->filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:43:35 +02:00
|
|
|
/** Implementation of the log_fn backend, used when we have
|
2011-03-16 22:11:27 +01:00
|
|
|
* variadic macros. All arguments are as for log_fn, except for
|
|
|
|
* <b>fn</b>, which is the name of the calling functions. */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2012-10-12 18:22:13 +02:00
|
|
|
log_fn_(int severity, log_domain_mask_t domain, const char *fn,
|
2008-03-05 23:31:39 +01:00
|
|
|
const char *format, ...)
|
2003-06-17 23:36:44 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-10-12 18:22:13 +02:00
|
|
|
if (severity > log_global_min_severity_)
|
2008-01-02 06:38:53 +01:00
|
|
|
return;
|
2003-06-17 23:36:44 +02:00
|
|
|
va_start(ap,format);
|
2012-12-26 16:51:58 +01:00
|
|
|
logv(severity, domain, fn, NULL, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
|
|
|
|
const char *fn, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *m;
|
|
|
|
if (severity > log_global_min_severity_)
|
|
|
|
return;
|
|
|
|
m = rate_limit_log(ratelim, approx_time());
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
va_start(ap, format);
|
|
|
|
logv(severity, domain, fn, m, format, ap);
|
2003-06-17 23:36:44 +02:00
|
|
|
va_end(ap);
|
2012-12-26 16:51:58 +01:00
|
|
|
tor_free(m);
|
2003-06-17 23:36:44 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Free all storage held by <b>victim</b>. */
|
2008-03-05 23:31:39 +01:00
|
|
|
static void
|
|
|
|
log_free(logfile_t *victim)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!victim)
|
|
|
|
return;
|
2008-03-05 23:31:39 +01:00
|
|
|
tor_free(victim->severities);
|
|
|
|
tor_free(victim->filename);
|
|
|
|
tor_free(victim);
|
|
|
|
}
|
|
|
|
|
2007-07-16 06:33:47 +02:00
|
|
|
/** Close all open log files, and free other static memory. */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2007-07-16 06:33:47 +02:00
|
|
|
logs_free_all(void)
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
2006-03-13 01:54:21 +01:00
|
|
|
logfile_t *victim, *next;
|
2014-09-11 05:30:37 +02:00
|
|
|
smartlist_t *messages, *messages2;
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2006-03-13 01:54:21 +01:00
|
|
|
next = logfiles;
|
|
|
|
logfiles = NULL;
|
2011-04-07 21:25:33 +02:00
|
|
|
messages = pending_cb_messages;
|
|
|
|
pending_cb_messages = NULL;
|
2014-09-11 05:30:37 +02:00
|
|
|
messages2 = pending_startup_messages;
|
|
|
|
pending_startup_messages = NULL;
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2006-03-13 01:54:21 +01:00
|
|
|
while (next) {
|
|
|
|
victim = next;
|
|
|
|
next = next->next;
|
2004-10-26 23:48:41 +02:00
|
|
|
close_log(victim);
|
2008-03-05 23:31:39 +01:00
|
|
|
log_free(victim);
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2007-07-16 06:33:47 +02:00
|
|
|
tor_free(appname);
|
2009-08-20 17:51:34 +02:00
|
|
|
|
2014-09-11 04:35:16 +02:00
|
|
|
SMARTLIST_FOREACH(messages, pending_log_message_t *, msg, {
|
|
|
|
pending_log_message_free(msg);
|
2011-04-07 21:25:33 +02:00
|
|
|
});
|
|
|
|
smartlist_free(messages);
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
if (messages2) {
|
|
|
|
SMARTLIST_FOREACH(messages2, pending_log_message_t *, msg, {
|
|
|
|
pending_log_message_free(msg);
|
|
|
|
});
|
|
|
|
smartlist_free(messages2);
|
|
|
|
}
|
|
|
|
|
2009-08-20 17:51:34 +02:00
|
|
|
/* We _could_ destroy the log mutex here, but that would screw up any logs
|
|
|
|
* that happened between here and the end of execution. */
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2004-09-23 06:59:02 +02:00
|
|
|
/** Remove and free the log entry <b>victim</b> from the linked-list
|
2006-06-09 08:35:45 +02:00
|
|
|
* logfiles (it is probably present, but it might not be due to thread
|
|
|
|
* racing issues). After this function is called, the caller shouldn't
|
|
|
|
* refer to <b>victim</b> anymore.
|
|
|
|
*
|
|
|
|
* Long-term, we need to do something about races in the log subsystem
|
|
|
|
* in general. See bug 222 for more details.
|
2004-09-23 06:59:02 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
static void
|
|
|
|
delete_log(logfile_t *victim)
|
|
|
|
{
|
2004-09-23 06:59:02 +02:00
|
|
|
logfile_t *tmpl;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim == logfiles)
|
2004-09-23 06:59:02 +02:00
|
|
|
logfiles = victim->next;
|
|
|
|
else {
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
|
2006-06-09 08:35:45 +02:00
|
|
|
// tor_assert(tmpl);
|
|
|
|
// tor_assert(tmpl->next == victim);
|
|
|
|
if (!tmpl)
|
|
|
|
return;
|
2004-09-23 06:59:02 +02:00
|
|
|
tmpl->next = victim->next;
|
|
|
|
}
|
2008-03-05 23:31:39 +01:00
|
|
|
log_free(victim);
|
2004-09-23 06:59:02 +02:00
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Helper: release system resources (but not memory) held by a single
|
|
|
|
* logfile_t. */
|
2005-09-30 03:09:52 +02:00
|
|
|
static void
|
|
|
|
close_log(logfile_t *victim)
|
2004-10-26 23:48:41 +02:00
|
|
|
{
|
2008-12-03 00:36:58 +01:00
|
|
|
if (victim->needs_close && victim->fd >= 0) {
|
|
|
|
close(victim->fd);
|
|
|
|
victim->fd = -1;
|
2004-10-26 23:48:41 +02:00
|
|
|
} else if (victim->is_syslog) {
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
2005-08-08 19:31:57 +02:00
|
|
|
if (--syslog_count == 0) {
|
2004-10-26 23:48:41 +02:00
|
|
|
/* There are no other syslogs; close the logging facility. */
|
|
|
|
closelog();
|
2005-08-08 19:31:57 +02:00
|
|
|
}
|
2004-10-26 23:48:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-13 23:18:38 +01:00
|
|
|
/** Adjust a log severity configuration in <b>severity_out</b> to contain
|
|
|
|
* every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_log_severity_config(int loglevelMin, int loglevelMax,
|
|
|
|
log_severity_list_t *severity_out)
|
2008-03-05 23:31:39 +01:00
|
|
|
{
|
|
|
|
int i;
|
2008-03-13 23:18:38 +01:00
|
|
|
tor_assert(loglevelMin >= loglevelMax);
|
|
|
|
tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
|
|
|
|
tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
|
|
|
|
memset(severity_out, 0, sizeof(log_severity_list_t));
|
2008-03-05 23:31:39 +01:00
|
|
|
for (i = loglevelMin; i >= loglevelMax; --i) {
|
2008-03-13 23:18:38 +01:00
|
|
|
severity_out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
|
2008-03-05 23:31:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
|
2008-12-03 00:36:58 +01:00
|
|
|
* to <b>fd</b>. Copies <b>severity</b>. Helper: does no locking. */
|
2008-01-02 06:38:53 +01:00
|
|
|
static void
|
2008-09-05 21:52:44 +02:00
|
|
|
add_stream_log_impl(const log_severity_list_t *severity,
|
2008-12-03 00:36:58 +01:00
|
|
|
const char *name, int fd)
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2004-10-26 23:48:41 +02:00
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
2008-12-03 00:36:58 +01:00
|
|
|
lf->fd = fd;
|
2004-05-20 21:47:28 +02:00
|
|
|
lf->filename = tor_strdup(name);
|
2008-03-08 02:11:54 +01:00
|
|
|
lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
|
2003-09-16 19:58:36 +02:00
|
|
|
lf->next = logfiles;
|
2007-02-21 06:57:12 +01:00
|
|
|
|
2008-01-02 06:38:53 +01:00
|
|
|
logfiles = lf;
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
|
|
|
|
2008-03-05 23:31:39 +01:00
|
|
|
/** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
|
2008-12-03 00:36:58 +01:00
|
|
|
* to <b>fd</b>. Steals a reference to <b>severity</b>; the caller must
|
2008-03-05 23:31:39 +01:00
|
|
|
* not use it after calling this function. */
|
2008-01-02 06:38:53 +01:00
|
|
|
void
|
2011-01-25 21:53:15 +01:00
|
|
|
add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
|
2008-01-02 06:38:53 +01:00
|
|
|
{
|
|
|
|
LOCK_LOGS();
|
2008-12-03 00:36:58 +01:00
|
|
|
add_stream_log_impl(severity, name, fd);
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize the global logging facility */
|
|
|
|
void
|
2014-09-11 05:30:37 +02:00
|
|
|
init_logging(int disable_startup_queue)
|
2008-01-02 06:38:53 +01:00
|
|
|
{
|
2009-08-20 17:51:34 +02:00
|
|
|
if (!log_mutex_initialized) {
|
|
|
|
tor_mutex_init(&log_mutex);
|
|
|
|
log_mutex_initialized = 1;
|
|
|
|
}
|
2015-03-14 19:12:03 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
if (strchr(__PRETTY_FUNCTION__, '(')) {
|
|
|
|
pretty_fn_has_parens = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-20 04:52:32 +01:00
|
|
|
if (pending_cb_messages == NULL)
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_cb_messages = smartlist_new();
|
2014-09-11 05:30:37 +02:00
|
|
|
if (disable_startup_queue)
|
|
|
|
queue_startup_messages = 0;
|
|
|
|
if (pending_startup_messages == NULL && queue_startup_messages) {
|
2014-09-11 04:35:16 +02:00
|
|
|
pending_startup_messages = smartlist_new();
|
2014-09-11 05:30:37 +02:00
|
|
|
}
|
2008-01-02 06:38:53 +01:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:53:15 +01:00
|
|
|
/** Set whether we report logging domains as a part of our log messages.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
logs_set_domain_logging(int enabled)
|
|
|
|
{
|
|
|
|
LOCK_LOGS();
|
|
|
|
log_domains_are_logged = enabled;
|
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:47:28 +02:00
|
|
|
/** Add a log handler to receive messages during startup (before the real
|
|
|
|
* logs are initialized).
|
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
2008-03-27 18:25:49 +01:00
|
|
|
add_temp_log(int min_severity)
|
2004-05-20 21:47:28 +02:00
|
|
|
{
|
2008-03-13 23:18:38 +01:00
|
|
|
log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
|
2008-03-27 18:25:49 +01:00
|
|
|
set_log_severity_config(min_severity, LOG_ERR, s);
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2008-12-03 00:36:58 +01:00
|
|
|
add_stream_log_impl(s, "<temp>", fileno(stdout));
|
2008-03-26 19:59:45 +01:00
|
|
|
tor_free(s);
|
2004-05-20 21:47:28 +02:00
|
|
|
logfiles->is_temporary = 1;
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-05-20 21:47:28 +02:00
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/**
|
2008-03-05 23:31:39 +01:00
|
|
|
* Add a log handler to send messages in <b>severity</b>
|
|
|
|
* to the function <b>cb</b>.
|
2005-03-17 13:38:37 +01:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2008-09-05 21:52:44 +02:00
|
|
|
add_callback_log(const log_severity_list_t *severity, log_callback cb)
|
2004-11-03 19:27:19 +01:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
2008-12-03 00:36:58 +01:00
|
|
|
lf->fd = -1;
|
2008-03-08 02:11:54 +01:00
|
|
|
lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
|
2016-01-12 21:39:49 +01:00
|
|
|
lf->filename = tor_strdup("<callback>");
|
2004-11-03 19:27:19 +01:00
|
|
|
lf->callback = cb;
|
|
|
|
lf->next = logfiles;
|
2007-02-21 06:57:12 +01:00
|
|
|
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
|
|
|
logfiles = lf;
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-11-03 19:27:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 20:13:37 +02:00
|
|
|
/** Adjust the configured severity of any logs whose callback function is
|
|
|
|
* <b>cb</b>. */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
change_callback_log_severity(int loglevelMin, int loglevelMax,
|
|
|
|
log_callback cb)
|
2005-04-06 00:56:17 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2008-03-17 04:37:52 +01:00
|
|
|
log_severity_list_t severities;
|
|
|
|
set_log_severity_config(loglevelMin, loglevelMax, &severities);
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2005-04-06 00:56:17 +02:00
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (lf->callback == cb) {
|
2008-03-17 04:37:52 +01:00
|
|
|
memcpy(lf->severities, &severities, sizeof(severities));
|
2005-04-06 00:56:17 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2005-04-06 00:56:17 +02:00
|
|
|
}
|
|
|
|
|
2011-01-13 00:38:52 +01:00
|
|
|
/** If there are any log messages that were generated with LD_NOCB waiting to
|
2010-11-20 04:52:32 +01:00
|
|
|
* be sent to callback-based loggers, send them now. */
|
|
|
|
void
|
|
|
|
flush_pending_log_callbacks(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
smartlist_t *messages, *messages_tmp;
|
|
|
|
|
|
|
|
LOCK_LOGS();
|
2016-02-05 04:14:17 +01:00
|
|
|
if (!pending_cb_messages || 0 == smartlist_len(pending_cb_messages)) {
|
2010-11-20 04:52:32 +01:00
|
|
|
UNLOCK_LOGS();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
messages = pending_cb_messages;
|
2012-01-18 21:53:30 +01:00
|
|
|
pending_cb_messages = smartlist_new();
|
2010-11-20 04:52:32 +01:00
|
|
|
do {
|
2014-09-11 04:35:16 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(messages, pending_log_message_t *, msg) {
|
2010-11-20 04:52:32 +01:00
|
|
|
const int severity = msg->severity;
|
|
|
|
const int domain = msg->domain;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (! lf->callback || lf->seems_dead ||
|
|
|
|
! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lf->callback(severity, domain, msg->msg);
|
|
|
|
}
|
2014-09-11 04:35:16 +02:00
|
|
|
pending_log_message_free(msg);
|
2010-11-20 04:52:32 +01:00
|
|
|
} SMARTLIST_FOREACH_END(msg);
|
|
|
|
smartlist_clear(messages);
|
|
|
|
|
|
|
|
messages_tmp = pending_cb_messages;
|
|
|
|
pending_cb_messages = messages;
|
|
|
|
messages = messages_tmp;
|
|
|
|
} while (smartlist_len(messages));
|
|
|
|
|
|
|
|
smartlist_free(messages);
|
|
|
|
|
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
/** Flush all the messages we stored from startup while waiting for log
|
|
|
|
* initialization.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
flush_log_messages_from_startup(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
|
|
|
|
LOCK_LOGS();
|
|
|
|
queue_startup_messages = 0;
|
|
|
|
pending_startup_messages_len = 0;
|
|
|
|
if (! pending_startup_messages)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(pending_startup_messages, pending_log_message_t *,
|
|
|
|
msg) {
|
|
|
|
int callbacks_deferred = 0;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (! logfile_wants_message(lf, msg->severity, msg->domain))
|
|
|
|
continue;
|
|
|
|
|
2015-02-06 21:17:48 +01:00
|
|
|
/* We configure a temporary startup log that goes to stdout, so we
|
|
|
|
* shouldn't replay to stdout/stderr*/
|
|
|
|
if (lf->fd == STDOUT_FILENO || lf->fd == STDERR_FILENO) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
logfile_deliver(lf, msg->fullmsg, strlen(msg->fullmsg), msg->msg,
|
|
|
|
msg->domain, msg->severity, &callbacks_deferred);
|
|
|
|
}
|
|
|
|
pending_log_message_free(msg);
|
|
|
|
} SMARTLIST_FOREACH_END(msg);
|
|
|
|
smartlist_free(pending_startup_messages);
|
|
|
|
pending_startup_messages = NULL;
|
|
|
|
|
|
|
|
out:
|
|
|
|
UNLOCK_LOGS();
|
|
|
|
}
|
|
|
|
|
2008-03-26 19:59:45 +01:00
|
|
|
/** Close any log handlers added by add_temp_log() or marked by
|
|
|
|
* mark_logs_temp(). */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
close_temp_logs(void)
|
2004-05-20 21:47:28 +02:00
|
|
|
{
|
2004-11-20 13:16:47 +01:00
|
|
|
logfile_t *lf, **p;
|
2008-01-02 06:38:53 +01:00
|
|
|
|
|
|
|
LOCK_LOGS();
|
2004-11-20 13:16:47 +01:00
|
|
|
for (p = &logfiles; *p; ) {
|
|
|
|
if ((*p)->is_temporary) {
|
|
|
|
lf = *p;
|
2004-11-20 13:17:19 +01:00
|
|
|
/* we use *p here to handle the edge case of the head of the list */
|
2004-11-20 13:16:47 +01:00
|
|
|
*p = (*p)->next;
|
|
|
|
close_log(lf);
|
2008-03-05 23:31:39 +01:00
|
|
|
log_free(lf);
|
2004-05-20 21:47:28 +02:00
|
|
|
} else {
|
2004-11-20 13:16:47 +01:00
|
|
|
p = &((*p)->next);
|
2004-05-20 21:47:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-02-21 06:57:12 +01:00
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-05-20 21:47:28 +02:00
|
|
|
}
|
|
|
|
|
2006-01-11 20:40:14 +01:00
|
|
|
/** Make all currently temporary logs (set to be closed by close_temp_logs)
|
|
|
|
* live again, and close all non-temporary logs. */
|
|
|
|
void
|
|
|
|
rollback_log_changes(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2006-01-11 20:40:14 +01:00
|
|
|
for (lf = logfiles; lf; lf = lf->next)
|
|
|
|
lf->is_temporary = ! lf->is_temporary;
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2006-01-11 20:40:14 +01:00
|
|
|
close_temp_logs();
|
|
|
|
}
|
|
|
|
|
2008-03-26 19:59:45 +01:00
|
|
|
/** Configure all log handles to be closed by close_temp_logs(). */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
mark_logs_temp(void)
|
2004-06-02 21:18:37 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2004-06-02 21:18:37 +02:00
|
|
|
for (lf = logfiles; lf; lf = lf->next)
|
|
|
|
lf->is_temporary = 1;
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-06-02 21:18:37 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/**
|
2008-12-03 00:36:58 +01:00
|
|
|
* Add a log handler to send messages to <b>filename</b>. If opening the
|
|
|
|
* logfile fails, -1 is returned and errno is set appropriately (by open(2)).
|
2003-11-19 03:09:43 +01:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2014-03-23 17:24:26 +01:00
|
|
|
add_file_log(const log_severity_list_t *severity, const char *filename,
|
|
|
|
const int truncate)
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
2008-12-03 00:36:58 +01:00
|
|
|
int fd;
|
2008-01-02 06:38:53 +01:00
|
|
|
logfile_t *lf;
|
2008-12-03 00:36:58 +01:00
|
|
|
|
2014-03-23 17:24:26 +01:00
|
|
|
int open_flags = O_WRONLY|O_CREAT;
|
|
|
|
open_flags |= truncate ? O_TRUNC : O_APPEND;
|
|
|
|
|
|
|
|
fd = tor_open_cloexec(filename, open_flags, 0644);
|
2008-12-03 00:36:58 +01:00
|
|
|
if (fd<0)
|
|
|
|
return -1;
|
2012-12-29 04:49:32 +01:00
|
|
|
if (tor_fd_seekend(fd)<0) {
|
|
|
|
close(fd);
|
2008-12-03 00:36:58 +01:00
|
|
|
return -1;
|
2012-12-29 04:49:32 +01:00
|
|
|
}
|
2008-12-03 00:36:58 +01:00
|
|
|
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2008-12-03 00:36:58 +01:00
|
|
|
add_stream_log_impl(severity, filename, fd);
|
2003-09-16 19:58:36 +02:00
|
|
|
logfiles->needs_close = 1;
|
2008-01-02 06:38:53 +01:00
|
|
|
lf = logfiles;
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
|
|
|
|
if (log_tor_version(lf, 0) < 0) {
|
|
|
|
delete_log(lf);
|
|
|
|
}
|
2010-01-25 20:09:18 +01:00
|
|
|
UNLOCK_LOGS();
|
2008-01-02 06:38:53 +01:00
|
|
|
|
2003-10-15 20:38:38 +02:00
|
|
|
return 0;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2003-09-27 00:27:24 +02:00
|
|
|
|
2004-10-26 23:48:41 +02:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
/**
|
|
|
|
* Add a log handler to send messages to they system log facility.
|
2015-09-30 18:02:52 +02:00
|
|
|
*
|
|
|
|
* If this is the first log handler, opens syslog with ident Tor or
|
|
|
|
* Tor-<syslog_identity_tag> if that is not NULL.
|
2004-10-26 23:48:41 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
2015-10-02 13:22:00 +02:00
|
|
|
add_syslog_log(const log_severity_list_t *severity,
|
|
|
|
const char* syslog_identity_tag)
|
2004-10-26 23:48:41 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2015-09-30 17:54:56 +02:00
|
|
|
if (syslog_count++ == 0) {
|
2004-11-09 02:24:10 +01:00
|
|
|
/* This is the first syslog. */
|
2015-09-30 17:54:56 +02:00
|
|
|
static char buf[256];
|
|
|
|
if (syslog_identity_tag) {
|
|
|
|
tor_snprintf(buf, sizeof(buf), "Tor-%s", syslog_identity_tag);
|
|
|
|
} else {
|
|
|
|
tor_snprintf(buf, sizeof(buf), "Tor");
|
|
|
|
}
|
|
|
|
openlog(buf, LOG_PID | LOG_NDELAY, LOGFACILITY);
|
|
|
|
}
|
2004-10-26 23:48:41 +02:00
|
|
|
|
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
2008-12-03 00:36:58 +01:00
|
|
|
lf->fd = -1;
|
2008-03-08 02:11:54 +01:00
|
|
|
lf->severities = tor_memdup(severity, sizeof(log_severity_list_t));
|
2004-10-26 23:48:41 +02:00
|
|
|
lf->filename = tor_strdup("<syslog>");
|
|
|
|
lf->is_syslog = 1;
|
2008-01-02 06:38:53 +01:00
|
|
|
|
|
|
|
LOCK_LOGS();
|
2004-10-26 23:48:41 +02:00
|
|
|
lf->next = logfiles;
|
|
|
|
logfiles = lf;
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-10-26 23:48:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-19 22:07:08 +02:00
|
|
|
/** If <b>level</b> is a valid log severity, return the corresponding
|
|
|
|
* numeric value. Otherwise, return -1. */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
parse_log_level(const char *level)
|
|
|
|
{
|
2004-05-19 22:07:08 +02:00
|
|
|
if (!strcasecmp(level, "err"))
|
|
|
|
return LOG_ERR;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "warn"))
|
|
|
|
return LOG_WARN;
|
|
|
|
if (!strcasecmp(level, "notice"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_NOTICE;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "info"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_INFO;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "debug"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_DEBUG;
|
2004-05-21 14:38:52 +02:00
|
|
|
return -1;
|
2004-05-19 22:07:08 +02:00
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Return the string equivalent of a given log level. */
|
2005-09-30 03:09:52 +02:00
|
|
|
const char *
|
|
|
|
log_level_to_string(int level)
|
2004-11-05 06:50:35 +01:00
|
|
|
{
|
|
|
|
return sev_to_string(level);
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** NULL-terminated array of names for log domains such that domain_list[dom]
|
|
|
|
* is a description of <b>dom</b>. */
|
2008-03-05 23:31:39 +01:00
|
|
|
static const char *domain_list[] = {
|
|
|
|
"GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
|
|
|
|
"HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
|
2015-02-05 16:46:27 +01:00
|
|
|
"OR", "EDGE", "ACCT", "HIST", "HANDSHAKE", "HEARTBEAT", "CHANNEL",
|
|
|
|
"SCHED", NULL
|
2008-03-05 23:31:39 +01:00
|
|
|
};
|
|
|
|
|
2008-12-18 18:18:06 +01:00
|
|
|
/** Return a bitmask for the log domain for which <b>domain</b> is the name,
|
|
|
|
* or 0 if there is no such name. */
|
2008-03-05 23:31:39 +01:00
|
|
|
static log_domain_mask_t
|
|
|
|
parse_log_domain(const char *domain)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; domain_list[i]; ++i) {
|
|
|
|
if (!strcasecmp(domain, domain_list[i]))
|
|
|
|
return (1u<<i);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-01-25 21:53:15 +01:00
|
|
|
|
|
|
|
/** Translate a bitmask of log domains to a string. */
|
|
|
|
static char *
|
|
|
|
domain_to_string(log_domain_mask_t domain, char *buf, size_t buflen)
|
2008-03-05 23:31:39 +01:00
|
|
|
{
|
2011-01-25 21:53:15 +01:00
|
|
|
char *cp = buf;
|
|
|
|
char *eos = buf+buflen;
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
|
|
|
if (! domain)
|
|
|
|
return buf;
|
|
|
|
while (1) {
|
|
|
|
const char *d;
|
|
|
|
int bit = tor_log2(domain);
|
|
|
|
size_t n;
|
2015-02-05 17:01:13 +01:00
|
|
|
if ((unsigned)bit >= ARRAY_LENGTH(domain_list)-1 ||
|
2015-02-05 16:46:27 +01:00
|
|
|
bit >= N_LOGGING_DOMAINS) {
|
2011-01-25 21:53:15 +01:00
|
|
|
tor_snprintf(buf, buflen, "<BUG:Unknown domain %lx>", (long)domain);
|
|
|
|
return buf+strlen(buf);
|
|
|
|
}
|
|
|
|
d = domain_list[bit];
|
|
|
|
n = strlcpy(cp, d, eos-cp);
|
|
|
|
if (n >= buflen) {
|
|
|
|
tor_snprintf(buf, buflen, "<BUG:Truncating domain %lx>", (long)domain);
|
|
|
|
return buf+strlen(buf);
|
|
|
|
}
|
|
|
|
cp += n;
|
|
|
|
domain &= ~(1<<bit);
|
|
|
|
|
|
|
|
if (domain == 0 || (eos-cp) < 2)
|
|
|
|
return cp;
|
|
|
|
|
|
|
|
memcpy(cp, ",", 2); /*Nul-terminated ,"*/
|
|
|
|
cp++;
|
|
|
|
}
|
2008-03-05 23:31:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse a log severity pattern in *<b>cfg_ptr</b>. Advance cfg_ptr after
|
|
|
|
* the end of the severityPattern. Set the value of <b>severity_out</b> to
|
|
|
|
* the parsed pattern. Return 0 on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* The syntax for a SeverityPattern is:
|
|
|
|
* <pre>
|
|
|
|
* SeverityPattern = *(DomainSeverity SP)* DomainSeverity
|
|
|
|
* DomainSeverity = (DomainList SP)? SeverityRange
|
|
|
|
* SeverityRange = MinSeverity ("-" MaxSeverity )?
|
|
|
|
* DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
|
|
|
|
* DomainSpec = "*" | Domain | "~" Domain
|
|
|
|
* </pre>
|
|
|
|
* A missing MaxSeverity defaults to ERR. Severities and domains are
|
|
|
|
* case-insensitive. "~" indicates negation for a domain; negation happens
|
|
|
|
* last inside a DomainList. Only one SeverityRange without a DomainList is
|
|
|
|
* allowed per line.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
parse_log_severity_config(const char **cfg_ptr,
|
|
|
|
log_severity_list_t *severity_out)
|
|
|
|
{
|
|
|
|
const char *cfg = *cfg_ptr;
|
|
|
|
int got_anything = 0;
|
|
|
|
int got_an_unqualified_range = 0;
|
|
|
|
memset(severity_out, 0, sizeof(*severity_out));
|
|
|
|
|
|
|
|
cfg = eat_whitespace(cfg);
|
|
|
|
while (*cfg) {
|
|
|
|
const char *dash, *space;
|
|
|
|
char *sev_lo, *sev_hi;
|
|
|
|
int low, high, i;
|
|
|
|
log_domain_mask_t domains = ~0u;
|
|
|
|
|
|
|
|
if (*cfg == '[') {
|
|
|
|
int err = 0;
|
|
|
|
char *domains_str;
|
|
|
|
smartlist_t *domains_list;
|
|
|
|
log_domain_mask_t neg_domains = 0;
|
|
|
|
const char *closebracket = strchr(cfg, ']');
|
|
|
|
if (!closebracket)
|
|
|
|
return -1;
|
|
|
|
domains = 0;
|
|
|
|
domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
|
2012-01-18 21:53:30 +01:00
|
|
|
domains_list = smartlist_new();
|
2008-03-05 23:31:39 +01:00
|
|
|
smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
|
|
|
|
-1);
|
|
|
|
tor_free(domains_str);
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(domains_list, const char *, domain) {
|
2008-03-05 23:31:39 +01:00
|
|
|
if (!strcmp(domain, "*")) {
|
|
|
|
domains = ~0u;
|
|
|
|
} else {
|
|
|
|
int d;
|
|
|
|
int negate=0;
|
|
|
|
if (*domain == '~') {
|
|
|
|
negate = 1;
|
|
|
|
++domain;
|
|
|
|
}
|
|
|
|
d = parse_log_domain(domain);
|
|
|
|
if (!d) {
|
2009-01-23 23:45:08 +01:00
|
|
|
log_warn(LD_CONFIG, "No such logging domain as %s", domain);
|
2008-03-05 23:31:39 +01:00
|
|
|
err = 1;
|
|
|
|
} else {
|
|
|
|
if (negate)
|
|
|
|
neg_domains |= d;
|
|
|
|
else
|
|
|
|
domains |= d;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(domain);
|
2008-03-05 23:31:39 +01:00
|
|
|
SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
|
|
|
|
smartlist_free(domains_list);
|
|
|
|
if (err)
|
|
|
|
return -1;
|
2011-01-25 21:03:36 +01:00
|
|
|
if (domains == 0 && neg_domains)
|
|
|
|
domains = ~neg_domains;
|
|
|
|
else
|
|
|
|
domains &= ~neg_domains;
|
2008-03-05 23:31:39 +01:00
|
|
|
cfg = eat_whitespace(closebracket+1);
|
|
|
|
} else {
|
|
|
|
++got_an_unqualified_range;
|
|
|
|
}
|
|
|
|
if (!strcasecmpstart(cfg, "file") ||
|
|
|
|
!strcasecmpstart(cfg, "stderr") ||
|
|
|
|
!strcasecmpstart(cfg, "stdout") ||
|
|
|
|
!strcasecmpstart(cfg, "syslog")) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (got_an_unqualified_range > 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
space = strchr(cfg, ' ');
|
|
|
|
dash = strchr(cfg, '-');
|
|
|
|
if (!space)
|
|
|
|
space = strchr(cfg, '\0');
|
|
|
|
if (dash && dash < space) {
|
|
|
|
sev_lo = tor_strndup(cfg, dash-cfg);
|
|
|
|
sev_hi = tor_strndup(dash+1, space-(dash+1));
|
|
|
|
} else {
|
|
|
|
sev_lo = tor_strndup(cfg, space-cfg);
|
|
|
|
sev_hi = tor_strdup("ERR");
|
|
|
|
}
|
2008-03-26 19:36:46 +01:00
|
|
|
low = parse_log_level(sev_lo);
|
|
|
|
high = parse_log_level(sev_hi);
|
|
|
|
tor_free(sev_lo);
|
|
|
|
tor_free(sev_hi);
|
|
|
|
if (low == -1)
|
2008-03-05 23:31:39 +01:00
|
|
|
return -1;
|
2008-03-26 19:36:46 +01:00
|
|
|
if (high == -1)
|
2008-03-05 23:31:39 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
got_anything = 1;
|
|
|
|
for (i=low; i >= high; --i)
|
|
|
|
severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
|
|
|
|
|
|
|
|
cfg = eat_whitespace(space);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
*cfg_ptr = cfg;
|
|
|
|
return got_anything ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Return the least severe log level that any current log is interested in. */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
get_min_log_level(void)
|
2004-05-19 22:07:08 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2008-03-05 23:31:39 +01:00
|
|
|
int i;
|
2004-05-19 22:07:08 +02:00
|
|
|
int min = LOG_ERR;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
2008-03-05 23:31:39 +01:00
|
|
|
for (i = LOG_DEBUG; i > min; --i)
|
|
|
|
if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
|
|
|
|
min = i;
|
2004-05-19 22:07:08 +02:00
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2004-11-20 08:33:55 +01:00
|
|
|
/** Switch all logs to output at most verbose level. */
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
switch_logs_debug(void)
|
2004-11-20 08:33:55 +01:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2008-03-05 23:31:39 +01:00
|
|
|
int i;
|
2008-01-02 06:38:53 +01:00
|
|
|
LOCK_LOGS();
|
2004-11-20 08:33:55 +01:00
|
|
|
for (lf = logfiles; lf; lf=lf->next) {
|
2008-03-05 23:31:39 +01:00
|
|
|
for (i = LOG_DEBUG; i >= LOG_ERR; --i)
|
2008-03-13 23:18:36 +01:00
|
|
|
lf->severities->masks[SEVERITY_MASK_IDX(i)] = ~0u;
|
2004-11-20 08:33:55 +01:00
|
|
|
}
|
2012-10-12 18:22:13 +02:00
|
|
|
log_global_min_severity_ = get_min_log_level();
|
2008-01-02 06:38:53 +01:00
|
|
|
UNLOCK_LOGS();
|
2004-11-20 08:33:55 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 17:24:26 +01:00
|
|
|
/** Truncate all the log files. */
|
|
|
|
void
|
|
|
|
truncate_logs(void)
|
|
|
|
{
|
2014-07-16 15:30:14 +02:00
|
|
|
logfile_t *lf;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
2014-07-16 13:58:55 +02:00
|
|
|
if (lf->fd >= 0) {
|
|
|
|
tor_ftruncate(lf->fd);
|
|
|
|
}
|
|
|
|
}
|
2014-03-23 17:24:26 +01:00
|
|
|
}
|
2016-01-15 16:57:03 +01:00
|
|
|
|