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 --- slave.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'slave.c') diff --git a/slave.c b/slave.c index 4fbd5c9b..4e8610ce 100644 --- a/slave.c +++ b/slave.c @@ -145,8 +145,8 @@ emit_notifications(int fd, const user_notifications_t *notifications) } static noreturn void -slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell, - const user_notifications_t *notifications) +slave_exec(int ptmx, char *argv[], char *const envp[], int err_fd, + bool login_shell, const user_notifications_t *notifications) { int pts = -1; const char *pts_name = ptsname(ptmx); @@ -232,7 +232,7 @@ slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell, } else file = argv[0]; - execvp(file, argv); + execvpe(file, argv, envp); err: (void)!write(err_fd, &errno, sizeof(errno)); @@ -246,8 +246,8 @@ err: pid_t slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, - const char *term_env, const char *conf_shell, bool login_shell, - const user_notifications_t *notifications) + char *const *envp, const char *term_env, const char *conf_shell, + bool login_shell, const user_notifications_t *notifications) { int fork_pipe[2]; if (pipe2(fork_pipe, O_CLOEXEC) < 0) { @@ -319,7 +319,8 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, if (is_valid_shell(shell_argv[0])) setenv("SHELL", shell_argv[0], 1); - slave_exec(ptmx, shell_argv, fork_pipe[1], login_shell, notifications); + slave_exec(ptmx, shell_argv, envp != NULL ? envp : environ, + fork_pipe[1], login_shell, notifications); BUG("Unexpected return from slave_exec()"); break; -- cgit v1.3 From fd414f79be177b23a67ce87b81657c2bfd80a35e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 11 Apr 2022 13:24:27 +0200 Subject: client/slave: explictly add ‘extern char **environ’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s not in any header files on FreeBSD. --- client.c | 2 ++ slave.c | 2 ++ 2 files changed, 4 insertions(+) (limited to 'slave.c') diff --git a/client.c b/client.c index 7d524204..7624e7db 100644 --- a/client.c +++ b/client.c @@ -25,6 +25,8 @@ #include "version.h" #include "xmalloc.h" +extern char **environ; + struct string { size_t len; char *str; diff --git a/slave.c b/slave.c index 4e8610ce..dc4a8bc6 100644 --- a/slave.c +++ b/slave.c @@ -23,6 +23,8 @@ #include "tokenize.h" #include "xmalloc.h" +extern char **environ; + static bool is_valid_shell(const char *shell) { -- cgit v1.3 From b3d0cdd4b20855c62a759b66da94907f6bf81cb7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 11 Apr 2022 14:02:18 +0200 Subject: slave: roll our own ‘execvpe()’ on FreeBSD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slave.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'slave.c') diff --git a/slave.c b/slave.c index dc4a8bc6..db60d7b5 100644 --- a/slave.c +++ b/slave.c @@ -25,6 +25,63 @@ extern char **environ; +#if defined(__FreeBSD__) +static char * +find_file_in_path(const char *file) +{ + if (strchr(file, '/') != NULL) + return xstrdup(file); + + const char *env_path = getenv("PATH"); + char *path_list = NULL; + + if (env_path != NULL && env_path[0] != '\0') + path_list = xstrdup(env_path); + else { + size_t sc_path_len = confstr(_CS_PATH, NULL, 0); + if (sc_path_len > 0) { + path_list = xmalloc(sc_path_len); + confstr(_CS_PATH, path_list, sc_path_len); + } else + return xstrdup(file); + } + + for (const char *path = strtok(path_list, ":"); + path != NULL; + path = strtok(NULL, ":")) + { + char *full = xasprintf("%s/%s", path, file); + if (access(full, F_OK) == 0) { + free(path_list); + return full; + } + + free(full); + } + + free(path_list); + return xstrdup(file); +} + +static int +foot_execvpe(const char *file, char *const argv[], char *const envp[]) +{ + char *path = find_file_in_path(file); + int ret = execve(path, argv, envp); + + /* + * Getting here is an error + */ + free(path); + return ret; +} + +#else /* !__FreeBSD__ */ + +#define foot_execvpe(file, argv, envp) execvpe(file, argv, envp) + +#endif /* !__FreeBSD__ */ + static bool is_valid_shell(const char *shell) { @@ -234,7 +291,7 @@ slave_exec(int ptmx, char *argv[], char *const envp[], int err_fd, } else file = argv[0]; - execvpe(file, argv, envp); + foot_execvpe(file, argv, envp); err: (void)!write(err_fd, &errno, sizeof(errno)); -- cgit v1.3