From: Karel Zak Date: Thu, 7 Nov 2024 11:26:25 +0000 (+0100) Subject: nsenter: support empty environ[] X-Git-Tag: v2.40.3~62 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ee8235a65edd01a4899f2557dc9aa2cb9c6de7bc;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. Backport from master (v2.41) branch. 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 24ae90d00..f35006f3a 100644 --- a/lib/env.c +++ b/lib/env.c @@ -93,6 +93,7 @@ struct ul_env_list *env_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 8f7bac922..58130e932 100644 --- a/sys-utils/nsenter.c +++ b/sys-utils/nsenter.c @@ -365,7 +365,6 @@ int main(int argc, char *argv[]) uid_t uid = 0; gid_t gid = 0; int keepcaps = 0; - struct ul_env_list *envls; #ifdef HAVE_LIBSELINUX bool selinux = 0; #endif @@ -644,12 +643,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_from_fd(env_fd)) == NULL) + struct ul_env_list *ls; + + ls = env_from_fd(env_fd); + if (!ls && errno) err(EXIT_FAILURE, _("failed to get environment variables")); clearenv(); - if (env_list_setenv(envls) < 0) + if (ls && env_list_setenv(ls) < 0) err(EXIT_FAILURE, _("failed to set environment variables")); - env_list_free(envls); + env_list_free(ls); close(env_fd); }