diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2026-11-12 10:50:55 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2026-11-12 11:01:00 +0000 |
| commit | f51f18d33557ea9078f7781f4a4235706228f5c9 (patch) | |
| tree | 7d518d3abac3ecc8bf2e38518c7c5ecbb6ee40a9 | |
| parent | ime: reset view & cancel selection on text commit (diff) | |
spawn-terminal: spawn with all the original command line optionsspawn-terminal-cmdline-arguments
Before this, spawn-terminal would spawn a new foot or footclient,
but *without* any of the command line options the user started the
original foot/footclient with.
We did have a hack for --config, but all other options were forgotten
in the new instance.
This patch fixes this.
* term_init() stores the raw argc/argv in the terminal struct.
* term_spawn_new() calls spawn() with the stored argc/argv.
* for single instances, main() calls term_init() with the unparsed
argc/argv.
* the client/server protocol is extended to always send the raw
argc/argv to the server.
* the server calls term_init() with the argc/argv from the client.
* bonus: on linux, we now use /proc/self/exe to determine which binary
to launch. This means if you run e.g. a foot from your build tree,
spawn-terminal will exec _that_ foot, not the system foot.
A couple of behavioral changes, and/or side-effects, caused by the
above (not exhaustive):
* footclient -E: spawn-terminal will not use the _exact_ environment
from the original footclient, but will simply pass -E to the new
footclient. This means the new instance will inherit the *current*
environment of the original instance.
* foot htop: spawn-instance will _also_ launch a new foot+htop. Before
this patch, spawn-instance would launch a new foot+shell.
Closes #1622
Diffstat (limited to '')
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | client-protocol.h | 31 | ||||
| -rw-r--r-- | client.c | 76 | ||||
| -rw-r--r-- | main.c | 25 | ||||
| -rw-r--r-- | server.c | 41 | ||||
| -rw-r--r-- | terminal.c | 30 | ||||
| -rw-r--r-- | terminal.h | 7 |
7 files changed, 171 insertions, 42 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d1a25691..5c47874a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,12 +95,15 @@ selection direction ([#2483][2483]). * Crash in `xdg_output_handle_done()` if called when a terminal is in the process of shutting down ([#2487][2487]). +* `spawn-terminal` not using the command line options from the + original instance ([#1622][1622]). [2432]: https://codeberg.org/dnkl/foot/pulls/2432 [2467]: https://codeberg.org/dnkl/foot/issues/2467 [2482]: https://codeberg.org/dnkl/foot/pulls/2482 [2483]: https://codeberg.org/dnkl/foot/issues/2483 [2487]: https://codeberg.org/dnkl/foot/issues/2487 +[1622]: https://codeberg.org/dnkl/foot/issues/1622 ### Security diff --git a/client-protocol.h b/client-protocol.h index efd601d7..391fb6d1 100644 --- a/client-protocol.h +++ b/client-protocol.h @@ -9,6 +9,27 @@ struct client_string { /* char str[static len]; */ }; +/* + * Data + options sent from client to server + * + * - hold: don't exit (the terminal instance) after the slave has terminated + * - no-wait: terminate the footclient instance immediately + * - xdga-token: for activation + * - exe: the filename/path to use when spawning a new footclient instance (used by spawn-terminal) + * - cwd: current working directory of the footclient process + * - overrides: -o style options + * - foot-argc/argv: the raw footclient command line, no processing + * - slave-argc/argv: argc+argv passed to the slave + * - envp: footclient's environment, copied to the server + * + * Note that almost everything can be derived from foot argc/argv. In + * the future, we _could_ strip the protocol, and do most of the + * command line parsing inside the server. For a very long time, foot + * argc/argv was *not* part of the protocol, but was added to allow + * spawn-terminal to spawn a new footclient instance with the exact + * same command line options. + */ + struct client_data { bool hold:1; bool no_wait:1; @@ -16,19 +37,23 @@ struct client_data { uint8_t reserved:5; uint8_t token_len; + uint16_t exe_len; uint16_t cwd_len; uint16_t override_count; - uint16_t argc; + uint16_t foot_argc; + uint16_t slave_argc; uint16_t env_count; + /* char exe[static exe_len]; */ /* char cwd[static cwd_len]; */ /* char token[static token_len]; */ /* struct client_string overrides[static override_count]; */ - /* struct client_string argv[static argc]; */ + /* struct client_string foot_argv[static foot_argc]; */ + /* struct client_string slave_argv[static slave_argc]; */ /* struct client_string envp[static env_count]; */ } __attribute__((packed)); -_Static_assert(sizeof(struct client_data) == 10, "protocol struct size error"); +_Static_assert(sizeof(struct client_data) == 14, "protocol struct size error"); enum client_ipc_code { FOOT_IPC_SIGUSR, @@ -100,6 +100,19 @@ print_usage(const char *prog_name) puts(options); } +static const char * +get_footclient_exe(void) +{ + /* Linux only, fallback to "foot" */ + static char lnk[PATH_MAX]; + ssize_t ret = readlink("/proc/self/exe", lnk, sizeof(lnk) - 1); + if (ret < 0) + return "footclient"; + + lnk[ret] = '\0'; + return lnk; +} + static bool NOINLINE push_string(string_list_t *string_list, const char *s, uint64_t *total_len) { @@ -152,6 +165,11 @@ main(int argc, char *const *argv) static const int foot_exit_failure = -36; int ret = foot_exit_failure; + const int foot_argc = argc; + const char *foot_argv[foot_argc]; + for (int i = 0; i < foot_argc; i++) + foot_argv[i] = argv[i]; + const char *const prog_name = argc > 0 ? argv[0] : "<nullptr>"; static const struct option longopts[] = { @@ -201,7 +219,8 @@ main(int argc, char *const *argv) /* malloc:ed and needs to be in scope of all goto's */ int fd = -1; char *_cwd = NULL; - struct client_string *cargv = NULL; + struct client_string *cfoot_argv = NULL; + struct client_string *cslave_argv = NULL; string_list_t overrides = tll_init(); string_list_t envp = tll_init(); @@ -426,6 +445,7 @@ main(int argc, char *const *argv) } } + const char *exe = get_footclient_exe(); const char *cwd = custom_cwd; if (cwd == NULL) { size_t buf_len = 1024; @@ -471,6 +491,7 @@ main(int argc, char *const *argv) } /* String lengths, including NULL terminator */ + const size_t exe_len = strlen(exe) + 1; const size_t cwd_len = strlen(cwd) + 1; const size_t override_count = tll_length(overrides); const size_t env_count = tll_length(envp); @@ -480,35 +501,52 @@ main(int argc, char *const *argv) .no_wait = no_wait, .xdga_token = xdga_token, .token_len = token_len, + .exe_len = exe_len, .cwd_len = cwd_len, .override_count = override_count, - .argc = argc, + .foot_argc = foot_argc, + .slave_argc = argc, .env_count = env_count, }; /* Total packet length, not (yet) including argv[] */ - total_len += sizeof(data) + cwd_len + token_len; + total_len += sizeof(data) + exe_len + cwd_len + token_len; + + cfoot_argv = xmalloc(foot_argc * sizeof(cfoot_argv[0])); + for (size_t i = 0; i < foot_argc; i++) { + const size_t arg_len = strlen(foot_argv[i]) + 1; + + if (arg_len >= 1 << (8 * sizeof(cfoot_argv[i].len))) { + LOG_ERR("argv length overflow"); + goto err; + } + + cfoot_argv[i].len = arg_len; + total_len += sizeof(cfoot_argv[i]) + cfoot_argv[i].len; + } /* Add argv[] size to total packet length */ - cargv = xmalloc(argc * sizeof(cargv[0])); + cslave_argv = xmalloc(argc * sizeof(cslave_argv[0])); for (size_t i = 0; i < argc; i++) { const size_t arg_len = strlen(argv[i]) + 1; - if (arg_len >= 1 << (8 * sizeof(cargv[i].len))) { + if (arg_len >= 1 << (8 * sizeof(cslave_argv[i].len))) { LOG_ERR("argv length overflow"); goto err; } - cargv[i].len = arg_len; - total_len += sizeof(cargv[i]) + cargv[i].len; + cslave_argv[i].len = arg_len; + total_len += sizeof(cslave_argv[i]) + cslave_argv[i].len; } /* Check for size overflows */ if (total_len >= 1llu << (8 * sizeof(uint32_t)) || + exe_len >= 1 << (8 * sizeof(data.exe_len)) || cwd_len >= 1 << (8 * sizeof(data.cwd_len)) || token_len >= 1 << (8 * sizeof(data.token_len)) || override_count > (size_t)(unsigned int)data.override_count || - argc > (int)(unsigned int)data.argc || + foot_argc > (int)(unsigned int)data.foot_argc || + argc > (int)(unsigned int)data.slave_argc || env_count > (size_t)(unsigned int)data.env_count) { LOG_ERR("size overflow"); @@ -518,6 +556,7 @@ main(int argc, char *const *argv) /* Send everything except argv[] */ if (sendall(fd, &(uint32_t){total_len}, sizeof(uint32_t)) < 0 || sendall(fd, &data, sizeof(data)) < 0 || + sendall(fd, exe, exe_len) < 0 || sendall(fd, cwd, cwd_len) < 0) { LOG_ERRNO("failed to send setup packet to server"); @@ -537,12 +576,22 @@ main(int argc, char *const *argv) if (!send_string_list(fd, &overrides)) goto err; - /* Send argv[] */ + /* Send foot_argv[] */ + for (size_t i = 0; i < foot_argc; i++) { + if (sendall(fd, &cfoot_argv[i], sizeof(cfoot_argv[i])) < 0 || + sendall(fd, foot_argv[i], cfoot_argv[i].len) < 0) + { + LOG_ERRNO("failed to send setup packet (foot argv) to server"); + goto err; + } + } + + /* Send slave_argv[] */ for (size_t i = 0; i < argc; i++) { - if (sendall(fd, &cargv[i], sizeof(cargv[i])) < 0 || - sendall(fd, argv[i], cargv[i].len) < 0) + if (sendall(fd, &cslave_argv[i], sizeof(cslave_argv[i])) < 0 || + sendall(fd, argv[i], cslave_argv[i].len) < 0) { - LOG_ERRNO("failed to send setup packet (argv) to server"); + LOG_ERRNO("failed to send setup packet (slave argv) to server"); goto err; } } @@ -619,7 +668,8 @@ main(int argc, char *const *argv) err: free_string_list(&envp); free_string_list(&overrides); - free(cargv); + free(cfoot_argv); + free(cslave_argv); free(_cwd); free(custom_cwd); if (fd != -1) @@ -108,7 +108,20 @@ print_usage(const char *prog_name) puts(options); } -bool +static const char * +get_foot_exe(void) +{ + /* Linux only, fallback to "foot" */ + static char lnk[PATH_MAX]; + ssize_t ret = readlink("/proc/self/exe", lnk, sizeof(lnk) - 1); + if (ret < 0) + return "foot"; + + lnk[ret] = '\0'; + return lnk; +} + +static bool locale_is_utf8(void) { static const char u8[] = u8"ö"; @@ -208,6 +221,12 @@ main(int argc, char *const *argv) * don't pass this on to programs launched by us */ unsetenv("DESKTOP_STARTUP_ID"); + /* Temporary "clean" copy of argv, for term_init() */ + const int foot_argc = argc; + const char *foot_argv[foot_argc]; + for (int i = 0; i < foot_argc; i++) + foot_argv[i] = argv[i]; + const char *const prog_name = argc > 0 ? argv[0] : "<nullptr>"; static const struct option longopts[] = { @@ -649,8 +668,8 @@ main(int argc, char *const *argv) goto out; if (!as_server && (term = term_init( - &conf, fdm, reaper, wayl, "foot", cwd, token, pty_path, - argc, argv, NULL, + &conf, fdm, reaper, wayl, get_foot_exe(), cwd, token, pty_path, + argc, argv, NULL, foot_argc, foot_argv, &term_shutdown_cb, &shutdown_ctx)) == NULL) { goto out; } @@ -146,7 +146,8 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) struct client *client = data; struct server *server = client->server; - char **argv = NULL; + char **foot_argv = NULL; + char **slave_argv = NULL; config_override_t overrides = tll_init(); char **envp = NULL; @@ -302,6 +303,10 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) memcpy(&cdata, p, sizeof(cdata)); p += sizeof(cdata); + CHECK_BUF_AND_NULL(cdata.exe_len); + const char *footclient_exe = (const char *)p; p += cdata.exe_len; + LOG_DBG("EXE = %.*s", cdata.exe_len, footclient_exe); + CHECK_BUF_AND_NULL(cdata.cwd_len); const char *cwd = (const char *)p; p += cdata.cwd_len; LOG_DBG("CWD = %.*s", cdata.cwd_len, cwd); @@ -330,16 +335,28 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) tll_push_back(overrides, xstrdup(str)); } - /* argv */ - argv = xcalloc(cdata.argc + 1, sizeof(argv[0])); - for (uint16_t i = 0; i < cdata.argc; i++) { + /* foot argv (for spawn-terminal) */ + foot_argv = xcalloc(cdata.foot_argc + 1, sizeof(foot_argv[0])); + for (uint16_t i = 0; i < cdata.foot_argc; i++) { + struct client_string arg; + CHECK_BUF(sizeof(arg)); + memcpy(&arg, p, sizeof(arg)); p += sizeof(arg); + + CHECK_BUF_AND_NULL(arg.len); + foot_argv[i] = (char *)p; p += arg.len; + LOG_DBG("foot argv[%hu] = %.*s", i, arg.len, foot_argv[i]); + } + + /* slave argv */ + slave_argv = xcalloc(cdata.slave_argc + 1, sizeof(slave_argv[0])); + for (uint16_t i = 0; i < cdata.slave_argc; i++) { struct client_string arg; CHECK_BUF(sizeof(arg)); memcpy(&arg, p, sizeof(arg)); p += sizeof(arg); CHECK_BUF_AND_NULL(arg.len); - argv[i] = (char *)p; p += arg.len; - LOG_DBG("argv[%hu] = %.*s", i, arg.len, argv[i]); + slave_argv[i] = (char *)p; p += arg.len; + LOG_DBG("slave argv[%hu] = %.*s", i, arg.len, slave_argv[i]); } /* envp */ @@ -390,9 +407,9 @@ 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, - NULL, cdata.argc, argv, (const char *const *)envp, - &term_shutdown_handler, instance); + server->fdm, server->reaper, server->wayl, footclient_exe, cwd, token, + NULL, cdata.slave_argc, slave_argv, (const char *const *)envp, + cdata.foot_argc, (const char **)foot_argv, &term_shutdown_handler, instance); if (instance->terminal == NULL) { LOG_ERR("failed to instantiate new terminal"); @@ -410,7 +427,8 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) // the instance is attached to the client instance->client = client; client->instance = instance; - free(argv); + free(foot_argv); + free(slave_argv); free(envp); tll_free_and_free(overrides, free); } @@ -420,7 +438,8 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) shutdown: LOG_DBG("client FD=%d: disconnected", client->fd); - free(argv); + free(foot_argv); + free(slave_argv); free(envp); tll_free_and_free(overrides, free); fdm_del(fdm, fd); @@ -1172,7 +1172,8 @@ struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, struct wayland *wayl, const char *foot_exe, const char *cwd, const char *token, const char *pty_path, - int argc, char *const *argv, const char *const *envp, + int slave_argc, char *const *slave_argv, const char *const *slave_envp, + int foot_argc, const char *const *foot_argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data) { int ptmx = -1; @@ -1412,8 +1413,13 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .ime_enabled = true, #endif .active_notifications = tll_init(), + .argc = foot_argc, + .argv = xcalloc(foot_argc, sizeof(char *)), }; + for (int i = 0; i < foot_argc; i++) + term->argv[i] = xstrdup(foot_argv[i]); + pixman_region32_init(&term->render.last_overlay_clip); term_update_ascii_printer(term); @@ -1437,8 +1443,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, if (!pty_path) { /* Start the slave/client */ if ((term->slave = slave_spawn( - term->ptmx, argc, term->cwd, argv, envp, &conf->env_vars, - conf->term, conf->shell, conf->login_shell, + term->ptmx, slave_argc, term->cwd, slave_argv, slave_envp, + &conf->env_vars, conf->term, conf->shell, conf->login_shell, &conf->notifications)) == -1) { goto err; @@ -1953,6 +1959,10 @@ term_destroy(struct terminal *term) free(term->mouse_user_cursor); free(term->color_stack.stack); + for (int i = 0; i < term->argc; i++) + free(term->argv[i]); + free(term->argv); + int ret = EXIT_SUCCESS; if (term->slave > 0) { @@ -3815,15 +3825,13 @@ term_bell(struct terminal *term) bool term_spawn_new(const struct terminal *term) { - char *argv[4]; - int argc = 0; + xassert(term->argc > 0); + char *argv[term->argc + 1]; - argv[argc++] = term->foot_exe; - if (term->conf->conf_path != NULL) { - argv[argc++] = "--config"; - argv[argc++] = term->conf->conf_path; - } - argv[argc] = NULL; + argv[0] = term->foot_exe; + for (int i = 1; i < term->argc; i++) + argv[i] = term->argv[i]; + argv[term->argc] = NULL; return spawn( term->reaper, term->cwd, argv, @@ -836,6 +836,10 @@ struct terminal { bool grapheme_shaping; bool size_notifications; + + /* For spawn-terminal */ + int argc; + char **argv; }; struct config; @@ -843,7 +847,8 @@ struct terminal *term_init( const struct config *conf, struct fdm *fdm, struct reaper *reaper, struct wayland *wayl, const char *foot_exe, const char *cwd, const char *token, const char *pty_path, - int argc, char *const *argv, const char *const *envp, + int slave_argc, char *const *slave_argv, const char *const *slave_envp, + int foot_argc, const char *const *foot_argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data); bool term_shutdown(struct terminal *term); |
