From 679b4df881077e67f6106b57c64cdd5a1d55bf53 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 25 Apr 2021 22:10:42 +0200 Subject: [PATCH] Fallback rescue mode handling Before 4e5d06b the only way to go into rescue mode was to skip certain steps and then attempt to load /lib/finit/rescue.conf. After 4e5d06b we keep the old handling as a fallback in case early sulogin fails. The new tty option 'rescue' is added, which recue.conf is updated with. This option implies notty mode and will try sulogin (again) before it falls back to start /bin/sh as a login shell. The reason we keep this is to be able to handle all possible use-cases and also allow sysadmins to set up the behavior that fits their needs. Signed-off-by: Joachim Wiberg --- src/conf.c | 2 +- src/rescue.conf | 3 +-- src/service.c | 1 + src/svc.h | 1 + src/tty.c | 25 +++++++++++++++++-------- src/tty.h | 1 + 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/conf.c b/src/conf.c index 805732ea..861caf00 100644 --- a/src/conf.c +++ b/src/conf.c @@ -672,7 +672,7 @@ int conf_reload(void) if (rescue) { int rc; - char line[80] = "tty [12345] @console noclear nologin"; + char line[80] = "tty [12345] rescue"; /* If rescue.conf is missing, fall back to a root shell */ rc = parse_conf(RESCUE_CONF, 0); diff --git a/src/rescue.conf b/src/rescue.conf index 9769f55d..69231099 100644 --- a/src/rescue.conf +++ b/src/rescue.conf @@ -1,3 +1,2 @@ runlevel 1 -hostname rescue -tty [12345] @console noclear nologin +tty [12345] rescue diff --git a/src/service.c b/src/service.c index 7530d44b..81ca2c6d 100644 --- a/src/service.c +++ b/src/service.c @@ -1301,6 +1301,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) svc->nowait = tty.nowait; svc->nologin = tty.nologin; svc->notty = tty.notty; + svc->rescue = tty.rescue; /* TTYs cannot be redirected */ log = NULL; diff --git a/src/svc.h b/src/svc.h index 6c74f985..6a08c8c7 100644 --- a/src/svc.h +++ b/src/svc.h @@ -149,6 +149,7 @@ typedef struct svc { char nowait; char nologin; char notty; + char rescue; }; }; diff --git a/src/tty.c b/src/tty.c index 6497f1e9..af047ce7 100644 --- a/src/tty.c +++ b/src/tty.c @@ -158,7 +158,9 @@ int tty_parse_args(char *cmd, struct tty *tty) else if (!strcmp(cmd, "nologin")) tty->nologin = 1; else if (!strcmp(cmd, "notty")) - tty->notty = 1; /* for rescue shells */ + tty->notty = 1; /* for board bringup */ + else if (!strcmp(cmd, "rescue")) + tty->rescue = 1; /* for rescue shells */ else if (!access(cmd, X_OK)) tty->cmd = cmd; else @@ -167,6 +169,10 @@ int tty_parse_args(char *cmd, struct tty *tty) cmd = strtok(NULL, " \t"); } while (cmd && tty->num < NELEMS(tty->args)); + /* rescue shells are always notty */ + if (tty->rescue) + tty->notty = 1; + /* skip /dev probe, we just want a bríngup shell */ if (tty->notty) return 0; @@ -242,12 +248,15 @@ int tty_exec(svc_t *svc) /* try to protect system with sulogin, fall back to root shell */ if (svc->notty) { - /* util-linux or busybox, no args for compat */ - if (whichp("sulogin")) - return execlp("sulogin", "sulogin", NULL); - /* check if bundled one is available */ - if (whichp(_PATH_SULOGIN) && !systemf(_PATH_SULOGIN)) - _exit(0); + if (svc->rescue) { + /* check if bundled one is available */ + if (whichp(_PATH_SULOGIN)) + return execl(_PATH_SULOGIN, _PATH_SULOGIN, NULL); + + /* util-linux or busybox, no args for compat */ + if (whichp("sulogin")) + return execlp("sulogin", "sulogin", NULL); + } /* * Become session leader and set controlling TTY @@ -257,7 +266,7 @@ int tty_exec(svc_t *svc) ioctl(STDIN_FILENO, TIOCSCTTY, 1); prctl(PR_SET_NAME, "finitsh", 0, 0, 0); - return execl(_PATH_BSHELL, _PATH_BSHELL, NULL); + return execl(_PATH_BSHELL, "-sh", NULL); } dev = tty_canonicalize(svc->dev); diff --git a/src/tty.h b/src/tty.h index 8e543c32..37797d95 100644 --- a/src/tty.h +++ b/src/tty.h @@ -40,6 +40,7 @@ struct tty { char nowait; char nologin; char notty; + char rescue; }; char *tty_canonicalize (char *dev);