From 910dcd720731c190adbfb9bb50c67bb9c4235003 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 6 Apr 2021 16:32:14 -0400 Subject: [PATCH] Move getenv to libcommon Move it to libcommon since it can be used by any library. Change-Id: Ic09b643e3a91b2844fe9dc13eac55fe90f8e15c2 Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- src/common/Makefile.am | 2 ++ src/{lib/lttng-ust => common}/getenv.c | 17 ++++++++++++++++- src/{lib/lttng-ust => common}/getenv.h | 10 +++++++--- src/lib/lttng-ust-ctl/ustctl.c | 2 +- src/lib/lttng-ust/Makefile.am | 3 --- src/lib/lttng-ust/getcpu.c | 2 +- src/lib/lttng-ust/lttng-clock.c | 2 +- src/lib/lttng-ust/lttng-ust-comm.c | 2 +- src/lib/lttng-ust/lttng-ust-statedump.c | 2 +- 9 files changed, 30 insertions(+), 12 deletions(-) rename src/{lib/lttng-ust => common}/getenv.c (82%) rename src/{lib/lttng-ust => common}/getenv.h (80%) diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 2ab4bfdc..e75ee250 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -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/lib/lttng-ust/getenv.c b/src/common/getenv.c similarity index 82% rename from src/lib/lttng-ust/getenv.c rename to src/common/getenv.c index 0a7bffd8..1909f4cf 100644 --- a/src/lib/lttng-ust/getenv.c +++ b/src/common/getenv.c @@ -9,9 +9,10 @@ #include #include #include +#include #include "common/logging.h" #include "common/macros.h" -#include "getenv.h" +#include "common/getenv.h" enum lttng_env_secure { LTTNG_ENV_SECURE, @@ -24,6 +25,9 @@ struct lttng_env { 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 @@ -50,12 +54,19 @@ 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 = <tng_env[i]; @@ -74,6 +85,9 @@ 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 = <tng_env[i]; @@ -84,4 +98,5 @@ void lttng_ust_getenv_init(void) } e->value = getenv(e->key); } + CMM_STORE_SHARED(lttng_ust_getenv_is_init, 1); } diff --git a/src/lib/lttng-ust/getenv.h b/src/common/getenv.h similarity index 80% rename from src/lib/lttng-ust/getenv.h rename to src/common/getenv.h index 61d49193..9bfb9f0b 100644 --- a/src/lib/lttng-ust/getenv.h +++ b/src/common/getenv.h @@ -4,8 +4,8 @@ * Copyright (C) 2015 Mathieu Desnoyers */ -#ifndef _COMPAT_GETENV_H -#define _COMPAT_GETENV_H +#ifndef _UST_COMMON_GETENV_H +#define _UST_COMMON_GETENV_H /* * Always add the lttng-ust environment variables using the lttng_ust_getenv() @@ -19,7 +19,11 @@ 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 /* _COMPAT_GETENV_H */ +#endif /* _UST_COMMON_GETENV_H */ diff --git a/src/lib/lttng-ust-ctl/ustctl.c b/src/lib/lttng-ust-ctl/ustctl.c index 3561a722..96107fe7 100644 --- a/src/lib/lttng-ust-ctl/ustctl.c +++ b/src/lib/lttng-ust-ctl/ustctl.c @@ -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" diff --git a/src/lib/lttng-ust/Makefile.am b/src/lib/lttng-ust/Makefile.am index 3548a9e6..51a18352 100644 --- a/src/lib/lttng-ust/Makefile.am +++ b/src/lib/lttng-ust/Makefile.am @@ -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 \ diff --git a/src/lib/lttng-ust/getcpu.c b/src/lib/lttng-ust/getcpu.c index 483f8131..1fb7b169 100644 --- a/src/lib/lttng-ust/getcpu.c +++ b/src/lib/lttng-ust/getcpu.c @@ -12,7 +12,7 @@ #include #include -#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/lttng-clock.c b/src/lib/lttng-ust/lttng-clock.c index edda7ae9..61b8e55a 100644 --- a/src/lib/lttng-ust/lttng-clock.c +++ b/src/lib/lttng-ust/lttng-clock.c @@ -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; diff --git a/src/lib/lttng-ust/lttng-ust-comm.c b/src/lib/lttng-ust/lttng-ust-comm.c index 219ef5d2..2f6fe18f 100644 --- a/src/lib/lttng-ust/lttng-ust-comm.c +++ b/src/lib/lttng-ust/lttng-ust-comm.c @@ -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" diff --git a/src/lib/lttng-ust/lttng-ust-statedump.c b/src/lib/lttng-ust/lttng-ust-statedump.c index cd4df182..2dae71f9 100644 --- a/src/lib/lttng-ust/lttng-ust-statedump.c +++ b/src/lib/lttng-ust/lttng-ust-statedump.c @@ -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 -- 2.34.1