From: Karel Zak Date: Thu, 7 Nov 2024 11:26:25 +0000 (+0100) Subject: nsenter: support empty environ[] X-Git-Tag: v2.42-start~153^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3b6e55d6c1eed0e88e319535fd18421b1a35ed3f;p=thirdparty%2Futil-linux.git nsenter: support empty environ[] There is no error when the /proc/#/environ file does not contain any NAME=value items. Fixes: https://github.com/util-linux/util-linux/issues/3270 Signed-off-by: Karel Zak --- diff --git a/lib/env.c b/lib/env.c index dfea1e5fc..0874fe482 100644 --- a/lib/env.c +++ b/lib/env.c @@ -172,6 +172,7 @@ struct ul_env_list *env_list_from_fd(int fd) ssize_t rc = 0; struct ul_env_list *ls = NULL; + errno = 0; if ((rc = read_all_alloc(fd, &buf)) < 1) return NULL; buf[rc] = '\0'; diff --git a/sys-utils/nsenter.c b/sys-utils/nsenter.c index 27962a9ec..d6717158b 100644 --- a/sys-utils/nsenter.c +++ b/sys-utils/nsenter.c @@ -531,7 +531,6 @@ int main(int argc, char *argv[]) int keepcaps = 0; int sock_fd = -1; int pid_fd = -1; - struct ul_env_list *envls; #ifdef HAVE_LIBSELINUX bool selinux = 0; #endif @@ -786,12 +785,15 @@ int main(int argc, char *argv[]) /* Pass environment variables of the target process to the spawned process */ if (env_fd >= 0) { - if ((envls = env_list_from_fd(env_fd)) == NULL) + struct ul_env_list *ls; + + ls = env_list_from_fd(env_fd); + if (!ls && errno) err(EXIT_FAILURE, _("failed to get environment variables")); clearenv(); - if (env_list_setenv(envls, 0) < 0) + if (ls && env_list_setenv(ls, 0) < 0) err(EXIT_FAILURE, _("failed to set environment variables")); - env_list_free(envls); + env_list_free(ls); close(env_fd); }