Coverage Report

Created: 2026-04-12 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/environ.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
21
#include <fnmatch.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <unistd.h>
25
26
#include "tmux.h"
27
28
/*
29
 * Environment - manipulate a set of environment variables.
30
 */
31
32
RB_HEAD(environ, environ_entry);
33
static int environ_cmp(struct environ_entry *, struct environ_entry *);
34
0
RB_GENERATE_STATIC(environ, environ_entry, entry, environ_cmp);
Unexecuted instantiation: environ.c:environ_RB_MINMAX
Unexecuted instantiation: environ.c:environ_RB_REMOVE
Unexecuted instantiation: environ.c:environ_RB_REMOVE_COLOR
Unexecuted instantiation: environ.c:environ_RB_FIND
Unexecuted instantiation: environ.c:environ_RB_INSERT
35
0
36
0
static int
37
0
environ_cmp(struct environ_entry *envent1, struct environ_entry *envent2)
38
0
{
39
0
  return (strcmp(envent1->name, envent2->name));
40
0
}
41
42
/* Initialise the environment. */
43
struct environ *
44
environ_create(void)
45
1
{
46
1
  struct environ  *env;
47
48
1
  env = xcalloc(1, sizeof *env);
49
1
  RB_INIT(env);
50
51
1
  return (env);
52
1
}
53
54
/* Free an environment. */
55
void
56
environ_free(struct environ *env)
57
0
{
58
0
  struct environ_entry  *envent, *envent1;
59
60
0
  if (env == NULL)
61
0
    return;
62
63
0
  RB_FOREACH_SAFE(envent, environ, env, envent1) {
64
0
    RB_REMOVE(environ, env, envent);
65
0
    free(envent->name);
66
0
    free(envent->value);
67
0
    free(envent);
68
0
  }
69
0
  free(env);
70
0
}
71
72
struct environ_entry *
73
environ_first(struct environ *env)
74
0
{
75
0
  return (RB_MIN(environ, env));
76
0
}
77
78
struct environ_entry *
79
environ_next(struct environ_entry *envent)
80
0
{
81
0
  return (RB_NEXT(environ, env, envent));
82
0
}
83
84
/* Copy one environment into another. */
85
void
86
environ_copy(struct environ *srcenv, struct environ *dstenv)
87
0
{
88
0
  struct environ_entry  *envent;
89
90
0
  RB_FOREACH(envent, environ, srcenv) {
91
0
    if (envent->value == NULL)
92
0
      environ_clear(dstenv, envent->name);
93
0
    else {
94
0
      environ_set(dstenv, envent->name, envent->flags,
95
0
          "%s", envent->value);
96
0
    }
97
0
  }
98
0
}
99
100
/* Find an environment variable. */
101
struct environ_entry *
102
environ_find(struct environ *env, const char *name)
103
0
{
104
0
  struct environ_entry  envent;
105
106
0
  envent.name = (char *) name;
107
0
  return (RB_FIND(environ, env, &envent));
108
0
}
109
110
/* Set an environment variable. */
111
void
112
environ_set(struct environ *env, const char *name, int flags, const char *fmt,
113
    ...)
