diff --git a/src/conf.c b/src/conf.c index 805732ea..df6528ca 100644 --- a/src/conf.c +++ b/src/conf.c @@ -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(); diff --git a/src/service.c b/src/service.c index 6d88a90c..bd132a6d 100644 --- a/src/service.c +++ b/src/service.c @@ -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; diff --git a/src/svc.c b/src/svc.c index f6d98a17..8f3df45e 100644 --- a/src/svc.c +++ b/src/svc.c @@ -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; diff --git a/src/svc.h b/src/svc.h index a09f0905..52009c5e 100644 --- a/src/svc.h +++ b/src/svc.h @@ -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; }; }; diff --git a/src/tty.c b/src/tty.c index 7c0cf015..e320a1d9 100644 --- a/src/tty.c +++ b/src/tty.c @@ -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; } /** diff --git a/src/tty.h b/src/tty.h index 15cde538..a8a151c5 100644 --- a/src/tty.h +++ b/src/tty.h @@ -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_ */