slave: prefix argv[0] with a '-' when spawning a login-shell

This commit is contained in:
Daniel Eklöf 2020-02-20 18:36:09 +01:00
parent 6af4b87ec5
commit b5efe984bb
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
7 changed files with 44 additions and 13 deletions

View file

@ -161,6 +161,7 @@ main(int argc, char *const *argv)
/* Calculate total length */
total_len += sizeof(cwd_len) + cwd_len;
total_len += sizeof(term_len) + term_len;
total_len += sizeof(uint8_t); /* login_shell */
total_len += sizeof(argc);
for (int i = 0; i < argc; i++) {
@ -190,6 +191,11 @@ main(int argc, char *const *argv)
goto err;
}
if (send(fd, &(uint8_t){login_shell}, sizeof(uint8_t), 0) != sizeof(uint8_t)) {
LOG_ERRNO("failed to send login-shell");
goto err;
}
LOG_DBG("argc = %d", argc);
if (send(fd, &argc, sizeof(argc), 0) != sizeof(argc)) {
LOG_ERRNO("failed to send argc/argv to server");

3
main.c
View file

@ -332,7 +332,8 @@ main(int argc, char *const *argv)
goto out;
if (!as_server && (term = term_init(
&conf, fdm, wayl, conf.term, "foot", cwd, argc, argv,
&conf, fdm, wayl, conf.term, conf.login_shell,
"foot", cwd, argc, argv,
&term_shutdown_cb, &shutdown_ctx)) == NULL) {
free(cwd);
goto out;

View file

@ -213,6 +213,9 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
goto shutdown;
}
CHECK_BUF(sizeof(uint8_t));
const uint8_t login_shell = *(const uint8_t *)p; p += sizeof(login_shell);
CHECK_BUF(sizeof(argc));
argc = *(int *)p; p += sizeof(argc);
argv = calloc(argc + 1, sizeof(argv[0]));
@ -238,7 +241,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data)
client->term = term_init(
server->conf, server->fdm, server->wayl,
strlen(term_env) > 0 ? term_env : server->conf->term,
strlen(term_env) > 0 ? term_env : server->conf->term, login_shell,
"footclient", cwd, argc, argv, &term_shutdown_handler, client);
if (client->term == NULL) {

31
slave.c
View file

@ -17,7 +17,7 @@
#include "tokenize.h"
static void
slave_exec(int ptmx, char *const argv[], int err_fd)
slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell)
{
int pts = -1;
const char *pts_name = ptsname(ptmx);
@ -56,7 +56,20 @@ slave_exec(int ptmx, char *const argv[], int err_fd)
close(pts);
pts = -1;
execvp(argv[0], argv);
const char *file;
if (login_shell) {
file = strdup(argv[0]);
char *arg0 = malloc(strlen(argv[0]) + 1 + 1);
arg0[0] = '-';
arg0[1] = '\0';
strcat(arg0, argv[0]);
argv[0] = arg0;
} else
file = argv[0];
execvp(file, argv);
err:
(void)!write(err_fd, &errno, sizeof(errno));
@ -70,7 +83,7 @@ err:
pid_t
slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
const char *term_env, const char *conf_shell)
const char *term_env, const char *conf_shell, bool login_shell)
{
int fork_pipe[2];
if (pipe2(fork_pipe, O_CLOEXEC) < 0) {
@ -107,7 +120,7 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
setenv("TERM", term_env, 1);
char **_shell_argv = NULL;
char *const *shell_argv = argv;
char **shell_argv = NULL;
if (argc == 0) {
char *shell_copy = strdup(conf_shell);
@ -117,9 +130,17 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
_exit(0);
}
shell_argv = _shell_argv;
} else {
size_t count = 0;
for (; argv[count] != NULL; count++)
;
shell_argv = malloc((count + 1) * sizeof(shell_argv[0]));
for (size_t i = 0; i < count; i++)
shell_argv[i] = argv[i];
shell_argv[count] = NULL;
}
slave_exec(ptmx, shell_argv, fork_pipe[1]);
slave_exec(ptmx, shell_argv, fork_pipe[1], login_shell);
assert(false);
break;

View file

@ -5,4 +5,4 @@
pid_t slave_spawn(
int ptmx, int argc, const char *cwd, char *const *argv, const char *term_env,
const char *conf_shell);
const char *conf_shell, bool login_shell);

View file

@ -589,8 +589,8 @@ load_fonts_from_conf(const struct terminal *term, const struct config *conf,
struct terminal *
term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl,
const char *term_env, const char *foot_exe, const char *cwd,
int argc, char *const *argv,
const char *term_env, bool login_shell, const char *foot_exe,
const char *cwd, int argc, char *const *argv,
void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data)
{
int ptmx = -1;
@ -777,7 +777,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl,
term_font_dpi_changed(term);
/* Start the slave/client */
if ((term->slave = slave_spawn(term->ptmx, argc, term->cwd, argv, term_env, conf->shell)) == -1)
if ((term->slave = slave_spawn(term->ptmx, argc, term->cwd, argv, term_env, conf->shell, login_shell)) == -1)
goto err;
if (term->width == 0 && term->height == 0) {

View file

@ -356,8 +356,8 @@ struct terminal {
struct config;
struct terminal *term_init(
const struct config *conf, struct fdm *fdm, struct wayland *wayl,
const char *term_env, const char *foot_exe, const char *cwd,
int argc, char *const *argv,
const char *term_env, bool login_shell, const char *foot_exe,
const char *cwd, int argc, char *const *argv,
void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data);
bool term_shutdown(struct terminal *term);