treewide: Replace perror() calls with calls to logging functions

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 <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Stefano Brivio 2024-06-15 00:37:11 +02:00
parent c1140df889
commit 92a22fef93
6 changed files with 38 additions and 58 deletions

10
arch.c
View file

@ -18,6 +18,8 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "log.h"
/** /**
* arch_avx2_exec() - Switch to AVX2 build if supported * arch_avx2_exec() - Switch to AVX2 build if supported
* @argv: Arguments from command line * @argv: Arguments from command line
@ -28,10 +30,8 @@ void arch_avx2_exec(char **argv)
char exe[PATH_MAX] = { 0 }; char exe[PATH_MAX] = { 0 };
const char *p; const char *p;
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) { if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0)
perror("readlink /proc/self/exe"); die_perror("Failed to read own /proc/self/exe link");
exit(EXIT_FAILURE);
}
p = strstr(exe, ".avx2"); p = strstr(exe, ".avx2");
if (p && strlen(p) == strlen(".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); snprintf(new_path, PATH_MAX + sizeof(".avx2"), "%s.avx2", exe);
execve(new_path, argv, environ); 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 #else

6
conf.c
View file

@ -1093,10 +1093,8 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid)
const struct passwd *pw; const struct passwd *pw;
/* cppcheck-suppress getpwnamCalled */ /* cppcheck-suppress getpwnamCalled */
pw = getpwnam("nobody"); pw = getpwnam("nobody");
if (!pw) { if (!pw)
perror("getpwnam"); die_perror("Can't get password file entry for nobody");
exit(EXIT_FAILURE);
}
*uid = pw->pw_uid; *uid = pw->pw_uid;
*gid = pw->pw_gid; *gid = pw->pw_gid;

View file

@ -316,34 +316,34 @@ int isolate_prefork(const struct ctx *c)
flags |= CLONE_NEWPID; flags |= CLONE_NEWPID;
if (unshare(flags)) { if (unshare(flags)) {
perror("unshare"); err_perror("Failed to detach isolating namespaces");
return -errno; return -errno;
} }
if (mount("", "/", "", MS_UNBINDABLE | MS_REC, NULL)) { if (mount("", "/", "", MS_UNBINDABLE | MS_REC, NULL)) {
perror("mount /"); err_perror("Failed to remount /");
return -errno; return -errno;
} }
if (mount("", TMPDIR, "tmpfs", if (mount("", TMPDIR, "tmpfs",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY, MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RDONLY,
"nr_inodes=2,nr_blocks=0")) { "nr_inodes=2,nr_blocks=0")) {
perror("mount tmpfs"); err_perror("Failed to mount empty tmpfs for pivot_root()");
return -errno; return -errno;
} }
if (chdir(TMPDIR)) { if (chdir(TMPDIR)) {
perror("chdir"); err_perror("Failed to change directory into empty tmpfs");
return -errno; return -errno;
} }
if (syscall(SYS_pivot_root, ".", ".")) { if (syscall(SYS_pivot_root, ".", ".")) {
perror("pivot_root"); err_perror("Failed to pivot_root() into empty tmpfs");
return -errno; return -errno;
} }
if (umount2(".", MNT_DETACH | UMOUNT_NOFOLLOW)) { if (umount2(".", MNT_DETACH | UMOUNT_NOFOLLOW)) {
perror("umount2"); err_perror("Failed to unmount original root filesystem");
return -errno; return -errno;
} }
@ -388,8 +388,6 @@ void isolate_postfork(const struct ctx *c)
} }
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) || if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
perror("prctl"); die_perror("Failed to apply seccomp filter");
exit(EXIT_FAILURE);
}
} }

12
log.c
View file

