1998-05-03 16:42:08 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Logging Functions
|
|
|
|
*
|
2000-06-05 12:49:04 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-03 16:42:08 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* DOC: Logging
|
|
|
|
*
|
|
|
|
* The Logging module offers a simple set of functions for writing
|
2000-06-08 12:37:21 +00:00
|
|
|
* messages to system logs and to the debug output. Message classes
|
|
|
|
* used by this module are described in |birdlib.h| and also in the
|
|
|
|
* user's manual.
|
2000-06-05 12:49:04 +00:00
|
|
|
*/
|
|
|
|
|
2024-08-29 08:21:08 +00:00
|
|
|
#include <netdb.h>
|
2021-02-08 08:51:59 +00:00
|
|
|
#include <stdatomic.h>
|
1998-05-03 16:42:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2024-08-29 08:21:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2001-03-06 13:40:39 +00:00
|
|
|
#include <time.h>
|
2010-02-21 13:34:53 +00:00
|
|
|
#include <unistd.h>
|
2016-07-11 18:22:55 +00:00
|
|
|
#include <errno.h>
|
1998-05-03 16:42:08 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
1999-12-06 12:34:45 +00:00
|
|
|
#include "nest/cli.h"
|
2018-11-20 16:38:19 +00:00
|
|
|
#include "conf/conf.h"
|
1998-06-17 14:33:29 +00:00
|
|
|
#include "lib/string.h"
|
1999-12-06 13:45:56 +00:00
|
|
|
#include "lib/lists.h"
|
2023-12-13 02:48:12 +00:00
|
|
|
#include "lib/socket.h"
|
2016-04-12 09:14:54 +00:00
|
|
|
#include "sysdep/unix/unix.h"
|
2023-08-21 16:44:10 +00:00
|
|
|
#include "sysdep/unix/io-loop.h"
|
|
|
|
|
|
|
|
static pool *log_pool;
|
1998-05-03 16:42:08 +00:00
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
static struct rfile *dbg_rf;
|
2023-08-21 16:44:10 +00:00
|
|
|
static char *current_syslog_name = NULL; /* NULL -> syslog closed */
|
2024-08-26 15:54:59 +00:00
|
|
|
const char *bird_name = NULL;
|
1998-05-03 16:42:08 +00:00
|
|
|
|
2024-01-26 13:50:36 +00:00
|
|
|
_Atomic uint max_thread_id = 1;
|
2023-05-03 17:01:19 +00:00
|
|
|
_Thread_local uint this_thread_id;
|
2021-02-08 08:51:59 +00:00
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
#include <pthread.h>
|
2014-02-07 12:09:55 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
static DOMAIN(logging) log_domain;
|
|
|
|
#define log_lock() LOCK_DOMAIN(logging, log_domain);
|
|
|
|
#define log_unlock() UNLOCK_DOMAIN(logging, log_domain);
|
|
|
|
|
|
|
|
static struct log_channel * _Atomic global_logs;
|
2013-10-05 18:12:28 +00:00
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
/* Logging flags to validly prepare logging messages */
|
|
|
|
|
|
|
|
static _Atomic uint logging_flags;
|
2023-08-21 16:44:10 +00:00
|
|
|
static _Atomic uint logging_mask;
|
2023-08-16 13:05:36 +00:00
|
|
|
|
2017-05-09 11:44:02 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
1998-05-03 16:42:08 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
static int syslog_priorities[] = {
|
1999-12-06 13:45:56 +00:00
|
|
|
LOG_DEBUG,
|
|
|
|
LOG_DEBUG,
|
1998-05-03 16:42:08 +00:00
|
|
|
LOG_DEBUG,
|
|
|
|
LOG_INFO,
|
1999-12-06 13:45:56 +00:00
|
|
|
LOG_ERR,
|
1998-05-03 16:42:08 +00:00
|
|
|
LOG_WARNING,
|
|
|
|
LOG_ERR,
|
1999-12-06 13:45:56 +00:00
|
|
|
LOG_ERR,
|
|
|
|
LOG_CRIT,
|
1998-05-03 16:42:08 +00:00
|
|
|
LOG_CRIT
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *class_names[] = {
|
|
|
|
"???",
|
|
|
|
"DBG",
|
1999-12-06 13:45:56 +00:00
|
|
|
"TRACE",
|
1998-05-03 16:42:08 +00:00
|
|
|
"INFO",
|
1999-12-06 13:45:56 +00:00
|
|
|
"RMT",
|
1998-05-03 16:42:08 +00:00
|
|
|
"WARN",
|
|
|
|
"ERR",
|
|
|
|
"AUTH",
|
1999-12-06 13:45:56 +00:00
|
|
|
"FATAL",
|
|
|
|
"BUG"
|
1998-05-03 16:42:08 +00:00
|
|
|
};
|
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
struct log_channel {
|
|
|
|
struct log_channel * _Atomic next;
|
|
|
|
const char *filename; /* Log filename */
|
|
|
|
const char *backup; /* Secondary filename (for log rotation) */
|
|
|
|
struct rfile * _Atomic rf; /* File handle */
|
|
|
|
off_t limit; /* Log size limit */
|
|
|
|
_Atomic uint mask; /* Classes to log */
|
|
|
|
uint new_mask; /* Pending new mask */
|
2024-01-28 20:42:39 +00:00
|
|
|
uint prepare; /* Which message parts to prepare */
|
2024-01-28 22:01:39 +00:00
|
|
|
const char *udp_host; /* UDP log dst host name */
|
|
|
|
ip_addr udp_ip; /* UDP log dst IP address */
|
|
|
|
uint udp_port; /* UDP log dst port */
|
|
|
|
sock * _Atomic udp_sk; /* UDP socket */
|
2023-08-21 16:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct log_thread_syncer {
|
|
|
|
struct bird_thread_syncer sync;
|
|
|
|
struct log_channel *lc_close;
|
|
|
|
struct rfile *rf_close;
|
2024-01-28 22:01:39 +00:00
|
|
|
sock *sk_close;
|
2023-08-21 16:44:10 +00:00
|
|
|
const char *name;
|
|
|
|
event lts_event;
|
|
|
|
};
|
2018-11-14 16:16:05 +00:00
|
|
|
|
|
|
|
static void
|
2023-08-21 16:44:10 +00:00
|
|
|
lts_done(struct bird_thread_syncer *sync)
|
2018-11-14 16:16:05 +00:00
|
|
|
{
|
2024-04-26 10:14:33 +00:00
|
|
|
SKIP_BACK_DECLARE(struct log_thread_syncer, lts, sync, sync);
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
log_lock();
|
|
|
|
if (lts->lc_close)
|
2018-11-14 16:16:05 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
lts->rf_close = atomic_load_explicit(<s->lc_close->rf, memory_order_relaxed);
|
2024-01-28 22:01:39 +00:00
|
|
|
lts->sk_close = atomic_load_explicit(<s->lc_close->udp_sk, memory_order_relaxed);
|
2023-08-21 16:44:10 +00:00
|
|
|
mb_free(lts->lc_close);
|
2018-11-14 16:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
if (lts->rf_close && lts->rf_close != &rf_stderr)
|
|
|
|
rfree(lts->rf_close);
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
if (lts->sk_close)
|
|
|
|
rfree(lts->sk_close);
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
mb_free(lts);
|
|
|
|
log_unlock();
|
2018-11-14 16:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
static void
|
|
|
|
lts_event(void *_lts)
|
2018-11-14 16:16:05 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
struct log_thread_syncer *lts = _lts;
|
|
|
|
bird_thread_sync_all(<s->sync, NULL, lts_done, lts->name);
|
|
|
|
}
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
static void
|
|
|
|
lts_request(struct log_channel *lc_close, struct rfile *rf_close, const char *name)
|
|
|
|
{
|
|
|
|
struct log_thread_syncer *lts = mb_allocz(log_pool, sizeof *lts);
|
|
|
|
lts->lc_close = lc_close;
|
|
|
|
lts->rf_close = rf_close;
|
|
|
|
lts->name = name;
|
|
|
|
lts->lts_event = (event) { .hook = lts_event, .data = lts, };
|
|
|
|
ev_send_loop(&main_birdloop, <s->lts_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_rotate(struct log_channel *lc)
|
|
|
|
{
|
|
|
|
struct log_thread_syncer *lts = mb_allocz(log_pool, sizeof *lts);
|
|
|
|
|
|
|
|
if ((rename(lc->filename, lc->backup) < 0) && (unlink(lc->filename) < 0))
|
|
|
|
return lts_request(lc, NULL, "Log Rotate Failed");
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-24 13:38:44 +00:00
|
|
|
struct rfile *rf = rf_open(log_pool, lc->filename, RF_APPEND, lc->limit);
|
2023-08-21 16:44:10 +00:00
|
|
|
if (!rf)
|
|
|
|
return lts_request(lc, NULL, "Log Rotate Failed");
|
|
|
|
|
|
|
|
lts_request(NULL, atomic_load_explicit(&lc->rf, memory_order_relaxed), "Log Rotate Close Old File");
|
|
|
|
atomic_store_explicit(&lc->rf, rf, memory_order_release);
|
2018-11-14 16:16:05 +00:00
|
|
|
}
|
1998-06-17 14:33:29 +00:00
|
|
|
|
2010-09-20 11:01:01 +00:00
|
|
|
/**
|
|
|
|
* log_commit - commit a log message
|
|
|
|
* @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
|
2016-05-12 13:49:44 +00:00
|
|
|
* @buf: message to write
|
2010-09-20 11:01:01 +00:00
|
|
|
*
|
|
|
|
* This function writes a message prepared in the log buffer to the
|
|
|
|
* log file (as specified in the configuration). The log buffer is
|
|
|
|
* reset after that. The log message is a full line, log_commit()
|
|
|
|
* terminates it.
|
|
|
|
*
|
|
|
|
* The message class is an integer, not a first char of a string like
|
|
|
|
* in log(), so it should be written like *L_INFO.
|
|
|
|
*/
|
|
|
|
void
|
2023-08-16 13:05:36 +00:00
|
|
|
log_commit(log_buffer *buf)
|
2010-09-20 11:01:01 +00:00
|
|
|
{
|
2024-01-28 20:42:39 +00:00
|
|
|
/* Store the last pointer */
|
|
|
|
buf->pos[LBP__MAX] = buf->buf.pos;
|
|
|
|
|
|
|
|
/* Append the too-long message if too long */
|
2023-08-16 13:05:36 +00:00
|
|
|
if (buf->buf.pos == buf->buf.end)
|
|
|
|
#define TOO_LONG " ... <too long>"
|
|
|
|
memcpy(buf->buf.end - sizeof TOO_LONG, TOO_LONG, sizeof TOO_LONG);
|
|
|
|
#undef TOO_LONG
|
2013-10-05 18:12:28 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
for (
|
|
|
|
struct log_channel *l = atomic_load_explicit(&global_logs, memory_order_acquire);
|
|
|
|
l;
|
|
|
|
l = atomic_load_explicit(&l->next, memory_order_acquire)
|
|
|
|
)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
uint mask = atomic_load_explicit(&l->mask, memory_order_acquire);
|
|
|
|
if (!(mask & (1 << buf->class)))
|
1999-12-06 13:45:56 +00:00
|
|
|
continue;
|
2023-08-21 16:44:10 +00:00
|
|
|
|
|
|
|
struct rfile *rf = atomic_load_explicit(&l->rf, memory_order_acquire);
|
2024-01-28 22:01:39 +00:00
|
|
|
sock *sk = atomic_load_explicit(&l->udp_sk, memory_order_acquire);
|
|
|
|
|
|
|
|
if (rf || sk)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2024-01-28 20:42:39 +00:00
|
|
|
/* Construct the iovec */
|
|
|
|
static char terminal_prefix[] = "bird: ",
|
|
|
|
newline[] = "\n";
|
|
|
|
STATIC_ASSERT(sizeof newline == 2);
|
|
|
|
|
|
|
|
struct iovec iov[LBP__MAX+2];
|
|
|
|
uint iov_count = 0;
|
|
|
|
if (BIT32_TEST(&l->prepare, LBPP_TERMINAL))
|
|
|
|
iov[iov_count++] = (struct iovec) {
|
|
|
|
.iov_base = terminal_prefix,
|
|
|
|
.iov_len = sizeof terminal_prefix - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (uint p = 0; p < LBP__MAX; p++)
|
|
|
|
if (BIT32_TEST(&l->prepare, p))
|
|
|
|
{
|
|
|
|
off_t sz = buf->pos[p+1] - buf->pos[p];
|
|
|
|
if (sz > 0)
|
|
|
|
iov[iov_count++] = (struct iovec) {
|
|
|
|
.iov_base = buf->pos[p],
|
|
|
|
.iov_len = sz,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
if (rf)
|
|
|
|
{
|
|
|
|
iov[iov_count++] = (struct iovec) {
|
|
|
|
.iov_base = newline,
|
|
|
|
.iov_len = sizeof newline - 1,
|
|
|
|
};
|
2024-01-28 20:42:39 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
do {
|
|
|
|
if (rf_writev(rf, iov, iov_count))
|
|
|
|
break;
|
2023-08-24 13:38:44 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
log_lock();
|
|
|
|
rf = atomic_load_explicit(&l->rf, memory_order_acquire);
|
|
|
|
if (rf_writev(rf, iov, iov_count))
|
|
|
|
{
|
|
|
|
log_unlock();
|
|
|
|
break;
|
2018-11-14 16:16:05 +00:00
|
|
|
}
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
log_rotate(l);
|
|
|
|
log_unlock();
|
2023-08-24 13:38:44 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
rf = atomic_load_explicit(&l->rf, memory_order_relaxed);
|
|
|
|
} while (!rf_writev(rf, iov, iov_count));
|
|
|
|
}
|
|
|
|
else if (sk)
|
|
|
|
{
|
|
|
|
while ((writev(sk->fd, iov, iov_count) < 0) && (errno == EINTR))
|
|
|
|
;
|
|
|
|
/* FIXME: Silently ignoring write errors */
|
|
|
|
}
|
1999-12-06 13:45:56 +00:00
|
|
|
}
|
2017-05-09 11:44:02 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
1999-12-06 13:45:56 +00:00
|
|
|
else
|
2023-08-16 13:05:36 +00:00
|
|
|
{
|
2024-01-28 20:42:39 +00:00
|
|
|
syslog(syslog_priorities[buf->class], "%s", buf->pos[LBP_MSG]);
|
2023-08-16 13:05:36 +00:00
|
|
|
}
|
1998-05-03 16:42:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-09-20 11:01:01 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
int buffer_vprint(buffer *buf, const char *fmt, va_list args);
|
2010-09-20 11:01:01 +00:00
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
void
|
|
|
|
log_prepare(log_buffer *buf, int class)
|
|
|
|
{
|
2024-01-28 20:42:39 +00:00
|
|
|
buf->class = class;
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
buf->buf.start = buf->buf.pos = buf->block;
|
|
|
|
buf->buf.end = buf->block + sizeof buf->block;
|
|
|
|
|
|
|
|
int lf = atomic_load_explicit(&logging_flags, memory_order_acquire);
|
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
buf->pos[LBP_TIMESTAMP] = buf->buf.pos;
|
|
|
|
if (BIT32_TEST(&lf, LBP_TIMESTAMP))
|
2023-08-16 13:05:36 +00:00
|
|
|
{
|
2024-06-13 14:30:51 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
const char *fmt = atomic_load_explicit(&global_runtime, memory_order_acquire)->tf_log.fmt1;
|
2023-08-16 13:05:36 +00:00
|
|
|
int t = tm_format_real_time(buf->buf.pos, buf->buf.end - buf->buf.pos, fmt, current_real_time());
|
2024-06-13 14:30:51 +00:00
|
|
|
rcu_read_unlock();
|
2023-08-16 13:05:36 +00:00
|
|
|
if (t)
|
|
|
|
buf->buf.pos += t;
|
|
|
|
else
|
|
|
|
buffer_puts(&buf->buf, "<time format error>");
|
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
*(buf->buf.pos++) = ' ';
|
2023-08-16 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
buf->pos[LBP_UDP_HEADER] = buf->buf.pos;
|
|
|
|
if (BIT32_TEST(&lf, LBP_UDP_HEADER))
|
|
|
|
{
|
|
|
|
/* Legacy RFC 3164 format, but with us precision */
|
|
|
|
buffer_print(&buf->buf, "<%d>", LOG_DAEMON | syslog_priorities[class]);
|
|
|
|
|
|
|
|
const char *fmt = "%b %d %T.%6f";
|
|
|
|
int t = tm_format_real_time(buf->buf.pos, buf->buf.end - buf->buf.pos, fmt, current_real_time());
|
|
|
|
if (t)
|
|
|
|
buf->buf.pos += t;
|
|
|
|
else
|
|
|
|
buffer_puts(&buf->buf, "<time format error>");
|
|
|
|
|
2024-06-13 14:30:51 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
const char *hostname = atomic_load_explicit(&global_runtime, memory_order_acquire)->hostname ?: "<none>";
|
2024-01-28 22:01:39 +00:00
|
|
|
buffer_print(&buf->buf, " %s %s: ", hostname, bird_name);
|
2024-06-13 14:30:51 +00:00
|
|
|
rcu_read_unlock();
|
2024-01-28 22:01:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
buf->pos[LBP_THREAD_ID] = buf->buf.pos;
|
|
|
|
if (BIT32_TEST(&lf, LBP_THREAD_ID))
|
|
|
|
buffer_print(&buf->buf, "[%04x] ", THIS_THREAD_ID);
|
|
|
|
|
|
|
|
buf->pos[LBP_CLASS] = buf->buf.pos;
|
|
|
|
if (BIT32_TEST(&lf, LBP_CLASS))
|
|
|
|
buffer_print(&buf->buf, "<%s> ", class_names[class]);
|
|
|
|
|
|
|
|
buf->pos[LBP_MSG] = buf->buf.pos;
|
2023-08-16 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-20 11:01:01 +00:00
|
|
|
static void
|
|
|
|
vlog(int class, const char *msg, va_list args)
|
|
|
|
{
|
2023-08-16 13:05:36 +00:00
|
|
|
static _Thread_local log_buffer buf;
|
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* No logging at all if nobody would receive the message either */
|
|
|
|
if (!(atomic_load_explicit(&logging_mask, memory_order_acquire) & (1 << class)))
|
|
|
|
return;
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
log_prepare(&buf, class);
|
|
|
|
buffer_vprint(&buf.buf, msg, args);
|
|
|
|
log_commit(&buf);
|
2010-09-20 11:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* log - log a message
|
|
|
|
* @msg: printf-like formatting string with message class information
|
|
|
|
* prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
|
|
|
|
*
|
|
|
|
* This function formats a message according to the format string @msg
|
2000-06-07 12:29:08 +00:00
|
|
|
* and writes it to the corresponding log file (as specified in the
|
2000-06-05 12:49:04 +00:00
|
|
|
* configuration). Please note that the message is automatically
|
|
|
|
* formatted as a full line, no need to include |\n| inside.
|
2010-09-20 11:01:01 +00:00
|
|
|
* It is essentially a sequence of log_reset(), logn() and log_commit().
|
2000-06-05 12:49:04 +00:00
|
|
|
*/
|
1998-05-03 16:42:08 +00:00
|
|
|
void
|
2014-12-03 09:57:31 +00:00
|
|
|
log_msg(const char *msg, ...)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
|
|
|
int class = 1;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
1998-12-20 14:24:35 +00:00
|
|
|
if (*msg >= 1 && *msg <= 8)
|
1998-05-03 16:42:08 +00:00
|
|
|
class = *msg++;
|
|
|
|
vlog(class, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2009-02-26 13:23:54 +00:00
|
|
|
void
|
2014-12-03 09:57:31 +00:00
|
|
|
log_rl(struct tbf *f, const char *msg, ...)
|
2009-02-26 13:23:54 +00:00
|
|
|
{
|
|
|
|
int class = 1;
|
|
|
|
va_list args;
|
|
|
|
|
2014-10-02 09:41:34 +00:00
|
|
|
/* Rate limiting is a bit tricky here as it also logs '...' during the first hit */
|
2017-11-28 16:06:10 +00:00
|
|
|
if (tbf_limit(f) && (f->drop > 1))
|
2009-02-26 13:23:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (*msg >= 1 && *msg <= 8)
|
|
|
|
class = *msg++;
|
2014-10-02 09:41:34 +00:00
|
|
|
|
|
|
|
va_start(args, msg);
|
2017-11-28 16:06:10 +00:00
|
|
|
vlog(class, (f->drop ? "..." : msg), args);
|
2009-02-26 13:23:54 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* bug - report an internal error
|
|
|
|
* @msg: a printf-like error message
|
|
|
|
*
|
|
|
|
* This function logs an internal error and aborts execution
|
|
|
|
* of the program.
|
|
|
|
*/
|
1998-12-20 14:24:35 +00:00
|
|
|
void
|
2014-12-03 09:57:31 +00:00
|
|
|
bug(const char *msg, ...)
|
1998-12-20 14:24:35 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
vlog(L_BUG[0], msg, args);
|
2016-03-29 08:37:31 +00:00
|
|
|
va_end(args);
|
2000-05-08 12:38:00 +00:00
|
|
|
abort();
|
1998-12-20 14:24:35 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* bug - report a fatal error
|
|
|
|
* @msg: a printf-like error message
|
|
|
|
*
|
|
|
|
* This function logs a fatal error and aborts execution
|
|
|
|
* of the program.
|
|
|
|
*/
|
1998-05-03 16:42:08 +00:00
|
|
|
void
|
2014-12-03 09:57:31 +00:00
|
|
|
die(const char *msg, ...)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
1998-12-20 14:24:35 +00:00
|
|
|
vlog(L_FATAL[0], msg, args);
|
2016-03-29 08:37:31 +00:00
|
|
|
va_end(args);
|
1998-05-03 16:42:08 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-02-08 08:51:59 +00:00
|
|
|
static struct timespec dbg_time_start;
|
|
|
|
|
2000-06-05 12:49:04 +00:00
|
|
|
/**
|
|
|
|
* debug - write to debug output
|
|
|
|
* @msg: a printf-like message
|
|
|
|
*
|
|
|
|
* This function formats the message @msg and prints it out
|
|
|
|
* to the debugging output. No newline character is appended.
|
|
|
|
*/
|
1998-05-03 16:42:08 +00:00
|
|
|
void
|
2014-12-03 09:57:31 +00:00
|
|
|
debug(const char *msg, ...)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
2021-05-26 14:42:02 +00:00
|
|
|
#define MAX_DEBUG_BUFSIZE 16384
|
1998-05-03 16:42:08 +00:00
|
|
|
va_list args;
|
2021-02-08 08:51:59 +00:00
|
|
|
char buf[MAX_DEBUG_BUFSIZE], *pos = buf;
|
|
|
|
int max = MAX_DEBUG_BUFSIZE;
|
1998-05-03 16:42:08 +00:00
|
|
|
|
|
|
|
va_start(args, msg);
|
2023-08-16 13:05:36 +00:00
|
|
|
if (dbg_rf)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2023-08-16 13:05:36 +00:00
|
|
|
int s = bvsnprintf(pos, max, msg, args);
|
|
|
|
if (s < 0)
|
2021-05-26 14:42:02 +00:00
|
|
|
bug("Extremely long debug output, split it.");
|
2019-07-10 09:12:41 +00:00
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
struct iovec i = {
|
|
|
|
.iov_base = buf,
|
|
|
|
.iov_len = s,
|
|
|
|
};
|
|
|
|
rf_writev(dbg_rf, &i, 1);
|
1999-12-06 13:45:56 +00:00
|
|
|
}
|
1998-05-03 16:42:08 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:40:53 +00:00
|
|
|
/**
|
|
|
|
* debug_safe - async-safe write to debug output
|
|
|
|
* @msg: a string message
|
|
|
|
*
|
|
|
|
* This function prints the message @msg to the debugging output in a
|
|
|
|
* way that is async safe and can be used in signal handlers. No newline
|
|
|
|
* character is appended.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
debug_safe(const char *msg)
|
|
|
|
{
|
2023-08-16 13:05:36 +00:00
|
|
|
if (dbg_rf)
|
2024-01-28 20:42:39 +00:00
|
|
|
rf_write_crude(dbg_rf, msg, strlen(msg));
|
2023-01-12 16:40:53 +00:00
|
|
|
}
|
|
|
|
|
2010-04-07 09:00:36 +00:00
|
|
|
static list *
|
2020-04-08 20:25:15 +00:00
|
|
|
default_log_list(int initial, const char **syslog_name)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
2018-12-04 15:55:25 +00:00
|
|
|
static list log_list;
|
|
|
|
init_list(&log_list);
|
2010-04-07 09:00:36 +00:00
|
|
|
*syslog_name = NULL;
|
1998-05-03 16:42:08 +00:00
|
|
|
|
2017-05-09 11:44:02 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
2023-08-16 13:05:36 +00:00
|
|
|
if (!dbg_rf)
|
1998-05-03 16:42:08 +00:00
|
|
|
{
|
2020-11-25 14:04:34 +00:00
|
|
|
static struct log_config lc_syslog;
|
|
|
|
lc_syslog = (struct log_config){
|
2024-01-28 20:42:39 +00:00
|
|
|
.mask = ~0,
|
2020-11-25 14:04:34 +00:00
|
|
|
};
|
|
|
|
|
2018-12-04 15:55:25 +00:00
|
|
|
add_tail(&log_list, &lc_syslog.n);
|
2010-04-07 09:00:36 +00:00
|
|
|
*syslog_name = bird_name;
|
1998-05-03 16:42:08 +00:00
|
|
|
}
|
1999-12-06 13:45:56 +00:00
|
|
|
#endif
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
if (dbg_rf && (dbg_rf != &rf_stderr))
|
2018-12-04 15:55:25 +00:00
|
|
|
{
|
2020-11-25 14:04:34 +00:00
|
|
|
static struct log_config lc_debug;
|
|
|
|
lc_debug = (struct log_config){
|
|
|
|
.mask = ~0,
|
2023-08-16 13:05:36 +00:00
|
|
|
.rf = dbg_rf,
|
2020-11-25 14:04:34 +00:00
|
|
|
};
|
|
|
|
|
2018-12-04 15:55:25 +00:00
|
|
|
add_tail(&log_list, &lc_debug.n);
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
if (initial || (dbg_rf == &rf_stderr))
|
2018-12-04 15:55:25 +00:00
|
|
|
{
|
2020-11-25 14:04:34 +00:00
|
|
|
static struct log_config lc_stderr;
|
|
|
|
lc_stderr = (struct log_config){
|
|
|
|
.mask = ~0,
|
|
|
|
.terminal_flag = 1,
|
2023-08-16 13:05:36 +00:00
|
|
|
.rf = &rf_stderr,
|
2020-11-25 14:04:34 +00:00
|
|
|
};
|
|
|
|
|
2018-12-04 15:55:25 +00:00
|
|
|
add_tail(&log_list, &lc_stderr.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &log_list;
|
1999-12-06 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
|
1999-12-06 13:45:56 +00:00
|
|
|
void
|
2020-04-08 20:25:15 +00:00
|
|
|
log_switch(int initial, list *logs, const char *new_syslog_name)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
if (initial)
|
|
|
|
{
|
|
|
|
log_domain = DOMAIN_NEW(logging);
|
|
|
|
log_lock();
|
|
|
|
log_pool = rp_new(&root_pool, log_domain.logging, "Log files");
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
#if HAVE_SYSLOG_H
|
|
|
|
/* Create syslog channel */
|
|
|
|
struct log_channel *lc = mb_alloc(log_pool, sizeof *lc);
|
|
|
|
|
2024-01-28 20:42:39 +00:00
|
|
|
*lc = (struct log_channel) {
|
|
|
|
.prepare = BIT32_ALL(LBP_MSG),
|
|
|
|
};
|
2023-08-21 16:44:10 +00:00
|
|
|
ASSERT_DIE(NULL == atomic_exchange_explicit(&global_logs, lc, memory_order_release));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
log_unlock();
|
|
|
|
}
|
2020-11-25 14:15:13 +00:00
|
|
|
|
2018-11-14 16:16:05 +00:00
|
|
|
if (!logs || EMPTY_LIST(*logs))
|
2018-12-04 15:55:25 +00:00
|
|
|
logs = default_log_list(initial, &new_syslog_name);
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
ASSERT_DIE(logs);
|
2018-11-14 16:16:05 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Prepare the new log configuration */
|
|
|
|
struct log_config *l;
|
|
|
|
WALK_LIST(l, *logs)
|
|
|
|
{
|
|
|
|
int erf = 0;
|
|
|
|
log_lock();
|
|
|
|
if (l->rf && (l->rf != &rf_stderr))
|
|
|
|
rmove(l->rf, log_pool);
|
|
|
|
else if (l->filename)
|
2023-08-16 13:05:36 +00:00
|
|
|
{
|
2023-08-24 13:38:44 +00:00
|
|
|
l->rf = rf_open(log_pool, l->filename, RF_APPEND, l->limit);
|
2023-08-21 16:44:10 +00:00
|
|
|
erf = l->rf ? 0 : errno;
|
2023-08-16 13:05:36 +00:00
|
|
|
}
|
2023-08-21 16:44:10 +00:00
|
|
|
log_unlock();
|
|
|
|
if (erf)
|
|
|
|
log(L_ERR "Failed to open log file '%s': %M", l->filename, erf);
|
|
|
|
}
|
2023-08-16 13:05:36 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
uint total_mask = 0;
|
|
|
|
uint flags = 0;
|
2010-04-07 09:00:36 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Update pre-existing log channels */
|
|
|
|
for (
|
|
|
|
struct log_channel * _Atomic *pprev = &global_logs, *ol;
|
|
|
|
ol = atomic_load_explicit(pprev, memory_order_acquire);
|
|
|
|
pprev = &ol->next)
|
|
|
|
{
|
|
|
|
ol->new_mask = 0;
|
2024-04-01 14:01:26 +00:00
|
|
|
struct rfile *orf = atomic_load_explicit(&ol->rf, memory_order_relaxed);
|
|
|
|
if (orf)
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
|
|
|
WALK_LIST(l, *logs)
|
2024-04-01 14:01:26 +00:00
|
|
|
if (l->rf && rf_same(l->rf, orf))
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
|
|
|
/* Merge the mask */
|
|
|
|
ol->new_mask |= l->mask;
|
|
|
|
total_mask |= l->mask;
|
|
|
|
|
|
|
|
/* Merge flags */
|
2024-01-28 20:42:39 +00:00
|
|
|
flags |= ol->prepare;
|
2023-08-21 16:44:10 +00:00
|
|
|
|
|
|
|
/* The filehandle is no longer needed */
|
2023-11-01 17:25:15 +00:00
|
|
|
if ((l->rf != &rf_stderr ) && (l->rf != dbg_rf))
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
|
|
|
log_lock();
|
|
|
|
rfree(l->rf);
|
|
|
|
log_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
l->rf = NULL;
|
2024-01-28 22:01:39 +00:00
|
|
|
l->found_old = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ol->udp_port)
|
|
|
|
{
|
|
|
|
WALK_LIST(l, *logs)
|
|
|
|
if (
|
|
|
|
(l->udp_port == ol->udp_port) && (
|
|
|
|
(l->udp_host && !strcmp(l->udp_host, ol->udp_host)) ||
|
|
|
|
(ipa_nonzero(l->udp_ip) && (ipa_equal(l->udp_ip, ol->udp_ip)))
|
|
|
|
))
|
|
|
|
{
|
|
|
|
/* Merge the mask */
|
|
|
|
ol->new_mask |= l->mask;
|
|
|
|
total_mask |= l->mask;
|
|
|
|
|
|
|
|
/* Merge flags */
|
|
|
|
flags |= ol->prepare;
|
|
|
|
|
|
|
|
/* The socket just stays open */
|
|
|
|
l->found_old = 1;
|
2023-08-21 16:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2024-01-28 22:01:39 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
WALK_LIST(l, *logs)
|
2024-01-28 22:01:39 +00:00
|
|
|
if (!l->filename && !l->rf && !l->udp_port)
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
|
|
|
ol->new_mask |= l->mask;
|
|
|
|
total_mask |= l->mask;
|
2024-01-28 22:01:39 +00:00
|
|
|
l->found_old = 1;
|
2023-08-21 16:44:10 +00:00
|
|
|
}
|
2024-01-28 22:01:39 +00:00
|
|
|
}
|
2010-04-07 09:00:36 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* First only extend masks */
|
|
|
|
atomic_fetch_or_explicit(&ol->mask, ol->new_mask, memory_order_acq_rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_fetch_or_explicit(&logging_mask, total_mask, memory_order_acq_rel);
|
2010-04-07 09:00:36 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Open new log channels */
|
|
|
|
WALK_LIST(l, *logs)
|
2016-11-01 10:37:49 +00:00
|
|
|
{
|
2024-01-28 22:01:39 +00:00
|
|
|
if (l->found_old)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!l->rf && !l->udp_port)
|
2023-08-21 16:44:10 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Truly new log channel */
|
|
|
|
log_lock();
|
|
|
|
struct log_channel *lc = mb_alloc(log_pool, sizeof *lc);
|
|
|
|
log_unlock();
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
if (l->rf)
|
|
|
|
{
|
|
|
|
*lc = (struct log_channel) {
|
|
|
|
.filename = l->filename,
|
|
|
|
.backup = l->backup,
|
|
|
|
.rf = l->rf,
|
|
|
|
.limit = l->limit,
|
|
|
|
.new_mask = l->mask,
|
|
|
|
.prepare = BIT32_ALL(LBP_TIMESTAMP, LBP_THREAD_ID, LBP_CLASS, LBP_MSG) |
|
|
|
|
(l->terminal_flag ? BIT32_VAL(LBPP_TERMINAL) : 0),
|
|
|
|
};
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
/* Now the file handle ownership is transferred to the log channel */
|
|
|
|
l->rf = NULL;
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
/* Find more */
|
|
|
|
for (struct log_config *ll = NODE_NEXT(l); NODE_VALID(ll); ll = NODE_NEXT(ll))
|
2024-04-01 14:01:26 +00:00
|
|
|
{
|
|
|
|
struct rfile *crf = atomic_load_explicit(&lc->rf, memory_order_relaxed);
|
|
|
|
if (ll->filename && ll->rf && rf_same(crf, ll->rf))
|
2024-01-28 22:01:39 +00:00
|
|
|
{
|
|
|
|
/* Merged with this channel */
|
|
|
|
lc->new_mask |= ll->mask;
|
|
|
|
total_mask |= ll->mask;
|
|
|
|
|
|
|
|
if (l->rf != &rf_stderr)
|
|
|
|
{
|
|
|
|
log_lock();
|
|
|
|
rfree(ll->rf);
|
|
|
|
log_unlock();
|
|
|
|
}
|
|
|
|
ll->rf = NULL;
|
|
|
|
}
|
2024-04-01 14:01:26 +00:00
|
|
|
}
|
2024-01-28 22:01:39 +00:00
|
|
|
}
|
|
|
|
else if (l->udp_port)
|
|
|
|
{
|
|
|
|
sock *sk;
|
|
|
|
|
|
|
|
ASSERT(l->udp_host || ipa_nonzero(l->udp_ip));
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
*lc = (struct log_channel) {
|
|
|
|
.new_mask = l->mask,
|
|
|
|
.prepare = BIT32_ALL(LBP_UDP_HEADER, LBP_MSG),
|
|
|
|
.udp_host = l->udp_host,
|
|
|
|
.udp_port = l->udp_port,
|
|
|
|
.udp_ip = l->udp_ip,
|
|
|
|
};
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
if (lc->udp_host && ipa_zero(lc->udp_ip))
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
2024-01-28 22:01:39 +00:00
|
|
|
const char *err_msg;
|
|
|
|
lc->udp_ip = resolve_hostname(lc->udp_host, SK_UDP, &err_msg);
|
2023-08-21 16:44:10 +00:00
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
if (ipa_zero(lc->udp_ip))
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
2024-08-26 16:09:46 +00:00
|
|
|
log(L_WARN "Cannot resolve hostname '%s': %s", l->udp_host, err_msg);
|
2024-01-28 22:01:39 +00:00
|
|
|
goto resolve_fail;
|
2023-08-21 16:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
log_lock();
|
|
|
|
sk = sk_new(log_pool);
|
|
|
|
log_unlock();
|
|
|
|
sk->type = SK_UDP;
|
|
|
|
sk->daddr = lc->udp_ip;
|
|
|
|
sk->dport = lc->udp_port;
|
|
|
|
sk->flags = SKF_CONNECT;
|
|
|
|
|
|
|
|
if (sk_open(sk, &main_birdloop) < 0)
|
|
|
|
{
|
2024-08-26 16:09:46 +00:00
|
|
|
log(L_WARN "Cannot open UDP log socket: %s%#m", sk->err);
|
2024-01-28 22:01:39 +00:00
|
|
|
rfree(sk);
|
|
|
|
resolve_fail:
|
|
|
|
log_lock();
|
|
|
|
mb_free(lc);
|
|
|
|
log_unlock();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_store_explicit(&lc->udp_sk, sk, memory_order_relaxed);
|
|
|
|
|
|
|
|
/* Find more */
|
|
|
|
for (struct log_config *ll = NODE_NEXT(l); NODE_VALID(ll); ll = NODE_NEXT(ll))
|
|
|
|
if (
|
|
|
|
(l->udp_port == ll->udp_port) && (
|
|
|
|
(l->udp_host && !strcmp(l->udp_host, ll->udp_host)) ||
|
|
|
|
(ipa_nonzero(l->udp_ip) && (ipa_equal(l->udp_ip, ll->udp_ip)))
|
|
|
|
))
|
|
|
|
{
|
|
|
|
/* Merged with this channel */
|
|
|
|
lc->new_mask |= ll->mask;
|
|
|
|
total_mask |= ll->mask;
|
|
|
|
|
|
|
|
ll->found_old = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mask union */
|
|
|
|
total_mask |= l->mask;
|
|
|
|
|
|
|
|
/* Store the new final local mask */
|
2023-08-21 16:44:10 +00:00
|
|
|
atomic_store_explicit(&lc->mask, lc->new_mask, memory_order_release);
|
|
|
|
|
2024-01-28 22:01:39 +00:00
|
|
|
/* Message preparation flags */
|
|
|
|
flags |= lc->prepare;
|
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Insert into the main log list */
|
|
|
|
struct log_channel *head = atomic_load_explicit(&global_logs, memory_order_acquire);
|
|
|
|
do atomic_store_explicit(&lc->next, head, memory_order_release);
|
|
|
|
while (!atomic_compare_exchange_strong_explicit(
|
|
|
|
&global_logs, &head, lc,
|
|
|
|
memory_order_acq_rel, memory_order_acquire));
|
2016-11-01 10:37:49 +00:00
|
|
|
}
|
2010-04-07 09:00:36 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Merge overall flags */
|
|
|
|
atomic_fetch_or_explicit(&logging_flags, flags, memory_order_acq_rel);
|
|
|
|
atomic_fetch_or_explicit(&logging_mask, total_mask, memory_order_acq_rel);
|
|
|
|
|
|
|
|
/* Close end-of-life log channels */
|
|
|
|
for (struct log_channel * _Atomic *pprev = &global_logs,
|
|
|
|
*ol = atomic_load_explicit(pprev, memory_order_acquire);
|
|
|
|
ol; )
|
2016-11-01 10:37:49 +00:00
|
|
|
{
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Store new mask after opening new files to minimize missing log message race conditions */
|
|
|
|
atomic_store_explicit(&ol->mask, ol->new_mask, memory_order_release);
|
|
|
|
|
2023-11-01 17:25:15 +00:00
|
|
|
/* Never close syslog channel or debug */
|
2024-04-01 14:01:26 +00:00
|
|
|
struct rfile *orf = atomic_load_explicit(&ol->rf, memory_order_relaxed);
|
|
|
|
sock *ousk = atomic_load_explicit(&ol->udp_sk, memory_order_relaxed);
|
|
|
|
if (ol->new_mask || (!orf && !ousk) || (orf == dbg_rf))
|
2023-08-21 16:44:10 +00:00
|
|
|
{
|
|
|
|
pprev = &ol->next;
|
|
|
|
ol = atomic_load_explicit(pprev, memory_order_acquire);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This file has no logging set up, remove from list */
|
|
|
|
struct log_channel *next = atomic_load_explicit(&ol->next, memory_order_acquire);
|
|
|
|
atomic_store_explicit(pprev, next, memory_order_release);
|
|
|
|
|
|
|
|
/* Free the channel after all worker threads leave the critical section */
|
|
|
|
log_lock();
|
|
|
|
lts_request(ol, NULL, "Log Reconfigure Close Old File");
|
|
|
|
log_unlock();
|
|
|
|
|
|
|
|
/* Continue to next */
|
|
|
|
ol = next;
|
|
|
|
}
|
2016-11-01 10:37:49 +00:00
|
|
|
}
|
2019-07-30 10:11:49 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
/* Set overall flags after files are closed */
|
|
|
|
atomic_store_explicit(&logging_flags, flags, memory_order_release);
|
|
|
|
atomic_store_explicit(&logging_mask, total_mask, memory_order_release);
|
2019-07-30 10:11:49 +00:00
|
|
|
|
2023-08-21 16:44:10 +00:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
if ((!current_syslog_name != !new_syslog_name)
|
|
|
|
|| bstrcmp(current_syslog_name, new_syslog_name))
|
|
|
|
{
|
|
|
|
char *old_syslog_name = current_syslog_name;
|
|
|
|
|
|
|
|
if (new_syslog_name)
|
|
|
|
{
|
|
|
|
current_syslog_name = xstrdup(new_syslog_name);
|
|
|
|
openlog(current_syslog_name, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current_syslog_name = NULL;
|
|
|
|
closelog();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_syslog_name)
|
|
|
|
xfree(old_syslog_name);
|
|
|
|
}
|
|
|
|
#endif
|
1998-05-03 16:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log_init_debug(char *f)
|
|
|
|
{
|
2024-08-26 15:54:59 +00:00
|
|
|
ASSERT_DIE(bird_name);
|
2021-06-28 13:43:45 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &dbg_time_start);
|
|
|
|
|
2023-08-16 13:05:36 +00:00
|
|
|
if (dbg_rf && dbg_rf != &rf_stderr)
|
2023-08-21 16:44:10 +00:00
|
|
|
rfree(dbg_rf);
|
2023-01-12 16:40:53 +00:00
|
|
|
|
1998-05-03 16:42:08 +00:00
|
|
|
if (!f)
|
2023-08-16 13:05:36 +00:00
|
|
|
dbg_rf = NULL;
|
1999-12-06 13:45:56 +00:00
|
|
|
else if (!*f)
|
2023-08-16 13:05:36 +00:00
|
|
|
dbg_rf = &rf_stderr;
|
2023-08-24 13:38:44 +00:00
|
|
|
else if (!(dbg_rf = rf_open(&root_pool, f, RF_APPEND, 0)))
|
2016-07-11 18:22:55 +00:00
|
|
|
{
|
|
|
|
/* Cannot use die() nor log() here, logging is not yet initialized */
|
|
|
|
fprintf(stderr, "bird: Unable to open debug file %s: %s\n", f, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
1998-05-03 16:42:08 +00:00
|
|
|
}
|
2024-08-26 15:54:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Setting BIRD name
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline char *
|
|
|
|
get_bird_name(char *s, char *def)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
if (!s)
|
|
|
|
return def;
|
|
|
|
t = strrchr(s, '/');
|
|
|
|
if (!t)
|
|
|
|
return s;
|
|
|
|
if (!t[1])
|
|
|
|
return def;
|
|
|
|
return t+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_daemon_name(char *path, char *def)
|
|
|
|
{
|
|
|
|
bird_name = get_bird_name(path, def);
|
|
|
|
}
|
2024-08-29 08:21:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* DNS resolver
|
|
|
|
*/
|
|
|
|
|
|
|
|
ip_addr
|
|
|
|
resolve_hostname(const char *host, int type, const char **err_msg)
|
|
|
|
{
|
|
|
|
struct addrinfo *res;
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = AF_UNSPEC,
|
|
|
|
.ai_socktype = (type == SK_UDP) ? SOCK_DGRAM : SOCK_STREAM,
|
|
|
|
.ai_flags = AI_ADDRCONFIG,
|
|
|
|
};
|
|
|
|
|
|
|
|
*err_msg = NULL;
|
|
|
|
|
|
|
|
int err_code = getaddrinfo(host, NULL, &hints, &res);
|
|
|
|
if (err_code != 0)
|
|
|
|
{
|
|
|
|
*err_msg = gai_strerror(err_code);
|
|
|
|
return IPA_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_addr addr = IPA_NONE;
|
|
|
|
uint unused;
|
|
|
|
|
|
|
|
sockaddr_read((sockaddr *) res->ai_addr, res->ai_family, &addr, NULL, &unused);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|