Blame SOURCES/0240-lib-Add-get_env_variable-function.patch

28bab8
From 4e4d79150fc71cd89a173bb137570db82df60de8 Mon Sep 17 00:00:00 2001
28bab8
From: Martin Kutlak <mkutlak@redhat.com>
28bab8
Date: Tue, 21 May 2019 10:38:21 +0200
28bab8
Subject: [PATCH] lib: Add get_env_variable function
28bab8
28bab8
Cherry-picked from https://github.com/abrt/libreport/commit/eb844f53ed3a7943b7e3f88d76daa94e2892f10a
28bab8
28bab8
Signed-off-by: Martin Kutlak <mkutlak@redhat.com>
28bab8
---
28bab8
 src/include/internal_libreport.h |  2 ++
28bab8
 src/lib/get_cmdline.c            | 53 ++++++++++++++++++++++++++++++++
28bab8
 2 files changed, 55 insertions(+)
28bab8
28bab8
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
28bab8
index 23cdfa0..d7bab4e 100644
28bab8
--- a/src/include/internal_libreport.h
28bab8
+++ b/src/include/internal_libreport.h
28bab8
@@ -638,6 +638,8 @@ struct strbuf *strbuf_prepend_strfv(struct strbuf *strbuf,
28bab8
 char* get_cmdline(pid_t pid);
28bab8
 #define get_environ libreport_get_environ
28bab8
 char* get_environ(pid_t pid);
28bab8
+#define get_env_variable libreport_get_env_variable
28bab8
+int get_env_variable(pid_t pid, const char *name, char **value);
28bab8
 
28bab8
 /* Takes ptr to time_t, or NULL if you want to use current time.
28bab8
  * Returns "YYYY-MM-DD-hh:mm:ss" string.
28bab8
diff --git a/src/lib/get_cmdline.c b/src/lib/get_cmdline.c
28bab8
index 55c49ea..0fc0cbf 100644
28bab8
--- a/src/lib/get_cmdline.c
28bab8
+++ b/src/lib/get_cmdline.c
28bab8
@@ -148,3 +148,56 @@ char* get_environ(pid_t pid)
28bab8
     snprintf(path, sizeof(path), "/proc/%lu/environ", (long)pid);
28bab8
     return get_escaped(path, '\n');
28bab8
 }
28bab8
+
28bab8
+int get_env_variable(pid_t pid, const char *name, char **value)
28bab8
+{
28bab8
+    char path[sizeof("/proc/%lu/environ") + sizeof(long)*3];
28bab8
+    snprintf(path, sizeof(path), "/proc/%lu/environ", (long)pid);
28bab8
+
28bab8
+    FILE *fenv = fopen(path, "re");
28bab8
+    if (fenv == NULL)
28bab8
+    {
28bab8
+        pwarn_msg("Failed to open environ file");
28bab8
+        return -errno;
28bab8
+    }
28bab8
+
28bab8
+    size_t len = strlen(name);
28bab8
+    int c = 0;
28bab8
+    while (c != EOF)
28bab8
+    {
28bab8
+        long i = 0;
28bab8
+        /* Check variable name */
28bab8
+        while ((c = fgetc(fenv)) != EOF && (i < len && c == name[i++]))
28bab8
+            ;
28bab8
+
28bab8
+        if (c == EOF)
28bab8
+            break;
28bab8
+
28bab8
+        const int skip = (c != '=' || name[i] != '\0');
28bab8
+        i = 0;
28bab8
+
28bab8
+        /* Read to the end of variable entry */
28bab8
+        while ((c = fgetc(fenv)) != EOF && c !='\0')
28bab8
+            ++i;
28bab8
+
28bab8
+        /* Go to the next entry if the read entry isn't the searched variable */
28bab8
+        if (skip)
28bab8
+            continue;
28bab8
+
28bab8
+        *value = xmalloc(i+1);
28bab8
+
28bab8
+        /* i+1 because we didn't count '\0'  */
28bab8
+        if (fseek(fenv, -(i+1), SEEK_CUR) < 0)
28bab8
+            error_msg_and_die("Failed to seek");
28bab8
+
28bab8
+        if (fread(*value, 1, i, fenv) != i)
28bab8
+            error_msg_and_die("Failed to read value");
28bab8
+
28bab8
+        (*value)[i] = '\0';
28bab8
+
28bab8
+        break;
28bab8
+    }
28bab8
+
28bab8
+    fclose(fenv);
28bab8
+    return 0;
28bab8
+}
28bab8
-- 
28bab8
2.21.0
28bab8