]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: avoid lseek() with pread() and pwrite()
authorSami Kerola <kerolasa@iki.fi>
Sun, 22 Mar 2020 20:54:00 +0000 (20:54 +0000)
committerSami Kerola <kerolasa@iki.fi>
Sun, 29 Mar 2020 19:19:12 +0000 (20:19 +0100)
This makes code a little less complicated by avoiding couple system calls.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
login-utils/login.c

index 545aa52ed1e548f4eca28c5f33433fc9bca5f2e0..457bd980a3ec4f54cdf92c1900d15b3ff86f29c5 100644 (file)
@@ -498,6 +498,7 @@ static void log_lastlog(struct login_context *cxt)
 {
        struct sigaction sa, oldsa_xfsz;
        struct lastlog ll;
+       off_t offset;
        time_t t;
        int fd;
 
@@ -515,16 +516,14 @@ static void log_lastlog(struct login_context *cxt)
        fd = open(_PATH_LASTLOG, O_RDWR, 0);
        if (fd < 0)
                goto done;
-
-       if (lseek(fd, (off_t) cxt->pwd->pw_uid * sizeof(ll), SEEK_SET) == -1)
-               goto done;
+       offset = cxt->pwd->pw_uid * sizeof(ll);
 
        /*
         * Print last log message.
         */
        if (!cxt->quiet) {
-               if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
-                                                       ll.ll_time != 0) {
+               if ((pread(fd, (void *)&ll, sizeof(ll), offset) == sizeof(ll)) &&
+                   ll.ll_time != 0) {
                        char time_string[CTIME_BUFSIZ];
 
                        time_t ll_time = (time_t) ll.ll_time;
@@ -538,8 +537,6 @@ static void log_lastlog(struct login_context *cxt)
                                printf(_("on %.*s\n"),
                                       (int)sizeof(ll.ll_line), ll.ll_line);
                }
-               if (lseek(fd, (off_t) cxt->pwd->pw_uid * sizeof(ll), SEEK_SET) == -1)
-                       goto done;
        }
 
        memset((char *)&ll, 0, sizeof(ll));
@@ -552,7 +549,7 @@ static void log_lastlog(struct login_context *cxt)
        if (cxt->hostname)
                str2memcpy(ll.ll_host, cxt->hostname, sizeof(ll.ll_host));
 
-       if (write_all(fd, (char *)&ll, sizeof(ll)))
+       if (pwrite(fd, (void *)&ll, sizeof(ll), offset) != sizeof(ll))
                warn(_("write lastlog failed"));
 done:
        if (fd >= 0)