[systemd-commits] 12 commits - src/bootchart src/core src/journal src/libsystemd-bus src/libudev src/nspawn src/nss-myhostname src/readahead src/shared src/systemctl src/timedate src/udev src/vconsole

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Sun Mar 31 12:53:46 PDT 2013


 src/bootchart/bootchart.c              |  126 ++++++++--------
 src/bootchart/store.c                  |    2 
 src/bootchart/svg.c                    |    3 
 src/core/dbus-execute.c                |    3 
 src/core/dbus.c                        |    3 
 src/core/execute.c                     |   54 +++---
 src/core/load-fragment.c               |   12 -
 src/core/loopback-setup.c              |   28 +--
 src/core/machine-id-setup.c            |    4 
 src/core/main.c                        |   22 +-
 src/core/manager.c                     |   22 +-
 src/core/mount.c                       |    6 
 src/core/umount.c                      |    8 -
 src/journal/journald-kmsg.c            |    8 -
 src/libsystemd-bus/bus-message.c       |    9 -
 src/libudev/libudev-util.c             |    3 
 src/nspawn/nspawn.c                    |    4 
 src/nss-myhostname/netlink.c           |  256 ++++++++++++++++-----------------
 src/readahead/readahead-collect.c      |   10 -
 src/shared/ask-password-api.c          |    9 -
 src/shared/dbus-loop.c                 |   19 --
 src/shared/strbuf.c                    |   49 ++++--
 src/shared/util.c                      |   21 +-
 src/shared/utmp-wtmp.c                 |   51 ++----
 src/systemctl/systemctl.c              |    2 
 src/timedate/timedated.c               |    6 
 src/udev/accelerometer/accelerometer.c |   35 +---
 src/udev/udevadm-trigger.c             |    2 
 src/vconsole/vconsole-setup.c          |    4 
 29 files changed, 395 insertions(+), 386 deletions(-)

New commits:
commit e62d8c3944745ed276e6d4f33153009860e5cfc5
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Mar 24 19:45:16 2013 -0400

    Modernization
    
    Use _cleanup_ and wrap lines to ~80 chars and such.

diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 53394c2..0a53207 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 85edca1..f735357 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), &param) < 0) {
+                        r = sched_setscheduler(0,
+                                               context->cpu_sched_policy |
+                                               (context->cpu_sched_reset_on_fork ?
+                                                SCHED_RESET_ON_FORK : 0),
+                                               &param);
+                        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 065b75a..bfd0d93 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 25f55fc..bd7fc46 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 a01710f..8f4eb0b 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 7839300..130dc39 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 3fcb90e..95e47e3 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 e2fd8df..643a5a8 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 8a0fb89..d091a22 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 da0a004..fec8998 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 760013c..b516b9b 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 c9b986f..3494b56 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 1191c7a..328b91b 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 c1f662b..e11fd58 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);
         }
 }
 

commit 3c8bed4ee061f96acb4d70a591a9849bddd2a659
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 22:12:56 2013 -0400

    strbuf: replace quick sort with bubble sort
    
    No need to call the heavy artillery, when the original array
    is sorted. Reduces complexity from n² log n to n log n, where
    n is the number of items in the array, not very large, but
    still.

diff --git a/src/shared/strbuf.c b/src/shared/strbuf.c
index abc5c0d..e64f1ca 100644
--- a/src/shared/strbuf.c
+++ b/src/shared/strbuf.c
@@ -95,13 +95,36 @@ void strbuf_cleanup(struct strbuf *str) {
         free(str);
 }
 
-static int strbuf_children_cmp(const void *v1, const void *v2) {
-        const struct strbuf_child_entry *n1 = v1;
-        const struct strbuf_child_entry *n2 = v2;
-
+static int strbuf_children_cmp(const struct strbuf_child_entry *n1,
+                               const struct strbuf_child_entry *n2) {
         return n1->c - n2->c;
 }
 
+static void bubbleinsert(struct strbuf_node *node,
+                         uint8_t c,
+                         struct strbuf_node *node_child) {
+
+        struct strbuf_child_entry new = {
+                .c = c,
+                .child = node_child,
+        };
+        int left = 0, right = node->children_count;
+
+        while (right > left) {
+                int middle = (right + left) / 2 ;
+                if (strbuf_children_cmp(&node->children[middle], &new) <= 0)
+                        left = middle + 1;
+                else
+                        right = middle;
+        }
+
+        memmove(node->children + left + 1, node->children + left,
+                sizeof(struct strbuf_child_entry) * (node->children_count - left));
+        node->children[left] = new;
+
+        node->children_count ++;
+}
+
 /* add string, return the index/offset into the buffer */
 ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         uint8_t c;
@@ -137,8 +160,9 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
                 /* lookup child node */
                 c = s[len - 1 - depth];
                 search.c = c;
-                child = bsearch(&search, node->children, node->children_count, sizeof(struct strbuf_child_entry),
-                                strbuf_children_cmp);
+                child = bsearch(&search, node->children, node->children_count,
+                                sizeof(struct strbuf_child_entry),
+                                (__compar_fn_t) strbuf_children_cmp);
                 if (!child)
                         break;
                 node = child->child;
@@ -171,10 +195,7 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         str->nodes_count++;
 
         node->children = child;
-        node->children[node->children_count].c = c;
-        node->children[node->children_count].child = node_child;
-        node->children_count++;
-        qsort(node->children, node->children_count, sizeof(struct strbuf_child_entry), strbuf_children_cmp);
+        bubbleinsert(node, c, node_child);
 
         return off;
 }