114
0
{
115
0
  struct environ_entry  *envent;
116
0
  va_list      ap;
117
118
0
  va_start(ap, fmt);
119
0
  if ((envent = environ_find(env, name)) != NULL) {
120
0
    envent->flags = flags;
121
0
    free(envent->value);
122
0
    xvasprintf(&envent->value, fmt, ap);
123
0
  } else {
124
0
    envent = xmalloc(sizeof *envent);
125
0
    envent->name = xstrdup(name);
126
0
    envent->flags = flags;
127
0
    xvasprintf(&envent->value, fmt, ap);
128
0
    RB_INSERT(environ, env, envent);
129
0
  }
130
0
  va_end(ap);
131
0
}
132
133
/* Clear an environment variable. */
134
void
135
environ_clear(struct environ *env, const char *name)
136
0
{
137
0
  struct environ_entry  *envent;
138
139
0
  if ((envent = environ_find(env, name)) != NULL) {
140
0
    free(envent->value);
141
0
    envent->value = NULL;
142
0
  } else {
143
0
    envent = xmalloc(sizeof *envent);
144
0
    envent->name = xstrdup(name);
145
0
    envent->flags = 0;
146
0
    envent->value = NULL;
147
0
    RB_INSERT(environ, env, envent);
148
0
  }
149
0
}
150
151
/* Set an environment variable from a NAME=VALUE string. */
152
void
153
environ_put(struct environ *env, const char *var, int flags)
154
0
{
155
0
  char  *name, *value;
156
157
0
  value = strchr(var, '=');
158
0
  if (value == NULL)
159
0
    return;
160
0
  value++;
161
162
0
  name = xstrdup(var);
163
0
  name[strcspn(name, "=")] = '\0';
164
165
0
  environ_set(env, name, flags, "%s", value);
166
0
  free(name);
167
0
}
168
169
/* Unset an environment variable. */
170
void
171
environ_unset(struct environ *env, const char *name)
172
0
{
173
0
  struct environ_entry  *envent;
174
175
0
  if ((envent = environ_find(env, name)) == NULL)
176
0
    return;
177
0
  RB_REMOVE(environ, env, envent);
178
0
  free(envent->name);
179
0
  free(envent->value);
180
0
  free(envent);
181
0
}
182
183
/* Copy variables from a destination into a source environment. */
184
void
185
environ_update(struct options *oo, struct environ *src, struct environ *dst)
186
0
{
187
0
  struct environ_entry    *envent;
188
0
  struct environ_entry    *envent1;
189
0
  struct options_entry    *o;
190
0
  struct options_array_item *a;
191
0
  union options_value   *ov;
192
0
  int        found;
193
194
0
  o = options_get(oo, "update-environment");
195
0
  if (o == NULL)
196
0
    return;
197
0
  a = options_array_first(o);
198
0
  while (a != NULL) {
199
0
    ov = options_array_item_value(a);
200
0
    found = 0;
201
0
    RB_FOREACH_SAFE(envent, environ, src, envent1) {
202
0
      if (fnmatch(ov->string, envent->name, 0) == 0) {
203
0
        environ_set(dst, envent->name, 0, "%s", envent->value);
204
0
        found = 1;
205
0
      }
206
0
    }
207
0
    if (!found)
208
0
      environ_clear(dst, ov->string);
209
0
    a = options_array_next(a);
210
0
  }
211
0
}
212
213
/* Push environment into the real environment - use after fork(). */
214
void
215
environ_push(struct environ *env)
216
0
{
217
0
  struct environ_entry  *envent;
218
219
0
  environ = xcalloc(1, sizeof *environ);
220
0
  RB_FOREACH(envent, environ, env) {
221
0
    if (envent->value != NULL &&
222
0
        *envent->name != '\0' &&
223
0
        (~envent->flags & ENVIRON_HIDDEN))
224
0
      setenv(envent->name, envent->value, 1);
225
0
  }
226
0
}
227
228
/* Log the environment. */
229
void
230
environ_log(struct environ *env, const char *fmt, ...)
231
0
{
232
0
  struct environ_entry  *envent;
233
0
  va_list      ap;
234
0
  char      *prefix;
235
236
0
  va_start(ap, fmt);
237
0
  vasprintf(&prefix, fmt, ap);
238
0
  va_end(ap);
239
240
0
  RB_FOREACH(envent, environ, env) {
241
0
    if (envent->value != NULL && *envent->name != '\0') {
242
0
      log_debug("%s%s=%s", prefix, envent->name,
243
0
          envent->value);
244
0
    }
245
0
  }
246
247
0
  free(prefix);
248
0
}
249
250
/* Create initial environment for new child. */
251
struct environ *
252
environ_for_session(struct session *s, int no_TERM)
253
0
{
254
0
  struct environ  *env;
255
0
  const char  *value;
256
0
  int    idx;
257
258
0
  env = environ_create();
259
0
  environ_copy(global_environ, env);
260
0
  if (s != NULL)
261
0
    environ_copy(s->environ, env);
262
263
0
  if (!no_TERM) {
264
0
    value = options_get_string(global_options, "default-terminal");
265
0
    environ_set(env, "TERM", 0, "%s", value);
266
0
    environ_set(env, "TERM_PROGRAM", 0, "%s", "tmux");
267
0
    environ_set(env, "TERM_PROGRAM_VERSION", 0, "%s", getversion());
268
0
    environ_set(env, "COLORTERM", 0, "truecolor");
269
0
  }
270
271
#ifdef HAVE_SYSTEMD
272
  environ_clear(env, "LISTEN_PID");
273
  environ_clear(env, "LISTEN_FDS");
274
  environ_clear(env, "LISTEN_FDNAMES");
275
#endif
276
277
0
  if (s != NULL)
278
0
    idx = s->id;
279
0
  else
280
0
    idx = -1;
281
0
  environ_set(env, "TMUX", 0, "%s,%ld,%d", socket_path, (long)getpid(),
282
0
      idx);
283
284
0
  return (env);
285
0
}