passt: Relicense to GPL 2.0, or any later version
In practical terms, passt doesn't benefit from the additional
protection offered by the AGPL over the GPL, because it's not
suitable to be executed over a computer network.
Further, restricting the distribution under the version 3 of the GPL
wouldn't provide any practical advantage either, as long as the passt
codebase is concerned, and might cause unnecessary compatibility
dilemmas.
Change licensing terms to the GNU General Public License Version 2,
or any later version, with written permission from all current and
past contributors, namely: myself, David Gibson, Laine Stump, Andrea
Bolognani, Paul Holzinger, Richard W.M. Jones, Chris Kuhn, Florian
Weimer, Giuseppe Scrivano, Stefan Hajnoczi, and Vasiliy Ulyanov.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2023-04-05 18:11:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2022-09-24 07:53:15 +00:00
|
|
|
|
|
|
|
/* PASST - Plug A Simple Socket Transport
|
|
|
|
* for qemu/UNIX domain socket mode
|
|
|
|
*
|
|
|
|
* PASTA - Pack A Subtle Tap Abstraction
|
|
|
|
* for network namespace/tap device mode
|
|
|
|
*
|
|
|
|
* log.c - Logging functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020-2022 Red Hat GmbH
|
|
|
|
* Author: Stefano Brivio <sbrivio@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2022-10-06 12:51:04 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2022-09-24 07:53:15 +00:00
|
|
|
#include <stdio.h>
|
2022-10-06 12:51:04 +00:00
|
|
|
#include <stdint.h>
|
2022-09-24 07:53:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2022-10-06 12:51:04 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "passt.h"
|
2022-09-24 07:53:15 +00:00
|
|
|
|
2022-10-06 12:51:04 +00:00
|
|
|
static int log_sock = -1; /* Optional socket to system logger */
|
|
|
|
static char log_ident[BUFSIZ]; /* Identifier string for openlog() */
|
2024-06-14 17:00:27 +00:00
|
|
|
static int log_mask; /* Current log priority mask */
|
2022-10-06 12:51:04 +00:00
|
|
|
|
|
|
|
static int log_file = -1; /* Optional log file descriptor */
|
|
|
|
static size_t log_size; /* Maximum log file size in bytes */
|
|
|
|
static size_t log_written; /* Currently used bytes in log file */
|
|
|
|
static size_t log_cut_size; /* Bytes to cut at start on rotation */
|
|
|
|
static char log_header[BUFSIZ]; /* File header, written back on cuts */
|
|
|
|
|
2024-07-24 15:39:55 +00:00
|
|
|
struct timespec log_start; /* Start timestamp */
|
2024-06-14 17:00:27 +00:00
|
|
|
|
2022-10-06 12:51:04 +00:00
|
|
|
int log_trace; /* --trace mode enabled */
|
2024-06-14 17:00:27 +00:00
|
|
|
bool log_conf_parsed; /* Logging options already parsed */
|
log, passt: Keep printing to stderr when passt is running in foreground
There are two cases where we want to stop printing to stderr: if it's
closed, and if pasta spawned a shell (and --debug wasn't given).
But if passt is running in foreground, we currently stop to report any
message, even error messages, once we're ready, as reported by
Laurent, because we set the log_runtime flag, which we use to indicate
we're ready, regardless of whether we're running in foreground or not.
Turn that flag (back) to log_stderr, and set it only when we really
want to stop printing to stderr.
Reported-by: Laurent Vivier <lvivier@redhat.com>
Fixes: afd9cdc9bb48 ("log, passt: Always print to stderr before initialisation is complete")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2024-08-06 12:07:37 +00:00
|
|
|
bool log_stderr = true; /* Not daemonised, no shell spawned */
|
2022-09-24 07:53:15 +00:00
|
|
|
|
2024-08-06 06:18:37 +00:00
|
|
|
#define LL_STRLEN (sizeof("-9223372036854775808"))
|
|
|
|
#define LOGTIME_STRLEN (LL_STRLEN + 5)
|
|
|
|
|
2024-08-06 06:18:38 +00:00
|
|
|
/**
|
|
|
|
* logtime() - Get the current time for logging purposes
|
|
|
|
* @ts: Buffer into which to store the timestamp
|
|
|
|
*
|
|
|
|
* Return: pointer to @now, or NULL if there was an error retrieving the time
|
|
|
|
*/
|
|
|
|
const struct timespec *logtime(struct timespec *ts)
|
|
|
|
{
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, ts))
|
|
|
|
return NULL;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
log, util: Fix sub-second part in relative log time calculation
For some reason, in commit 01efc71ddd25 ("log, conf: Add support for
logging to file"), I added calculations for relative logging
timestamps using the difference for the seconds part only, not for
accounting for the fractional part.
Fix that by storing the initial timestamp, log_start, as a timespec
struct, and by calculating the difference from the starting time. Do
this in a macro as we need the same format in a few places.
To calculate the difference, turn the existing timespec_diff_ms() to
microseconds, timespec_diff_us(), and rewrite timespec_diff_ms() to
use that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-07-24 15:21:12 +00:00
|
|
|
/**
|
2024-08-06 06:18:37 +00:00
|
|
|
* logtime_fmt() - Format timestamp into a string for the log
|
|
|
|
* @buf: Buffer into which to format the time
|
|
|
|
* @size: Size of @buf
|
2024-08-06 06:18:38 +00:00
|
|
|
* @ts: Time to format (or NULL on error)
|
2024-08-06 06:18:37 +00:00
|
|
|
*
|
|
|
|
* Return: number of characters written to @buf (excluding \0)
|
log, util: Fix sub-second part in relative log time calculation
For some reason, in commit 01efc71ddd25 ("log, conf: Add support for
logging to file"), I added calculations for relative logging
timestamps using the difference for the seconds part only, not for
accounting for the fractional part.
Fix that by storing the initial timestamp, log_start, as a timespec
struct, and by calculating the difference from the starting time. Do
this in a macro as we need the same format in a few places.
To calculate the difference, turn the existing timespec_diff_ms() to
microseconds, timespec_diff_us(), and rewrite timespec_diff_ms() to
use that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-07-24 15:21:12 +00:00
|
|
|
*/
|
2024-08-06 06:18:37 +00:00
|
|
|
static int logtime_fmt(char *buf, size_t size, const struct timespec *ts)
|
|
|
|
{
|
2024-08-06 06:18:38 +00:00
|
|
|
if (ts) {
|
|
|
|
int64_t delta = timespec_diff_us(ts, &log_start);
|
|
|
|
|
|
|
|
return snprintf(buf, size, "%lli.%04lli", delta / 1000000LL,
|
|
|
|
(delta / 100LL) % 10000);
|
|
|
|
}
|
2024-08-06 06:18:37 +00:00
|
|
|
|
2024-08-06 06:18:38 +00:00
|
|
|
return snprintf(buf, size, "<error>");
|
2024-08-06 06:18:37 +00:00
|
|
|
}
|
log, util: Fix sub-second part in relative log time calculation
For some reason, in commit 01efc71ddd25 ("log, conf: Add support for
logging to file"), I added calculations for relative logging
timestamps using the difference for the seconds part only, not for
accounting for the fractional part.
Fix that by storing the initial timestamp, log_start, as a timespec
struct, and by calculating the difference from the starting time. Do
this in a macro as we need the same format in a few places.
To calculate the difference, turn the existing timespec_diff_ms() to
microseconds, timespec_diff_us(), and rewrite timespec_diff_ms() to
use that.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-07-24 15:21:12 +00:00
|
|
|
|
2024-07-29 04:22:59 +00:00
|
|
|
/* Prefixes for log file messages, indexed by priority */
|
|
|
|
const char *logfile_prefix[] = {
|
|
|
|
NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */
|
|
|
|
"ERROR: ",
|
|
|
|
"WARNING: ",
|
|
|
|
NULL, /* Unused: LOG_NOTICE */
|
|
|
|
"info: ",
|
|
|
|
" ", /* LOG_DEBUG */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef FALLOC_FL_COLLAPSE_RANGE
|
|
|
|
/**
|
|
|
|
* logfile_rotate_fallocate() - Write header, set log_written after fallocate()
|
|
|
|
* @fd: Log file descriptor
|
|
|
|
* @now: Current timestamp
|
|
|
|
*
|
treewide: Allow additional system calls for i386/i686
I haven't tested i386 for a long time (after playing with some
openSUSE i586 image a couple of years ago). It turns out that a number
of system calls we actually need were denied by the seccomp filter,
and not even basic functionality works.
Add some system calls that glibc started using with the 64-bit time
("t64") transition, see also:
https://wiki.debian.org/ReleaseGoals/64bit-time
that is: clock_gettime64, timerfd_gettime64, fcntl64, and
recvmmsg_time64.
Add further system calls that are needed regardless of time_t width,
that is, mmap2 (valgrind profile only), _llseek and sigreturn (common
outside x86_64), and socketcall (same as s390x).
I validated this against an almost full run of the test suite, with
just a few selected tests skipped. Fixes needed to run most tests on
i386/i686, and other assorted fixes for tests, are included in
upcoming patches.
Reported-by: Uroš Knupleš <uros@knuples.net>
Analysed-by: Faidon Liambotis <paravoid@debian.org>
Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1078981
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-08-19 21:42:30 +00:00
|
|
|
* #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek i686:_llseek
|
2024-07-29 04:22:59 +00:00
|
|
|
*/
|
|
|
|
static void logfile_rotate_fallocate(int fd, const struct timespec *now)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *nl;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == -1)
|
|
|
|
return;
|
|
|
|
if (read(fd, buf, BUFSIZ) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
n = snprintf(buf, BUFSIZ, "%s - log truncated at ", log_header);
|
2024-08-06 06:18:37 +00:00
|
|
|
n += logtime_fmt(buf + n, BUFSIZ - n, now);
|
2024-07-29 04:22:59 +00:00
|
|
|
|
|
|
|
/* Avoid partial lines by padding the header with spaces */
|
|
|
|
nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
|
|
|
|
if (nl)
|
|
|
|
memset(buf + n, ' ', nl - (buf + n));
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == -1)
|
|
|
|
return;
|
|
|
|
if (write(fd, buf, BUFSIZ) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_written -= log_cut_size;
|
|
|
|
}
|
|
|
|
#endif /* FALLOC_FL_COLLAPSE_RANGE */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* logfile_rotate_move() - Fallback: move recent entries toward start, then cut
|
|
|
|
* @fd: Log file descriptor
|
|
|
|
* @now: Current timestamp
|
|
|
|
*
|
|
|
|
* #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek
|
|
|
|
* #syscalls ftruncate
|
|
|
|
*/
|
|
|
|
static void logfile_rotate_move(int fd, const struct timespec *now)
|
|
|
|
{
|
|
|
|
int header_len, write_offset, end, discard, n;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *nl;
|
|
|
|
|
|
|
|
header_len = snprintf(buf, BUFSIZ, "%s - log truncated at ",
|
|
|
|
log_header);
|
2024-08-06 06:18:37 +00:00
|
|
|
header_len += logtime_fmt(buf + header_len, BUFSIZ - header_len, now);
|
2024-07-29 04:22:59 +00:00
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == -1)
|
|
|
|
return;
|
|
|
|
if (write(fd, buf, header_len) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
end = write_offset = header_len;
|
|
|
|
discard = log_cut_size + header_len;
|
|
|
|
|
|
|
|
/* Try to cut cleanly at newline */
|
|
|
|
if (lseek(fd, discard, SEEK_SET) == -1)
|
|
|
|
goto out;
|
|
|
|
if ((n = read(fd, buf, BUFSIZ)) <= 0)
|
|
|
|
goto out;
|
|
|
|
if ((nl = memchr(buf, '\n', n)))
|
|
|
|
discard += (nl - buf) + 1;
|
|
|
|
|
|
|
|
/* Go to first block to be moved */
|
|
|
|
if (lseek(fd, discard, SEEK_SET) == -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
while ((n = read(fd, buf, BUFSIZ)) > 0) {
|
|
|
|
end = header_len;
|
|
|
|
|
|
|
|
if (lseek(fd, write_offset, SEEK_SET) == -1)
|
|
|
|
goto out;
|
|
|
|
if ((n = write(fd, buf, n)) == -1)
|
|
|
|
goto out;
|
|
|
|
write_offset += n;
|
|
|
|
|
|
|
|
if ((n = lseek(fd, 0, SEEK_CUR)) == -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (lseek(fd, discard - header_len, SEEK_CUR) == -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
end = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ftruncate(fd, end))
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_written = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* logfile_rotate() - "Rotate" log file once it's full
|
|
|
|
* @fd: Log file descriptor
|
|
|
|
* @now: Current timestamp
|
|
|
|
*
|
|
|
|
* Return: 0 on success, negative error code on failure
|
|
|
|
*
|
|
|
|
* #syscalls fcntl
|
|
|
|
*
|
|
|
|
* fallocate() passed as EXTRA_SYSCALL only if FALLOC_FL_COLLAPSE_RANGE is there
|
|
|
|
*/
|
|
|
|
static int logfile_rotate(int fd, const struct timespec *now)
|
|
|
|
{
|
|
|
|
if (fcntl(fd, F_SETFL, O_RDWR /* Drop O_APPEND: explicit lseek() */))
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
#ifdef FALLOC_FL_COLLAPSE_RANGE
|
|
|
|
/* Only for Linux >= 3.15, extent-based ext4 or XFS, glibc >= 2.18 */
|
|
|
|
if (!fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, log_cut_size))
|
|
|
|
logfile_rotate_fallocate(fd, now);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
logfile_rotate_move(fd, now);
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND))
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* logfile_write() - Write entry to log file, trigger rotation if full
|
|
|
|
* @newline: Append newline at the end of the message, if missing
|
2024-09-03 21:45:53 +00:00
|
|
|
* @cont: Continuation of a previous message, on the same line
|
2024-07-29 04:22:59 +00:00
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
2024-08-06 06:18:39 +00:00
|
|
|
* @now: Timestamp
|
2024-07-29 04:22:59 +00:00
|
|
|
* @format: Same as vsyslog() format
|
|
|
|
* @ap: Same as vsyslog() ap
|
|
|
|
*/
|
2024-09-03 21:45:53 +00:00
|
|
|
static void logfile_write(bool newline, bool cont, int pri,
|
|
|
|
const struct timespec *now,
|
2024-08-06 06:18:39 +00:00
|
|
|
const char *format, va_list ap)
|
2024-07-29 04:22:59 +00:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2024-09-03 21:45:53 +00:00
|
|
|
int n = 0;
|
2024-07-29 04:22:59 +00:00
|
|
|
|
2024-09-03 21:45:53 +00:00
|
|
|
if (!cont) {
|
|
|
|
n += logtime_fmt(buf, BUFSIZ, now);
|
|
|
|
n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]);
|
|
|
|
}
|
2024-07-29 04:22:59 +00:00
|
|
|
|
|
|
|
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
|
|
|
|
|
|
|
|
if (newline && format[strlen(format)] != '\n')
|
|
|
|
n += snprintf(buf + n, BUFSIZ - n, "\n");
|
|
|
|
|
2024-08-06 06:18:38 +00:00
|
|
|
if ((log_written + n >= log_size) && logfile_rotate(log_file, now))
|
2024-07-29 04:22:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ((n = write(log_file, buf, n)) >= 0)
|
|
|
|
log_written += n;
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:36:17 +00:00
|
|
|
/**
|
|
|
|
* vlogmsg() - Print or send messages to log or output files as configured
|
|
|
|
* @newline: Append newline at the end of the message, if missing
|
2024-08-12 08:20:34 +00:00
|
|
|
* @cont: Continuation of a previous message, on the same line
|
2024-07-24 14:36:17 +00:00
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Message
|
|
|
|
* @ap: Variable argument list
|
|
|
|
*/
|
2024-08-12 08:20:34 +00:00
|
|
|
void vlogmsg(bool newline, bool cont, int pri, const char *format, va_list ap)
|
2023-10-13 04:50:28 +00:00
|
|
|
{
|
log: setlogmask(0) can actually result in a system call, don't use it
Before commit 32d07f5e59f2 ("passt, pasta: Completely avoid dynamic
memory allocation"), we didn't store the current log mask in a
variable, and we fetched it using setlogmask(0) wherever needed.
But after that commit, we can use our log_mask copy instead. And we
should: with recent glibc versions, setlogmask(0) actually results in
a system call, which causes a substantial overhead with high transfer
rates: we use setlogmask(0) even to decide we don't want to print
debug messages.
Now that we rely on log_mask in early stages, before setlogmask() is
called, we need to initialise that variable to the special LOG_EMERG
mask value right away: define LOG_EARLY to make this clearer, and,
while at it, group conditions in vlogmsg() into something more terse.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-02-01 23:22:16 +00:00
|
|
|
bool debug_print = (log_mask & LOG_MASK(LOG_DEBUG)) && log_file == -1;
|
2024-08-06 06:18:39 +00:00
|
|
|
const struct timespec *now;
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
now = logtime(&ts);
|
2023-10-13 04:50:28 +00:00
|
|
|
|
2024-08-12 08:20:34 +00:00
|
|
|
if (debug_print && !cont) {
|
2024-08-06 06:18:38 +00:00
|
|
|
char timestr[LOGTIME_STRLEN];
|
2024-08-06 06:18:37 +00:00
|
|
|
|
2024-08-06 06:18:38 +00:00
|
|
|
logtime_fmt(timestr, sizeof(timestr), now);
|
2024-10-24 21:44:43 +00:00
|
|
|
FPRINTF(stderr, "%s: ", timestr);
|
2023-10-13 04:50:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 17:00:27 +00:00
|
|
|
if ((log_mask & LOG_MASK(LOG_PRI(pri))) || !log_conf_parsed) {
|
2023-10-13 04:50:30 +00:00
|
|
|
va_list ap2;
|
|
|
|
|
|
|
|
va_copy(ap2, ap); /* Don't clobber ap, we need it again */
|
2023-10-13 04:50:28 +00:00
|
|
|
if (log_file != -1)
|
2024-09-03 21:45:53 +00:00
|
|
|
logfile_write(newline, cont, pri, now, format, ap2);
|
log: setlogmask(0) can actually result in a system call, don't use it
Before commit 32d07f5e59f2 ("passt, pasta: Completely avoid dynamic
memory allocation"), we didn't store the current log mask in a
variable, and we fetched it using setlogmask(0) wherever needed.
But after that commit, we can use our log_mask copy instead. And we
should: with recent glibc versions, setlogmask(0) actually results in
a system call, which causes a substantial overhead with high transfer
rates: we use setlogmask(0) even to decide we don't want to print
debug messages.
Now that we rely on log_mask in early stages, before setlogmask() is
called, we need to initialise that variable to the special LOG_EMERG
mask value right away: define LOG_EARLY to make this clearer, and,
while at it, group conditions in vlogmsg() into something more terse.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-02-01 23:22:16 +00:00
|
|
|
else if (!(log_mask & LOG_MASK(LOG_DEBUG)))
|
2024-07-24 14:36:17 +00:00
|
|
|
passt_vsyslog(newline, pri, format, ap2);
|
2023-11-07 11:17:07 +00:00
|
|
|
|
|
|
|
va_end(ap2);
|
2023-10-13 04:50:28 +00:00
|
|
|
}
|
|
|
|
|
log, passt: Always print to stderr before initialisation is complete
After commit 15001b39ef1d ("conf: set the log level much earlier"), we
had a phase during initialisation when messages wouldn't be printed to
standard error anymore.
Commit f67238aa864d ("passt, log: Call __openlog() earlier, log to
stderr until we detach") fixed that, but only for the case where no
log files are given.
If a log file is configured, vlogmsg() will not call passt_vsyslog(),
but during initialisation, LOG_PERROR is set, so to avoid duplicated
prints (which would result from passt_vsyslog() printing to stderr),
we don't call fprintf() from vlogmsg() either.
This is getting a bit too complicated. Instead of abusing LOG_PERROR,
define an internal logging flag that clearly represents that we're not
done with the initialisation phase yet.
If this flag is not set, make sure we always print to stderr, if the
log mask matches.
Reported-by: Yalan Zhang <yalzhang@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-06-14 20:47:51 +00:00
|
|
|
if (debug_print || !log_conf_parsed ||
|
log, passt: Keep printing to stderr when passt is running in foreground
There are two cases where we want to stop printing to stderr: if it's
closed, and if pasta spawned a shell (and --debug wasn't given).
But if passt is running in foreground, we currently stop to report any
message, even error messages, once we're ready, as reported by
Laurent, because we set the log_runtime flag, which we use to indicate
we're ready, regardless of whether we're running in foreground or not.
Turn that flag (back) to log_stderr, and set it only when we really
want to stop printing to stderr.
Reported-by: Laurent Vivier <lvivier@redhat.com>
Fixes: afd9cdc9bb48 ("log, passt: Always print to stderr before initialisation is complete")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2024-08-06 12:07:37 +00:00
|
|
|
(log_stderr && (log_mask & LOG_MASK(LOG_PRI(pri))))) {
|
2024-06-05 00:42:42 +00:00
|
|
|
(void)vfprintf(stderr, format, ap);
|
2024-07-24 14:36:17 +00:00
|
|
|
if (newline && format[strlen(format)] != '\n')
|
2024-10-24 21:44:43 +00:00
|
|
|
FPRINTF(stderr, "\n");
|
2023-10-13 04:50:28 +00:00
|
|
|
}
|
2022-09-24 07:53:15 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 22:25:23 +00:00
|
|
|
/**
|
|
|
|
* logmsg() - vlogmsg() wrapper for variable argument lists
|
2024-07-24 14:36:17 +00:00
|
|
|
* @newline: Append newline at the end of the message, if missing
|
2024-08-12 08:20:34 +00:00
|
|
|
* @cont: Continuation of a previous message, on the same line
|
2024-06-14 22:25:23 +00:00
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Message
|
|
|
|
*/
|
2024-08-12 08:20:34 +00:00
|
|
|
void logmsg(bool newline, bool cont, int pri, const char *format, ...)
|
2023-10-13 04:50:30 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2024-08-12 08:20:34 +00:00
|
|
|
vlogmsg(newline, cont, pri, format, ap);
|
2023-10-13 04:50:30 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2024-06-14 22:25:23 +00:00
|
|
|
/**
|
|
|
|
* logmsg_perror() - vlogmsg() wrapper with perror()-like functionality
|
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Message
|
|
|
|
*/
|
|
|
|
void logmsg_perror(int pri, const char *format, ...)
|
|
|
|
{
|
|
|
|
int errno_copy = errno;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2024-08-12 08:20:34 +00:00
|
|
|
vlogmsg(false, false, pri, format, ap);
|
2024-06-14 22:25:23 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2024-08-12 08:20:34 +00:00
|
|
|
logmsg(true, true, pri, ": %s", strerror(errno_copy));
|
2024-06-14 22:25:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 12:59:26 +00:00
|
|
|
/**
|
|
|
|
* trace_init() - Set log_trace depending on trace (debug) mode
|
|
|
|
* @enable: Tracing debug mode enabled if non-zero
|
|
|
|
*/
|
2022-09-24 07:53:15 +00:00
|
|
|
void trace_init(int enable)
|
|
|
|
{
|
|
|
|
log_trace = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-23 17:32:20 +00:00
|
|
|
* __openlog() - Non-optional openlog() implementation, for custom vsyslog()
|
2022-09-24 07:53:15 +00:00
|
|
|
* @ident: openlog() identity (program name)
|
log, passt: Always print to stderr before initialisation is complete
After commit 15001b39ef1d ("conf: set the log level much earlier"), we
had a phase during initialisation when messages wouldn't be printed to
standard error anymore.
Commit f67238aa864d ("passt, log: Call __openlog() earlier, log to
stderr until we detach") fixed that, but only for the case where no
log files are given.
If a log file is configured, vlogmsg() will not call passt_vsyslog(),
but during initialisation, LOG_PERROR is set, so to avoid duplicated
prints (which would result from passt_vsyslog() printing to stderr),
we don't call fprintf() from vlogmsg() either.
This is getting a bit too complicated. Instead of abusing LOG_PERROR,
define an internal logging flag that clearly represents that we're not
done with the initialisation phase yet.
If this flag is not set, make sure we always print to stderr, if the
log mask matches.
Reported-by: Yalan Zhang <yalzhang@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-06-14 20:47:51 +00:00
|
|
|
* @option: openlog() options, unused
|
2022-09-24 07:53:15 +00:00
|
|
|
* @facility: openlog() facility (LOG_DAEMON)
|
|
|
|
*/
|
|
|
|
void __openlog(const char *ident, int option, int facility)
|
|
|
|
{
|
log, passt: Always print to stderr before initialisation is complete
After commit 15001b39ef1d ("conf: set the log level much earlier"), we
had a phase during initialisation when messages wouldn't be printed to
standard error anymore.
Commit f67238aa864d ("passt, log: Call __openlog() earlier, log to
stderr until we detach") fixed that, but only for the case where no
log files are given.
If a log file is configured, vlogmsg() will not call passt_vsyslog(),
but during initialisation, LOG_PERROR is set, so to avoid duplicated
prints (which would result from passt_vsyslog() printing to stderr),
we don't call fprintf() from vlogmsg() either.
This is getting a bit too complicated. Instead of abusing LOG_PERROR,
define an internal logging flag that clearly represents that we're not
done with the initialisation phase yet.
If this flag is not set, make sure we always print to stderr, if the
log mask matches.
Reported-by: Yalan Zhang <yalzhang@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-06-14 20:47:51 +00:00
|
|
|
(void)option;
|
|
|
|
|
2022-09-24 07:53:15 +00:00
|
|
|
if (log_sock < 0) {
|
|
|
|
struct sockaddr_un a = { .sun_family = AF_UNIX, };
|
|
|
|
|
|
|
|
log_sock = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (log_sock < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
strncpy(a.sun_path, _PATH_LOG, sizeof(a.sun_path));
|
|
|
|
if (connect(log_sock, (const struct sockaddr *)&a, sizeof(a))) {
|
|
|
|
close(log_sock);
|
|
|
|
log_sock = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_mask |= facility;
|
|
|
|
strncpy(log_ident, ident, sizeof(log_ident) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __setlogmask() - setlogmask() wrapper, to allow custom vsyslog()
|
|
|
|
* @mask: Same as setlogmask() mask
|
|
|
|
*/
|
|
|
|
void __setlogmask(int mask)
|
|
|
|
{
|
|
|
|
log_mask = mask;
|
|
|
|
setlogmask(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* passt_vsyslog() - vsyslog() implementation not using heap memory
|
2024-07-24 14:36:17 +00:00
|
|
|
* @newline: Append newline at the end of the message, if missing
|
2022-09-24 07:53:15 +00:00
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Same as vsyslog() format
|
|
|
|
* @ap: Same as vsyslog() ap
|
|
|
|
*/
|
2024-07-24 14:36:17 +00:00
|
|
|
void passt_vsyslog(bool newline, int pri, const char *format, va_list ap)
|
2022-09-24 07:53:15 +00:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
log, passt: Always print to stderr before initialisation is complete
After commit 15001b39ef1d ("conf: set the log level much earlier"), we
had a phase during initialisation when messages wouldn't be printed to
standard error anymore.
Commit f67238aa864d ("passt, log: Call __openlog() earlier, log to
stderr until we detach") fixed that, but only for the case where no
log files are given.
If a log file is configured, vlogmsg() will not call passt_vsyslog(),
but during initialisation, LOG_PERROR is set, so to avoid duplicated
prints (which would result from passt_vsyslog() printing to stderr),
we don't call fprintf() from vlogmsg() either.
This is getting a bit too complicated. Instead of abusing LOG_PERROR,
define an internal logging flag that clearly represents that we're not
done with the initialisation phase yet.
If this flag is not set, make sure we always print to stderr, if the
log mask matches.
Reported-by: Yalan Zhang <yalzhang@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-06-14 20:47:51 +00:00
|
|
|
int n;
|
2022-09-24 07:53:15 +00:00
|
|
|
|
2023-02-23 17:32:20 +00:00
|
|
|
/* Send without timestamp, the system logger should add it */
|
log, passt: Always print to stderr before initialisation is complete
After commit 15001b39ef1d ("conf: set the log level much earlier"), we
had a phase during initialisation when messages wouldn't be printed to
standard error anymore.
Commit f67238aa864d ("passt, log: Call __openlog() earlier, log to
stderr until we detach") fixed that, but only for the case where no
log files are given.
If a log file is configured, vlogmsg() will not call passt_vsyslog(),
but during initialisation, LOG_PERROR is set, so to avoid duplicated
prints (which would result from passt_vsyslog() printing to stderr),
we don't call fprintf() from vlogmsg() either.
This is getting a bit too complicated. Instead of abusing LOG_PERROR,
define an internal logging flag that clearly represents that we're not
done with the initialisation phase yet.
If this flag is not set, make sure we always print to stderr, if the
log mask matches.
Reported-by: Yalan Zhang <yalzhang@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
2024-06-14 20:47:51 +00:00
|
|
|
n = snprintf(buf, BUFSIZ, "<%i> %s: ", pri, log_ident);
|
2022-09-24 07:53:15 +00:00
|
|
|
|
|
|
|
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
|
|
|
|
|
2024-07-24 14:36:17 +00:00
|
|
|
if (newline && format[strlen(format)] != '\n')
|
2022-09-24 07:53:15 +00:00
|
|
|
n += snprintf(buf + n, BUFSIZ - n, "\n");
|
|
|
|
|
log, passt: Keep printing to stderr when passt is running in foreground
There are two cases where we want to stop printing to stderr: if it's
closed, and if pasta spawned a shell (and --debug wasn't given).
But if passt is running in foreground, we currently stop to report any
message, even error messages, once we're ready, as reported by
Laurent, because we set the log_runtime flag, which we use to indicate
we're ready, regardless of whether we're running in foreground or not.
Turn that flag (back) to log_stderr, and set it only when we really
want to stop printing to stderr.
Reported-by: Laurent Vivier <lvivier@redhat.com>
Fixes: afd9cdc9bb48 ("log, passt: Always print to stderr before initialisation is complete")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
2024-08-06 12:07:37 +00:00
|
|
|
if (log_sock >= 0 && send(log_sock, buf, n, 0) != n && log_stderr)
|
2024-10-24 21:44:43 +00:00
|
|
|
FPRINTF(stderr, "Failed to send %i bytes to syslog\n", n);
|
2022-09-24 07:53:15 +00:00
|
|
|
}
|
2022-10-06 12:51:04 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-10 08:35:47 +00:00
|
|
|
* logfile_init() - Open log file and write header with PID, version, path
|
2022-10-06 12:51:04 +00:00
|
|
|
* @name: Identifier for header: passt or pasta
|
|
|
|
* @path: Path to log file
|
|
|
|
* @size: Maximum size of log file: log_cut_size is calculatd here
|
|
|
|
*/
|
|
|
|
void logfile_init(const char *name, const char *path, size_t size)
|
|
|
|
{
|
|
|
|
char nl = '\n', exe[PATH_MAX] = { 0 };
|
|
|
|
int n;
|
|
|
|
|
2024-06-14 22:37:11 +00:00
|
|
|
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0)
|
|
|
|
die_perror("Failed to read own /proc/self/exe link");
|
2022-10-06 12:51:04 +00:00
|
|
|
|
2024-10-24 22:10:36 +00:00
|
|
|
log_file = output_file_open(path, O_APPEND | O_RDWR);
|
2023-02-15 08:24:37 +00:00
|
|
|
if (log_file == -1)
|
2024-06-17 09:55:04 +00:00
|
|
|
die_perror("Couldn't open log file %s", path);
|
2022-10-06 12:51:04 +00:00
|
|
|
|
|
|
|
log_size = size ? size : LOGFILE_SIZE_DEFAULT;
|
|
|
|
|
2022-10-10 08:35:47 +00:00
|
|
|
n = snprintf(log_header, sizeof(log_header), "%s " VERSION ": %s (%i)",
|
2022-10-06 12:51:04 +00:00
|
|
|
name, exe, getpid());
|
|
|
|
|
|
|
|
if (write(log_file, log_header, n) <= 0 ||
|
2024-06-14 22:37:11 +00:00
|
|
|
write(log_file, &nl, 1) <= 0)
|
|
|
|
die_perror("Couldn't write to log file");
|
2022-10-06 12:51:04 +00:00
|
|
|
|
|
|
|
/* For FALLOC_FL_COLLAPSE_RANGE: VFS block size can be up to one page */
|
|
|
|
log_cut_size = ROUND_UP(log_size * LOGFILE_CUT_RATIO / 100, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|