X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futmp-wtmp.c;h=3494b569081ccd0e4ae5fd17123abc2e0d7752a6;hb=d1148ed10a474ccc949113a8ec06e7e29c4c7cb0;hp=c9b986fc08480f77696685050a2b5277fcf1539f;hpb=4dd1de72e8e7ece77e8831e77eea650de404af75;p=elogind.git diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index c9b986fc0..3494b5690 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -292,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; @@ -300,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); @@ -321,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); @@ -353,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)) {