chiark / gitweb /
Modernization
[elogind.git] / src / shared / utmp-wtmp.c
index 046fb584fb08cb18506f89ffc1d0a68eca03f313..3494b569081ccd0e4ae5fd17123abc2e0d7752a6 100644 (file)
@@ -77,15 +77,11 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
                 a = found->ut_pid & 0xFF;
                 b = (found->ut_pid >> 8) & 0xFF;
 
-                if (a < 0 || b < 0)
-                        r = -EIO;
-                else {
-                        *runlevel = a;
+                *runlevel = a;
+                if (previous)
+                        *previous = b;
 
-                        if (previous)
-                                *previous = b;
-                        r = 0;
-                }
+                r = 0;
         }
 
         endutxent();
@@ -296,7 +292,7 @@ int utmp_put_runlevel(int runlevel, int previous) {
 #define TIMEOUT_MSEC 50
 
 static int write_to_terminal(const char *tty, const char *message) {
-        int fd, r;
+        int _cleanup_close_ fd = -1;
         const char *p;
         size_t left;
         usec_t end;
@@ -304,14 +300,10 @@ static int write_to_terminal(const char *tty, const char *message) {
         assert(tty);
         assert(message);
 
-        if ((fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC)) < 0)
+        fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0 || !isatty(fd))
                 return -errno;
 
-        if (!isatty(fd)) {
-                r = -errno;
-                goto finish;
-        }
-
         p = message;
         left = strlen(message);
 
@@ -325,30 +317,26 @@ static int write_to_terminal(const char *tty, const char *message) {
 
                 t = now(CLOCK_MONOTONIC);
 
-                if (t >= end) {
-                        r = -ETIME;
-                        goto finish;
-                }
+                if (t >= end)
+                        return -ETIME;
 
                 zero(pollfd);
                 pollfd.fd = fd;
                 pollfd.events = POLLOUT;
 
-                if ((k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC)) < 0)
+                k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
+                if (k < 0)
                         return -errno;
 
-                if (k <= 0) {
-                        r = -ETIME;
-                        goto finish;
-                }
-
-                if ((n = write(fd, p, left)) < 0) {
+                if (k == 0)
+                        return -ETIME;
 
+                n = write(fd, p, left);
+                if (n < 0) {
                         if (errno == EAGAIN)
                                 continue;
 
-                        r = -errno;
-                        goto finish;
+                        return -errno;
                 }
 
                 assert((size_t) n <= left);
@@ -357,12 +345,7 @@ static int write_to_terminal(const char *tty, const char *message) {
                 left -= n;
         }
 
-        r = 0;
-
-finish:
-        close_nointr_nofail(fd);
-
-        return r;
+        return 0;
 }
 
 int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
@@ -403,10 +386,12 @@ int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
                 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
                         continue;
 
+                /* this access is fine, because strlen("/dev/") << 32 (UT_LINESIZE) */
                 if (path_startswith(u->ut_line, "/dev/"))
                         path = u->ut_line;
                 else {
-                        if (asprintf(&buf, "/dev/%s", u->ut_line) < 0) {
+                        if (asprintf(&buf, "/dev/%.*s",
+                                     (int) sizeof(u->ut_line), u->ut_line) < 0) {
                                 r = -ENOMEM;
                                 goto finish;
                         }