From d02124902b4e27c3ed3ef48442ce17c5e0af9d56 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 11 Apr 2022 12:19:40 +0200 Subject: client: add -E,--client-environment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- server.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'server.c') diff --git a/server.c b/server.c index 2c41b479..31b8c633 100644 --- a/server.c +++ b/server.c @@ -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; -- cgit v1.3