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 20:11:44 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2022-09-24 09:53:15 +02: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 14:51:04 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2022-09-24 09:53:15 +02:00
|
|
|
#include <stdio.h>
|
2022-10-06 14:51:04 +02:00
|
|
|
#include <stdint.h>
|
2022-09-24 09:53:15 +02: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 14:51:04 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "passt.h"
|
2022-09-24 09:53:15 +02:00
|
|
|
|
2022-10-06 14:51:04 +02:00
|
|
|
static int log_sock = -1; /* Optional socket to system logger */
|
|
|
|
static char log_ident[BUFSIZ]; /* Identifier string for openlog() */
|
|
|
|
static int log_mask; /* Current log priority mask */
|
|
|
|
static int log_opt; /* Options for openlog() */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
static time_t log_start; /* Start timestamp */
|
|
|
|
int log_trace; /* --trace mode enabled */
|
2023-06-04 07:14:49 +02:00
|
|
|
int log_to_stdout; /* Print to stdout instead of stderr */
|
2022-09-24 09:53:15 +02:00
|
|
|
|
2023-02-15 12:47:48 +01:00
|
|
|
#define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG))
|
|
|
|
|
2023-10-13 06:50:30 +02:00
|
|
|
void vlogmsg(int pri, const char *format, va_list ap)
|
2023-10-13 06:50:28 +02:00
|
|
|
{
|
|
|
|
FILE *out = log_to_stdout ? stdout : stderr;
|
|
|
|
struct timespec tp;
|
|
|
|
|
|
|
|
if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) {
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
fprintf(out, "%lli.%04lli: ",
|
|
|
|
(long long int)tp.tv_sec - log_start,
|
|
|
|
(long long int)tp.tv_nsec / (100L * 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((LOG_MASK(LOG_PRI(pri)) & log_mask) || BEFORE_DAEMON) {
|
2023-10-13 06:50:30 +02:00
|
|
|
va_list ap2;
|
|
|
|
|
|
|
|
va_copy(ap2, ap); /* Don't clobber ap, we need it again */
|
2023-10-13 06:50:28 +02:00
|
|
|
if (log_file != -1)
|
2023-10-13 06:50:30 +02:00
|
|
|
logfile_write(pri, format, ap2);
|
2023-10-13 06:50:28 +02:00
|
|
|
else if (!(setlogmask(0) & LOG_MASK(LOG_DEBUG)))
|
2023-10-13 06:50:30 +02:00
|
|
|
passt_vsyslog(pri, format, ap2);
|
2023-11-07 12:17:07 +01:00
|
|
|
|
|
|
|
va_end(ap2);
|
2023-10-13 06:50:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) ||
|
|
|
|
(BEFORE_DAEMON && !(log_opt & LOG_PERROR))) {
|
2023-10-13 06:50:30 +02:00
|
|
|
(void)vfprintf(out, format, ap);
|
2023-10-13 06:50:28 +02:00
|
|
|
if (format[strlen(format)] != '\n')
|
|
|
|
fprintf(out, "\n");
|
|
|
|
}
|
2022-09-24 09:53:15 +02:00
|
|
|
}
|
|
|
|
|
2023-10-13 06:50:30 +02:00
|
|
|
void logmsg(int pri, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vlogmsg(pri, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:51:04 +02: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 */
|
|
|
|
};
|
|
|
|
|
2022-10-06 14:59:26 +02:00
|
|
|
/**
|
|
|
|
* trace_init() - Set log_trace depending on trace (debug) mode
|
|
|
|
* @enable: Tracing debug mode enabled if non-zero
|
|
|
|
*/
|
2022-09-24 09:53:15 +02:00
|
|
|
void trace_init(int enable)
|
|
|
|
{
|
|
|
|
log_trace = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-23 18:32:20 +01:00
|
|
|
* __openlog() - Non-optional openlog() implementation, for custom vsyslog()
|
2022-09-24 09:53:15 +02:00
|
|
|
* @ident: openlog() identity (program name)
|
|
|
|
* @option: openlog() options
|
|
|
|
* @facility: openlog() facility (LOG_DAEMON)
|
|
|
|
*/
|
|
|
|
void __openlog(const char *ident, int option, int facility)
|
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tp);
|
2022-10-06 14:51:04 +02:00
|
|
|
log_start = tp.tv_sec;
|
2022-09-24 09:53:15 +02: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);
|
|
|
|
log_opt = option;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __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
|
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Same as vsyslog() format
|
|
|
|
* @ap: Same as vsyslog() ap
|
|
|
|
*/
|
|
|
|
void passt_vsyslog(int pri, const char *format, va_list ap)
|
|
|
|
{
|
2023-02-23 18:32:20 +01:00
|
|
|
int prefix_len, n;
|
2022-09-24 09:53:15 +02:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
2023-02-23 18:32:20 +01:00
|
|
|
/* Send without timestamp, the system logger should add it */
|
|
|
|
n = prefix_len = snprintf(buf, BUFSIZ, "<%i> %s: ", pri, log_ident);
|
2022-09-24 09:53:15 +02:00
|
|
|
|
|
|
|
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
|
|
|
|
|
|
|
|
if (format[strlen(format)] != '\n')
|
|
|
|
n += snprintf(buf + n, BUFSIZ - n, "\n");
|
|
|
|
|
|
|
|
if (log_opt & LOG_PERROR)
|
2023-02-23 18:32:20 +01:00
|
|
|
fprintf(stderr, "%s", buf + prefix_len);
|
2022-09-24 09:53:15 +02:00
|
|
|
|
|
|
|
if (send(log_sock, buf, n, 0) != n)
|
|
|
|
fprintf(stderr, "Failed to send %i bytes to syslog\n", n);
|
|
|
|
}
|
2022-10-06 14:51:04 +02:00
|
|
|
|
|
|
|
/**
|
2022-10-10 10:35:47 +02:00
|
|
|
* logfile_init() - Open log file and write header with PID, version, path
|
2022-10-06 14:51:04 +02: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;
|
|
|
|
|
|
|
|
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) {
|
|
|
|
perror("readlink /proc/self/exe");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2023-02-15 09:24:37 +01:00
|
|
|
if (log_file == -1)
|
|
|
|
die("Couldn't open log file %s: %s", path, strerror(errno));
|
2022-10-06 14:51:04 +02:00
|
|
|
|
|
|
|
log_size = size ? size : LOGFILE_SIZE_DEFAULT;
|
|
|
|
|
2022-10-10 10:35:47 +02:00
|
|
|
n = snprintf(log_header, sizeof(log_header), "%s " VERSION ": %s (%i)",
|
2022-10-06 14:51:04 +02:00
|
|
|
name, exe, getpid());
|
|
|
|
|
|
|
|
if (write(log_file, log_header, n) <= 0 ||
|
|
|
|
write(log_file, &nl, 1) <= 0) {
|
|
|
|
perror("Couldn't write to log file\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FALLOC_FL_COLLAPSE_RANGE
|
|
|
|
/**
|
|
|
|
* logfile_rotate_fallocate() - Write header, set log_written after fallocate()
|
|
|
|
* @fd: Log file descriptor
|
|
|
|
* @ts: Current timestamp
|
|
|
|
*
|
|
|
|
* #syscalls lseek ppc64le:_llseek ppc64:_llseek armv6l:_llseek armv7l:_llseek
|
|
|
|
*/
|
2023-09-29 07:50:19 +02:00
|
|
|
static void logfile_rotate_fallocate(int fd, const struct timespec *ts)
|
2022-10-06 14:51:04 +02:00
|
|
|
{
|
2024-01-15 07:39:43 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *nl;
|
2022-10-06 14:51:04 +02:00
|
|
|
int n;
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == -1)
|
|
|
|
return;
|
|
|
|
if (read(fd, buf, BUFSIZ) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
n = snprintf(buf, BUFSIZ,
|
2023-03-10 23:15:35 +01:00
|
|
|
"%s - log truncated at %lli.%04lli", log_header,
|
|
|
|
(long long int)(ts->tv_sec - log_start),
|
|
|
|
(long long int)(ts->tv_nsec / (100L * 1000)));
|
2022-10-06 14:51:04 +02: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
|
|
|
|
* @ts: Current timestamp
|
|
|
|
*
|
|
|
|
* #syscalls lseek ppc64le:_llseek ppc64:_llseek armv6l:_llseek armv7l:_llseek
|
|
|
|
* #syscalls ftruncate
|
|
|
|
*/
|
2023-09-29 07:50:19 +02:00
|
|
|
static void logfile_rotate_move(int fd, const struct timespec *ts)
|
2022-10-06 14:51:04 +02:00
|
|
|
{
|
|
|
|
int header_len, write_offset, end, discard, n;
|
2024-01-15 07:39:43 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
const char *nl;
|
2022-10-06 14:51:04 +02:00
|
|
|
|
|
|
|
header_len = snprintf(buf, BUFSIZ,
|
2023-03-10 23:15:35 +01:00
|
|
|
"%s - log truncated at %lli.%04lli\n", log_header,
|
|
|
|
(long long int)(ts->tv_sec - log_start),
|
|
|
|
(long long int)(ts->tv_nsec / (100L * 1000)));
|
2022-10-06 14:51:04 +02: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
|
|
|
|
* @ts: 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
|
|
|
|
*/
|
2023-09-29 07:50:19 +02:00
|
|
|
static int logfile_rotate(int fd, const struct timespec *ts)
|
2022-10-06 14:51:04 +02:00
|
|
|
{
|
|
|
|
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, ts);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
logfile_rotate_move(fd, ts);
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND))
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* logfile_write() - Write entry to log file, trigger rotation if full
|
|
|
|
* @pri: Facility and level map, same as priority for vsyslog()
|
|
|
|
* @format: Same as vsyslog() format
|
|
|
|
* @ap: Same as vsyslog() ap
|
|
|
|
*/
|
|
|
|
void logfile_write(int pri, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts))
|
|
|
|
return;
|
|
|
|
|
2023-03-10 23:15:35 +01:00
|
|
|
n = snprintf(buf, BUFSIZ, "%lli.%04lli: %s",
|
|
|
|
(long long int)(ts.tv_sec - log_start),
|
|
|
|
(long long int)(ts.tv_nsec / (100L * 1000)),
|
2022-10-06 14:51:04 +02:00
|
|
|
logfile_prefix[pri]);
|
|
|
|
|
|
|
|
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
|
|
|
|
|
|
|
|
if (format[strlen(format)] != '\n')
|
|
|
|
n += snprintf(buf + n, BUFSIZ - n, "\n");
|
|
|
|
|
|
|
|
if ((log_written + n >= log_size) && logfile_rotate(log_file, &ts))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((n = write(log_file, buf, n)) >= 0)
|
|
|
|
log_written += n;
|
|
|
|
}
|