Refactor fallback shell handling, run as regular service

This patch re-enables the fallback shell handling after the big TTY
refactor.  We do this by allowing the fallback shell to run as a
regular service.

Note: this also adds the "hidden" support for 'notty' option for
      tty configurations stanzas.  This is just to pick up from
      where the kernel left us, reusing stdin + stdout.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-04-14 16:29:19 +02:00
parent 85a212ef93
commit c0368d2b16
6 changed files with 52 additions and 37 deletions
+4
View File
@@ -737,6 +737,10 @@ int conf_reload(void)
globfree(&gl);
/* Verify config, do we have at least one TTY available? */
if (tty_fallback(FINIT_CONF))
logit(LOG_WARNING, "warning, no getty enabled on local TTYs.");
/* Mark any reverse deps as chenaged. */
service_update_rdeps();
+1
View File
@@ -1248,6 +1248,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
svc->noclear = tty.noclear;
svc->nowait = tty.nowait;
svc->nologin = tty.nologin;
svc->notty = tty.notty;
/* TTYs cannot be redirected */
log = NULL;
+4
View File
@@ -431,6 +431,10 @@ svc_t *svc_find_by_tty(char *dev)
{
svc_t *svc, *iter = NULL;
/* this is valid for fallback shells */
if (!dev)
return NULL;
for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) {
if (!svc_is_tty(svc))
continue;
+4 -3
View File
@@ -143,9 +143,10 @@ typedef struct svc {
char dev[32];
char baud[10];
char term[10];
int noclear;
int nowait;
int nologin;
char noclear;
char nowait;
char nologin;
char notty;
};
};
+34 -31
View File
@@ -35,14 +35,11 @@
#include "finit.h"
#include "conf.h"
#include "helpers.h"
#include "service.h"
#include "tty.h"
#include "util.h"
#include "utmp-api.h"
#ifdef FALLBACK_SHELL
static pid_t fallback = 0;
#endif
char *tty_canonicalize(char *dev)
{
static char path[80];
@@ -134,12 +131,18 @@ int tty_parse_args(char *cmd, struct tty *tty)
tty->nowait = 1;
else if (!strcmp(cmd, "nologin"))
tty->nologin = 1;
else if (!strcmp(cmd, "notty"))
tty->notty = 1; /* for fallback shell */
else
tty->args[tty->num++] = cmd;
cmd = strtok(NULL, " \t");
} while (cmd && tty->num < NELEMS(tty->args));
/* skip /dev probe, we just want a bríngup shell */
if (tty->notty)
return 0;
/* Iterate over all args */
for (i = 0; i < tty->num; i++) {
_d("Checking arg %s for dev and cmd ...", tty->args[i]);
@@ -214,6 +217,18 @@ int tty_exec(svc_t *tty)
char *dev;
int rc;
if (tty->notty) {
/*
* Become session leader and set controlling TTY
* to enable Ctrl-C and job control in shell.
*/
setsid();
ioctl(STDIN_FILENO, TIOCSCTTY, 1);
prctl(PR_SET_NAME, "finitsh", 0, 0, 0);
return execl(_PATH_BSHELL, _PATH_BSHELL, NULL);
}
dev = tty_canonicalize(tty->dev);
if (!dev) {
_d("%s: Cannot find TTY device: %s", tty->dev, strerror(errno));
@@ -243,39 +258,27 @@ int tty_exec(svc_t *tty)
/*
* Fallback shell if no TTYs are active
*/
int tty_fallback(pid_t lost)
int tty_fallback(char *file)
{
#ifdef FALLBACK_SHELL
if (lost == 1) {
if (fallback) {
kill(fallback, SIGKILL);
fallback = 0;
}
svc_t *svc, *iter = NULL;
size_t num = 0;
return 0;
for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) {
if (!svc_is_tty(svc) || svc_is_removed(svc))
continue;
num++;
}
if (fallback != lost || tty_num_active())
return 0;
#ifdef FALLBACK_SHELL
char line[32] = "tty [12345789] notty noclear";
fallback = fork();
if (fallback)
return 1;
if (!num) {
_d("No TTY active in configuration, enabling fallback shell.");
return service_register(SVC_TYPE_TTY, line, global_rlimit, file);
}
#endif
/*
* Become session leader and set controlling TTY
* to enable Ctrl-C and job control in shell.
*/
setsid();
ioctl(STDIN_FILENO, TIOCSCTTY, 1);
prctl(PR_SET_NAME, "finitsh", 0, 0, 0);
_exit(execl(_PATH_BSHELL, _PATH_BSHELL, NULL));
#else
(void)lost;
#endif /* FALLBACK_SHELL */
return 0;
return num == 0;
}
/**
+5 -3
View File
@@ -36,9 +36,10 @@ struct tty {
char *dev;
char *baud;
char *term;
int noclear;
int nowait;
int nologin;
char noclear;
char nowait;
char nologin;
char notty;
};
char *tty_canonicalize (char *dev);
@@ -49,6 +50,7 @@ int tty_atcon (char *buf, size_t len);
int tty_parse_args (char *cmdline, struct tty *tty);
int tty_exec (svc_t *tty);
int tty_fallback (char *file);
#endif /* FINIT_TTY_H_ */