Move getenv to libcommon
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 6 Apr 2021 20:32:14 +0000 (16:32 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 13 Apr 2021 18:49:13 +0000 (14:49 -0400)
Move it to libcommon since it can be used by any library.

Change-Id: Ic09b643e3a91b2844fe9dc13eac55fe90f8e15c2
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/common/Makefile.am
src/common/getenv.c [new file with mode: 0644]
src/common/getenv.h [new file with mode: 0644]
src/lib/lttng-ust-ctl/ustctl.c
src/lib/lttng-ust/Makefile.am
src/lib/lttng-ust/getcpu.c
src/lib/lttng-ust/getenv.c [deleted file]
src/lib/lttng-ust/getenv.h [deleted file]
src/lib/lttng-ust/lttng-clock.c
src/lib/lttng-ust/lttng-ust-comm.c
src/lib/lttng-ust/lttng-ust-statedump.c

index 2ab4bfdc6271e63c0fdf37050b87df8f16e4a4af..e75ee2509e44c0c657bcc373016a18f61849051b 100644 (file)
@@ -129,6 +129,8 @@ libsnprintf_la_SOURCES = \
 
 # Common library
 libcommon_la_SOURCES = \
+       getenv.c \
+       getenv.h \
        logging.c \
        logging.h \
        patient.c
diff --git a/src/common/getenv.c b/src/common/getenv.c
new file mode 100644 (file)
index 0000000..1909f4c
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <sys/types.h>
+#include <urcu/system.h>
+#include "common/logging.h"
+#include "common/macros.h"
+#include "common/getenv.h"
+
+enum lttng_env_secure {
+       LTTNG_ENV_SECURE,
+       LTTNG_ENV_NOT_SECURE,
+};
+
+struct lttng_env {
+       const char *key;
+       enum lttng_env_secure secure;
+       char *value;
+};
+
+static
+int lttng_ust_getenv_is_init = 0;
+
+static struct lttng_env lttng_env[] = {
+       /*
+        * LTTNG_UST_DEBUG is used directly by snprintf, because it
+        * needs to be already set for ERR() used in
+        * lttng_ust_getenv_init().
+        */
+       { "LTTNG_UST_DEBUG", LTTNG_ENV_NOT_SECURE, NULL, },
+
+       /* Env. var. which can be used in setuid/setgid executables. */
+       { "LTTNG_UST_WITHOUT_BADDR_STATEDUMP", LTTNG_ENV_NOT_SECURE, NULL, },
+       { "LTTNG_UST_REGISTER_TIMEOUT", LTTNG_ENV_NOT_SECURE, NULL, },
+
+       /* Env. var. which are not fetched in setuid/setgid executables. */
+       { "LTTNG_UST_CLOCK_PLUGIN", LTTNG_ENV_SECURE, NULL, },
+       { "LTTNG_UST_GETCPU_PLUGIN", LTTNG_ENV_SECURE, NULL, },
+       { "LTTNG_UST_ALLOW_BLOCKING", LTTNG_ENV_SECURE, NULL, },
+       { "HOME", LTTNG_ENV_SECURE, NULL, },
+       { "LTTNG_HOME", LTTNG_ENV_SECURE, NULL, },
+};
+
+static
+int lttng_is_setuid_setgid(void)
+{
+       return geteuid() != getuid() || getegid() != getgid();
+}
+
+/*
+ * Wrapper over getenv that will only return the values of whitelisted
+ * environment variables when the current process is setuid and/or setgid.
+ */
+char *lttng_ust_getenv(const char *name)
+{
+       size_t i;
+       struct lttng_env *e;
+       bool found = false;
+
+       if (!CMM_LOAD_SHARED(lttng_ust_getenv_is_init))
+               abort();
+
+       for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
+               e = &lttng_env[i];
+
+               if (strcmp(e->key, name) == 0) {
+                       found = true;
+                       break;
+               }
+       }
+       if (!found) {
+               return NULL;
+       }
+       return e->value;
+}
+
+void lttng_ust_getenv_init(void)
+{
+       size_t i;
+
+       if (CMM_LOAD_SHARED(lttng_ust_getenv_is_init))
+               return;
+
+       for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
+               struct lttng_env *e = &lttng_env[i];
+
+               if (e->secure == LTTNG_ENV_SECURE && lttng_is_setuid_setgid()) {
+                       ERR("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.",
+                               e->key);
+                       continue;
+               }
+               e->value = getenv(e->key);
+       }
+       CMM_STORE_SHARED(lttng_ust_getenv_is_init, 1);
+}
diff --git a/src/common/getenv.h b/src/common/getenv.h
new file mode 100644 (file)
index 0000000..9bfb9f0
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#ifndef _UST_COMMON_GETENV_H
+#define _UST_COMMON_GETENV_H
+
+/*
+ * Always add the lttng-ust environment variables using the lttng_ust_getenv()
+ * infrastructure rather than using getenv() directly.  This ensures that we
+ * don't trigger races between getenv() invoked by lttng-ust listener threads
+ * invoked concurrently with setenv() called by an otherwise single-threaded
+ * application thread. (the application is not aware that it runs with
+ * lttng-ust)
+ */
+
+char *lttng_ust_getenv(const char *name)
+       __attribute__((visibility("hidden")));
+
+
+/*
+ * Initialize the internal filtered list of environment variables.
+ */
+void lttng_ust_getenv_init(void)
+       __attribute__((visibility("hidden")));
+
+#endif /* _UST_COMMON_GETENV_H */
index 3561a722db657580483a301beab6e7ff9c6dd919..96107fe743bb31dba8cb2ec499347e7ed427b61d 100644 (file)
@@ -28,7 +28,7 @@
 #include "common/wait.h"
 #include "lib/lttng-ust/lttng-rb-clients.h"
 #include "common/clock.h"
