]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - lib/env.c
Merge branch 'pager/less-quirk' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / lib / env.c
index ac481eea7e4f1bfcb4f47666f736c29c23f8c6e6..2bdfe5697e900fdf0273ee83125ff60e2cb39918 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -19,6 +19,7 @@
 #include <sys/types.h>
 
 #include "env.h"
+#include "all-io.h"
 
 #ifndef HAVE_ENVIRON_DECL
 extern char **environ;
@@ -69,6 +70,8 @@ static struct ul_env_list *env_list_add(struct ul_env_list *ls0, const char *str
 
        sz = strlen(str) + 1;
        p = malloc(sizeof(struct ul_env_list) + sz);
+       if (!p)
+               return ls0;
 
        ls = (struct ul_env_list *) p;
        p += sizeof(struct ul_env_list);
@@ -79,6 +82,32 @@ static struct ul_env_list *env_list_add(struct ul_env_list *ls0, const char *str
        return ls;
 }
 
+/*
+ * Use env_from_fd() to read environment from @fd.
+ *
+ * @fd must be /proc/<pid>/environ file.
+*/
+struct ul_env_list *env_from_fd(int fd)
+{
+       char *buf = NULL, *p;
+       ssize_t rc = 0;
+       struct ul_env_list *ls = NULL;
+
+       if ((rc = read_all_alloc(fd, &buf)) < 1)
+               return NULL;
+       buf[rc] = '\0';
+       p = buf;
+
+       while (rc > 0) {
+               ls = env_list_add(ls, p);
+               p += strlen(p) + 1;
+               rc -= strlen(p) + 1;
+       }
+
+       free(buf);
+       return ls;
+}
+
 /*
  * Use setenv() for all stuff in @ls.
  *
@@ -112,7 +141,7 @@ void env_list_free(struct ul_env_list *ls)
 }
 
 /*
- * Removes unwanted variables from environ[]. If @ls is not NULL than stores
+ * Removes unwanted variables from environ[]. If @org is not NULL than stores
  * unwnated variables to the list.
  */
 void __sanitize_env(struct ul_env_list **org)
@@ -130,7 +159,7 @@ void __sanitize_env(struct ul_env_list **org)
                         if (strncmp(*cur, *bad, strlen(*bad)) == 0) {
                                if (org)
                                        *org = env_list_add(*org, *cur);
-                                last = remote_entry(envp, cur - envp, last);
+                                last = remove_entry(envp, cur - envp, last);
                                 cur--;
                                 break;
                         }
@@ -145,7 +174,7 @@ void __sanitize_env(struct ul_env_list **org)
                                 continue;  /* OK */
                        if (org)
                                *org = env_list_add(*org, *cur);
-                        last = remote_entry(envp, cur - envp, last);
+                        last = remove_entry(envp, cur - envp, last);
                         cur--;
                         break;
                 }
@@ -159,9 +188,7 @@ void sanitize_env(void)
 
 char *safe_getenv(const char *arg)
 {
-       uid_t ruid = getuid();
-
-       if (ruid != 0 || (ruid != geteuid()) || (getgid() != getegid()))
+       if ((getuid() != geteuid()) || (getgid() != getegid()))
                return NULL;
 #ifdef HAVE_PRCTL
        if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)