diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/ask-password-api.c | 9 | ||||
-rw-r--r-- | src/shared/dbus-loop.c | 19 | ||||
-rw-r--r-- | src/shared/util.c | 21 | ||||
-rw-r--r-- | src/shared/utmp-wtmp.c | 39 |
4 files changed, 35 insertions, 53 deletions
diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 8a0fb89a84..d091a22dea 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -255,14 +255,16 @@ static int create_socket(char **name) { assert(name); - if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) { + fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (fd < 0) { log_error("socket() failed: %m"); return -errno; } zero(sa); sa.un.sun_family = AF_UNIX; - snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull()); + snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, + "/run/systemd/ask-password/sck.%llu", random_ull()); u = umask(0177); r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); @@ -280,7 +282,8 @@ static int create_socket(char **name) { goto fail; } - if (!(c = strdup(sa.un.sun_path))) { + c = strdup(sa.un.sun_path); + if (!c) { r = log_oom(); goto fail; } diff --git a/src/shared/dbus-loop.c b/src/shared/dbus-loop.c index da0a00443a..fec8998bc1 100644 --- a/src/shared/dbus-loop.c +++ b/src/shared/dbus-loop.c @@ -44,7 +44,7 @@ typedef struct EpollData { } EpollData; static dbus_bool_t add_watch(DBusWatch *watch, void *data) { - EpollData *e; + EpollData _cleanup_free_ *e = NULL; struct epoll_event ev; assert(watch); @@ -63,10 +63,8 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) { if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) { - if (errno != EEXIST) { - free(e); + if (errno != EEXIST) return FALSE; - } /* Hmm, bloody D-Bus creates multiple watches on the * same fd. epoll() does not like that. As a dirty @@ -74,14 +72,11 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) { * one we can safely add to the epoll(). */ e->fd = dup(e->fd); - if (e->fd < 0) { - free(e); + if (e->fd < 0) return FALSE; - } if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) { close_nointr_nofail(e->fd); - free(e); return FALSE; } @@ -89,12 +84,13 @@ static dbus_bool_t add_watch(DBusWatch *watch, void *data) { } dbus_watch_set_data(watch, e, NULL); + e = NULL; /* prevent freeing */ return TRUE; } static void remove_watch(DBusWatch *watch, void *data) { - EpollData *e; + EpollData _cleanup_free_ *e = NULL; assert(watch); @@ -106,8 +102,6 @@ static void remove_watch(DBusWatch *watch, void *data) { if (e->fd_is_dupped) close_nointr_nofail(e->fd); - - free(e); } static void toggle_watch(DBusWatch *watch, void *data) { @@ -186,7 +180,7 @@ fail: } static void remove_timeout(DBusTimeout *timeout, void *data) { - EpollData *e; + EpollData _cleanup_free_ *e = NULL; assert(timeout); @@ -196,7 +190,6 @@ static void remove_timeout(DBusTimeout *timeout, void *data) { assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0); close_nointr_nofail(e->fd); - free(e); } static void toggle_timeout(DBusTimeout *timeout, void *data) { diff --git a/src/shared/util.c b/src/shared/util.c index 760013c1fb..b516b9b053 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1873,18 +1873,18 @@ int flush_fd(int fd) { ssize_t l; int r; - if ((r = poll(&pollfd, 1, 0)) < 0) { - + r = poll(&pollfd, 1, 0); + if (r < 0) { if (errno == EINTR) continue; return -errno; - } - if (r == 0) + } else if (r == 0) return 0; - if ((l = read(fd, buf, sizeof(buf))) < 0) { + l = read(fd, buf, sizeof(buf)); + if (l < 0) { if (errno == EINTR) continue; @@ -1893,9 +1893,7 @@ int flush_fd(int fd) { return 0; return -errno; - } - - if (l <= 0) + } else if (l == 0) return 0; } } @@ -2068,10 +2066,12 @@ fail: } int release_terminal(void) { - int r = 0, fd; + int r = 0; struct sigaction sa_old, sa_new; + int _cleanup_close_ fd; - if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0) + fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC); + if (fd < 0) return -errno; /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed @@ -2087,7 +2087,6 @@ int release_terminal(void) { assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); - close_nointr_nofail(fd); return r; } diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index c9b986fc08..3494b56908 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)) { |