-#include "lib/lttng-ust/getenv.h"
+#include "common/getenv.h"
 #include "lib/lttng-ust/lttng-tracer-core.h"
 #include "lib/lttng-ust/lttng-counter-client.h"
 
index 3548a9e624299ddcc4c94421eff20e5fb1fb0455..51a18352d53e12700106df4ab3355ad73908f9ab 100644 (file)
@@ -87,7 +87,6 @@ liblttng_ust_runtime_la_SOURCES = \
        lttng-ust-tracef-provider.h \
        tracelog.c \
        lttng-ust-tracelog-provider.h \
-       getenv.h \
        string-utils.c \
        string-utils.h \
        event-notifier-notification.c \
@@ -112,8 +111,6 @@ liblttng_ust_support_la_SOURCES = \
        lttng-tracer.h \
        lttng-tracer-core.h \
        ust-core.c \
-       getenv.h \
-       getenv.c \
        lttng-ust-dynamic-type.c \
        lttng-rb-clients.h \
        lttng-ring-buffer-client-template.h \
index 483f81313977dd0e6867458cc20ec3db93fe4e86..1fb7b16918c06dabbb0fbe37fa82001ac467dee9 100644 (file)
@@ -12,7 +12,7 @@
 #include <urcu/system.h>
 #include <urcu/arch.h>
 
-#include "getenv.h"
+#include "common/getenv.h"
 #include "lib/lttng-ust/getcpu.h"
 
 /* Function pointer to the current getcpu callback. */
