ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit ae8ca1598bd54db8eea053c04d2b11c294fe0afa
parent 8d187be64adf89859d9d13c57c41239f482baad5
Author: sin <sin@2f30.org>
Date:   Wed,  2 Jul 2014 11:45:10 +0100

Clear utmp entries for the specified tty in getty(8)

Diffstat:
Mgetty.c | 29+++++++++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/getty.c b/getty.c @@ -10,6 +10,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <utmp.h> #include "util.h" @@ -25,12 +26,15 @@ static char *defaultterm = "linux"; int main(int argc, char *argv[]) { - int fd; - struct sigaction sa; char term[128], logname[LOGIN_NAME_MAX], c; char hostname[HOST_NAME_MAX + 1]; + struct utmp usr; + struct sigaction sa; + FILE *fp; + int fd; unsigned int i = 0; ssize_t n; + long pos; ARGBEGIN { default: @@ -84,6 +88,27 @@ main(int argc, char *argv[]) if (fchmod(fd, 0600) < 0) eprintf("chmod %s:", tty); + /* Clear all utmp entries for this tty */ + fp = fopen("/var/run/utmp", "r+"); + if (fp) { + do { + pos = ftell(fp); + if (fread(&usr, sizeof(usr), 1, fp) != 1) + break; + if (usr.ut_line[0] == '\0') + continue; + if (strcmp(usr.ut_line, tty) != 0) + continue; + memset(&usr, 0, sizeof(usr)); + fseek(fp, pos, SEEK_SET); + if (fwrite(&usr, sizeof(usr), 1, fp) != 1) + break; + } while (1); + if (ferror(fp)) + weprintf("%s: I/O error:", "/var/run/utmp"); + fclose(fp); + } + if (argc > 2) return execvp(argv[2], argv + 2);