summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ungol.org>2020-03-31 18:46:55 -0400
committerJakob Kaivo <jkk@ungol.org>2020-03-31 18:46:55 -0400
commit3093c4f7c3cb37179e3c4f2114091d0dec7c795d (patch)
treeeea6fc8784407b7c6f9f6ccec3dbb10c0e9d3d36
parent2cccc6ea7ff16cb4166582168edeebefd406ff85 (diff)
clean-up fallback code a bitHEADmaster
-rw-r--r--getty.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/getty.c b/getty.c
index e44f299..745eed9 100644
--- a/getty.c
+++ b/getty.c
@@ -32,14 +32,9 @@
#ifdef __linux__
#include <sys/ioctl.h>
-#define DEFAULT_GETTY_COMMAND "/bin/login"
#define DEFAULT_GETTY_TYPE "vt100"
#endif
-#ifndef DEFAULT_GETTY_COMMAND
-#define DEFAULT_GETTY_COMMAND "/sys/bin/login"
-#endif
-
#ifndef DEFAULT_GETTY_DEVICE
#define DEFAULT_GETTY_DEVICE "/dev/console"
#endif
@@ -48,10 +43,15 @@
#define DEFAULT_GETTY_TYPE "ecma-48"
#endif
+static const char *fallbacks[] = {
+ "/sys/bin/login",
+ "/bin/login",
+};
+static const size_t nfallbacks = sizeof(fallbacks) / sizeof(fallbacks[0]);
+
int main(int argc, char *argv[])
{
-
- char *command = DEFAULT_GETTY_COMMAND;
+ char *command = fallbacks[0];
char *device = DEFAULT_GETTY_DEVICE;
char *type = DEFAULT_GETTY_TYPE;
@@ -83,20 +83,15 @@ int main(int argc, char *argv[])
setsid();
int fd = open(device, O_RDWR | O_CLOEXEC | O_TTY_INIT, 0600);
- if (fd == -1) {
- fprintf(stderr, "getty: %s: %s\n", device, strerror(errno));
- return 1;
- }
-
- if (!isatty(fd)) {
+ if (fd == -1 || !isatty(fd)) {
fprintf(stderr, "getty: %s: %s\n", device, strerror(errno));
return 1;
}
-
#ifdef __linux__
if (ioctl (fd, TIOCSCTTY, (void *) 1) == -1) {
- perror("ioctl");
+ perror("getty: ioctl");
+ return 1;
}
#endif
@@ -107,6 +102,14 @@ int main(int argc, char *argv[])
setenv("TERM", type, 1);
/* TODO: wordexp() */
char *args[] = { command, NULL };
+
execv(args[0], args);
+ if (errno == ENOENT) {
+ for (size_t i = 0; i < nfallbacks; i++) {
+ char *fallback[] = { fallbacks[i], NULL };
+ execv(fallback[0], fallback);
+ }
+ }
+
return 1;
}