From 6aebc60dd20d3528ad5eee0438a4e65f02adfae0 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Thu, 3 Apr 2008 17:34:34 -0400 Subject: Add a crude hack to wait for runlevels to finish. (#437379) --- event.d/serial | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/event.d/serial b/event.d/serial index 6c29af04..8751ef84 100644 --- a/event.d/serial +++ b/event.d/serial @@ -3,6 +3,18 @@ start on fedora.serial-console-available * stop on runlevel [016] instance +pre-start script + while /bin/true ; do + runlevel=$(/sbin/runlevel | /bin/awk '{ print $2 }') + case "$runlevel" in + 2|3|4|5) + LANG=C /sbin/initctl status rc$runlevel | grep -wq "rc$runlevel (stop) waiting" && break + ;; + *) + ;; + esac + sleep 1 + done exec /sbin/agetty /dev/$1 $2 vt100-nav post-stop script if [ "$UPSTART_EVENT" != "${UPSTART_EVENT##fedora.serial-console-available}" ]; then -- cgit v1.2.1 From 8107b70fe8168264cbf5b5fed32e41fcafd0e2df Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Thu, 3 Apr 2008 17:38:43 -0400 Subject: Add a 'securetty' command that frobs /etc/securetty. Call it from the serial event. (#437381) --- event.d/serial | 2 ++ initscripts.spec | 1 + src/Makefile | 6 +++- src/securetty.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/securetty.c diff --git a/event.d/serial b/event.d/serial index 8751ef84..80695338 100644 --- a/event.d/serial +++ b/event.d/serial @@ -15,6 +15,8 @@ pre-start script esac sleep 1 done + /sbin/securetty $1 +end script exec /sbin/agetty /dev/$1 $2 vt100-nav post-stop script if [ "$UPSTART_EVENT" != "${UPSTART_EVENT##fedora.serial-console-available}" ]; then diff --git a/initscripts.spec b/initscripts.spec index 08de49a8..1314cfdb 100644 --- a/initscripts.spec +++ b/initscripts.spec @@ -185,6 +185,7 @@ rm -rf $RPM_BUILD_ROOT /sbin/fstab-decode /sbin/genhostid /sbin/getkey +/sbin/securetty %attr(2755,root,root) /sbin/netreport /sbin/initlog /lib/udev/rename_device diff --git a/src/Makefile b/src/Makefile index 887e2373..b793300b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ CFLAGS+=$(RPM_OPT_FLAGS) -Wall -D_GNU_SOURCE PROGS=usernetctl doexec netreport testd usleep ipcalc initlog \ fstab-decode getkey ppp-watch consoletype genhostid rename_device \ - console_init console_check + console_init console_check securetty PPPWATCH_OBJS=ppp-watch.o shvar.o CONSOLE_INIT_OBJS=console_init.o shvar.o INITLOG_OBJS=initlog.o process.o @@ -28,6 +28,7 @@ install: install -m 755 getkey $(ROOT)/sbin/getkey install -m 755 ppp-watch $(ROOT)/sbin/ppp-watch install -m 755 consoletype $(ROOT)/sbin/consoletype + install -m 755 securetty $(ROOT)/sbin/securetty install -m 755 rename_device $(ROOT)/lib/udev/rename_device install -m 755 console_init $(ROOT)/lib/udev/console_init install -m 755 console_check $(ROOT)/lib/udev/console_check @@ -72,6 +73,9 @@ usernetctl.o: usernetctl.c usernetctl: usernetctl.c usernetctl.o $(CC) $(LDFLAGS) -pie -o $@ $@.o +securetty: securetty.o + $(CC) $(LDFLAGS) -o $@ $< + shvar.o: shvar.c $(CC) $(CFLAGS) `pkg-config glib-2.0 --cflags` -c shvar.c -o shvar.o diff --git a/src/securetty.c b/src/securetty.c new file mode 100644 index 00000000..f1505076 --- /dev/null +++ b/src/securetty.c @@ -0,0 +1,94 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +void alarm_handler(int num) { + return; +} + +int open_and_lock_securetty() { + int fd; + struct flock lock; + struct sigaction act, oldact; + + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 0; + + fd = open("/etc/securetty", O_RDWR); + if (fd == -1) { + syslog(LOG_ERR, "Couldn't open /etc/securetty: %s",strerror(errno)); + return -1; + } + act.sa_handler = alarm_handler; + act.sa_flags = 0; + sigaction(SIGALRM, &act, &oldact); + alarm(2); + while (fcntl(fd, F_SETLKW, &lock) == -1) { + if (errno == EINTR) { + syslog(LOG_ERR, "Couldn't lock /etc/securetty: Timeout exceeded"); + } else { + syslog(LOG_ERR, "Couldn't lock /etc/securetty: %s",strerror(errno)); + } + return -1; + } + alarm(0); + sigaction(SIGALRM, &oldact, NULL); + return fd; +} + +int rewrite_securetty(char *terminal) { + int fd; + char *buf, *pos; + struct stat sbuf; + + fd = open_and_lock_securetty(); + if (fd == -1) + return 1; + if (fstat(fd, &sbuf) == -1) { + close(fd); + syslog(LOG_ERR, "Couldn't stat /etc/securetty: %s",strerror(errno)); + return 1; + } + buf = malloc(sbuf.st_size + 1); + if (read(fd, buf, sbuf.st_size) != sbuf.st_size) { + close(fd); + syslog(LOG_ERR, "Couldn't read /etc/securetty: %s",strerror(errno)); + return 1; + } + if (!strncmp(buf,terminal,strlen(terminal)) && buf[strlen(terminal)] == '\n') + goto out_ok; + if ((pos = strstr(buf, terminal))) { + if (pos[strlen(terminal)] == '\n' && *(pos-1) == '\n') + goto out_ok; + } + if (lseek(fd, 0, SEEK_END) == -1) { + close(fd); + syslog(LOG_ERR, "Couldn't seek to end of /etc/securetty: %s",strerror(errno)); + return 1; + } + write(fd, terminal, strlen(terminal)); + write(fd, "\n", 1); +out_ok: + close(fd); + return 0; +} + +int main(int argc, char **argv) { + if (argc < 2 ) { + fprintf(stderr, "Usage: securetty \n"); + exit(1); + } + openlog("securetty", LOG_CONS, LOG_DAEMON); + return rewrite_securetty(argv[1]); +} -- cgit v1.2.1