@ -206,10 +206,8 @@ void logfile_init(const char *name, const char *path, size_t size)
char nl = '\n', exe[PATH_MAX] = { 0 }; char nl = '\n', exe[PATH_MAX] = { 0 };
int n; int n;
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) { if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0)
perror("readlink /proc/self/exe"); die_perror("Failed to read own /proc/self/exe link");
exit(EXIT_FAILURE);
}
log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC, log_file = open(path, O_CREAT | O_TRUNC | O_APPEND | O_RDWR | O_CLOEXEC,
S_IRUSR | S_IWUSR); S_IRUSR | S_IWUSR);
@ -222,10 +220,8 @@ void logfile_init(const char *name, const char *path, size_t size)
name, exe, getpid()); name, exe, getpid());
if (write(log_file, log_header, n) <= 0 || if (write(log_file, log_header, n) <= 0 ||
write(log_file, &nl, 1) <= 0) { write(log_file, &nl, 1) <= 0)
perror("Couldn't write to log file\n"); die_perror("Couldn't write to log file");
exit(EXIT_FAILURE);
}
/* For FALLOC_FL_COLLAPSE_RANGE: VFS block size can be up to one page */ /* 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); log_cut_size = ROUND_UP(log_size * LOGFILE_CUT_RATIO / 100, PAGE_SIZE);

41
passt.c
View file

@ -136,14 +136,13 @@ static void secret_init(struct ctx *c)
} }
if (dev_random >= 0) if (dev_random >= 0)
close(dev_random); close(dev_random);
if (random_read < sizeof(c->hash_secret)) {
if (random_read < sizeof(c->hash_secret))
#else #else
if (getrandom(&c->hash_secret, sizeof(c->hash_secret), if (getrandom(&c->hash_secret, sizeof(c->hash_secret),
GRND_RANDOM) < 0) { GRND_RANDOM) < 0)
#endif /* !HAS_GETRANDOM */ #endif /* !HAS_GETRANDOM */
perror("TCP initial sequence getrandom"); die_perror("Failed to get random bytes for hash table and TCP");
exit(EXIT_FAILURE);
}
} }
/** /**
@ -250,20 +249,16 @@ int main(int argc, char **argv)
madvise(pkt_buf, TAP_BUF_BYTES, MADV_HUGEPAGE); madvise(pkt_buf, TAP_BUF_BYTES, MADV_HUGEPAGE);
c.epollfd = epoll_create1(EPOLL_CLOEXEC); c.epollfd = epoll_create1(EPOLL_CLOEXEC);
if (c.epollfd == -1) { if (c.epollfd == -1)
perror("epoll_create1"); die_perror("Failed to create epoll file descriptor");
exit(EXIT_FAILURE);
} 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; c.nofile = limit.rlim_cur = limit.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &limit)) { if (setrlimit(RLIMIT_NOFILE, &limit))
perror("setrlimit"); die_perror("Failed to set current limit for open files");
exit(EXIT_FAILURE);
}
sock_probe_mem(&c); sock_probe_mem(&c);
conf(&c, argc, argv); conf(&c, argc, argv);
@ -293,10 +288,8 @@ int main(int argc, char **argv)
pcap_init(&c); pcap_init(&c);
if (!c.foreground) { if (!c.foreground) {
if ((devnull_fd = open("/dev/null", O_RDWR | O_CLOEXEC)) < 0) { if ((devnull_fd = open("/dev/null", O_RDWR | O_CLOEXEC)) < 0)
perror("/dev/null open"); die_perror("Failed to open /dev/null");
exit(EXIT_FAILURE);
}
} }
if (isolate_prefork(&c)) if (isolate_prefork(&c))
@ -320,10 +313,8 @@ loop:
/* NOLINTNEXTLINE(bugprone-branch-clone): intervals can be the same */ /* NOLINTNEXTLINE(bugprone-branch-clone): intervals can be the same */
/* cppcheck-suppress [duplicateValueTernary, unmatchedSuppression] */ /* cppcheck-suppress [duplicateValueTernary, unmatchedSuppression] */
nfds = epoll_wait(c.epollfd, events, EPOLL_EVENTS, TIMER_INTERVAL); nfds = epoll_wait(c.epollfd, events, EPOLL_EVENTS, TIMER_INTERVAL);
if (nfds == -1 && errno != EINTR) { if (nfds == -1 && errno != EINTR)
perror("epoll_wait"); die_perror("epoll_wait() failed in main loop");
exit(EXIT_FAILURE);
}
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);

View file

@ -197,8 +197,7 @@ static int pasta_spawn_cmd(void *arg)
a = (const struct pasta_spawn_cmd_arg *)arg; a = (const struct pasta_spawn_cmd_arg *)arg;
execvp(a->exe, a->argv); execvp(a->exe, a->argv);
perror("execvp"); die_perror("Failed to start command or shell");
exit(EXIT_FAILURE);
} }
/** /**
@ -261,10 +260,8 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid,
CLONE_NEWUTS | CLONE_NEWNS | SIGCHLD, CLONE_NEWUTS | CLONE_NEWNS | SIGCHLD,
(void *)&arg); (void *)&arg);
if (pasta_child_pid == -1) { if (pasta_child_pid == -1)
perror("clone"); die_perror("Failed to clone process with detached namespaces");
exit(EXIT_FAILURE);
}
NS_CALL(pasta_wait_for_ns, c); NS_CALL(pasta_wait_for_ns, c);
if (c->pasta_netns_fd < 0) if (c->pasta_netns_fd < 0)