client: add -E,--client-environment

When this option is used, the child process in the new terminal
instance will inherit its environment from the footclient process,
instead of the foot server’s.

Implemented by sending (yet another) dynamic string list as part of
the client -> server setup packet. When the new option is *not* used,
the setup packet is now 2 bytes larger than before.

On the server side, the slave process now uses execvpe() instead of
execvp(). There’s plumbing to propagate a new ‘envp’ argument from
term_init() all the way down to slave_exec(). If ‘envp’ is NULL, we
use ‘environ’ instead (thus matching the old behavior of execvp()).

Closes #1004
This commit is contained in:
Daniel Eklöf 2022-04-11 12:19:40 +02:00
parent 856086bbbe
commit d02124902b
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
9 changed files with 100 additions and 39 deletions

View file

@ -146,6 +146,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
char **argv = NULL;
config_override_t overrides = tll_init();
char **envp = NULL;
if (events & EPOLLHUP)
goto shutdown;
@ -281,7 +282,21 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
argv[i] = (char *)p; p += arg.len;
LOG_DBG("argv[%hu] = %.*s", i, arg.len, argv[i]);
}
argv[cdata.argc] = NULL;
/* envp */
envp = cdata.env_count != 0
? xcalloc(cdata.env_count + 1, sizeof(envp[0]))
: NULL;
for (uint16_t i = 0; i < cdata.env_count; i++) {
struct client_string e;
CHECK_BUF(sizeof(e));
memcpy(&e, p, sizeof(e)); p += sizeof(e);
CHECK_BUF_AND_NULL(e.len);
envp[i] = (char *)p; p += e.len;
LOG_DBG("env[%hu] = %.*s", i, e.len, envp[i]);
}
#undef CHECK_BUF_AND_NULL
#undef CHECK_BUF
@ -317,7 +332,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
instance->terminal = term_init(
conf != NULL ? conf : server->conf,
server->fdm, server->reaper, server->wayl, "footclient", cwd, token,
cdata.argc, argv, &term_shutdown_handler, instance);
cdata.argc, argv, envp, &term_shutdown_handler, instance);
if (instance->terminal == NULL) {
LOG_ERR("failed to instantiate new terminal");
@ -336,6 +351,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
instance->client = client;
client->instance = instance;
free(argv);
free(envp);
tll_free_and_free(overrides, free);
}
@ -345,6 +361,7 @@ shutdown:
LOG_DBG("client FD=%d: disconnected", client->fd);
free(argv);
free(envp);
tll_free_and_free(overrides, free);
fdm_del(fdm, fd);
client->fd = -1;