]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: use _cleanup_close_ where appropriate
authorLennart Poettering <lennart@poettering.net>
Wed, 12 May 2021 11:47:45 +0000 (13:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 May 2021 15:12:01 +0000 (17:12 +0200)
src/basic/terminal-util.c

index 9bc6bb2024f8f73115fa7ba67b453d8e03c38e89..1b3e551204ea6d80fea5601040b64fb1e4bf5180 100644 (file)
@@ -316,14 +316,13 @@ int reset_terminal(const char *name) {
 }
 
 int open_terminal(const char *name, int mode) {
+        _cleanup_close_ int fd = -1;
         unsigned c = 0;
-        int fd;
 
         /*
-         * If a TTY is in the process of being closed opening it might
-         * cause EIO. This is horribly awful, but unlikely to be
-         * changed in the kernel. Hence we work around this problem by
-         * retrying a couple of times.
+         * If a TTY is in the process of being closed opening it might cause EIO. This is horribly awful, but
+         * unlikely to be changed in the kernel. Hence we work around this problem by retrying a couple of
+         * times.
          *
          * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
          */
@@ -343,16 +342,14 @@ int open_terminal(const char *name, int mode) {
                 if (c >= 20)
                         return -errno;
 
-                usleep(50 * USEC_PER_MSEC);
+                (void) usleep(50 * USEC_PER_MSEC);
                 c++;
         }
 
-        if (isatty(fd) <= 0) {
-                safe_close(fd);
-                return -ENOTTY;
-        }
+        if (isatty(fd) < 1)
+                return negative_errno();
 
-        return fd;
+        return TAKE_FD(fd);
 }
 
 int acquire_terminal(