[PATCH v2 0/6] Fixes for early logging/prints and related cleanups
The most apparent issue fixed by this series is the one from 3/6: with a log file configured, we wouldn't print to standard error anymore, during initialisation, which means that users such as libvirt lost the ability to report meaningful error messages that occurred during initialisation, in that case. v2: - turn flag bitmap into simple, separate boolean flags - move errno description after message in _perror() functions - make some of the old perror() messages more descriptive Stefano Brivio (6): conf, passt: Don't try to log to stderr after we close it conf, log: Instead of abusing log levels, add log_conf_parsed flag log, passt: Always print to stderr before initialisation is complete log: Add _perror() logging function variants treewide: Replace perror() calls with calls to logging functions treewide: Replace strerror() calls arch.c | 10 ++++---- conf.c | 45 ++++++++++++++++++---------------- fwd.c | 2 +- isolation.c | 46 +++++++++++++++-------------------- log.c | 49 +++++++++++++++++++++++++------------ log.h | 25 ++++++++++++++++--- netlink.c | 4 +-- passt.1 | 3 ++- passt.c | 70 +++++++++++++++++++++++------------------------------ pasta.c | 41 +++++++++++++++---------------- pcap.c | 8 +++--- tap.c | 14 +++++------ tcp.c | 24 ++++++------------ util.c | 12 ++++----- 14 files changed, 179 insertions(+), 174 deletions(-) -- 2.43.0
If we don't run in foreground, we close standard error as we
daemonise, so it makes no sense to check if the controlling terminal
is an interactive terminal or if --force-stderr was given, to decide
if we want to log to standard error.
Make --force-stderr depend on --foreground.
Signed-off-by: Stefano Brivio
On Tue, Jun 18, 2024 at 09:14:22AM +0200, Stefano Brivio wrote:
If we don't run in foreground, we close standard error as we daemonise, so it makes no sense to check if the controlling terminal is an interactive terminal or if --force-stderr was given, to decide if we want to log to standard error.
Make --force-stderr depend on --foreground.
Signed-off-by: Stefano Brivio
--- conf.c | 3 +++ passt.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/conf.c b/conf.c index 94b3ed6..dbdbb62 100644 --- a/conf.c +++ b/conf.c @@ -1693,6 +1693,9 @@ void conf(struct ctx *c, int argc, char **argv)
conf_ugid(runas, &uid, &gid);
+ if (!c->foreground && c->force_stderr) + die("Can't log to standard error if not running in foreground"); + if (logfile) { logfile_init(c->mode == MODE_PASTA ? "pasta" : "passt", logfile, logsize); diff --git a/passt.c b/passt.c index a5e2c5a..aa9648a 100644 --- a/passt.c +++ b/passt.c @@ -302,7 +302,7 @@ int main(int argc, char **argv) if (isolate_prefork(&c)) die("Failed to sandbox process, exiting");
- if (!c.force_stderr && !isatty(fileno(stderr))) + if (!c.foreground || (!c.force_stderr && !isatty(fileno(stderr)))) __openlog(log_name, 0, LOG_DAEMON);
Hm.. kind of preexisting, but shouldn't we still skip the __openlog() if we have a logfile? Or make __openlog() open either the syslog or the logfile as appropriate (but in that case we should rename it not to look like openlog(3)).
if (!c.foreground)
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
On Wed, 19 Jun 2024 12:14:53 +1000
David Gibson
On Tue, Jun 18, 2024 at 09:14:22AM +0200, Stefano Brivio wrote:
If we don't run in foreground, we close standard error as we daemonise, so it makes no sense to check if the controlling terminal is an interactive terminal or if --force-stderr was given, to decide if we want to log to standard error.
Make --force-stderr depend on --foreground.
Signed-off-by: Stefano Brivio
--- conf.c | 3 +++ passt.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/conf.c b/conf.c index 94b3ed6..dbdbb62 100644 --- a/conf.c +++ b/conf.c @@ -1693,6 +1693,9 @@ void conf(struct ctx *c, int argc, char **argv)
conf_ugid(runas, &uid, &gid);
+ if (!c->foreground && c->force_stderr) + die("Can't log to standard error if not running in foreground"); + if (logfile) { logfile_init(c->mode == MODE_PASTA ? "pasta" : "passt", logfile, logsize); diff --git a/passt.c b/passt.c index a5e2c5a..aa9648a 100644 --- a/passt.c +++ b/passt.c @@ -302,7 +302,7 @@ int main(int argc, char **argv) if (isolate_prefork(&c)) die("Failed to sandbox process, exiting");
- if (!c.force_stderr && !isatty(fileno(stderr))) + if (!c.foreground || (!c.force_stderr && !isatty(fileno(stderr)))) __openlog(log_name, 0, LOG_DAEMON);
Hm.. kind of preexisting, but shouldn't we still skip the __openlog() if we have a logfile?
Ah, true. I would add this as a separate patch.
Or make __openlog() open either the syslog or the logfile as appropriate (but in that case we should rename it not to look like openlog(3)).
I would rather keep __openlog() as openlog() implementation, because the semantics are well specified like this. We just need another function, or even a direct setting, for LOG_PERROR (or get rid of that flag, internally?). -- Stefano
On Wed, Jun 19, 2024 at 10:34:48AM +0200, Stefano Brivio wrote:
On Wed, 19 Jun 2024 12:14:53 +1000 David Gibson
wrote: On Tue, Jun 18, 2024 at 09:14:22AM +0200, Stefano Brivio wrote:
If we don't run in foreground, we close standard error as we daemonise, so it makes no sense to check if the controlling terminal is an interactive terminal or if --force-stderr was given, to decide if we want to log to standard error.
Make --force-stderr depend on --foreground.
Signed-off-by: Stefano Brivio
--- conf.c | 3 +++ passt.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/conf.c b/conf.c index 94b3ed6..dbdbb62 100644 --- a/conf.c +++ b/conf.c @@ -1693,6 +1693,9 @@ void conf(struct ctx *c, int argc, char **argv)
conf_ugid(runas, &uid, &gid);
+ if (!c->foreground && c->force_stderr) + die("Can't log to standard error if not running in foreground"); + if (logfile) { logfile_init(c->mode == MODE_PASTA ? "pasta" : "passt", logfile, logsize); diff --git a/passt.c b/passt.c index a5e2c5a..aa9648a 100644 --- a/passt.c +++ b/passt.c @@ -302,7 +302,7 @@ int main(int argc, char **argv) if (isolate_prefork(&c)) die("Failed to sandbox process, exiting");
- if (!c.force_stderr && !isatty(fileno(stderr))) + if (!c.foreground || (!c.force_stderr && !isatty(fileno(stderr)))) __openlog(log_name, 0, LOG_DAEMON);
Hm.. kind of preexisting, but shouldn't we still skip the __openlog() if we have a logfile?
Ah, true. I would add this as a separate patch.
Or make __openlog() open either the syslog or the logfile as appropriate (but in that case we should rename it not to look like openlog(3)).
I would rather keep __openlog() as openlog() implementation, because the semantics are well specified like this.
I concur.
We just need another function, or even a direct setting, for LOG_PERROR (or get rid of that flag, internally?).
I feel like our needs for when we log to stderr are specific enough that it's simpler to just not use LOG_PERROR at all, and handle the printing to stderr ourselves. -- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
We currently use a LOG_EMERG log mask to represent the fact that we
don't know yet what the mask resulting from configuration should be,
before the command line is parsed.
However, we have the necessity of representing another phase as well,
that is, configuration is parsed but we didn't daemonise yet, or
we're not ready for operation yet. The next patch will add that
notion explicitly.
Mapping these cases to further log levels isn't really practical.
Introduce boolean log flags to represent them, instead of abusing
log priorities.
Signed-off-by: Stefano Brivio
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. Then, set LOG_PERROR only as we set this internal
flag, to make sure we don't duplicate messages.
Reported-by: Yalan Zhang
In many places, we have direct perror() calls, which completely bypass
logging functions and log files.
They are definitely convenient: offer similar convenience with
_perror() logging variants, so that we can drop those direct perror()
calls.
Signed-off-by: Stefano Brivio
On Tue, Jun 18, 2024 at 09:14:25AM +0200, Stefano Brivio wrote:
In many places, we have direct perror() calls, which completely bypass logging functions and log files.
They are definitely convenient: offer similar convenience with _perror() logging variants, so that we can drop those direct perror() calls.
Signed-off-by: Stefano Brivio
--- log.c | 21 +++++++++++++++++++++ log.h | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/log.c b/log.c index 5853496..9ddc58c 100644 --- a/log.c +++ b/log.c @@ -79,6 +79,11 @@ void vlogmsg(int pri, const char *format, va_list ap) } }
+/** + * logmsg() - vlogmsg() wrapper for variable argument lists + * @pri: Facility and level map, same as priority for vsyslog() + * @format: Message + */ void logmsg(int pri, const char *format, ...) { va_list ap; @@ -88,6 +93,22 @@ void logmsg(int pri, const char *format, ...) va_end(ap); }
+/** + * 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, ...) +{ + va_list ap; + + va_start(ap, format); + vlogmsg(pri, format, ap); + va_end(ap); + + logmsg(pri, ": %s", strerror(errno));
The vlogmsg() above could invoke syscalls which clobber errno, so you need to save it beforehand.
+} + /* Prefixes for log file messages, indexed by priority */ const char *logfile_prefix[] = { NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ diff --git a/log.h b/log.h index 1d6dd1d..bdeffde 100644 --- a/log.h +++ b/log.h @@ -16,11 +16,18 @@ void vlogmsg(int pri, const char *format, va_list ap); void logmsg(int pri, const char *format, ...) __attribute__((format(printf, 2, 3))); +void logmsg_perror(int pri, const char *format, ...) + __attribute__((format(printf, 2, 3))); + +#define err(...) logmsg( LOG_ERR, __VA_ARGS__) +#define warn(...) logmsg( LOG_WARNING, __VA_ARGS__) +#define info(...) logmsg( LOG_INFO, __VA_ARGS__) +#define debug(...) logmsg( LOG_DEBUG, __VA_ARGS__)
-#define err(...) logmsg(LOG_ERR, __VA_ARGS__) -#define warn(...) logmsg(LOG_WARNING, __VA_ARGS__) -#define info(...) logmsg(LOG_INFO, __VA_ARGS__) -#define debug(...) logmsg(LOG_DEBUG, __VA_ARGS__) +#define err_perror(...) logmsg_perror( LOG_ERR, __VA_ARGS__) +#define warn_perror(...) logmsg_perror( LOG_WARNING, __VA_ARGS__) +#define info_perror(...) logmsg_perror( LOG_INFO, __VA_ARGS__) +#define debug_perror(...) logmsg_perror( LOG_DEBUG, __VA_ARGS__)
#define die(...) \ do { \ @@ -28,6 +35,12 @@ void logmsg(int pri, const char *format, ...) exit(EXIT_FAILURE); \ } while (0)
+#define die_perror(...) \ + do { \ + err_perror(__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } while (0) + extern int log_trace; extern bool log_conf_parsed; extern bool log_daemon_ready;
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
On Wed, 19 Jun 2024 12:21:56 +1000
David Gibson
On Tue, Jun 18, 2024 at 09:14:25AM +0200, Stefano Brivio wrote:
In many places, we have direct perror() calls, which completely bypass logging functions and log files.
They are definitely convenient: offer similar convenience with _perror() logging variants, so that we can drop those direct perror() calls.
Signed-off-by: Stefano Brivio
--- log.c | 21 +++++++++++++++++++++ log.h | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/log.c b/log.c index 5853496..9ddc58c 100644 --- a/log.c +++ b/log.c @@ -79,6 +79,11 @@ void vlogmsg(int pri, const char *format, va_list ap) } }
+/** + * logmsg() - vlogmsg() wrapper for variable argument lists + * @pri: Facility and level map, same as priority for vsyslog() + * @format: Message + */ void logmsg(int pri, const char *format, ...) { va_list ap; @@ -88,6 +93,22 @@ void logmsg(int pri, const char *format, ...) va_end(ap); }
+/** + * 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, ...) +{ + va_list ap; + + va_start(ap, format); + vlogmsg(pri, format, ap); + va_end(ap); + + logmsg(pri, ": %s", strerror(errno));
The vlogmsg() above could invoke syscalls which clobber errno, so you need to save it beforehand.
Oops, nice catch. -- Stefano
perror() prints directly to standard error, but in many cases standard
error might be already closed, or we might want to skip logging, based
on configuration. Our logging functions provide all that.
While at it, make errors more descriptive, replacing some of the
existing basic perror-style messages.
Signed-off-by: Stefano Brivio
On Tue, Jun 18, 2024 at 09:14:26AM +0200, Stefano Brivio wrote:
perror() prints directly to standard error, but in many cases standard error might be already closed, or we might want to skip logging, based on configuration. Our logging functions provide all that.
While at it, make errors more descriptive, replacing some of the existing basic perror-style messages.
Signed-off-by: Stefano Brivio
As noted elsewhere, I'm not a huge fan of the _perror() helpers, but
regardless of that this is a big improvement to clarity.
Reviewed-by: David Gibson
--- arch.c | 10 +++++----- conf.c | 6 ++---- isolation.c | 18 ++++++++---------- log.c | 12 ++++-------- passt.c | 41 ++++++++++++++++------------------------- pasta.c | 9 +++------ 6 files changed, 38 insertions(+), 58 deletions(-)
diff --git a/arch.c b/arch.c index 80a41bc..04bebfc 100644 --- a/arch.c +++ b/arch.c @@ -18,6 +18,8 @@ #include
#include +#include "log.h" + /** * arch_avx2_exec() - Switch to AVX2 build if supported * @argv: Arguments from command line @@ -28,10 +30,8 @@ void arch_avx2_exec(char **argv) char exe[PATH_MAX] = { 0 }; const char *p;
- if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) { - perror("readlink /proc/self/exe"); - exit(EXIT_FAILURE); - } + if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) + die_perror("Failed to read own /proc/self/exe link");
p = strstr(exe, ".avx2"); if (p && strlen(p) == strlen(".avx2")) @@ -42,7 +42,7 @@ void arch_avx2_exec(char **argv)
snprintf(new_path, PATH_MAX + sizeof(".avx2"), "%s.avx2", exe); execve(new_path, argv, environ); - perror("Can't run AVX2 build, using non-AVX2 version"); + warn_perror("Can't run AVX2 build, using non-AVX2 version"); } } #else diff --git a/conf.c b/conf.c index 14feee1..344eb07 100644 --- a/conf.c +++ b/conf.c @@ -1098,10 +1098,8 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid) const struct passwd *pw; /* cppcheck-suppress getpwnamCalled */ pw = getpwnam("nobody"); - if (!pw) { - perror("getpwnam"); - exit(EXIT_FAILURE); - } + if (!pw) + die_perror("Can't get password file entry for nobody");
*uid = pw->pw_uid; *gid = pw->pw_gid; diff --git a/isolation.c b/isolation.c index ca2c68b..c936674 100644 --- a/isolation.c +++ b/isolation.c @@ -316,34 +316,34 @@ int isolate_prefork(const struct ctx *c) flags |= CLONE_NEWPID;
if (unshare(flags)) { - perror("unshare"); + err_perror("Failed to detach isolating namespaces"); return -errno; }
if (mount("", "/", "", MS_UNBINDABLE | MS_REC, NULL)) { - perror("mount /"); + err_perror("Failed to remount /"); return -errno; }
if (mount("", TMPDIR, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, "nr_inodes=2,nr_blocks=0")) { - perror("mount tmpfs"); + err_perror("Failed to mount empty tmpfs for pivot_root()"); return -errno; }
if (chdir(TMPDIR)) { - perror("chdir"); + err_perror("Failed to change directory into empty tmpfs"); return -errno; }
if (syscall(SYS_pivot_root, ".", ".")) { - perror("pivot_root"); + err_perror("Failed to pivot_root() into empty tmpfs"); return -errno; }
if (umount2(".", MNT_DETACH | UMOUNT_NOFOLLOW)) { - perror("umount2"); + err_perror("Failed to unmount original root filesystem"); return -errno; }
@@ -388,8 +388,6 @@ void isolate_postfork(const struct ctx *c) }
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || - prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { - perror("prctl"); - exit(EXIT_FAILURE); - } + prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) + die_perror("Failed to apply seccomp filter"); } diff --git a/log.c b/log.c index 9ddc58c..ec833c4 100644 --- a/log.c +++ b/log.c @@ -208,10 +208,8 @@ 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); - } + if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) + die_perror("Failed to read own /proc/self/exe link");
log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR); @@ -224,10 +222,8 @@ void logfile_init(const char *name, const char *path, size_t size) 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); - } + write(log_file, &nl, 1) <= 0) + die_perror("Couldn't write to log file");
/* 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); diff --git a/passt.c b/passt.c index 7436120..542d3fb 100644 --- a/passt.c +++ b/passt.c @@ -136,14 +136,13 @@ static void secret_init(struct ctx *c) } if (dev_random >= 0) close(dev_random); - if (random_read < sizeof(c->hash_secret)) { + + if (random_read < sizeof(c->hash_secret)) #else if (getrandom(&c->hash_secret, sizeof(c->hash_secret), - GRND_RANDOM) < 0) { + GRND_RANDOM) < 0) #endif /* !HAS_GETRANDOM */ - perror("TCP initial sequence getrandom"); - exit(EXIT_FAILURE); - } + die_perror("Failed to get random bytes for hash table and TCP"); }
/** @@ -250,20 +249,16 @@ int main(int argc, char **argv) madvise(pkt_buf, TAP_BUF_BYTES, MADV_HUGEPAGE);
c.epollfd = epoll_create1(EPOLL_CLOEXEC); - if (c.epollfd == -1) { - perror("epoll_create1"); - exit(EXIT_FAILURE); - } + if (c.epollfd == -1) + die_perror("Failed to create epoll file descriptor"); + + if (getrlimit(RLIMIT_NOFILE, &limit)) + die_perror("Failed to get maximum value of open files limit");
- if (getrlimit(RLIMIT_NOFILE, &limit)) { - perror("getrlimit"); - exit(EXIT_FAILURE); - } c.nofile = limit.rlim_cur = limit.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &limit)) { - perror("setrlimit"); - exit(EXIT_FAILURE); - } + if (setrlimit(RLIMIT_NOFILE, &limit)) + die_perror("Failed to set current limit for open files"); + sock_probe_mem(&c);
conf(&c, argc, argv); @@ -293,10 +288,8 @@ int main(int argc, char **argv) pcap_init(&c);
if (!c.foreground) { - if ((devnull_fd = open("/dev/null", O_RDWR | O_CLOEXEC)) < 0) { - perror("/dev/null open"); - exit(EXIT_FAILURE); - } + if ((devnull_fd = open("/dev/null", O_RDWR | O_CLOEXEC)) < 0) + die_perror("Failed to open /dev/null"); }
if (isolate_prefork(&c)) @@ -324,10 +317,8 @@ loop: /* NOLINTNEXTLINE(bugprone-branch-clone): intervals can be the same */ /* cppcheck-suppress [duplicateValueTernary, unmatchedSuppression] */ nfds = epoll_wait(c.epollfd, events, EPOLL_EVENTS, TIMER_INTERVAL); - if (nfds == -1 && errno != EINTR) { - perror("epoll_wait"); - exit(EXIT_FAILURE); - } + if (nfds == -1 && errno != EINTR) + die_perror("epoll_wait() failed in main loop");
clock_gettime(CLOCK_MONOTONIC, &now);
diff --git a/pasta.c b/pasta.c index b85ea2b..d08391f 100644 --- a/pasta.c +++ b/pasta.c @@ -197,8 +197,7 @@ static int pasta_spawn_cmd(void *arg) a = (const struct pasta_spawn_cmd_arg *)arg; execvp(a->exe, a->argv);
- perror("execvp"); - exit(EXIT_FAILURE); + die_perror("Failed to start command or shell"); }
/** @@ -261,10 +260,8 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid, CLONE_NEWUTS | CLONE_NEWNS | SIGCHLD, (void *)&arg);
- if (pasta_child_pid == -1) { - perror("clone"); - exit(EXIT_FAILURE); - } + if (pasta_child_pid == -1) + die_perror("Failed to clone process with detached namespaces");
NS_CALL(pasta_wait_for_ns, c); if (c->pasta_netns_fd < 0)
-- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Now that we have logging functions embedding perror() functionality,
we can make _some_ calls more terse by using them. In many places,
the strerror() calls are still more convenient because, for example,
they are used in flow debugging functions, or because the return code
variable of interest is not 'errno'.
While at it, convert a few error messages from a scant perror style
to proper failure descriptions.
Signed-off-by: Stefano Brivio
On Tue, Jun 18, 2024 at 09:14:27AM +0200, Stefano Brivio wrote:
Now that we have logging functions embedding perror() functionality, we can make _some_ calls more terse by using them. In many places, the strerror() calls are still more convenient because, for example, they are used in flow debugging functions, or because the return code variable of interest is not 'errno'.
While at it, convert a few error messages from a scant perror style to proper failure descriptions.
Signed-off-by: Stefano Brivio
--- conf.c | 31 +++++++++++++++++-------------- fwd.c | 2 +- isolation.c | 28 +++++++++++----------------- log.c | 2 +- netlink.c | 4 ++-- passt.c | 12 ++++-------- pasta.c | 32 ++++++++++++++++---------------- pcap.c | 8 +++----- tap.c | 14 +++++++------- tcp.c | 24 ++++++++---------------- util.c | 12 +++++------- 11 files changed, 75 insertions(+), 94 deletions(-) diff --git a/conf.c b/conf.c index 344eb07..2a6f05c 100644 --- a/conf.c +++ b/conf.c @@ -461,7 +461,7 @@ static void get_dns(struct ctx *c) }
if (line_len < 0) - warn("Error reading /etc/resolv.conf: %s", strerror(errno)); + warn_perror("Error reading /etc/resolv.conf"); close(fd);
out: @@ -592,8 +592,8 @@ static unsigned int conf_ip4(unsigned int ifi, if (IN4_IS_ADDR_UNSPECIFIED(&ip4->gw)) { int rc = nl_route_get_def(nl_sock, ifi, AF_INET, &ip4->gw); if (rc < 0) { - err("Couldn't discover IPv4 gateway address: %s", - strerror(-rc)); + errno = -rc;
I don't love this. Taking a re-entrant bit of code and making it non-reentrant by bouncing information through a global. I mean, it works in this case, but still.. -- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
On Wed, 19 Jun 2024 12:29:06 +1000
David Gibson
On Tue, Jun 18, 2024 at 09:14:27AM +0200, Stefano Brivio wrote:
Now that we have logging functions embedding perror() functionality, we can make _some_ calls more terse by using them. In many places, the strerror() calls are still more convenient because, for example, they are used in flow debugging functions, or because the return code variable of interest is not 'errno'.
While at it, convert a few error messages from a scant perror style to proper failure descriptions.
Signed-off-by: Stefano Brivio
--- conf.c | 31 +++++++++++++++++-------------- fwd.c | 2 +- isolation.c | 28 +++++++++++----------------- log.c | 2 +- netlink.c | 4 ++-- passt.c | 12 ++++-------- pasta.c | 32 ++++++++++++++++---------------- pcap.c | 8 +++----- tap.c | 14 +++++++------- tcp.c | 24 ++++++++---------------- util.c | 12 +++++------- 11 files changed, 75 insertions(+), 94 deletions(-) diff --git a/conf.c b/conf.c index 344eb07..2a6f05c 100644 --- a/conf.c +++ b/conf.c @@ -461,7 +461,7 @@ static void get_dns(struct ctx *c) }
if (line_len < 0) - warn("Error reading /etc/resolv.conf: %s", strerror(errno)); + warn_perror("Error reading /etc/resolv.conf"); close(fd);
out: @@ -592,8 +592,8 @@ static unsigned int conf_ip4(unsigned int ifi, if (IN4_IS_ADDR_UNSPECIFIED(&ip4->gw)) { int rc = nl_route_get_def(nl_sock, ifi, AF_INET, &ip4->gw); if (rc < 0) { - err("Couldn't discover IPv4 gateway address: %s", - strerror(-rc)); + errno = -rc;
I don't love this. Taking a re-entrant bit of code and making it non-reentrant by bouncing information through a global. I mean, it works in this case, but still..
Hmm, right, I'll drop this type of change. -- Stefano
participants (2)
-
David Gibson
-
Stefano Brivio