/* * BIRD Library -- Logging Functions * * (c) 1998--2000 Martin Mares * * Can be freely distributed and used under the terms of the GNU GPL. */ /** * DOC: Logging * * The Logging module offers a simple set of functions for writing * 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. */ #include #include #include #include #include #include #include #include "nest/bird.h" #include "nest/cli.h" #include "conf/conf.h" #include "lib/string.h" #include "lib/lists.h" #include "lib/socket.h" #include "sysdep/unix/unix.h" #include "sysdep/unix/io-loop.h" static pool *log_pool; static struct rfile *dbg_rf; static char *current_syslog_name = NULL; /* NULL -> syslog closed */ _Atomic uint max_thread_id = 1; _Thread_local uint this_thread_id; #include 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; /* Logging flags to validly prepare logging messages */ static _Atomic uint logging_flags; static _Atomic uint logging_mask; #ifdef HAVE_SYSLOG_H #include static int syslog_priorities[] = { LOG_DEBUG, LOG_DEBUG, LOG_DEBUG, LOG_INFO, LOG_ERR, LOG_WARNING, LOG_ERR, LOG_ERR, LOG_CRIT, LOG_CRIT }; #endif static char *class_names[] = { "???", "DBG", "TRACE", "INFO", "RMT", "WARN", "ERR", "AUTH", "FATAL", "BUG" }; 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 */ uint prepare; /* Which message parts to prepare */ 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 */ }; struct log_thread_syncer { struct bird_thread_syncer sync; struct log_channel *lc_close; struct rfile *rf_close; sock *sk_close; const char *name; event lts_event; }; static void lts_done(struct bird_thread_syncer *sync) { SKIP_BACK_DECLARE(struct log_thread_syncer, lts, sync, sync); log_lock(); if (lts->lc_close) { lts->rf_close = atomic_load_explicit(<s->lc_close->rf, memory_order_relaxed); lts->sk_close = atomic_load_explicit(<s->lc_close->udp_sk, memory_order_relaxed); mb_free(lts->lc_close); } if (lts->rf_close && lts->rf_close != &rf_stderr) rfree(lts->rf_close); if (lts->sk_close) rfree(lts->sk_close); mb_free(lts); log_unlock(); } static void lts_event(void *_lts) { struct log_thread_syncer *lts = _lts; bird_thread_sync_all(<s->sync, NULL, lts_done, lts->name); } 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"); struct rfile *rf = rf_open(log_pool, lc->filename, RF_APPEND, lc->limit); 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); } /** * log_commit - commit a log message * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|) * @buf: message to write * * 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 log_commit(log_buffer *buf) { /* Store the last pointer */ buf->pos[LBP__MAX] = buf->buf.pos; /* Append the too-long message if too long */ if (buf->buf.pos == buf->buf.end) #define TOO_LONG " ... " memcpy(buf->buf.end - sizeof TOO_LONG, TOO_LONG, sizeof TOO_LONG); #undef TOO_LONG for ( struct log_channel *l = atomic_load_explicit(&global_logs, memory_order_acquire); l; l = atomic_load_explicit(&l->next, memory_order_acquire) ) { uint mask = atomic_load_explicit(&l->mask, memory_order_acquire); if (!(mask & (1 << buf->class))) continue; struct rfile *rf = atomic_load_explicit(&l->rf, memory_order_acquire); sock *sk = atomic_load_explicit(&l->udp_sk, memory_order_acquire); if (rf || sk) { /* 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, }; } if (rf) { iov[iov_count++] = (struct iovec) { .iov_base = newline, .iov_len = sizeof newline - 1, }; do { if (rf_writev(rf, iov, iov_count)) break; log_lock(); rf = atomic_load_explicit(&l->rf, memory_order_acquire); if (rf_writev(rf, iov, iov_count)) { log_unlock(); break; } log_rotate(l); log_unlock(); 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 */ } } #ifdef HAVE_SYSLOG_H else { syslog(syslog_priorities[buf->class], "%s", buf->pos[LBP_MSG]); } #endif } } int buffer_vprint(buffer *buf, const char *fmt, va_list args); void log_prepare(log_buffer *buf, int class) { buf->class = class; 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); buf->pos[LBP_TIMESTAMP] = buf->buf.pos; if (BIT32_TEST(&lf, LBP_TIMESTAMP)) { rcu_read_lock(); const char *fmt = atomic_load_explicit(&global_runtime, memory_order_acquire)->tf_log.fmt1; int t = tm_format_real_time(buf->buf.pos, buf->buf.end - buf->buf.pos, fmt, current_real_time()); rcu_read_unlock(); if (t) buf->buf.pos += t; else buffer_puts(&buf->buf, "