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 committed 2021-04-14 16:29:19 +02:00
1 parent 85a212ef93
commit c0368d2b16
6 files changed
+52 -37

No files matched your search

+4
View File
@@ -737,6 +737,10 @@ int conf_reload(void)
globfree(&gl); 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. */ /* Mark any reverse deps as chenaged. */
service_update_rdeps(); 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->noclear = tty.noclear;
svc->nowait = tty.nowait; svc->nowait = tty.nowait;
svc->nologin = tty.nologin; svc->nologin = tty.nologin;
svc->notty = tty.notty;
/* TTYs cannot be redirected */ /* TTYs cannot be redirected */
log = NULL; log = NULL;
+4
View File
@@ -431,6 +431,10 @@ svc_t *svc_find_by_tty(char *dev)
{ {
svc_t *svc, *iter = NULL; 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)) { for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) {
if (!svc_is_tty(svc)) if (!svc_is_tty(svc))
continue; continue;
+4 -3
View File
@@ -143,9 +143,10 @@ typedef struct svc {
char dev[32]; char dev[32];
char baud[10]; char baud[10];
char term[10]; char term[10];
int noclear; char noclear;
int nowait; char nowait;
int nologin; char nologin;
char notty;
}; };
}; };
+34 -31
View File
@@ -35,14 +35,11 @@
#include "finit.h" #include "finit.h"
#include "conf.h" #include "conf.h"
#include "helpers.h" #include "helpers.h"
#include "service.h"
#include "tty.h" #include "tty.h"
#include "util.h" #include "util.h"
#include "utmp-api.h" #include "utmp-api.h"
#ifdef FALLBACK_SHELL
static pid_t fallback = 0;
#endif
char *tty_canonicalize(char *dev) char *tty_canonicalize(char *dev)
{ {
static char path[80]; static char path[80];
@@ -134,12 +131,18 @@ int tty_parse_args(char *cmd, struct tty *tty)
tty->nowait = 1; tty->nowait = 1;
else if (!strcmp(cmd, "nologin")) else if (!strcmp(cmd, "nologin"))
tty->nologin = 1; tty->nologin = 1;
else if (!strcmp(cmd, "notty"))
tty->notty = 1; /* for fallback shell */
else else
tty->args[tty->num++] = cmd; tty->args[tty->num++] = cmd;
cmd = strtok(NULL, " \t"); cmd = strtok(NULL, " \t");
} while (cmd && tty->num < NELEMS(tty->args)); } 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 */ /* Iterate over all args */
for (i = 0; i < tty->num; i++) { for (i = 0; i < tty->num; i++) {
_d("Checking arg %s for dev and cmd ...", tty->args[i]); _d("Checking arg %s for dev and cmd ...", tty->args[i]);
@@ -214,6 +217,18 @@ int tty_exec(svc_t *tty)
char *dev; char *dev;
int rc; 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); dev = tty_canonicalize(tty->dev);
if (!dev) { if (!dev) {
_d("%s: Cannot find TTY device: %s", tty->dev, strerror(errno)); _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 * Fallback shell if no TTYs are active
*/ */
int tty_fallback(pid_t lost) int tty_fallback(char *file)
{ {
#ifdef FALLBACK_SHELL svc_t *svc, *iter = NULL;
if (lost == 1) { size_t num = 0;
if (fallback) {
kill(fallback, SIGKILL);
fallback = 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()) #ifdef FALLBACK_SHELL
return 0; char line[32] = "tty [12345789] notty noclear";
fallback = fork(); if (!num) {
if (fallback) _d("No TTY active in configuration, enabling fallback shell.");
return 1; return service_register(SVC_TYPE_TTY, line, global_rlimit, file);
}
#endif
/* return num == 0;
* 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;
} }
/** /**
+5 -3
View File
@@ -36,9 +36,10 @@ struct tty {
char *dev; char *dev;
char *baud; char *baud;
char *term; char *term;
int noclear; char noclear;
int nowait; char nowait;
int nologin; char nologin;
char notty;
}; };
char *tty_canonicalize (char *dev); 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_parse_args (char *cmdline, struct tty *tty);
int tty_exec (svc_t *tty); int tty_exec (svc_t *tty);
int tty_fallback (char *file);
#endif /* FINIT_TTY_H_ */ #endif /* FINIT_TTY_H_ */