From e62d8c3944745ed276e6d4f33153009860e5cfc5 Mon Sep 17 00:00:00 2001 From: Zbigniew JÄ™drzejewski-Szmek Date: Sun, 24 Mar 2013 19:45:16 -0400 Subject: Modernization Use _cleanup_ and wrap lines to ~80 chars and such. --- src/core/dbus-execute.c | 3 ++- src/core/execute.c | 38 ++++++++++++++++++++++---------------- src/core/loopback-setup.c | 28 +++++++++++----------------- src/core/main.c | 22 +++++++++++++++------- src/core/manager.c | 22 +++++++++++++--------- src/core/mount.c | 6 ++++-- src/core/umount.c | 8 ++++---- src/readahead/readahead-collect.c | 10 +++++++--- src/shared/ask-password-api.c | 9 ++++++--- src/shared/dbus-loop.c | 19 ++++++------------- src/shared/util.c | 21 ++++++++++----------- src/shared/utmp-wtmp.c | 39 +++++++++++++-------------------------- src/systemctl/systemctl.c | 2 +- src/vconsole/vconsole-setup.c | 4 +--- 14 files changed, 115 insertions(+), 116 deletions(-) diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 53394c25f4..0a53207e59 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -161,11 +161,12 @@ int bus_execute_append_cpu_sched_priority(DBusMessageIter *i, const char *proper n = c->cpu_sched_priority; else { struct sched_param p; - n = 0; zero(p); if (sched_getparam(0, &p) >= 0) n = p.sched_priority; + else + n = 0; } if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, &n)) diff --git a/src/core/execute.c b/src/core/execute.c index 85edca17e3..f7353579e9 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -1215,8 +1215,12 @@ int exec_spawn(ExecCommand *command, zero(param); param.sched_priority = context->cpu_sched_priority; - if (sched_setscheduler(0, context->cpu_sched_policy | - (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { + r = sched_setscheduler(0, + context->cpu_sched_policy | + (context->cpu_sched_reset_on_fork ? + SCHED_RESET_ON_FORK : 0), + ¶m); + if (r < 0) { err = -errno; r = EXIT_SETSCHEDULER; goto fail_child; @@ -1437,7 +1441,8 @@ int exec_spawn(ExecCommand *command, } } - if (!(our_env = new0(char*, 7))) { + our_env = new0(char*, 7); + if (!our_env) { err = -ENOMEM; r = EXIT_MEMORY; goto fail_child; @@ -1477,20 +1482,21 @@ int exec_spawn(ExecCommand *command, assert(n_env <= 7); - if (!(final_env = strv_env_merge( - 5, - environment, - our_env, - context->environment, - files_env, - pam_env, - NULL))) { + final_env = strv_env_merge(5, + environment, + our_env, + context->environment, + files_env, + pam_env, + NULL); + if (!final_env) { err = -ENOMEM; r = EXIT_MEMORY; goto fail_child; } - if (!(final_argv = replace_env_argv(argv, final_env))) { + final_argv = replace_env_argv(argv, final_env); + if (!final_argv) { err = -ENOMEM; r = EXIT_MEMORY; goto fail_child; @@ -1519,10 +1525,10 @@ int exec_spawn(ExecCommand *command, } log_struct_unit(LOG_DEBUG, - unit_id, - "MESSAGE=Forked %s as %lu", - command->path, (unsigned long) pid, - NULL); + unit_id, + "MESSAGE=Forked %s as %lu", + command->path, (unsigned long) pid, + NULL); /* We add the new process to the cgroup both in the child (so * that we can be sure that no user code is ever executed diff --git a/src/core/loopback-setup.c b/src/core/loopback-setup.c index 065b75a6e3..bfd0d93204 100644 --- a/src/core/loopback-setup.c +++ b/src/core/loopback-setup.c @@ -229,7 +229,8 @@ static int read_response(int fd, unsigned requests_max) { } static int check_loopback(void) { - int r, fd; + int r; + int _cleanup_close_ fd; union { struct sockaddr sa; struct sockaddr_in in; @@ -251,8 +252,6 @@ static int check_loopback(void) { else r = errno == EADDRNOTAVAIL ? 0 : -errno; - close_nointr_nofail(fd); - return r; } @@ -263,7 +262,7 @@ int loopback_setup(void) { struct sockaddr_nl nl; } sa; unsigned requests = 0, i; - int fd; + int _cleanup_close_ fd = -1; bool eperm = false; errno = 0; @@ -279,16 +278,16 @@ int loopback_setup(void) { sa.nl.nl_family = AF_NETLINK; if (bind(fd, &sa.sa, sizeof(sa)) < 0) { r = -errno; - goto finish; + goto error; } r = add_adresses(fd, if_loopback, &requests); if (r < 0) - goto finish; + goto error; r = start_interface(fd, if_loopback, &requests); if (r < 0) - goto finish; + goto error; for (i = 0; i < requests; i++) { r = read_response(fd, requests); @@ -296,22 +295,17 @@ int loopback_setup(void) { if (r == -EPERM) eperm = true; else if (r < 0) - goto finish; + goto error; } if (eperm && check_loopback() < 0) { r = -EPERM; - goto finish; + goto error; } - r = 0; - -finish: - if (r < 0) - log_warning("Failed to configure loopback device: %s", strerror(-r)); - - if (fd >= 0) - close_nointr_nofail(fd); + return 0; +error: + log_warning("Failed to configure loopback device: %s", strerror(-r)); return r; } diff --git a/src/core/main.c b/src/core/main.c index 25f55fced2..bd7fc468b4 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -116,8 +116,10 @@ _noreturn_ static void crash(int sig) { sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); - if ((pid = fork()) < 0) - log_error("Caught <%s>, cannot fork for core dump: %s", signal_to_string(sig), strerror(errno)); + pid = fork(); + if (pid < 0) + log_error("Caught <%s>, cannot fork for core dump: %s", + signal_to_string(sig), strerror(errno)); else if (pid == 0) { struct rlimit rl; @@ -147,12 +149,17 @@ _noreturn_ static void crash(int sig) { int r; /* Order things nicely. */ - if ((r = wait_for_terminate(pid, &status)) < 0) - log_error("Caught <%s>, waitpid() failed: %s", signal_to_string(sig), strerror(-r)); + r = wait_for_terminate(pid, &status); + if (r < 0) + log_error("Caught <%s>, waitpid() failed: %s", + signal_to_string(sig), strerror(-r)); else if (status.si_code != CLD_DUMPED) - log_error("Caught <%s>, core dump failed.", signal_to_string(sig)); + log_error("Caught <%s>, core dump failed.", + signal_to_string(sig)); else - log_error("Caught <%s>, dumped core as pid %lu.", signal_to_string(sig), (unsigned long) pid); + log_error("Caught <%s>, dumped core as pid %lu.", + signal_to_string(sig), + (unsigned long) pid); } } @@ -183,7 +190,8 @@ _noreturn_ static void crash(int sig) { _exit(1); } - log_info("Successfully spawned crash shell as pid %lu.", (unsigned long) pid); + log_info("Successfully spawned crash shell as pid %lu.", + (unsigned long) pid); } log_info("Freezing execution."); diff --git a/src/core/manager.c b/src/core/manager.c index a01710f445..8f4eb0b9c5 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -95,7 +95,7 @@ static int manager_setup_notify(Manager *m) { struct sockaddr_un un; } sa; struct epoll_event ev; - int one = 1; + int one = 1, r; assert(m); @@ -116,12 +116,15 @@ static int manager_setup_notify(Manager *m) { sa.un.sun_path[0] = 0; - if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { + r = bind(m->notify_watch.fd, &sa.sa, + offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)); + if (r < 0) { log_error("bind() failed: %m"); return -errno; } - if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) { + r = setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); + if (r < 0) { log_error("SO_PASSCRED failed: %m"); return -errno; } @@ -130,7 +133,8 @@ static int manager_setup_notify(Manager *m) { ev.events = EPOLLIN; ev.data.ptr = &m->notify_watch; - if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0) { + r = epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev); + if (r < 0) { log_error("Failed to add notification socket fd to epoll: %m"); return -errno; } @@ -1188,7 +1192,7 @@ static int manager_process_notify_fd(Manager *m) { uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; } control; Unit *u; - char **tags; + char _cleanup_strv_free_ **tags = NULL; zero(iovec); iovec.iov_base = buf; @@ -1226,7 +1230,8 @@ static int manager_process_notify_fd(Manager *m) { if (!u) { u = cgroup_unit_by_pid(m, ucred->pid); if (!u) { - log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid); + log_warning("Cannot find unit for notify message of PID %lu.", + (unsigned long) ucred->pid); continue; } } @@ -1241,8 +1246,6 @@ static int manager_process_notify_fd(Manager *m) { if (UNIT_VTABLE(u)->notify_message) UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags); - - strv_free(tags); } return 0; @@ -1898,7 +1901,8 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { /* We set SOCK_NONBLOCK here so that we rather drop the * message then wait for plymouth */ - if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) { + fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (fd < 0) { log_error("socket() failed: %m"); return; } diff --git a/src/core/mount.c b/src/core/mount.c index 7839300110..130dc39795 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -1710,7 +1710,8 @@ static int mount_enumerate(Manager *m) { assert(m); if (!m->proc_self_mountinfo) { - if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"))) + m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); + if (!m->proc_self_mountinfo) return -errno; m->mount_watch.type = WATCH_MOUNT; @@ -1724,7 +1725,8 @@ static int mount_enumerate(Manager *m) { return -errno; } - if ((r = mount_load_proc_self_mountinfo(m, false)) < 0) + r = mount_load_proc_self_mountinfo(m, false); + if (r < 0) goto fail; return 0; diff --git a/src/core/umount.c b/src/core/umount.c index 3fcb90eca4..95e47e3c5a 100644 --- a/src/core/umount.c +++ b/src/core/umount.c @@ -379,12 +379,14 @@ static int delete_loopback(const char *device) { } static int delete_dm(dev_t devnum) { - int fd, r; + int _cleanup_close_ fd = -1; + int r; struct dm_ioctl dm; assert(major(devnum) != 0); - if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0) + fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); + if (fd < 0) return -errno; zero(dm); @@ -396,8 +398,6 @@ static int delete_dm(dev_t devnum) { dm.dev = devnum; r = ioctl(fd, DM_DEV_REMOVE, &dm); - close_nointr_nofail(fd); - return r >= 0 ? 0 : -errno; } diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index e2fd8dfd05..643a5a8cea 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -284,13 +284,16 @@ static int collect(const char *root) { goto finish; } - if (!(files = hashmap_new(string_hash_func, string_compare_func))) { + files = hashmap_new(string_hash_func, string_compare_func); + if (!files) { log_error("Failed to allocate set."); r = -ENOMEM; goto finish; } - if ((fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) { + fanotify_fd = fanotify_init(FAN_CLOEXEC|FAN_NONBLOCK, + O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME); + if (fanotify_fd < 0) { log_error("Failed to create fanotify object: %m"); r = -errno; goto finish; @@ -302,7 +305,8 @@ static int collect(const char *root) { goto finish; } - if ((inotify_fd = open_inotify()) < 0) { + inotify_fd = open_inotify(); + if (inotify_fd < 0) { r = inotify_fd; goto finish; } 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)) { diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 1191c7a1e1..328b91bc35 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -2980,7 +2980,7 @@ static int print_property(const char *name, DBusMessageIter *iter) { } static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) { - _cleanup_free_ DBusMessage *reply = NULL; + DBusMessage _cleanup_free_ *reply = NULL; const char *interface = ""; int r; DBusMessageIter iter, sub, sub2, sub3; diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index c1f662baa6..e11fd589cb 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -191,8 +191,8 @@ static void font_copy_to_all_vcs(int fd) { for (i = 1; i <= 15; i++) { char vcname[16]; - int vcfd; struct console_font_op cfo; + int _cleanup_close_ vcfd = -1; if (i == vcs.v_active) continue; @@ -212,8 +212,6 @@ static void font_copy_to_all_vcs(int fd) { cfo.op = KD_FONT_OP_COPY; cfo.height = vcs.v_active-1; /* tty1 == index 0 */ ioctl(vcfd, KDFONTOP, &cfo); - - close_nointr_nofail(vcfd); } } -- cgit v1.2.3-54-g00ecf