commit a9c307e5d2157c561eae7fc76303cadf7a77b99e
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 20:32:56 2013 -0400

    strbuf: fix leak on memory error
    
    Not very likely, but let's fix it for the matter of
    principle.

diff --git a/src/shared/strbuf.c b/src/shared/strbuf.c
index 915cd3a..abc5c0d 100644
--- a/src/shared/strbuf.c
+++ b/src/shared/strbuf.c
@@ -158,14 +158,18 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
         node_child = new0(struct strbuf_node, 1);
         if (!node_child)
                 return -ENOMEM;
-        str->nodes_count++;
         node_child->value_off = off;
         node_child->value_len = len;
 
         /* extend array, add new entry, sort for bisection */
         child = realloc(node->children, (node->children_count + 1) * sizeof(struct strbuf_child_entry));
-        if (!child)
+        if (!child) {
+                free(node_child);
                 return -ENOMEM;
+        }
+
+        str->nodes_count++;
+
         node->children = child;
         node->children[node->children_count].c = c;
         node->children[node->children_count].child = node_child;

commit 522cd7f18bf1e4a024d6771186a0149c5ca8109c
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 19:34:23 2013 -0400

    bootchart: fix a potential buffer overrun
    
    If the configured number of samples was close to MAXSAMPLES,
    the samples buffer could be overrun:
    
    - by 1, because of off-by-one in the condition (samples > arg_samples_len),
    and
    - by many in case of an overrun, because the number of samples to
      capture was increased, instead of being decreased.
    
    Simplify things by converting to a normal for-loop.
    
    In store.c: change buffer size from 4095 to 4096. 4095 is a strange
    number.

diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
index 052e637..208c452 100644
--- a/src/bootchart/bootchart.c
+++ b/src/bootchart/bootchart.c
@@ -288,7 +288,7 @@ int main(int argc, char *argv[]) {
         log_uptime();
 
         /* main program loop */
-        while (!exiting) {
+        for (samples = 0; !exiting && samples < arg_samples_len; samples++) {
                 int res;
                 double sample_stop;
                 struct timespec req;
@@ -315,7 +315,7 @@ int main(int argc, char *argv[]) {
                                        NULL);
 
                 /* wait for /proc to become available, discarding samples */
-                if (!(graph_start > 0.0))
+                if (graph_start <= 0.0)
                         log_uptime();
                 else
                         log_sample(samples);
@@ -334,7 +334,7 @@ int main(int argc, char *argv[]) {
                  * we'll lose all the missed samples and overrun our total
                  * time
                  */
-                if ((newint_ns > 0) || (newint_s > 0)) {
+                if (newint_ns > 0 || newint_s > 0) {
                         req.tv_sec = newint_s;
                         req.tv_nsec = newint_ns;
 
@@ -350,14 +350,8 @@ int main(int argc, char *argv[]) {
                 } else {
                         overrun++;
                         /* calculate how many samples we lost and scrap them */
-                        arg_samples_len = arg_samples_len + ((int)(newint_ns / interval));
+                        arg_samples_len -= (int)(newint_ns / interval);
                 }
-
-                samples++;
-
-                if (samples > arg_samples_len)
-                        break;
-
         }
 
         /* do some cleanup, close fd's */
diff --git a/src/bootchart/store.c b/src/bootchart/store.c
index 343365e..b2652c8 100644
--- a/src/bootchart/store.c
+++ b/src/bootchart/store.c
@@ -114,7 +114,7 @@ static int pid_cmdline_strscpy(char *buffer, size_t buf_len, int pid) {
 void log_sample(int sample) {
         static int vmstat;
         static int schedstat;
-        char buf[4095];
+        char buf[4096];
         char key[256];
         char val[256];
         char rt[256];

commit 4cd5f79d0bf9fb62636485dd36ee961774361c14
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 18:43:34 2013 -0400

    bootchart: split out option parsing from main()

diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
index e608af2..052e637 100644
--- a/src/bootchart/bootchart.c
+++ b/src/bootchart/bootchart.c
@@ -96,20 +96,10 @@ static void signal_handler(int sig) {
         exiting = 1;
 }
 
-int main(int argc, char *argv[]) {
-        _cleanup_free_ char *build = NULL;
-        struct sigaction sig;
-        struct ps_struct *ps;
-        char output_file[PATH_MAX];
-        char datestr[200];
-        time_t t = 0;
-        const char *fn;
-        _cleanup_fclose_ FILE *f;
-        int gind;
-        int i, r;
-        char *init = NULL, *output = NULL;
-        struct rlimit rlim;
+#define BOOTCHART_CONF "/etc/systemd/bootchart.conf"
 
+static void parse_conf(void) {
+        char *init = NULL, *output = NULL;
         const ConfigTableItem items[] = {
                 { "Bootchart", "Samples",          config_parse_int,    0, &arg_samples_len },
                 { "Bootchart", "Frequency",        config_parse_double, 0, &arg_hz          },
@@ -123,43 +113,46 @@ int main(int argc, char *argv[]) {
                 { "Bootchart", "ScaleY",           config_parse_double, 0, &arg_scale_y     },
                 { NULL, NULL, NULL, 0, NULL }
         };
+        _cleanup_fclose_ FILE *f;
+        int r;
 
-        fn = "/etc/systemd/bootchart.conf";
-        f = fopen(fn, "re");
-        if (f) {
-            r = config_parse(fn, f, NULL, config_item_table_lookup, (void*) items, true, NULL);
-            if (r < 0)
-                    log_warning("Failed to parse configuration file: %s", strerror(-r));
-
-            if (init != NULL)
-                    strscpy(arg_init_path, sizeof(arg_init_path), init);
-            if (output != NULL)
-                    strscpy(arg_output_path, sizeof(arg_output_path), output);
-        }
+        f = fopen(BOOTCHART_CONF, "re");
+        if (!f)
+                return;
 
-        while (1) {
-                static struct option opts[] = {
-                        {"rel",       no_argument,        NULL,  'r'},
-                        {"freq",      required_argument,  NULL,  'f'},
-                        {"samples",   required_argument,  NULL,  'n'},
-                        {"pss",       no_argument,        NULL,  'p'},
-                        {"output",    required_argument,  NULL,  'o'},
-                        {"init",      required_argument,  NULL,  'i'},
-                        {"no-filter", no_argument,        NULL,  'F'},
-                        {"cmdline",   no_argument,        NULL,  'C'},
-                        {"help",      no_argument,        NULL,  'h'},
-                        {"scale-x",   required_argument,  NULL,  'x'},
-                        {"scale-y",   required_argument,  NULL,  'y'},
-                        {"entropy",   no_argument,        NULL,  'e'},
-                        {NULL, 0, NULL, 0}
-                };
-
-                gind = 0;
-
-                i = getopt_long(argc, argv, "erpf:n:o:i:FChx:y:", opts, &gind);
-                if (i == -1)
-                        break;
-                switch (i) {
+        r = config_parse(BOOTCHART_CONF, f,
+                         NULL, config_item_table_lookup, (void*) items, true, NULL);
+        if (r < 0)
+                log_warning("Failed to parse configuration file: %s", strerror(-r));
+
+        if (init != NULL)
+                strscpy(arg_init_path, sizeof(arg_init_path), init);
+        if (output != NULL)
+                strscpy(arg_output_path, sizeof(arg_output_path), output);
+}
+
+static int parse_args(int argc, char *argv[]) {
+        static struct option options[] = {
+                {"rel",       no_argument,        NULL,  'r'},
+                {"freq",      required_argument,  NULL,  'f'},
+                {"samples",   required_argument,  NULL,  'n'},
+                {"pss",       no_argument,        NULL,  'p'},
+                {"output",    required_argument,  NULL,  'o'},
+                {"init",      required_argument,  NULL,  'i'},
+                {"no-filter", no_argument,        NULL,  'F'},
+                {"cmdline",   no_argument,        NULL,  'C'},
+                {"help",      no_argument,        NULL,  'h'},
+                {"scale-x",   required_argument,  NULL,  'x'},
+                {"scale-y",   required_argument,  NULL,  'y'},
+                {"entropy",   no_argument,        NULL,  'e'},
+                {NULL, 0, NULL, 0}
+        };
+        int c;
+
+        while ((c = getopt_long(argc, argv, "erpf:n:o:i:FChx:y:", options, NULL)) >= 0) {
+                int r;
+
+                switch (c) {
                 case 'r':
                         arg_relative = true;
                         break;
@@ -233,14 +226,33 @@ int main(int argc, char *argv[]) {
 
         if (arg_samples_len > MAXSAMPLES) {
                 fprintf(stderr, "Error: samples exceeds maximum\n");
-                exit(EXIT_FAILURE);
+                return -EINVAL;
         }
 
         if (arg_hz <= 0.0) {
                 fprintf(stderr, "Error: Frequency needs to be > 0\n");
-                exit(EXIT_FAILURE);
+                return -EINVAL;
         }
 
+        return 0;
+}
+
+int main(int argc, char *argv[]) {
+        _cleanup_free_ char *build = NULL;
+        struct sigaction sig;
+        struct ps_struct *ps;
+        char output_file[PATH_MAX];
+        char datestr[200];
+        time_t t = 0;
+        int r;
+        struct rlimit rlim;
+
+        parse_conf();
+
+        r = parse_args(argc, argv);
+        if (r < 0)
+                return EXIT_FAILURE;
+
         /*
          * If the kernel executed us through init=/usr/lib/systemd/systemd-bootchart, then
          * fork:

commit 4b94f3b8f7693f076e5c85bc2c02cf028192d8de
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 17:47:48 2013 -0400

    journald: check return value of strappend

diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
index b819876..2f53632 100644
--- a/src/journal/journald-kmsg.c
+++ b/src/journal/journald-kmsg.c
@@ -250,10 +250,12 @@ static void dev_kmsg_record(Server *s, char *p, size_t l) {
                                         break;
 
                                 g = udev_list_entry_get_name(ll);
-                                b = strappend("_UDEV_DEVLINK=", g);
                                 if (g) {
-                                        IOVEC_SET_STRING(iovec[n++], b);
-                                        z++;
+                                        b = strappend("_UDEV_DEVLINK=", g);
+                                        if (b) {
+                                                IOVEC_SET_STRING(iovec[n++], b);
+                                                z++;
+                                        }
                                 }
 
                                 j++;

commit 27f6e5c7823f0b763a92d2938d222b900c5d70e6
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 17:34:15 2013 -0400

    bus: check return value from bus_type_get_alignment/size

diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 85b9cba..c7cc32e 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -784,7 +784,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
 
 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
         struct bus_container *c;
-        size_t sz, align;
+        ssize_t align, sz;
         uint32_t k;
         void *a;
         char *e = NULL;
@@ -1590,10 +1590,12 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
         }
 
         default: {
-                size_t sz, align, rindex;
+                ssize_t sz, align;
+                size_t rindex;
 
                 align = bus_type_get_alignment(type);
                 sz = bus_type_get_size(type);
+                assert(align > 0 && sz > 0);
 
                 rindex = m->rindex;
                 r = message_peek_body(m, &rindex, align, sz, &q);
@@ -2392,10 +2394,11 @@ static int message_skip_fields(
                         (*signature)++;
 
                 } else if (bus_type_is_basic(t)) {
-                        size_t align, k;
+                        ssize_t align, k;
 
                         align = bus_type_get_alignment(t);
                         k = bus_type_get_size(t);
+                        assert(align > 0 && k > 0);
 
                         r = message_peek_fields(m, ri, align, k, NULL);
                         if (r < 0)

commit 10d18763ec6f31ded1535f810be765f7fab2d97d
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 16:33:55 2013 -0400

    nspawn, machine-id-setup: warn if read-only mount call fails
    
    They are not crucial, but they shouldn't fail.

diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index ca16397..146c565 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -235,7 +235,9 @@ int machine_id_setup(void) {
                 log_info("Installed transient /etc/machine-id file.");
 
                 /* Mark the mount read-only */
-                mount(NULL, "/etc/machine-id", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
+                if (mount(NULL, "/etc/machine-id", NULL,
+                          MS_BIND|MS_RDONLY|MS_REMOUNT, NULL) < 0)
+                        log_warning("Failed to make transient /etc/machine-id read-only");
         }
 
 finish:
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 9268cbc..b8b692b 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -537,8 +537,8 @@ static int setup_boot_id(const char *dest) {
         if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
                 log_error("Failed to bind mount boot id: %m");
                 r = -errno;
-        } else
-                mount(from, to, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
+        } else if (mount(from, to, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL))
+                log_warning("Failed to make boot id read-only: %m");
 
         unlink(from);
         return r;

commit d73c3269cd3c92bb26f5e161516c587f2874c8c5
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 10:46:04 2013 -0400

    nss-myhostname: use _cleanup_ and split function into two
    
    The triply nested loop is just too much. Let's split out the
    middle loop's body, so the whole thing is easier to read. Also
    modernize the style a bit, using structure initialization to
    avoid memset and such.

diff --git a/src/nss-myhostname/netlink.c b/src/nss-myhostname/netlink.c
index 53c3b50..2329f00 100644
--- a/src/nss-myhostname/netlink.c
+++ b/src/nss-myhostname/netlink.c
@@ -35,174 +35,168 @@
 #include <stdlib.h>
 
 #include "ifconf.h"
+#include "macro.h"
+#include "util.h"
 
-int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
+#define SEQ 4711
 
+static int read_reply(int fd, struct address **list, unsigned *n_list) {
+        ssize_t bytes;
+        struct cmsghdr *cmsg;
+        struct ucred *ucred;
+        struct nlmsghdr *p;
+        uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
         struct {
                 struct nlmsghdr hdr;
-                struct rtgenmsg gen;
-        } req;
-        struct rtgenmsg *gen;
-        int fd, r, on = 1;
-        uint32_t seq = 4711;
-        struct address *list = NULL;
-        unsigned n_list = 0;
-
-        fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
-        if (fd < 0)
+                struct ifaddrmsg ifaddrmsg;
+                uint8_t payload[16*1024];
+        } resp;
+        struct iovec iov = {
+                .iov_base = &resp,
+                .iov_len = sizeof(resp),
+        };
+        struct msghdr msg = {
+                .msg_name = NULL,
+                .msg_namelen = 0,
+                .msg_iov = &iov,
+                .msg_iovlen = 1,
+                .msg_control = cred_buffer,
+                .msg_controllen = sizeof(cred_buffer),
+                .msg_flags = 0,
+        };
+
+        assert(fd >= 0);
+        assert(list);
+
+        bytes = recvmsg(fd, &msg, 0);
+        if (bytes < 0)
                 return -errno;
 
-        if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
-                r = -errno;
-                goto finish;
-        }
+        cmsg = CMSG_FIRSTHDR(&msg);
+        if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS)
+                return -EIO;
 
-        memset(&req, 0, sizeof(req));
-        req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
-        req.hdr.nlmsg_type = RTM_GETADDR;
-        req.hdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK;
-        req.hdr.nlmsg_seq = seq;
-        req.hdr.nlmsg_pid = 0;
+        ucred = (struct ucred*) CMSG_DATA(cmsg);
+        if (ucred->uid != 0 || ucred->pid != 0)
+                return 0;
 
-        gen = NLMSG_DATA(&req.hdr);
-        gen->rtgen_family = AF_UNSPEC;
+        for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
+                struct ifaddrmsg *ifaddrmsg;
+                struct rtattr *a;
+                size_t l;
+                void *local = NULL, *address = NULL;
 
-        if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0) {
-                r = -errno;
-                goto finish;
-        }
+                if (!NLMSG_OK(p, (size_t) bytes))
+                        return -EIO;
 
-        for (;;) {
-                ssize_t bytes;
-                struct msghdr msg;
-                struct cmsghdr *cmsg;
-                struct ucred *ucred;
-                struct iovec iov;
-                struct nlmsghdr *p;
-                uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
-                struct {
-                        struct nlmsghdr hdr;
-                        struct ifaddrmsg ifaddrmsg;
-                        uint8_t payload[16*1024];
-                } resp;
-
-                memset(&iov, 0, sizeof(iov));
-                iov.iov_base = &resp;
-                iov.iov_len = sizeof(resp);
-
-                memset(&msg, 0, sizeof(msg));
-                msg.msg_name = NULL;
-                msg.msg_namelen = 0;
-                msg.msg_iov = &iov;
-                msg.msg_iovlen = 1;
-                msg.msg_control = cred_buffer;
-                msg.msg_controllen = sizeof(cred_buffer);
-                msg.msg_flags = 0;
-
-                bytes = recvmsg(fd, &msg, 0);
-                if (bytes < 0) {
-                        r = -errno;
-                        goto finish;
-                }
+                if (p->nlmsg_seq != SEQ)
+                        continue;
+
+                if (p->nlmsg_type == NLMSG_DONE)
+                        return 1;
+
+                if (p->nlmsg_type == NLMSG_ERROR) {
+                        struct nlmsgerr *nlmsgerr;
 
-                cmsg = CMSG_FIRSTHDR(&msg);
-                if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
-                        r = -EIO;
-                        goto finish;
+                        nlmsgerr = NLMSG_DATA(p);
+                        return -nlmsgerr->error;
                 }
 
-                ucred = (struct ucred*) CMSG_DATA(cmsg);
-                if (ucred->uid != 0 || ucred->pid != 0)
+                if (p->nlmsg_type != RTM_NEWADDR)
                         continue;
 
-                for (p = &resp.hdr; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
-                        struct ifaddrmsg *ifaddrmsg;
-                        struct rtattr *a;
-                        size_t l;
-                        void *local = NULL, *address = NULL;
+                ifaddrmsg = NLMSG_DATA(p);
 
-                        if (!NLMSG_OK(p, (size_t) bytes)) {
-                                r = -EIO;
-                                goto finish;
-                        }
+                if (ifaddrmsg->ifa_family != AF_INET &&
+                    ifaddrmsg->ifa_family != AF_INET6)
+                        continue;
 
-                        if (p->nlmsg_seq != seq)
-                                continue;
+                if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
+                    ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
+                        continue;
 
-                        if (p->nlmsg_type == NLMSG_DONE) {
-                                r = 0;
-                                goto finish;
-                        }
+                if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
+                        continue;
 
-                        if (p->nlmsg_type == NLMSG_ERROR) {
-                                struct nlmsgerr *nlmsgerr;
+                l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
+                a = IFA_RTA(ifaddrmsg);
 
-                                nlmsgerr = NLMSG_DATA(p);
-                                r = -nlmsgerr->error;
-                                goto finish;
-                        }
+                while (RTA_OK(a, l)) {
 
-                        if (p->nlmsg_type != RTM_NEWADDR)
-                                continue;
+                        if (a->rta_type == IFA_ADDRESS)
+                                address = RTA_DATA(a);
+                        else if (a->rta_type == IFA_LOCAL)
+                                local = RTA_DATA(a);
 
-                        ifaddrmsg = NLMSG_DATA(p);
+                        a = RTA_NEXT(a, l);
+                }
 
-                        if (ifaddrmsg->ifa_family != AF_INET &&
-                            ifaddrmsg->ifa_family != AF_INET6)
-                                continue;
+                if (local)
+                        address = local;
 
-                        if (ifaddrmsg->ifa_scope == RT_SCOPE_HOST ||
-                            ifaddrmsg->ifa_scope == RT_SCOPE_NOWHERE)
-                                continue;
+                if (!address)
+                        continue;
 
-                        if (ifaddrmsg->ifa_flags & IFA_F_DEPRECATED)
-                                continue;
+                *list = realloc(*list, (*n_list+1) * sizeof(struct address));
+                if (!*list)
+                        return -ENOMEM;
 
-                        l = NLMSG_PAYLOAD(p, sizeof(struct ifaddrmsg));
-                        a = IFA_RTA(ifaddrmsg);
+                (*list)[*n_list].family = ifaddrmsg->ifa_family;
+                (*list)[*n_list].scope = ifaddrmsg->ifa_scope;
+                memcpy((*list)[*n_list].address,
+                       address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
+                (*list)[*n_list].ifindex = ifaddrmsg->ifa_index;
 
-                        while (RTA_OK(a, l)) {
+                (*n_list)++;
+        }
 
-                                if (a->rta_type == IFA_ADDRESS)
-                                        address = RTA_DATA(a);
-                                else if (a->rta_type == IFA_LOCAL)
-                                        local = RTA_DATA(a);
+        return 0;
+}
 
-                                a = RTA_NEXT(a, l);
-                        }
 
-                        if (local)
-                                address = local;
+int ifconf_acquire_addresses(struct address **_list, unsigned *_n_list) {
 
-                        if (!address)
-                                continue;
+        struct {
+                struct nlmsghdr hdr;
+                struct rtgenmsg gen;
+        } req = { {
+                        .nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
+                        .nlmsg_type = RTM_GETADDR,
+                        .nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP|NLM_F_ACK,
+                        .nlmsg_seq = SEQ,
+                        .nlmsg_pid = 0,
+                }, {
+                        .rtgen_family = AF_UNSPEC,
+                }
+        };
+        int r, on = 1;
+        struct address *list = NULL;
+        unsigned n_list = 0;
+        int _cleanup_close_ fd;
 
-                        list = realloc(list, (n_list+1) * sizeof(struct address));
-                        if (!list) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+        fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
+        if (fd < 0)
+                return -errno;
 
-                        list[n_list].family = ifaddrmsg->ifa_family;
-                        list[n_list].scope = ifaddrmsg->ifa_scope;
-                        memcpy(list[n_list].address, address, ifaddrmsg->ifa_family == AF_INET ? 4 : 16);
-                        list[n_list].ifindex = ifaddrmsg->ifa_index;
+        if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0)
+                return -errno;
 
-                        n_list++;
-                }
-        }
+        if (send(fd, &req, req.hdr.nlmsg_len, 0) < 0)
+                return -errno;
 
-finish:
-        close(fd);
+        while((r = read_reply(fd, &list, &n_list)) == 0)
+                ;
 
-        if (r < 0)
+        if (r < 0) {
                 free(list);
-        else {
-                qsort(list, n_list, sizeof(struct address), address_compare);
-
-                *_list = list;
-                *_n_list = n_list;
+                return r;
         }
 
-        return r;
+        assert(n_list == 0 || list);
+        qsort(list, n_list, sizeof(struct address), address_compare);
+
+        *_list = list;
+        *_n_list = n_list;
+
+        return 0;
 }

commit 7a2262061252a33f6f0a009e89556fbee5b0cb41
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 02:22:15 2013 -0400

    accelerometer: remove dead if and modernize
    
    Based on a coverity warning.

diff --git a/src/udev/accelerometer/accelerometer.c b/src/udev/accelerometer/accelerometer.c
index f50db71..b6913b4 100644
--- a/src/udev/accelerometer/accelerometer.c
+++ b/src/udev/accelerometer/accelerometer.c
@@ -46,6 +46,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stdbool.h>
 #include <math.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -178,7 +179,7 @@ get_prev_orientation(struct udev_device *dev)
         return string_to_orientation(value);
 }
 
-#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = 1; } }
+#define SET_AXIS(axis, code_) if (ev[i].code == code_) { if (got_##axis == 0) { axis = ev[i].value; got_##axis = true; } }
 
 /* accelerometers */
 static void test_orientation(struct udev *udev,
@@ -186,54 +187,46 @@ static void test_orientation(struct udev *udev,
                              const char *devpath)
 {
         OrientationUp old, new;
-        int fd, r;
+        int _cleanup_close_ fd = -1;
         struct input_event ev[64];
-        int got_syn = 0;
-        int got_x, got_y, got_z;
+        bool got_syn = false;
+        bool got_x = false, got_y = false, got_z = false;
         int x = 0, y = 0, z = 0;
         char text[64];
 
         old = get_prev_orientation(dev);
 
-        if ((fd = open(devpath, O_RDONLY)) < 0)
+        fd = open(devpath, O_RDONLY);
+        if (fd < 0)
                 return;
 
-        got_x = got_y = got_z = 0;
-
         while (1) {
-                int i;
+                int i, r;
 
                 r = read(fd, ev, sizeof(struct input_event) * 64);
 
-                if (r < (int) sizeof(struct input_event)) {
-                        close(fd);
+                if (r < (int) sizeof(struct input_event))
                         return;
-                }
 
                 for (i = 0; i < r / (int) sizeof(struct input_event); i++) {
-                        if (got_syn == 1) {
+                        if (got_syn) {
                                 if (ev[i].type == EV_ABS) {
                                         SET_AXIS(x, ABS_X);
                                         SET_AXIS(y, ABS_Y);
                                         SET_AXIS(z, ABS_Z);
                                 }
                         }
-                        if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT) {
-                                got_syn = 1;
-                        }
+                        if (ev[i].type == EV_SYN && ev[i].code == SYN_REPORT)
+                                got_syn = true;
                         if (got_x && got_y && got_z)
                                 goto read_dev;
                 }
         }
 
 read_dev:
-        close(fd);
-
-        if (!got_x || !got_y || !got_z)
-                return;
-
         new = orientation_calc(old, x, y, z);
-        snprintf(text, sizeof(text), "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
+        snprintf(text, sizeof(text),
+                 "ID_INPUT_ACCELEROMETER_ORIENTATION=%s", orientation_to_string(new));
         puts(text);
 }
 

commit cbb21cca098f9063b0ff5548b2c73ab517f55ea5
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 01:40:11 2013 -0400

    execute: convert secure bits into mask properly
    
    C.f. http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=5975c725dfd6f7d36f493ab1453fbdbd35c1f0e3

diff --git a/src/core/execute.c b/src/core/execute.c
index 6aa0083..85edca1 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -671,9 +671,9 @@ static int enforce_user(const ExecContext *context, uid_t uid) {
 
                 /* First step: If we need to keep capabilities but
                  * drop privileges we need to make sure we keep our
-                 * caps, whiel we drop privileges. */
+                 * caps, while we drop privileges. */
                 if (uid != 0) {
-                        int sb = context->secure_bits|SECURE_KEEP_CAPS;
+                        int sb = context->secure_bits | 1<<SECURE_KEEP_CAPS;
 
                         if (prctl(PR_GET_SECUREBITS) != sb)
                                 if (prctl(PR_SET_SECUREBITS, sb) < 0)
@@ -1963,12 +1963,12 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
         if (c->secure_bits)
                 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
                         prefix,
-                        (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
-                        (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
-                        (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
-                        (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
-                        (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
-                        (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
+                        (c->secure_bits & 1<<SECURE_KEEP_CAPS) ? " keep-caps" : "",
+                        (c->secure_bits & 1<<SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
+                        (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
+                        (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
+                        (c->secure_bits & 1<<SECURE_NOROOT) ? " noroot" : "",
+                        (c->secure_bits & 1<<SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
 
         if (c->capability_bounding_set_drop) {
                 unsigned long l;
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 6d90428..65a2a39 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -846,17 +846,17 @@ int config_parse_exec_secure_bits(
 
         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
                 if (first_word(w, "keep-caps"))
-                        c->secure_bits |= SECURE_KEEP_CAPS;
+                        c->secure_bits |= 1<<SECURE_KEEP_CAPS;
                 else if (first_word(w, "keep-caps-locked"))
-                        c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
+                        c->secure_bits |= 1<<SECURE_KEEP_CAPS_LOCKED;
                 else if (first_word(w, "no-setuid-fixup"))
-                        c->secure_bits |= SECURE_NO_SETUID_FIXUP;
+                        c->secure_bits |= 1<<SECURE_NO_SETUID_FIXUP;
                 else if (first_word(w, "no-setuid-fixup-locked"))
-                        c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
+                        c->secure_bits |= 1<<SECURE_NO_SETUID_FIXUP_LOCKED;
                 else if (first_word(w, "noroot"))
-                        c->secure_bits |= SECURE_NOROOT;
+                        c->secure_bits |= 1<<SECURE_NOROOT;
                 else if (first_word(w, "noroot-locked"))
-                        c->secure_bits |= SECURE_NOROOT_LOCKED;
+                        c->secure_bits |= 1<<SECURE_NOROOT_LOCKED;
                 else {
                         log_error("[%s:%u] Failed to parse secure bits, ignoring: %s",
                                   filename, line, rvalue);

commit 4dd1de72e8e7ece77e8831e77eea650de404af75
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Mar 30 01:38:36 2013 -0400

    Remove some dead code
    
    Based on coverity report.

diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
index cd89689..0fb9fff 100644
--- a/src/bootchart/svg.c
+++ b/src/bootchart/svg.c
@@ -823,9 +823,6 @@ static void svg_ps_bars(void) {
                 double starttime;
                 int t;
 
-                if (!ps)
-                        continue;
-
                 enc_name = xml_comment_encode(ps->name);
                 if(!enc_name)
                         continue;
diff --git a/src/core/dbus.c b/src/core/dbus.c
index 62cbf89..b92c7d0 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -978,9 +978,8 @@ static DBusConnection* manager_bus_connect_private(Manager *m, DBusBusType type)
         }
 
         return connection;
+
 fail:
-        if (connection)
-                dbus_connection_close(connection);
         dbus_error_free(&error);
         return NULL;
 }
diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c
index 44f6e4a..714dc50 100644
--- a/src/libudev/libudev-util.c
+++ b/src/libudev/libudev-util.c
@@ -241,8 +241,7 @@ int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
                         return -EINVAL;
                 base[0] = '\0';
         }
-        if (base == NULL)
-                return -EINVAL;
+
         strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
         return 0;
 }
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index 0a01809..c9b986f 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -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();
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index 16fffd0..1d4d739 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -367,8 +367,6 @@ static int read_ntp(DBusConnection *bus) {
                         goto finish;
                 }
 
-                if (reply)
-                        dbus_message_unref(reply);
                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
                 if (!reply) {
                         if (streq(error.name, "org.freedesktop.DBus.Error.FileNotFound")) {
@@ -450,8 +448,6 @@ static int start_ntp(DBusConnection *bus, DBusError *error) {
                         goto finish;
                 }
 
-                if (reply)
-                        dbus_message_unref(reply);
                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
                 if (!reply) {
                         if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound") ||
@@ -540,8 +536,6 @@ static int enable_ntp(DBusConnection *bus, DBusError *error) {
                         }
                 }
 
-                if (reply)
-                        dbus_message_unref(reply);
                 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
                 if (!reply) {
                         if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound")) {
diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c
index 9624c66..f472996 100644
--- a/src/udev/udevadm-trigger.c
+++ b/src/udev/udevadm-trigger.c
@@ -214,7 +214,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                 exec_list(udev_enumerate, action);
                 goto exit;
         default:
-                goto exit;
+                assert_not_reached("device_type");
         }
 exit:
         udev_enumerate_unref(udev_enumerate);



More information about the systemd-commits mailing list