diff --git a/src/lib/lttng-ust/getenv.c b/src/lib/lttng-ust/getenv.c
deleted file mode 100644 (file)
index 0a7bffd..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <sys/types.h>
-#include "common/logging.h"
-#include "common/macros.h"
-#include "getenv.h"
-
-enum lttng_env_secure {
-       LTTNG_ENV_SECURE,
-       LTTNG_ENV_NOT_SECURE,
-};
-
-struct lttng_env {
-       const char *key;
-       enum lttng_env_secure secure;
-       char *value;
-};
-
-static struct lttng_env lttng_env[] = {
-       /*
-        * LTTNG_UST_DEBUG is used directly by snprintf, because it
-        * needs to be already set for ERR() used in
-        * lttng_ust_getenv_init().
-        */
-       { "LTTNG_UST_DEBUG", LTTNG_ENV_NOT_SECURE, NULL, },
-
-       /* Env. var. which can be used in setuid/setgid executables. */
-       { "LTTNG_UST_WITHOUT_BADDR_STATEDUMP", LTTNG_ENV_NOT_SECURE, NULL, },
-       { "LTTNG_UST_REGISTER_TIMEOUT", LTTNG_ENV_NOT_SECURE, NULL, },
-
-       /* Env. var. which are not fetched in setuid/setgid executables. */
-       { "LTTNG_UST_CLOCK_PLUGIN", LTTNG_ENV_SECURE, NULL, },
-       { "LTTNG_UST_GETCPU_PLUGIN", LTTNG_ENV_SECURE, NULL, },
-       { "LTTNG_UST_ALLOW_BLOCKING", LTTNG_ENV_SECURE, NULL, },
-       { "HOME", LTTNG_ENV_SECURE, NULL, },
-       { "LTTNG_HOME", LTTNG_ENV_SECURE, NULL, },
-};
-
-static
-int lttng_is_setuid_setgid(void)
-{
-       return geteuid() != getuid() || getegid() != getgid();
-}
-
-char *lttng_ust_getenv(const char *name)
-{
-       size_t i;
-       struct lttng_env *e;
-       bool found = false;
-
-       for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
-               e = &lttng_env[i];
-
-               if (strcmp(e->key, name) == 0) {
-                       found = true;
-                       break;
-               }
-       }
-       if (!found) {
-               return NULL;
-       }
-       return e->value;
-}
-
-void lttng_ust_getenv_init(void)
-{
-       size_t i;
-
-       for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
-               struct lttng_env *e = &lttng_env[i];
-
-               if (e->secure == LTTNG_ENV_SECURE && lttng_is_setuid_setgid()) {
-                       ERR("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.",
-                               e->key);
-                       continue;
-               }
-               e->value = getenv(e->key);
-       }
-}
diff --git a/src/lib/lttng-ust/getenv.h b/src/lib/lttng-ust/getenv.h
deleted file mode 100644 (file)
index 61d4919..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#ifndef _COMPAT_GETENV_H
-#define _COMPAT_GETENV_H
-
-/*
- * Always add the lttng-ust environment variables using the lttng_ust_getenv()
- * infrastructure rather than using getenv() directly.  This ensures that we
- * don't trigger races between getenv() invoked by lttng-ust listener threads
- * invoked concurrently with setenv() called by an otherwise single-threaded
- * application thread. (the application is not aware that it runs with
- * lttng-ust)
- */
-
-char *lttng_ust_getenv(const char *name)
-       __attribute__((visibility("hidden")));
-
-void lttng_ust_getenv_init(void)
-       __attribute__((visibility("hidden")));
-
-#endif /* _COMPAT_GETENV_H */
index edda7ae933544650f0d0494a2c016ecb572afa74..61b8e55a5db69848db78a52f034c34200119076e 100644 (file)
@@ -18,7 +18,7 @@
 #include "common/logging.h"
 
 #include "clock.h"
-#include "getenv.h"
+#include "common/getenv.h"
 
 struct lttng_ust_trace_clock *lttng_ust_trace_clock;
 
index 219ef5d21ca64a21c08457c86f8b822d73efc540..2f6fe18faefa2a604e594ef0e487a7dee5bdc376 100644 (file)
@@ -51,7 +51,7 @@
 #include "lttng-ust-statedump.h"
 #include "clock.h"
 #include "lib/lttng-ust/getcpu.h"
-#include "getenv.h"
+#include "common/getenv.h"
 #include "ust-events-internal.h"
 #include "context-internal.h"
 #include "common/align.h"
index cd4df182c0e04564f3fdcefc95864eb682f2ccbb..2dae71f963794d8578af14ffbb552bbdb017f2fb 100644 (file)
@@ -21,7 +21,7 @@
 #include "lttng-tracer-core.h"
 #include "lttng-ust-statedump.h"
 #include "jhash.h"
-#include "getenv.h"
+#include "common/getenv.h"
 #include "ust-events-internal.h"
 
 #define TRACEPOINT_DEFINE
This page took 0.030133 seconds and 4 git commands to generate.