Remove unused ip field from struct lttng_ust_ring_buffer_ctx_private
[lttng-ust.git] / src / common / getenv.c
CommitLineData
6f626d28 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
6f626d28 3 *
c0c0989a 4 * Copyright (C) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6f626d28
MD
5 */
6
7#include <stdlib.h>
8#include <unistd.h>
9#include <stdbool.h>
b4051ad8 10#include <stddef.h>
6f626d28 11#include <sys/types.h>
042dbbc8 12#include <pthread.h>
910dcd72 13#include <urcu/system.h>
9d315d6d
MJ
14#include "common/logging.h"
15#include "common/macros.h"
910dcd72 16#include "common/getenv.h"
6f626d28
MD
17
18enum lttng_env_secure {
19 LTTNG_ENV_SECURE,
20 LTTNG_ENV_NOT_SECURE,
21};
22
23struct lttng_env {
24 const char *key;
25 enum lttng_env_secure secure;
26 char *value;
27};
28
042dbbc8
MD
29/* lttng_ust_getenv_init_mutex provides mutual exclusion for initialization. */
30static pthread_mutex_t lttng_ust_getenv_init_mutex = PTHREAD_MUTEX_INITIALIZER;
31static int lttng_ust_getenv_is_init = 0;
910dcd72 32
6f626d28
MD
33static struct lttng_env lttng_env[] = {
34 /*
35 * LTTNG_UST_DEBUG is used directly by snprintf, because it
36 * needs to be already set for ERR() used in
37 * lttng_ust_getenv_init().
38 */
39 { "LTTNG_UST_DEBUG", LTTNG_ENV_NOT_SECURE, NULL, },
40
41 /* Env. var. which can be used in setuid/setgid executables. */
42 { "LTTNG_UST_WITHOUT_BADDR_STATEDUMP", LTTNG_ENV_NOT_SECURE, NULL, },
43 { "LTTNG_UST_REGISTER_TIMEOUT", LTTNG_ENV_NOT_SECURE, NULL, },
44
45 /* Env. var. which are not fetched in setuid/setgid executables. */
46 { "LTTNG_UST_CLOCK_PLUGIN", LTTNG_ENV_SECURE, NULL, },
47 { "LTTNG_UST_GETCPU_PLUGIN", LTTNG_ENV_SECURE, NULL, },
b2c5f61a 48 { "LTTNG_UST_ALLOW_BLOCKING", LTTNG_ENV_SECURE, NULL, },
6f626d28
MD
49 { "HOME", LTTNG_ENV_SECURE, NULL, },
50 { "LTTNG_HOME", LTTNG_ENV_SECURE, NULL, },
51};
52
53static
54int lttng_is_setuid_setgid(void)
55{
56 return geteuid() != getuid() || getegid() != getgid();
57}
58
910dcd72
MJ
59/*
60 * Wrapper over getenv that will only return the values of whitelisted
61 * environment variables when the current process is setuid and/or setgid.
62 */
4c41b460 63char *lttng_ust_getenv(const char *name)
6f626d28
MD
64{
65 size_t i;
66 struct lttng_env *e;
67 bool found = false;
68
407937dc
MD
69 /*
70 * Perform lazy initialization of lttng_ust_getenv for early use
71 * by library constructors.
72 */
73 lttng_ust_getenv_init();
910dcd72 74
6f626d28
MD
75 for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
76 e = &lttng_env[i];
77
78 if (strcmp(e->key, name) == 0) {
79 found = true;
80 break;
81 }
82 }
83 if (!found) {
84 return NULL;
85 }
86 return e->value;
87}
88
89void lttng_ust_getenv_init(void)
90{
91 size_t i;
92
042dbbc8
MD
93 /*
94 * Return early if the init has already completed.
95 */
96 if (CMM_LOAD_SHARED(lttng_ust_getenv_is_init)) {
97 /*
98 * Load lttng_ust_getenv_is_init before reading environment cache.
99 */
100 cmm_smp_rmb();
910dcd72 101 return;
042dbbc8
MD
102 }
103
104 pthread_mutex_lock(&lttng_ust_getenv_init_mutex);
105
106 /*
107 * Check again if the init has completed in another thread now that we
108 * have acquired the mutex.
109 */
110 if (lttng_ust_getenv_is_init)
111 goto end_init;
910dcd72 112
6f626d28
MD
113 for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
114 struct lttng_env *e = &lttng_env[i];
115
116 if (e->secure == LTTNG_ENV_SECURE && lttng_is_setuid_setgid()) {
117 ERR("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.",
118 e->key);
119 continue;
120 }
121 e->value = getenv(e->key);
122 }
042dbbc8
MD
123
124 /*
125 * Store environment cache before setting lttng_ust_getenv_is_init to 1.
126 */
127 cmm_smp_wmb();
910dcd72 128 CMM_STORE_SHARED(lttng_ust_getenv_is_init, 1);
042dbbc8
MD
129end_init:
130 pthread_mutex_unlock(&lttng_ust_getenv_init_mutex);
6f626d28 131}
This page took 0.031958 seconds and 4 git commands to generate.