2009-08-08 21:52:43 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-08-08 21:52:43 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-10-07 08:23:55 +00:00
|
|
|
#include <fnmatch.h>
|
2009-08-08 21:52:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-03-09 17:02:38 +00:00
|
|
|
#include <unistd.h>
|
2009-08-08 21:52:43 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Environment - manipulate a set of environment variables.
|
|
|
|
*/
|
|
|
|
|
2015-10-28 09:51:55 +00:00
|
|
|
RB_HEAD(environ, environ_entry);
|
2016-10-10 21:29:23 +00:00
|
|
|
static int environ_cmp(struct environ_entry *, struct environ_entry *);
|
|
|
|
RB_GENERATE_STATIC(environ, environ_entry, entry, environ_cmp);
|
2009-08-08 21:52:43 +00:00
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static int
|
2009-08-08 21:52:43 +00:00
|
|
|
environ_cmp(struct environ_entry *envent1, struct environ_entry *envent2)
|
|
|
|
{
|
|
|
|
return (strcmp(envent1->name, envent2->name));
|
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Initialise the environment. */
|
2015-10-28 09:51:55 +00:00
|
|
|
struct environ *
|
|
|
|
environ_create(void)
|
2009-08-08 21:52:43 +00:00
|
|
|
{
|
2015-10-28 09:51:55 +00:00
|
|
|
struct environ *env;
|
|
|
|
|
|
|
|
env = xcalloc(1, sizeof *env);
|
2009-08-08 21:52:43 +00:00
|
|
|
RB_INIT(env);
|
2015-10-28 09:51:55 +00:00
|
|
|
|
|
|
|
return (env);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Free an environment. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
|
|
|
environ_free(struct environ *env)
|
|
|
|
{
|
2015-10-28 09:51:55 +00:00
|
|
|
struct environ_entry *envent, *envent1;
|
2009-08-08 21:52:43 +00:00
|
|
|
|
2015-10-28 09:51:55 +00:00
|
|
|
RB_FOREACH_SAFE(envent, environ, env, envent1) {
|
2009-08-08 21:52:43 +00:00
|
|
|
RB_REMOVE(environ, env, envent);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(envent->name);
|
|
|
|
free(envent->value);
|
|
|
|
free(envent);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
2015-10-28 09:51:55 +00:00
|
|
|
free(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct environ_entry *
|
|
|
|
environ_first(struct environ *env)
|
|
|
|
{
|
|
|
|
return (RB_MIN(environ, env));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct environ_entry *
|
|
|
|
environ_next(struct environ_entry *envent)
|
|
|
|
{
|
|
|
|
return (RB_NEXT(environ, env, envent));
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Copy one environment into another. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
|
|
|
environ_copy(struct environ *srcenv, struct environ *dstenv)
|
|
|
|
{
|
|
|
|
struct environ_entry *envent;
|
|
|
|
|
2015-11-24 23:46:15 +00:00
|
|
|
RB_FOREACH(envent, environ, srcenv) {
|
|
|
|
if (envent->value == NULL)
|
|
|
|
environ_clear(dstenv, envent->name);
|
2020-03-31 17:14:40 +00:00
|
|
|
else {
|
|
|
|
environ_set(dstenv, envent->name, envent->flags,
|
|
|
|
"%s", envent->value);
|
|
|
|
}
|
2015-11-24 23:46:15 +00:00
|
|
|
}
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Find an environment variable. */
|
2009-08-08 21:52:43 +00:00
|
|
|
struct environ_entry *
|
|
|
|
environ_find(struct environ *env, const char *name)
|
|
|
|
{
|
|
|
|
struct environ_entry envent;
|
|
|
|
|
|
|
|
envent.name = (char *) name;
|
|
|
|
return (RB_FIND(environ, env, &envent));
|
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Set an environment variable. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(struct environ *env, const char *name, int flags, const char *fmt,
|
|
|
|
...)
|
2009-08-08 21:52:43 +00:00
|
|
|
{
|
|
|
|
struct environ_entry *envent;
|
2015-11-24 23:46:15 +00:00
|
|
|
va_list ap;
|
2009-08-08 21:52:43 +00:00
|
|
|
|
2015-11-24 23:46:15 +00:00
|
|
|
va_start(ap, fmt);
|
2009-08-08 21:52:43 +00:00
|
|
|
if ((envent = environ_find(env, name)) != NULL) {
|
2020-03-31 17:14:40 +00:00
|
|
|
envent->flags = flags;
|
2012-07-10 11:53:01 +00:00
|
|
|
free(envent->value);
|
2015-11-24 23:46:15 +00:00
|
|
|
xvasprintf(&envent->value, fmt, ap);
|
2009-08-08 21:52:43 +00:00
|
|
|
} else {
|
|
|
|
envent = xmalloc(sizeof *envent);
|
|
|
|
envent->name = xstrdup(name);
|
2020-03-31 17:14:40 +00:00
|
|
|
envent->flags = flags;
|
2015-11-24 23:46:15 +00:00
|
|
|
xvasprintf(&envent->value, fmt, ap);
|
|
|
|
RB_INSERT(environ, env, envent);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear an environment variable. */
|
|
|
|
void
|
|
|
|
environ_clear(struct environ *env, const char *name)
|
|
|
|
{
|
|
|
|
struct environ_entry *envent;
|
|
|
|
|
|
|
|
if ((envent = environ_find(env, name)) != NULL) {
|
|
|
|
free(envent->value);
|
|
|
|
envent->value = NULL;
|
|
|
|
} else {
|
|
|
|
envent = xmalloc(sizeof *envent);
|
|
|
|
envent->name = xstrdup(name);
|
2020-03-31 17:14:40 +00:00
|
|
|
envent->flags = 0;
|
2015-11-24 23:46:15 +00:00
|
|
|
envent->value = NULL;
|
2009-08-08 21:52:43 +00:00
|
|
|
RB_INSERT(environ, env, envent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Set an environment variable from a NAME=VALUE string. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_put(struct environ *env, const char *var, int flags)
|
2009-08-08 21:52:43 +00:00
|
|
|
{
|
2010-04-04 19:02:09 +00:00
|
|
|
char *name, *value;
|
2009-08-08 21:52:43 +00:00
|
|
|
|
|
|
|
value = strchr(var, '=');
|
|
|
|
if (value == NULL)
|
|
|
|
return;
|
|
|
|
value++;
|
|
|
|
|
|
|
|
name = xstrdup(var);
|
|
|
|
name[strcspn(name, "=")] = '\0';
|
|
|
|
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(env, name, flags, "%s", value);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(name);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 19:02:09 +00:00
|
|
|
/* Unset an environment variable. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
|
|
|
environ_unset(struct environ *env, const char *name)
|
|
|
|
{
|
|
|
|
struct environ_entry *envent;
|
|
|
|
|
|
|
|
if ((envent = environ_find(env, name)) == NULL)
|
|
|
|
return;
|
|
|
|
RB_REMOVE(environ, env, envent);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(envent->name);
|
|
|
|
free(envent->value);
|
|
|
|
free(envent);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:14:40 +00:00
|
|
|
/* Copy variables from a destination into a source environment. */
|
2009-08-08 21:52:43 +00:00
|
|
|
void
|
2017-01-24 20:15:32 +00:00
|
|
|
environ_update(struct options *oo, struct environ *src, struct environ *dst)
|
2009-08-08 21:52:43 +00:00
|
|
|
{
|
2019-03-18 11:58:40 +00:00
|
|
|
struct environ_entry *envent;
|
|
|
|
struct options_entry *o;
|
|
|
|
struct options_array_item *a;
|
2019-04-23 20:36:55 +00:00
|
|
|
union options_value *ov;
|
2009-08-08 21:52:43 +00:00
|
|
|
|
2017-01-24 20:15:32 +00:00
|
|
|
o = options_get(oo, "update-environment");
|
2019-03-18 11:58:40 +00:00
|
|
|
if (o == NULL)
|
2017-01-24 20:15:32 +00:00
|
|
|
return;
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_first(o);
|
|
|
|
while (a != NULL) {
|
2019-04-23 20:36:55 +00:00
|
|
|
ov = options_array_item_value(a);
|
2020-10-07 08:23:55 +00:00
|
|
|
RB_FOREACH(envent, environ, src) {
|
|
|
|
if (fnmatch(ov->string, envent->name, 0) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (envent == NULL)
|
2019-04-23 20:36:55 +00:00
|
|
|
environ_clear(dst, ov->string);
|
2009-08-08 21:52:43 +00:00
|
|
|
else
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(dst, envent->name, 0, "%s", envent->value);
|
2019-03-18 11:58:40 +00:00
|
|
|
a = options_array_next(a);
|
2009-08-08 21:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-04 19:02:09 +00:00
|
|
|
|
|
|
|
/* Push environment into the real environment - use after fork(). */
|
|
|
|
void
|
|
|
|
environ_push(struct environ *env)
|
|
|
|
{
|
2016-05-26 12:15:42 +00:00
|
|
|
struct environ_entry *envent;
|
2010-04-04 19:02:09 +00:00
|
|
|
|
2016-05-26 12:15:42 +00:00
|
|
|
environ = xcalloc(1, sizeof *environ);
|
2010-04-04 19:02:09 +00:00
|
|
|
RB_FOREACH(envent, environ, env) {
|
2020-03-31 17:14:40 +00:00
|
|
|
if (envent->value != NULL &&
|
|
|
|
*envent->name != '\0' &&
|
|
|
|
(~envent->flags & ENVIRON_HIDDEN))
|
2010-04-04 19:02:09 +00:00
|
|
|
setenv(envent->name, envent->value, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 09:28:32 +00:00
|
|
|
|
|
|
|
/* Log the environment. */
|
|
|
|
void
|
2017-05-11 07:34:54 +00:00
|
|
|
environ_log(struct environ *env, const char *fmt, ...)
|
2016-07-15 09:28:32 +00:00
|
|
|
{
|
|
|
|
struct environ_entry *envent;
|
2017-05-11 07:34:54 +00:00
|
|
|
va_list ap;
|
|
|
|
char *prefix;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vasprintf(&prefix, fmt, ap);
|
|
|
|
va_end(ap);
|
2016-07-15 09:28:32 +00:00
|
|
|
|
2016-07-15 09:52:34 +00:00
|
|
|
RB_FOREACH(envent, environ, env) {
|
|
|
|
if (envent->value != NULL && *envent->name != '\0') {
|
|
|
|
log_debug("%s%s=%s", prefix, envent->name,
|
|
|
|
envent->value);
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 07:34:54 +00:00
|
|
|
|
|
|
|
free(prefix);
|
2016-07-15 09:28:32 +00:00
|
|
|
}
|
2017-03-09 17:02:38 +00:00
|
|
|
|
|
|
|
/* Create initial environment for new child. */
|
|
|
|
struct environ *
|
2017-04-25 15:35:10 +00:00
|
|
|
environ_for_session(struct session *s, int no_TERM)
|
2017-03-09 17:02:38 +00:00
|
|
|
{
|
|
|
|
struct environ *env;
|
|
|
|
const char *value;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
env = environ_create();
|
|
|
|
environ_copy(global_environ, env);
|
|
|
|
if (s != NULL)
|
|
|
|
environ_copy(s->environ, env);
|
|
|
|
|
2017-04-25 15:35:10 +00:00
|
|
|
if (!no_TERM) {
|
|
|
|
value = options_get_string(global_options, "default-terminal");
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(env, "TERM", 0, "%s", value);
|
2020-05-16 15:40:44 +00:00
|
|
|
environ_set(env, "TERM_PROGRAM", 0, "%s", "tmux");
|
|
|
|
environ_set(env, "TERM_PROGRAM_VERSION", 0, "%s", getversion());
|
2017-04-25 15:35:10 +00:00
|
|
|
}
|
2017-03-09 17:02:38 +00:00
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
idx = s->id;
|
|
|
|
else
|
|
|
|
idx = -1;
|
2020-03-31 17:14:40 +00:00
|
|
|
environ_set(env, "TMUX", 0, "%s,%ld,%d", socket_path, (long)getpid(),
|
|
|
|
idx);
|
2017-03-09 17:02:38 +00:00
|
|
|
|
|
|
|
return (env);
|
|
|
|
}
|