From 9c8f476cb6d2944dddeae873de92562a73e6faa2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Sat, 11 Oct 2025 17:36:54 -0700 Subject: [PATCH] libct/nsenter: save errno in sane_kill Since sane_kill after a failed read or write, but before reporting the error from that read or write, it may change the errno value in case kill(2) fails. Save and restore the errno around the call to kill. While at it, - change the code to return early; - don't return kill return value as no one is using it, and the errno value no longer correlates. Signed-off-by: Kir Kolyshkin --- libcontainer/nsenter/nsexec.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c index 728e68bb0..ad5f42ffe 100644 --- a/libcontainer/nsenter/nsexec.c +++ b/libcontainer/nsenter/nsexec.c @@ -637,12 +637,14 @@ void join_namespaces(char *nsspec) __close_namespaces(to_join, joined, ns_list, ns_len); } -static inline int sane_kill(pid_t pid, int signum) +static inline void sane_kill(pid_t pid, int signum) { - if (pid > 0) - return kill(pid, signum); - else - return 0; + if (pid <= 0) + return; + + int saved_errno = errno; + kill(pid, signum); + errno = saved_errno; } void try_unshare(int flags, const char *msg)