diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2015-12-28 20:11:37 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2015-12-28 20:11:37 +0000 |
commit | afcc85933b2602c23b384683139a5ed600cfa28f (patch) | |
tree | c6112faf4582c4822944219195b1903899d53bc5 | |
parent | ffeab0080e4e25e92151bef52dd5618b665eaf42 (diff) |
Switch login(3) from lseek+read/write to pread/pwrite and only do the pread()
if the data is needed. Use O_CLOEXEC on the internal fd as MT paranoia.
Fix cast in offset calculation; delete register keyword;
prefer memset() over bzero()
ok millert@
-rw-r--r-- | lib/libutil/login.c | 21 | ||||
-rw-r--r-- | lib/libutil/logout.c | 8 |
2 files changed, 15 insertions, 14 deletions
diff --git a/lib/libutil/login.c b/lib/libutil/login.c index 7d3eec4a025..95210578fdd 100644 --- a/lib/libutil/login.c +++ b/lib/libutil/login.c @@ -1,4 +1,4 @@ -/* $OpenBSD: login.c,v 1.10 2005/08/02 21:46:23 espie Exp $ */ +/* $OpenBSD: login.c,v 1.11 2015/12/28 20:11:36 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -43,28 +43,29 @@ void login(struct utmp *utp) { struct utmp old_ut; - register int fd; - int tty; + int fd, tty; + off_t pos; tty = ttyslot(); - if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) >= 0) { - (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); + if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT|O_CLOEXEC, 0644)) + >= 0) { /* * Prevent luser from zero'ing out ut_host. * If the new ut_line is empty but the old one is not * and ut_line and ut_name match, preserve the old ut_line. */ - if (read(fd, &old_ut, sizeof(struct utmp)) == - sizeof(struct utmp) && utp->ut_host[0] == '\0' && + pos = (off_t)tty * sizeof(struct utmp); + if (utp->ut_host[0] == '\0' && + pread(fd, &old_ut, sizeof(struct utmp), pos) == + sizeof(struct utmp) && old_ut.ut_host[0] != '\0' && strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 && strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0) (void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE); - (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); - (void)write(fd, utp, sizeof(struct utmp)); + (void)pwrite(fd, utp, sizeof(struct utmp), pos); (void)close(fd); } - if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) >= 0) { (void)write(fd, utp, sizeof(struct utmp)); (void)close(fd); } diff --git a/lib/libutil/logout.c b/lib/libutil/logout.c index 51116f1fa43..13c11fa863f 100644 --- a/lib/libutil/logout.c +++ b/lib/libutil/logout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: logout.c,v 1.8 2005/08/02 21:46:23 espie Exp $ */ +/* $OpenBSD: logout.c,v 1.9 2015/12/28 20:11:36 guenther Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -47,14 +47,14 @@ logout(const char *line) int fd, rval; UTMP ut; - if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0) + if ((fd = open(_PATH_UTMP, O_RDWR|O_CLOEXEC)) < 0) return(0); rval = 0; while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) { if (!ut.ut_name[0] || strncmp(ut.ut_line, line, UT_LINESIZE)) continue; - bzero(ut.ut_name, UT_NAMESIZE); - bzero(ut.ut_host, UT_HOSTSIZE); + memset(ut.ut_name, 0, UT_NAMESIZE); + memset(ut.ut_host, 0, UT_HOSTSIZE); (void)time(&ut.ut_time); (void)lseek(fd, -(off_t)sizeof(UTMP), SEEK_CUR); (void)write(fd, &ut, sizeof(UTMP)); |