]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
nsenter: support empty environ[]
authorKarel Zak <kzak@redhat.com>
Thu, 7 Nov 2024 11:26:25 +0000 (12:26 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 7 Nov 2024 11:26:25 +0000 (12:26 +0100)
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 <kzak@redhat.com>
lib/env.c
sys-utils/nsenter.c

index dfea1e5fcc697b306c08e0aa3b7ec58623830ed6..0874fe482b83b0c020d28f6e4901b35c1f1ed5ca 100644 (file)
--- 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';
index 27962a9ecdffbda481129c75d3aad1445dcdf71e..d6717158b74f077ac678d6be1b879046cfd403d2 100644 (file)
@@ -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);
        }