chiark / gitweb /
util: replace close_nointr_nofail() by a more useful safe_close()
authorLennart Poettering <lennart@poettering.net>
Tue, 18 Mar 2014 18:22:43 +0000 (19:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 Mar 2014 18:31:34 +0000 (19:31 +0100)
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:

        fd = safe_close(fd);

Which will close an fd if it is open, and reset the fd variable
correctly.

By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.

80 files changed:
src/core/audit-fd.c
src/core/automount.c
src/core/busname.c
src/core/cgroup.c
src/core/dbus.c
src/core/execute.c
src/core/ima-setup.c
src/core/load-fragment.c
src/core/machine-id-setup.c
src/core/main.c
src/core/manager.c
src/core/path.c
src/core/service.c
src/core/smack-setup.c
src/core/socket.c
src/core/switch-root.c
src/core/umount.c
src/fsck/fsck.c
src/initctl/initctl.c
src/journal/cat.c
src/journal/catalog.c
src/journal/coredumpctl.c
src/journal/journal-authenticate.c
src/journal/journal-file.c
src/journal/journal-send.c
src/journal/journal-verify.c
src/journal/journalctl.c
src/journal/journald-console.c
src/journal/journald-kmsg.c
src/journal/journald-server.c
src/journal/journald-stream.c
src/journal/sd-journal.c
src/journal/test-journal-verify.c
src/journal/test-mmap-cache.c
src/libsystemd-network/dhcp-network.c
src/libsystemd-network/ipv4ll-network.c
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd-network/sd-ipv4ll.c
src/libsystemd/sd-bus/bus-container.c
src/libsystemd/sd-bus/bus-kernel.c
src/libsystemd/sd-bus/bus-message.c
src/libsystemd/sd-bus/bus-socket.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-bus/sd-memfd.c
src/libsystemd/sd-bus/test-bus-chat.c
src/libsystemd/sd-bus/test-bus-kernel-benchmark.c
src/libsystemd/sd-bus/test-bus-kernel.c
src/libsystemd/sd-event/sd-event.c
src/libsystemd/sd-login/sd-login.c
src/libsystemd/sd-resolve/sd-resolve.c
src/libsystemd/sd-rtnl/sd-rtnl.c
src/login/logind-button.c
src/login/logind-inhibit.c
src/login/logind-session.c
src/login/logind.c
src/login/pam-module.c
src/login/test-inhibit.c
src/machine/machinectl.c
src/network/sd-network.c
src/nspawn/nspawn.c
src/readahead/readahead-collect.c
src/readahead/readahead-common.c
src/readahead/readahead-replay.c
src/reply-password/reply-password.c
src/shared/ask-password-api.c
src/shared/fdset.c
src/shared/install.c
src/shared/log.c
src/shared/logs-show.c
src/shared/spawn-polkit-agent.c
src/shared/util.c
src/shared/util.h
src/shared/watchdog.c
src/shutdownd/shutdownd.c
src/socket-proxy/socket-proxyd.c
src/test/test-util.c
src/tmpfiles/tmpfiles.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/udev/net/link-config.c
src/vconsole/vconsole-setup.c

index 5955bd846e32bf8eaba66e5e698e5364ca87312c..4326d17360aa54e1df2a56891548201414948849 100644 (file)
@@ -55,7 +55,7 @@ int get_audit_fd(void) {
 void close_audit_fd(void) {
 
         if (initialized && audit_fd >= 0)
 void close_audit_fd(void) {
 
         if (initialized && audit_fd >= 0)
-                close_nointr_nofail(audit_fd);
+                safe_close(audit_fd);
 
         initialized = true;
         audit_fd = -ECONNRESET;
 
         initialized = true;
         audit_fd = -ECONNRESET;
index 889b20e25dd7addeb04d733e2663652a96e6f1df..77d80b2789e49bc6bbd8dcc1c5d9d7dbd73f5d3b 100644 (file)
@@ -90,9 +90,7 @@ static void unmount_autofs(Automount *a) {
         automount_send_ready(a, -EHOSTDOWN);
 
         a->pipe_event_source = sd_event_source_unref(a->pipe_event_source);
         automount_send_ready(a, -EHOSTDOWN);
 
         a->pipe_event_source = sd_event_source_unref(a->pipe_event_source);
-
-        close_nointr_nofail(a->pipe_fd);
-        a->pipe_fd = -1;
+        a->pipe_fd = safe_close(a->pipe_fd);
 
         /* If we reload/reexecute things we keep the mount point
          * around */
 
         /* If we reload/reexecute things we keep the mount point
          * around */
@@ -310,8 +308,7 @@ static int open_dev_autofs(Manager *m) {
 
         init_autofs_dev_ioctl(&param);
         if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
 
         init_autofs_dev_ioctl(&param);
         if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
-                close_nointr_nofail(m->dev_autofs_fd);
-                m->dev_autofs_fd = -1;
+                m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -411,8 +408,9 @@ static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, in
 }
 
 int automount_send_ready(Automount *a, int status) {
 }
 
 int automount_send_ready(Automount *a, int status) {
-        int ioctl_fd, r;
+        _cleanup_close_ int ioctl_fd = -1;
         unsigned token;
         unsigned token;
+        int r;
 
         assert(a);
         assert(status <= 0);
 
         assert(a);
         assert(status <= 0);
@@ -421,10 +419,8 @@ int automount_send_ready(Automount *a, int status) {
                 return 0;
 
         ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
                 return 0;
 
         ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
-        if (ioctl_fd < 0) {
-                r = ioctl_fd;
-                goto fail;
-        }
+        if (ioctl_fd < 0)
+                return ioctl_fd;
 
         if (status)
                 log_debug_unit(UNIT(a)->id, "Sending failure: %s", strerror(-status));
 
         if (status)
                 log_debug_unit(UNIT(a)->id, "Sending failure: %s", strerror(-status));
@@ -450,18 +446,15 @@ int automount_send_ready(Automount *a, int status) {
                         r = k;
         }
 
                         r = k;
         }
 
-fail:
-        if (ioctl_fd >= 0)
-                close_nointr_nofail(ioctl_fd);
-
         return r;
 }
 
 static void automount_enter_waiting(Automount *a) {
         return r;
 }
 
 static void automount_enter_waiting(Automount *a) {
+        _cleanup_close_ int ioctl_fd = -1;
         int p[2] = { -1, -1 };
         char name[32], options[128];
         bool mounted = false;
         int p[2] = { -1, -1 };
         char name[32], options[128];
         bool mounted = false;
-        int r, ioctl_fd = -1, dev_autofs_fd;
+        int r, dev_autofs_fd;
         struct stat st;
 
         assert(a);
         struct stat st;
 
         assert(a);
@@ -500,8 +493,7 @@ static void automount_enter_waiting(Automount *a) {
 
         mounted = true;
 
 
         mounted = true;
 
-        close_nointr_nofail(p[1]);
-        p[1] = -1;
+        p[1] = safe_close(p[1]);
 
         if (stat(a->where, &st) < 0) {
                 r = -errno;
 
         if (stat(a->where, &st) < 0) {
                 r = -errno;
@@ -528,9 +520,6 @@ static void automount_enter_waiting(Automount *a) {
          * the direct mount will not receive events from the
          * kernel. */
 
          * the direct mount will not receive events from the
          * kernel. */
 
-        close_nointr_nofail(ioctl_fd);
-        ioctl_fd = -1;
-
         r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, p[0], EPOLLIN, automount_dispatch_io, a);
         if (r < 0)
                 goto fail;
         r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, p[0], EPOLLIN, automount_dispatch_io, a);
         if (r < 0)
                 goto fail;
@@ -543,10 +532,7 @@ static void automount_enter_waiting(Automount *a) {
         return;
 
 fail:
         return;
 
 fail:
-        assert_se(close_pipe(p) == 0);
-
-        if (ioctl_fd >= 0)
-                close_nointr_nofail(ioctl_fd);
+        close_pipe(p);
 
         if (mounted)
                 repeat_unmount(a->where);
 
         if (mounted)
                 repeat_unmount(a->where);
@@ -713,9 +699,7 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse pipe-fd value %s", value);
                 else {
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse pipe-fd value %s", value);
                 else {
-                        if (a->pipe_fd >= 0)
-                                close_nointr_nofail(a->pipe_fd);
-
+                        safe_close(a->pipe_fd);
                         a->pipe_fd = fdset_remove(fds, fd);
                 }
         } else
                         a->pipe_fd = fdset_remove(fds, fd);
                 }
         } else
@@ -809,8 +793,7 @@ fail:
 static void automount_shutdown(Manager *m) {
         assert(m);
 
 static void automount_shutdown(Manager *m) {
         assert(m);
 
-        if (m->dev_autofs_fd >= 0)
-                close_nointr_nofail(m->dev_autofs_fd);
+        m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
 }
 
 static void automount_reset_failed(Unit *u) {
 }
 
 static void automount_reset_failed(Unit *u) {
index bd7d02d73b6940ad3246d38cf86a4fbf44bf6444..9d7a7965380a22d06d6622e58e30c1abced55c9b 100644 (file)
@@ -56,11 +56,7 @@ static void busname_done(Unit *u) {
         unit_ref_unset(&n->service);
 
         n->event_source = sd_event_source_unref(n->event_source);
         unit_ref_unset(&n->service);
 
         n->event_source = sd_event_source_unref(n->event_source);
-
-        if (n->starter_fd >= 0) {
-                close_nointr_nofail(n->starter_fd);
-                n->starter_fd = -1;
-        }
+        n->starter_fd = safe_close(n->starter_fd);
 }
 
 static int busname_add_default_default_dependencies(BusName *n) {
 }
 
 static int busname_add_default_default_dependencies(BusName *n) {
@@ -122,8 +118,6 @@ static int busname_add_extras(BusName *n) {
         return 0;
 }
 
         return 0;
 }
 
-
-
 static int busname_verify(BusName *n) {
         char *e;
 
 static int busname_verify(BusName *n) {
         char *e;
 
@@ -202,8 +196,7 @@ static void busname_close_fd(BusName *n) {
         if (n->starter_fd <= 0)
                 return;
 
         if (n->starter_fd <= 0)
                 return;
 
-        close_nointr_nofail(n->starter_fd);
-        n->starter_fd = -1;
+        n->starter_fd = safe_close(n->starter_fd);
 }
 
 static int busname_watch_fd(BusName *n) {
 }
 
 static int busname_watch_fd(BusName *n) {
@@ -454,8 +447,7 @@ static int busname_deserialize_item(Unit *u, const char *key, const char *value,
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
                 else {
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse starter fd value %s", value);
                 else {
-                        if (n->starter_fd >= 0)
-                                close_nointr_nofail(n->starter_fd);
+                        safe_close(n->starter_fd);
                         n->starter_fd = fdset_remove(fds, fd);
                 }
         } else
                         n->starter_fd = fdset_remove(fds, fd);
                 }
         } else
index 18de888ba677b6d7ae7cc43ea46bd19bdda2acd1..9cc8544fc78b6cdfd97d44bbca88da855b6942ee 100644 (file)
@@ -862,8 +862,7 @@ int manager_setup_cgroup(Manager *m) {
         }
 
         /* 5. And pin it, so that it cannot be unmounted */
         }
 
         /* 5. And pin it, so that it cannot be unmounted */
-        if (m->pin_cgroupfs_fd >= 0)
-                close_nointr_nofail(m->pin_cgroupfs_fd);
+        safe_close(m->pin_cgroupfs_fd);
 
         m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
         if (r < 0) {
 
         m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
         if (r < 0) {
@@ -888,10 +887,7 @@ void manager_shutdown_cgroup(Manager *m, bool delete) {
         if (delete && m->cgroup_root)
                 cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false);
 
         if (delete && m->cgroup_root)
                 cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false);
 
-        if (m->pin_cgroupfs_fd >= 0) {
-                close_nointr_nofail(m->pin_cgroupfs_fd);
-                m->pin_cgroupfs_fd = -1;
-        }
+        m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd);
 
         free(m->cgroup_root);
         m->cgroup_root = NULL;
 
         free(m->cgroup_root);
         m->cgroup_root = NULL;
index 72f36bdc1c3f4c97884c2fd91bc3e6e22c48ea9b..d482e6c53777a67c4c33a2ee8fd00892a87827d7 100644 (file)
@@ -1086,10 +1086,7 @@ void bus_done(Manager *m) {
         if (m->private_listen_event_source)
                 m->private_listen_event_source = sd_event_source_unref(m->private_listen_event_source);
 
         if (m->private_listen_event_source)
                 m->private_listen_event_source = sd_event_source_unref(m->private_listen_event_source);
 
-        if (m->private_listen_fd >= 0) {
-                close_nointr_nofail(m->private_listen_fd);
-                m->private_listen_fd = -1;
-        }
+        m->private_listen_fd = safe_close(m->private_listen_fd);
 }
 
 int bus_fdset_add_all(Manager *m, FDSet *fds) {
 }
 
 int bus_fdset_add_all(Manager *m, FDSet *fds) {
index ca807dc8cb2fa86ff9b2afbba8dc9ecc70d88fe8..a46f25de3bb49e167700c8844327814cb2886e5e 100644 (file)
@@ -123,7 +123,7 @@ static int shift_fds(int fds[], unsigned n_fds) {
                         if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
                                 return -errno;
 
                         if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
                                 return -errno;
 
-                        close_nointr_nofail(fds[i]);
+                        safe_close(fds[i]);
                         fds[i] = nfd;
 
                         /* Hmm, the fd we wanted isn't free? Then
                         fds[i] = nfd;
 
                         /* Hmm, the fd we wanted isn't free? Then
@@ -209,7 +209,7 @@ static int open_null_as(int flags, int nfd) {
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
-                close_nointr_nofail(fd);
+                safe_close(fd);
         } else
                 r = nfd;
 
         } else
                 r = nfd;
 
@@ -234,12 +234,12 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
 
         r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
         if (r < 0) {
 
         r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
         if (r < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         if (shutdown(fd, SHUT_RD) < 0) {
                 return -errno;
         }
 
         if (shutdown(fd, SHUT_RD) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -263,7 +263,7 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
-                close_nointr_nofail(fd);
+                safe_close(fd);
         } else
                 r = nfd;
 
         } else
                 r = nfd;
 
@@ -280,7 +280,7 @@ static int open_terminal_as(const char *path, mode_t mode, int nfd) {
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
-                close_nointr_nofail(fd);
+                safe_close(fd);
         } else
                 r = nfd;
 
         } else
                 r = nfd;
 
@@ -340,7 +340,7 @@ static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty
 
                 if (fd != STDIN_FILENO) {
                         r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
 
                 if (fd != STDIN_FILENO) {
                         r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                 } else
                         r = STDIN_FILENO;
 
                 } else
                         r = STDIN_FILENO;
 
@@ -504,7 +504,7 @@ static int setup_confirm_stdio(int *_saved_stdin,
         }
 
         if (fd >= 2)
         }
 
         if (fd >= 2)
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
         *_saved_stdin = saved_stdin;
         *_saved_stdout = saved_stdout;
 
         *_saved_stdin = saved_stdin;
         *_saved_stdout = saved_stdout;
@@ -512,20 +512,15 @@ static int setup_confirm_stdio(int *_saved_stdin,
         return 0;
 
 fail:
         return 0;
 
 fail:
-        if (saved_stdout >= 0)
-                close_nointr_nofail(saved_stdout);
-
-        if (saved_stdin >= 0)
-                close_nointr_nofail(saved_stdin);
-
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(saved_stdout);
+        safe_close(saved_stdin);
+        safe_close(fd);
 
         return r;
 }
 
 _printf_(1, 2) static int write_confirm_message(const char *format, ...) {
 
         return r;
 }
 
 _printf_(1, 2) static int write_confirm_message(const char *format, ...) {
-        int fd;
+        _cleanup_close_ int fd = -1;
         va_list ap;
 
         assert(format);
         va_list ap;
 
         assert(format);
@@ -538,8 +533,6 @@ _printf_(1, 2) static int write_confirm_message(const char *format, ...) {
         vdprintf(fd, format, ap);
         va_end(ap);
 
         vdprintf(fd, format, ap);
         va_end(ap);
 
-        close_nointr_nofail(fd);
-
         return 0;
 }
 
         return 0;
 }
 
@@ -561,11 +554,8 @@ static int restore_confirm_stdio(int *saved_stdin,
                 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
                         r = -errno;
 
                 if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
                         r = -errno;
 
-        if (*saved_stdin >= 0)
-                close_nointr_nofail(*saved_stdin);
-
-        if (*saved_stdout >= 0)
-                close_nointr_nofail(*saved_stdout);
+        safe_close(*saved_stdin);
+        safe_close(*saved_stdout);
 
         return r;
 }
 
         return r;
 }
@@ -1125,10 +1115,9 @@ finish:
 static void do_idle_pipe_dance(int idle_pipe[4]) {
         assert(idle_pipe);
 
 static void do_idle_pipe_dance(int idle_pipe[4]) {
         assert(idle_pipe);
 
-        if (idle_pipe[1] >= 0)
-                close_nointr_nofail(idle_pipe[1]);
-        if (idle_pipe[2] >= 0)
-                close_nointr_nofail(idle_pipe[2]);
+
+        safe_close(idle_pipe[1]);
+        safe_close(idle_pipe[2]);
 
         if (idle_pipe[0] >= 0) {
                 int r;
 
         if (idle_pipe[0] >= 0) {
                 int r;
@@ -1143,12 +1132,11 @@ static void do_idle_pipe_dance(int idle_pipe[4]) {
                         fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT2_USEC);
                 }
 
                         fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT2_USEC);
                 }
 
-                close_nointr_nofail(idle_pipe[0]);
+                safe_close(idle_pipe[0]);
 
         }
 
 
         }
 
-        if (idle_pipe[3] >= 0)
-                close_nointr_nofail(idle_pipe[3]);
+        safe_close(idle_pipe[3]);
 }
 
 static int build_environment(
 }
 
 static int build_environment(
@@ -2730,9 +2718,7 @@ int exec_runtime_deserialize_item(ExecRuntime **rt, Unit *u, const char *key, co
                 if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse netns socket value %s", value);
                 else {
                 if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse netns socket value %s", value);
                 else {
-                        if ((*rt)->netns_storage_socket[0] >= 0)
-                                close_nointr_nofail((*rt)->netns_storage_socket[0]);
-
+                        safe_close((*rt)->netns_storage_socket[0]);
                         (*rt)->netns_storage_socket[0] = fdset_remove(fds, fd);
                 }
         } else if (streq(key, "netns-socket-1")) {
                         (*rt)->netns_storage_socket[0] = fdset_remove(fds, fd);
                 }
         } else if (streq(key, "netns-socket-1")) {
@@ -2745,9 +2731,7 @@ int exec_runtime_deserialize_item(ExecRuntime **rt, Unit *u, const char *key, co
                 if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse netns socket value %s", value);
                 else {
                 if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd))
                         log_debug_unit(u->id, "Failed to parse netns socket value %s", value);
                 else {
-                        if ((*rt)->netns_storage_socket[1] >= 0)
-                                close_nointr_nofail((*rt)->netns_storage_socket[1]);
-
+                        safe_close((*rt)->netns_storage_socket[1]);
                         (*rt)->netns_storage_socket[1] = fdset_remove(fds, fd);
                 }
         } else
                         (*rt)->netns_storage_socket[1] = fdset_remove(fds, fd);
                 }
         } else
index 7f8ec23d58363ec4cafabc0f6accc740699b0569..ed65096f043a9379ac8b58a46f5bd19693777cf7 100644 (file)
@@ -47,7 +47,7 @@ int ima_setup(void) {
        struct stat st;
        ssize_t policy_size = 0, written = 0;
        char *policy;
        struct stat st;
        ssize_t policy_size = 0, written = 0;
        char *policy;
-       int policyfd = -1, imafd = -1;
+       _cleanup_close_ int policyfd = -1, imafd = -1;
        int result = 0;
 
        if (stat(IMA_POLICY_PATH, &st) < 0)
        int result = 0;
 
        if (stat(IMA_POLICY_PATH, &st) < 0)
@@ -98,10 +98,6 @@ int ima_setup(void) {
 out_mmap:
        munmap(policy, policy_size);
 out:
 out_mmap:
        munmap(policy, policy_size);
 out:
-       if (policyfd >= 0)
-                close_nointr_nofail(policyfd);
-       if (imafd >= 0)
-                close_nointr_nofail(imafd);
        if (result)
                 return result;
 #endif /* HAVE_IMA */
        if (result)
                 return result;
 #endif /* HAVE_IMA */
index 06e3031d65cd51ed50e392c45ee3dac7e2aae74e..1c7ac75dd8cf377f769c38bdc3c6754f541f18f7 100644 (file)
@@ -3085,7 +3085,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
         f = fdopen(fd, "re");
         if (!f) {
                 r = -errno;
         f = fdopen(fd, "re");
         if (!f) {
                 r = -errno;
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return r;
         }
 
                 return r;
         }
 
index 43720d628971103a928e613a9c8ae052a75c72e7..d459afe900d11de3c3450457283a650610c509e5 100644 (file)
@@ -78,7 +78,7 @@ static int generate(char id[34], const char *root) {
         fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
         if (fd >= 0) {
                 k = loop_read(fd, id, 33, false);
         fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
         if (fd >= 0) {
                 k = loop_read(fd, id, 33, false);
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
                 if (k == 33 && id[32] == '\n') {
 
 
                 if (k == 33 && id[32] == '\n') {
 
@@ -104,7 +104,7 @@ static int generate(char id[34], const char *root) {
                 fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
                 if (fd >= 0) {
                         k = loop_read(fd, uuid, 36, false);
                 fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
                 if (fd >= 0) {
                         k = loop_read(fd, uuid, 36, false);
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
 
                         if (k >= 36) {
                                 r = shorten_uuid(id, uuid);
 
                         if (k >= 36) {
                                 r = shorten_uuid(id, uuid);
@@ -216,8 +216,7 @@ int machine_id_setup(const char *root) {
                         return 0;
         }
 
                         return 0;
         }
 
-        close_nointr_nofail(fd);
-        fd = -1;
+        fd = safe_close(fd);
 
         /* Hmm, we couldn't write it? So let's write it to
          * /run/machine-id as a replacement */
 
         /* Hmm, we couldn't write it? So let's write it to
          * /run/machine-id as a replacement */
index cc876efa9c2c0377464c070b7e20c29774040bf2..e0fbb6e147b851f9d56d86be3715eb3088764e84 100644 (file)
@@ -240,7 +240,7 @@ static int console_setup(bool do_reset) {
         if (r < 0)
                 log_error("Failed to reset /dev/console: %s", strerror(-r));
 
         if (r < 0)
                 log_error("Failed to reset /dev/console: %s", strerror(-r));
 
-        close_nointr_nofail(tty_fd);
+        safe_close(tty_fd);
         return r;
 }
 
         return r;
 }
 
index ce5888e840d14892adf05b1d6921a8c918441c0e..632ce74ac29b291a10e6bbf66bf257c5b446f582 100644 (file)
@@ -250,8 +250,7 @@ static int manager_setup_time_change(Manager *m) {
 
         if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
                 log_debug("Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m");
 
         if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
                 log_debug("Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m");
-                close_nointr_nofail(m->time_change_fd);
-                m->time_change_fd = -1;
+                m->time_change_fd = safe_close(m->time_change_fd);
                 return 0;
         }
 
                 return 0;
         }
 
@@ -793,14 +792,10 @@ void manager_free(Manager *m) {
         sd_event_source_unref(m->idle_pipe_event_source);
         sd_event_source_unref(m->run_queue_event_source);
 
         sd_event_source_unref(m->idle_pipe_event_source);
         sd_event_source_unref(m->run_queue_event_source);
 
-        if (m->signal_fd >= 0)
-                close_nointr_nofail(m->signal_fd);
-        if (m->notify_fd >= 0)
-                close_nointr_nofail(m->notify_fd);
-        if (m->time_change_fd >= 0)
-                close_nointr_nofail(m->time_change_fd);
-        if (m->kdbus_fd >= 0)
-                close_nointr_nofail(m->kdbus_fd);
+        safe_close(m->signal_fd);
+        safe_close(m->notify_fd);
+        safe_close(m->time_change_fd);
+        safe_close(m->kdbus_fd);
 
         manager_close_idle_pipe(m);
 
 
         manager_close_idle_pipe(m);
 
@@ -1756,9 +1751,7 @@ static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint
 
         /* Restart the watch */
         m->time_change_event_source = sd_event_source_unref(m->time_change_event_source);
 
         /* Restart the watch */
         m->time_change_event_source = sd_event_source_unref(m->time_change_event_source);
-
-        close_nointr_nofail(m->time_change_fd);
-        m->time_change_fd = -1;
+        m->time_change_fd = safe_close(m->time_change_fd);
 
         manager_setup_time_change(m);
 
 
         manager_setup_time_change(m);
 
@@ -2042,7 +2035,7 @@ int manager_open_serialization(Manager *m, FILE **_f) {
 
         f = fdopen(fd, "w+");
         if (!f) {
 
         f = fdopen(fd, "w+");
         if (!f) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -2263,11 +2256,8 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
                         if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse notify fd: %s", l + 10);
                         else {
                         if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse notify fd: %s", l + 10);
                         else {
-                                if (m->notify_fd >= 0) {
-                                        m->notify_event_source = sd_event_source_unref(m->notify_event_source);
-                                        close_nointr_nofail(m->notify_fd);
-                                }
-
+                                m->notify_event_source = sd_event_source_unref(m->notify_event_source);
+                                safe_close(m->notify_fd);
                                 m->notify_fd = fdset_remove(fds, fd);
                         }
 
                                 m->notify_fd = fdset_remove(fds, fd);
                         }
 
@@ -2289,9 +2279,7 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
                         if (safe_atoi(l + 9, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse kdbus fd: %s", l + 9);
                         else {
                         if (safe_atoi(l + 9, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse kdbus fd: %s", l + 9);
                         else {
-                                if (m->kdbus_fd >= 0)
-                                        close_nointr_nofail(m->kdbus_fd);
-
+                                safe_close(m->kdbus_fd);
                                 m->kdbus_fd = fdset_remove(fds, fd);
                         }
 
                                 m->kdbus_fd = fdset_remove(fds, fd);
                         }
 
index 93c51bb3bec5b0363003c7624e077f252bfd9b4a..20e454d96f4c1ba04a463c7ac92d920303e12948 100644 (file)
@@ -152,11 +152,7 @@ void path_spec_unwatch(PathSpec *s) {
         assert(s);
 
         s->event_source = sd_event_source_unref(s->event_source);
         assert(s);
 
         s->event_source = sd_event_source_unref(s->event_source);
-
-        if (s->inotify_fd >= 0) {
-                close_nointr_nofail(s->inotify_fd);
-                s->inotify_fd = -1;
-        }
+        s->inotify_fd = safe_close(s->inotify_fd);
 }
 
 int path_spec_fd_event(PathSpec *s, uint32_t revents) {
 }
 
 int path_spec_fd_event(PathSpec *s, uint32_t revents) {
index 386692a10aaa81c3a9bc3e837b04e9a4768c911a..fe7ddd1841041706a0845dd1d03ad5aa50561656 100644 (file)
@@ -226,8 +226,7 @@ static void service_close_socket_fd(Service *s) {
         if (s->socket_fd < 0)
                 return;
 
         if (s->socket_fd < 0)
                 return;
 
-        close_nointr_nofail(s->socket_fd);
-        s->socket_fd = -1;
+        s->socket_fd = safe_close(s->socket_fd);
 }
 
 static void service_connection_unref(Service *s) {
 }
 
 static void service_connection_unref(Service *s) {
@@ -2684,8 +2683,7 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                         log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
                 else {
 
                         log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
                 else {
 
-                        if (s->socket_fd >= 0)
-                                close_nointr_nofail(s->socket_fd);
+                        safe_close(s->socket_fd);
                         s->socket_fd = fdset_remove(fds, fd);
                 }
         } else if (streq(key, "main-exec-status-pid")) {
                         s->socket_fd = fdset_remove(fds, fd);
                 }
         } else if (streq(key, "main-exec-status-pid")) {
index a68605c27216407a49e45a8d007bda77af5c9372..5f6dabf82a228fbcacb6194a7b132ba43404ae49 100644 (file)
@@ -87,7 +87,7 @@ static int write_rules(const char* dstpath, const char* srcdir) {
                 if (!policy) {
                         if (r == 0)
                                 r = -errno;
                 if (!policy) {
                         if (r == 0)
                                 r = -errno;
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         log_error("Failed to open %s: %m", entry->d_name);
                         continue;
                 }
                         log_error("Failed to open %s: %m", entry->d_name);
                         continue;
                 }
index b39bec2f261f9fafb2336f52181c44135e906d4a..b2a3e954cbfd61a91fdb2bf3c281743f1f80d826 100644 (file)
@@ -121,9 +121,7 @@ void socket_free_ports(Socket *s) {
 
                 sd_event_source_unref(p->event_source);
 
 
                 sd_event_source_unref(p->event_source);
 
-                if (p->fd >= 0)
-                        close_nointr_nofail(p->fd);
-
+                safe_close(p->fd);
                 free(p->path);
                 free(p);
         }
                 free(p->path);
                 free(p);
         }
@@ -700,7 +698,7 @@ static void socket_close_fds(Socket *s) {
                 if (p->fd < 0)
                         continue;
 
                 if (p->fd < 0)
                         continue;
 
-                close_nointr_nofail(p->fd);
+                p->fd = safe_close(p->fd);
 
                 /* One little note: we should never delete any sockets
                  * in the file system here! After all some other
 
                 /* One little note: we should never delete any sockets
                  * in the file system here! After all some other
@@ -709,8 +707,6 @@ static void socket_close_fds(Socket *s) {
                  * we delete sockets in the file system before we
                  * create a new one, not after we stopped using
                  * one! */
                  * we delete sockets in the file system before we
                  * create a new one, not after we stopped using
                  * one! */
-
-                p->fd = -1;
         }
 }
 
         }
 }
 
@@ -880,9 +876,7 @@ static int fifo_address_create(
 
 fail:
         label_context_clear();
 
 fail:
         label_context_clear();
-
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -917,8 +911,7 @@ static int special_address_create(
         return 0;
 
 fail:
         return 0;
 
 fail:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -977,9 +970,7 @@ static int mq_address_create(
         return 0;
 
 fail:
         return 0;
 
 fail:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
+        safe_close(fd);
         return r;
 }
 
         return r;
 }
 
@@ -1472,7 +1463,7 @@ static void socket_enter_running(Socket *s, int cfd) {
                 log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id);
 
                 if (cfd >= 0)
                 log_debug_unit(UNIT(s)->id, "Suppressing connection request on %s since unit stop is scheduled.", UNIT(s)->id);
 
                 if (cfd >= 0)
-                        close_nointr_nofail(cfd);
+                        safe_close(cfd);
                 else  {
                         /* Flush all sockets by closing and reopening them */
                         socket_close_fds(s);
                 else  {
                         /* Flush all sockets by closing and reopening them */
                         socket_close_fds(s);
@@ -1520,7 +1511,7 @@ static void socket_enter_running(Socket *s, int cfd) {
 
                 if (s->n_connections >= s->max_connections) {
                         log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections);
 
                 if (s->n_connections >= s->max_connections) {
                         log_warning_unit(UNIT(s)->id, "%s: Too many incoming connections (%u)", UNIT(s)->id, s->n_connections);
-                        close_nointr_nofail(cfd);
+                        safe_close(cfd);
                         return;
                 }
 
                         return;
                 }
 
@@ -1535,7 +1526,7 @@ static void socket_enter_running(Socket *s, int cfd) {
 
                         /* ENOTCONN is legitimate if TCP RST was received.
                          * This connection is over, but the socket unit lives on. */
 
                         /* ENOTCONN is legitimate if TCP RST was received.
                          * This connection is over, but the socket unit lives on. */
-                        close_nointr_nofail(cfd);
+                        safe_close(cfd);
                         return;
                 }
 
                         return;
                 }
 
@@ -1586,9 +1577,7 @@ fail:
                          bus_error_message(&error, r));
 
         socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
                          bus_error_message(&error, r));
 
         socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
-
-        if (cfd >= 0)
-                close_nointr_nofail(cfd);
+        safe_close(cfd);
 }
 
 static void socket_run_next(Socket *s) {
 }
 
 static void socket_run_next(Socket *s) {
@@ -1819,8 +1808,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                         break;
 
                         if (p) {
                                         break;
 
                         if (p) {
-                                if (p->fd >= 0)
-                                        close_nointr_nofail(p->fd);
+                                safe_close(p->fd);
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
@@ -1839,8 +1827,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                         break;
 
                         if (p) {
                                         break;
 
                         if (p) {
-                                if (p->fd >= 0)
-                                        close_nointr_nofail(p->fd);
+                                safe_close(p->fd);
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
@@ -1859,8 +1846,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                         break;
 
                         if (p) {
                                         break;
 
                         if (p) {
-                                if (p->fd >= 0)
-                                        close_nointr_nofail(p->fd);
+                                safe_close(p->fd);
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
@@ -1878,8 +1864,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                         break;
 
                         if (p) {
                                         break;
 
                         if (p) {
-                                if (p->fd >= 0)
-                                        close_nointr_nofail(p->fd);
+                                safe_close(p->fd);
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
@@ -1897,8 +1882,7 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                         break;
 
                         if (p) {
                                         break;
 
                         if (p) {
-                                if (p->fd >= 0)
-                                        close_nointr_nofail(p->fd);
+                                safe_close(p->fd);
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
index ce0e41d510caf2a1e3053cf571716591b72c1afc..518ec1f0a70a3e7a7da8f545a36610172e7499e0 100644 (file)
@@ -41,11 +41,10 @@ int switch_root(const char *new_root) {
                 "/sys\0"
                 "/run\0";
 
                 "/sys\0"
                 "/run\0";
 
-        int r, old_root_fd = -1;
+        _cleanup_close_ int old_root_fd = -1;
         struct stat new_root_stat;
         bool old_root_remove;
         struct stat new_root_stat;
         bool old_root_remove;
-        const char *i;
-        _cleanup_free_ char *temporary_old_root = NULL;
+        const char *i, *temporary_old_root;
 
         if (path_equal(new_root, "/"))
                 return 0;
 
         if (path_equal(new_root, "/"))
                 return 0;
@@ -56,16 +55,13 @@ int switch_root(const char *new_root) {
          * directory we choose for this, but it should be more likely
          * than not that /mnt exists and is suitable as mount point
          * and is on the same fs as the old root dir */
          * directory we choose for this, but it should be more likely
          * than not that /mnt exists and is suitable as mount point
          * and is on the same fs as the old root dir */
-        temporary_old_root = strappend(new_root, "/mnt");
-        if (!temporary_old_root)
-                return -ENOMEM;
+        temporary_old_root = strappenda(new_root, "/mnt");
 
         old_root_remove = in_initrd();
 
         if (stat(new_root, &new_root_stat) < 0) {
 
         old_root_remove = in_initrd();
 
         if (stat(new_root, &new_root_stat) < 0) {
-                r = -errno;
                 log_error("Failed to stat directory %s: %m", new_root);
                 log_error("Failed to stat directory %s: %m", new_root);
-                goto fail;
+                return -errno;
         }
 
         /* Work-around for a kernel bug: for some reason the kernel
         }
 
         /* Work-around for a kernel bug: for some reason the kernel
@@ -104,9 +100,8 @@ int switch_root(const char *new_root) {
         }
 
         if (chdir(new_root) < 0) {
         }
 
         if (chdir(new_root) < 0) {
-                r = -errno;
                 log_error("Failed to change directory to %s: %m", new_root);
                 log_error("Failed to change directory to %s: %m", new_root);
-                goto fail;
+                return -errno;
         }
 
         if (old_root_remove) {
         }
 
         if (old_root_remove) {
@@ -123,27 +118,23 @@ int switch_root(const char *new_root) {
                 /* Immediately get rid of the old root. Since we are
                  * running off it we need to do this lazily. */
                 if (umount2(temporary_old_root, MNT_DETACH) < 0) {
                 /* Immediately get rid of the old root. Since we are
                  * running off it we need to do this lazily. */
                 if (umount2(temporary_old_root, MNT_DETACH) < 0) {
-                        r = -errno;
                         log_error("Failed to umount old root dir %s: %m", temporary_old_root);
                         log_error("Failed to umount old root dir %s: %m", temporary_old_root);
-                        goto fail;
+                        return -errno;
                 }
 
         } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) {
                 }
 
         } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) {
-                r = -errno;
                 log_error("Failed to mount moving %s to /: %m", new_root);
                 log_error("Failed to mount moving %s to /: %m", new_root);
-                goto fail;
+                return -errno;
         }
 
         if (chroot(".") < 0) {
         }
 
         if (chroot(".") < 0) {
-                r = -errno;
                 log_error("Failed to change root: %m");
                 log_error("Failed to change root: %m");
-                goto fail;
+                return -errno;
         }
 
         if (chdir("/") < 0) {
         }
 
         if (chdir("/") < 0) {
-                r = -errno;
                 log_error("Failed to change directory: %m");
                 log_error("Failed to change directory: %m");
-                goto fail;
+                return -errno;
         }
 
         if (old_root_fd >= 0) {
         }
 
         if (old_root_fd >= 0) {
@@ -157,11 +148,5 @@ int switch_root(const char *new_root) {
                 }
         }
 
                 }
         }
 
-        r = 0;
-
-fail:
-        if (old_root_fd >= 0)
-                close_nointr_nofail(old_root_fd);
-
-        return r;
+        return 0;
 }
 }
index 2d166c1c92c967a69f6d060b3b8c5c8bc15faf67..d1258f0f08b299244f3635e38fa536c4598fbebd 100644 (file)
@@ -329,14 +329,14 @@ static int dm_list_get(MountPoint **head) {
 }
 
 static int delete_loopback(const char *device) {
 }
 
 static int delete_loopback(const char *device) {
-        int fd, r;
+        _cleanup_close_ int fd = -1;
+        int r;
 
 
-        if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
+        fd = open(device, O_RDONLY|O_CLOEXEC);
+        if (fd < 0)
                 return errno == ENOENT ? 0 : -errno;
 
         r = ioctl(fd, LOOP_CLR_FD, 0);
                 return errno == ENOENT ? 0 : -errno;
 
         r = ioctl(fd, LOOP_CLR_FD, 0);
-        close_nointr_nofail(fd);
-
         if (r >= 0)
                 return 1;
 
         if (r >= 0)
                 return 1;
 
index b6691c96ff05845c4fb0624259b066933f82c983..f60cc1d9126ce4ebca93c70bd6081c95e3dcef5c 100644 (file)
@@ -142,7 +142,7 @@ static int process_progress(int fd) {
 
         f = fdopen(fd, "r");
         if (!f) {
 
         f = fdopen(fd, "r");
         if (!f) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -329,15 +329,12 @@ int main(int argc, char *argv[]) {
         } else if (pid == 0) {
                 /* Child */
                 if (progress_pipe[0] >= 0)
         } else if (pid == 0) {
                 /* Child */
                 if (progress_pipe[0] >= 0)
-                        close_nointr_nofail(progress_pipe[0]);
+                        safe_close(progress_pipe[0]);
                 execv(cmdline[0], (char**) cmdline);
                 _exit(8); /* Operational error */
         }
 
                 execv(cmdline[0], (char**) cmdline);
                 _exit(8); /* Operational error */
         }
 
-        if (progress_pipe[1] >= 0) {
-                close_nointr_nofail(progress_pipe[1]);
-                progress_pipe[1] = -1;
-        }
+        progress_pipe[1] = safe_close(progress_pipe[1]);
 
         if (progress_pipe[0] >= 0) {
                 process_progress(progress_pipe[0]);
 
         if (progress_pipe[0] >= 0) {
                 process_progress(progress_pipe[0]);
index 468df35bac9eef2920a29715324f87ef002f5270..f9613ea3b1fe282ab2f1f67e85cea28b564d15fc 100644 (file)
@@ -245,7 +245,7 @@ static void fifo_free(Fifo *f) {
                 if (f->server)
                         epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL);
 
                 if (f->server)
                         epoll_ctl(f->server->epoll_fd, EPOLL_CTL_DEL, f->fd, NULL);
 
-                close_nointr_nofail(f->fd);
+                safe_close(f->fd);
         }
 
         free(f);
         }
 
         free(f);
@@ -257,8 +257,7 @@ static void server_done(Server *s) {
         while (s->fifos)
                 fifo_free(s->fifos);
 
         while (s->fifos)
                 fifo_free(s->fifos);
 
-        if (s->epoll_fd >= 0)
-                close_nointr_nofail(s->epoll_fd);
+        safe_close(s->epoll_fd);
 
         if (s->bus) {
                 sd_bus_flush(s->bus);
 
         if (s->bus) {
                 sd_bus_flush(s->bus);
index 02b75642a33ab9fb3de3e30c20fea8607db73909..60625cb6ddd311d84ec0f51a404d58e2a4c9f007 100644 (file)
@@ -152,7 +152,7 @@ int main(int argc, char *argv[]) {
         }
 
         if (fd >= 3)
         }
 
         if (fd >= 3)
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
         fd = -1;
 
 
         fd = -1;
 
@@ -170,11 +170,8 @@ int main(int argc, char *argv[]) {
         log_error("Failed to execute process: %s", strerror(-r));
 
 finish:
         log_error("Failed to execute process: %s", strerror(-r));
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
-        if (saved_stderr >= 0)
-                close_nointr_nofail(saved_stderr);
+        safe_close(fd);
+        safe_close(saved_stderr);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }
index 2823232cbfdcbb3847a076057c0655e4dd7ff300..3ed0b7ee81d1827628d0ce8a4607b9c5876541eb 100644 (file)
@@ -469,18 +469,18 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p
                 return -errno;
 
         if (fstat(fd, &st) < 0) {
                 return -errno;
 
         if (fstat(fd, &st) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         if (st.st_size < (off_t) sizeof(CatalogHeader)) {
                 return -errno;
         }
 
         if (st.st_size < (off_t) sizeof(CatalogHeader)) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -EINVAL;
         }
 
         p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
         if (p == MAP_FAILED) {
                 return -EINVAL;
         }
 
         p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
         if (p == MAP_FAILED) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -491,7 +491,7 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p
             h->incompatible_flags != 0 ||
             le64toh(h->n_items) <= 0 ||
             st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
             h->incompatible_flags != 0 ||
             le64toh(h->n_items) <= 0 ||
             st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 munmap(p, st.st_size);
                 return -EBADMSG;
         }
                 munmap(p, st.st_size);
                 return -EBADMSG;
         }
index 3bceb48ff731351afb2dff3c9d9d675ad4da44e6..bf943bc032528ca717630c5c293570f5d736409d 100644 (file)
@@ -490,8 +490,7 @@ static int run_gdb(sd_journal *j) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        close_nointr_nofail(fd);
-        fd = -1;
+        fd = safe_close(fd);
 
         pid = fork();
         if (pid < 0) {
 
         pid = fork();
         if (pid < 0) {
index f416b79a34ca3962c317c04e7e3f72aeda05f935..5ab1982bf097ab4cca7d26f6b216f0d86ae48e16 100644 (file)
@@ -418,10 +418,9 @@ finish:
         if (m)
                 munmap(m, PAGE_ALIGN(sizeof(FSSHeader)));
 
         if (m)
                 munmap(m, PAGE_ALIGN(sizeof(FSSHeader)));
 
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
+        safe_close(fd);
         free(p);
         free(p);
+
         return r;
 }
 
         return r;
 }
 
index 0e1fc7f7bc2403ba850d66e27bc192af7b820c29..f2f1f35fc3b64f4c9d7a6afb379654740ff4ecde 100644 (file)
@@ -133,9 +133,7 @@ void journal_file_close(JournalFile *f) {
         if (f->header)
                 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
 
         if (f->header)
                 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
 
-        if (f->fd >= 0)
-                close_nointr_nofail(f->fd);
-
+        safe_close(f->fd);
         free(f->path);
 
         if (f->mmap)
         free(f->path);
 
         if (f->mmap)
index ca9199f7187d79657c5c115c4b25758d1ebba340..d92e84baab5e2ad53e7df3b5744ded6ca557c6ef 100644 (file)
@@ -66,7 +66,7 @@ retry:
         fd_inc_sndbuf(fd, SNDBUF_SIZE);
 
         if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
         fd_inc_sndbuf(fd, SNDBUF_SIZE);
 
         if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 goto retry;
         }
 
                 goto retry;
         }
 
@@ -316,7 +316,7 @@ _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
 
         n = writev(buffer_fd, w, j);
         if (n < 0) {
 
         n = writev(buffer_fd, w, j);
         if (n < 0) {
-                close_nointr_nofail(buffer_fd);
+                safe_close(buffer_fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -336,7 +336,7 @@ _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
         mh.msg_controllen = cmsg->cmsg_len;
 
         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
         mh.msg_controllen = cmsg->cmsg_len;
 
         k = sendmsg(fd, &mh, MSG_NOSIGNAL);
-        close_nointr_nofail(buffer_fd);
+        safe_close(buffer_fd);
 
         if (k < 0)
                 return -errno;
 
         if (k < 0)
                 return -errno;
@@ -412,12 +412,12 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
 
         r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
         if (r < 0) {
 
         r = connect(fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
         if (r < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         if (shutdown(fd, SHUT_RD) < 0) {
                 return -errno;
         }
 
         if (shutdown(fd, SHUT_RD) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -445,12 +445,12 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
 
         r = loop_write(fd, header, l, false);
         if (r < 0) {
 
         r = loop_write(fd, header, l, false);
         if (r < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return (int) r;
         }
 
         if ((size_t) r != l) {
                 return (int) r;
         }
 
         if ((size_t) r != l) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
index 9434cc925cc58c1dc4e32cf02c165750c52b1149..31bae5a8f8f01fca57e5b34e0c2b6466f86dfafc 100644 (file)
@@ -1222,9 +1222,9 @@ int journal_file_verify(
         mmap_cache_close_fd(f->mmap, entry_fd);
         mmap_cache_close_fd(f->mmap, entry_array_fd);
 
         mmap_cache_close_fd(f->mmap, entry_fd);
         mmap_cache_close_fd(f->mmap, entry_array_fd);
 
-        close_nointr_nofail(data_fd);
-        close_nointr_nofail(entry_fd);
-        close_nointr_nofail(entry_array_fd);
+        safe_close(data_fd);
+        safe_close(entry_fd);
+        safe_close(entry_array_fd);
 
         if (first_contained)
                 *first_contained = le64toh(f->header->head_entry_realtime);
 
         if (first_contained)
                 *first_contained = le64toh(f->header->head_entry_realtime);
@@ -1247,17 +1247,17 @@ fail:
 
         if (data_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, data_fd);
 
         if (data_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, data_fd);
-                close_nointr_nofail(data_fd);
+                safe_close(data_fd);
         }
 
         if (entry_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, entry_fd);
         }
 
         if (entry_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, entry_fd);
-                close_nointr_nofail(entry_fd);
+                safe_close(entry_fd);
         }
 
         if (entry_array_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, entry_array_fd);
         }
 
         if (entry_array_fd >= 0) {
                 mmap_cache_close_fd(f->mmap, entry_array_fd);
-                close_nointr_nofail(entry_array_fd);
+                safe_close(entry_array_fd);
         }
 
         return r;
         }
 
         return r;
index 0619b256b9598a6685eb7205126d4eb7abbd9e90..019629047b02a5bc445adec70f3909d86881311a 100644 (file)
@@ -1290,7 +1290,7 @@ static int setup_keys(void) {
         n = now(CLOCK_REALTIME);
         n /= arg_interval;
 
         n = now(CLOCK_REALTIME);
         n /= arg_interval;
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
         fd = mkostemp_safe(k, O_WRONLY|O_CLOEXEC);
         if (fd < 0) {
                 log_error("Failed to open %s: %m", k);
         fd = mkostemp_safe(k, O_WRONLY|O_CLOEXEC);
         if (fd < 0) {
                 log_error("Failed to open %s: %m", k);
@@ -1389,8 +1389,7 @@ static int setup_keys(void) {
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         if (k) {
                 unlink(k);
 
         if (k) {
                 unlink(k);
index 35da52af2afa7a663ba24ee1074339f334ff55d2..3db5fc50a17cb03998b5243ab0f0d532c27f70bd 100644 (file)
@@ -107,5 +107,5 @@ void server_forward_console(
         if (writev(fd, iovec, n) < 0)
                 log_debug("Failed to write to %s for logging: %m", tty);
 
         if (writev(fd, iovec, n) < 0)
                 log_debug("Failed to write to %s for logging: %m", tty);
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
 }
 }
index 05b128f843db940ebbdc5419299718a1cbae3a4e..35948ea754c4e510509f401c27d6c5a6184e2016 100644 (file)
@@ -428,19 +428,14 @@ int server_open_dev_kmsg(Server *s) {
         return 0;
 
 fail:
         return 0;
 
 fail:
-        if (s->dev_kmsg_event_source)
-                s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
-
-        if (s->dev_kmsg_fd >= 0) {
-                close_nointr_nofail(s->dev_kmsg_fd);
-                s->dev_kmsg_fd = -1;
-        }
+        s->dev_kmsg_event_source = sd_event_source_unref(s->dev_kmsg_event_source);
+        s->dev_kmsg_fd = safe_close(s->dev_kmsg_fd);
 
         return r;
 }
 
 int server_open_kernel_seqnum(Server *s) {
 
         return r;
 }
 
 int server_open_kernel_seqnum(Server *s) {
-        int fd;
+        _cleanup_close_ int fd;
         uint64_t *p;
 
         assert(s);
         uint64_t *p;
 
         assert(s);
@@ -457,18 +452,15 @@ int server_open_kernel_seqnum(Server *s) {
 
         if (posix_fallocate(fd, 0, sizeof(uint64_t)) < 0) {
                 log_error("Failed to allocate sequential number file, ignoring: %m");
 
         if (posix_fallocate(fd, 0, sizeof(uint64_t)) < 0) {
                 log_error("Failed to allocate sequential number file, ignoring: %m");
-                close_nointr_nofail(fd);
                 return 0;
         }
 
         p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
         if (p == MAP_FAILED) {
                 log_error("Failed to map sequential number file, ignoring: %m");
                 return 0;
         }
 
         p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
         if (p == MAP_FAILED) {
                 log_error("Failed to map sequential number file, ignoring: %m");
-                close_nointr_nofail(fd);
                 return 0;
         }
 
                 return 0;
         }
 
-        close_nointr_nofail(fd);
         s->kernel_seqnum = p;
 
         return 0;
         s->kernel_seqnum = p;
 
         return 0;
index ef39d0a60203c4a2b217ec862ba57b6c618cea43..c2a60d5e83c701838748bfdd05d8428ec799cb6b 100644 (file)
@@ -1432,8 +1432,7 @@ static int server_open_hostname(Server *s) {
                 if (r == -EPERM) {
                         log_warning("Failed to register hostname fd in event loop: %s. Ignoring.",
                                         strerror(-r));
                 if (r == -EPERM) {
                         log_warning("Failed to register hostname fd in event loop: %s. Ignoring.",
                                         strerror(-r));
-                        close_nointr_nofail(s->hostname_fd);
-                        s->hostname_fd = -1;
+                        s->hostname_fd = safe_close(s->hostname_fd);
                         return 0;
                 }
 
                         return 0;
                 }
 
@@ -1643,20 +1642,11 @@ void server_done(Server *s) {
         sd_event_source_unref(s->hostname_event_source);
         sd_event_unref(s->event);
 
         sd_event_source_unref(s->hostname_event_source);
         sd_event_unref(s->event);
 
-        if (s->syslog_fd >= 0)
-                close_nointr_nofail(s->syslog_fd);
-
-        if (s->native_fd >= 0)
-                close_nointr_nofail(s->native_fd);
-
-        if (s->stdout_fd >= 0)
-                close_nointr_nofail(s->stdout_fd);
-
-        if (s->dev_kmsg_fd >= 0)
-                close_nointr_nofail(s->dev_kmsg_fd);
-
-        if (s->hostname_fd >= 0)
-                close_nointr_nofail(s->hostname_fd);
+        safe_close(s->syslog_fd);
+        safe_close(s->native_fd);
+        safe_close(s->stdout_fd);
+        safe_close(s->dev_kmsg_fd);
+        safe_close(s->hostname_fd);
 
         if (s->rate_limit)
                 journal_rate_limit_free(s->rate_limit);
 
         if (s->rate_limit)
                 journal_rate_limit_free(s->rate_limit);
index c46ffe5d455e0345b55f805c49f1255a6ecf5361..89da150a605bacdb87e79a29a28eb787bc02dbcf 100644 (file)
@@ -341,8 +341,7 @@ void stdout_stream_free(StdoutStream *s) {
                 s->event_source = sd_event_source_unref(s->event_source);
         }
 
                 s->event_source = sd_event_source_unref(s->event_source);
         }
 
-        if (s->fd >= 0)
-                close_nointr_nofail(s->fd);
+        safe_close(s->fd);
 
 #ifdef HAVE_SELINUX
         if (s->security_context)
 
 #ifdef HAVE_SELINUX
         if (s->security_context)
@@ -377,13 +376,13 @@ static int stdout_stream_new(sd_event_source *es, int listen_fd, uint32_t revent
 
         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
                 log_warning("Too many stdout streams, refusing connection.");
 
         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
                 log_warning("Too many stdout streams, refusing connection.");
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return 0;
         }
 
         stream = new0(StdoutStream, 1);
         if (!stream) {
                 return 0;
         }
 
         stream = new0(StdoutStream, 1);
         if (!stream) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return log_oom();
         }
 
                 return log_oom();
         }
 
index b54bc21090bc14b9cc3afe45af857bb9670487d4..7587211506f5f02d5503dc1c730201ebf88450aa 100644 (file)
@@ -1850,8 +1850,7 @@ _public_ void sd_journal_close(sd_journal *j) {
         hashmap_free(j->directories_by_path);
         hashmap_free(j->directories_by_wd);
 
         hashmap_free(j->directories_by_path);
         hashmap_free(j->directories_by_wd);
 
-        if (j->inotify_fd >= 0)
-                close_nointr_nofail(j->inotify_fd);
+        safe_close(j->inotify_fd);
 
         if (j->mmap) {
                 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
 
         if (j->mmap) {
                 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
index 05400742074e1e1ac122617267892bb2ee9a30fd..3b181c679493daf62c061afd6bc09fde0e1dc0e0 100644 (file)
@@ -48,7 +48,7 @@ static void bit_toggle(const char *fn, uint64_t p) {
         r = pwrite(fd, &b, 1, p/8);
         assert(r == 1);
 
         r = pwrite(fd, &b, 1, p/8);
         assert(r == 1);
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
 }
 
 static int raw_verify(const char *fn, const char *verification_key) {
 }
 
 static int raw_verify(const char *fn, const char *verification_key) {
index 7d03bfe9d15525cada49d31fd005c612d3f92d88..b7bb260fcfdce9516c4ce010b763861c240d5e34 100644 (file)
@@ -72,9 +72,9 @@ int main(int argc, char *argv[]) {
 
         mmap_cache_unref(m);
 
 
         mmap_cache_unref(m);
 
-        close_nointr_nofail(x);
-        close_nointr_nofail(y);
-        close_nointr_nofail(z);
+        safe_close(x);
+        safe_close(y);
+        safe_close(z);
 
         return 0;
 }
 
         return 0;
 }
index 934e8bf13ee5837755958870c6129c3a0caa4e29..29e915c473f6026781483ebbf9ba370c9d339f11 100644 (file)
@@ -52,7 +52,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
                 return -errno;
 
         if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
                 return -errno;
 
         if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
-                close_nointr_nofail(s);
+                safe_close(s);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -73,7 +73,7 @@ int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
                 return -errno;
 
         if (bind(s, &src.sa, sizeof(src.in)) < 0) {
                 return -errno;
 
         if (bind(s, &src.sa, sizeof(src.in)) < 0) {
-                close_nointr_nofail(s);
+                safe_close(s);
                 return -errno;
         }
 
                 return -errno;
         }
 
index 51734d7e4dc982988d2492ee6bb118be14985217..68666fb71dd556bc8da157b74ef49d76fb7b3378 100644 (file)
@@ -48,7 +48,7 @@ int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
         link->ll.sll_halen = ETH_ALEN;
 
         if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
         link->ll.sll_halen = ETH_ALEN;
 
         if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
-                close_nointr_nofail(s);
+                safe_close(s);
                 return -errno;
         }
 
                 return -errno;
         }
 
index 5374db74cb9b00e38c360f40a9ca2e2ca5f7371d..2d3af2f1874cf7cc07075a9978cc9b34281b9622 100644 (file)
@@ -184,9 +184,7 @@ static int client_stop(sd_dhcp_client *client, int error) {
         client->receive_message =
                 sd_event_source_unref(client->receive_message);
 
         client->receive_message =
                 sd_event_source_unref(client->receive_message);
 
-        if (client->fd >= 0)
-                close_nointr_nofail(client->fd);
-        client->fd = -1;
+        client->fd = safe_close(client->fd);
 
         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
 
 
         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
 
@@ -546,12 +544,8 @@ static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
         sd_dhcp_client *client = userdata;
         int r;
 
         sd_dhcp_client *client = userdata;
         int r;
 
-        if (client->fd >= 0) {
-                client->receive_message =
-                        sd_event_source_unref(client->receive_message);
-                close_nointr_nofail(client->fd);
-                client->fd = -1;
-        }
+        client->receive_message = sd_event_source_unref(client->receive_message);
+        client->fd = safe_close(client->fd);
 
         client->state = DHCP_STATE_REBINDING;
         client->attempt = 1;
 
         client->state = DHCP_STATE_REBINDING;
         client->attempt = 1;
@@ -849,8 +843,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
 
                         client->receive_message =
                                 sd_event_source_unref(client->receive_message);
 
                         client->receive_message =
                                 sd_event_source_unref(client->receive_message);
-                        close_nointr_nofail(client->fd);
-                        client->fd = -1;
+                        client->fd = safe_close(client->fd);
                 }
 
                 r = 0;
                 }
 
                 r = 0;
index c6f6e014310a2c7fbafd29717735cabf0c0d8464..689dce9adffba972f5996106dbfed22c4d0ae275 100644 (file)
@@ -113,9 +113,7 @@ static int ipv4ll_stop(sd_ipv4ll *ll, int event) {
         assert(ll);
 
         ll->receive_message = sd_event_source_unref(ll->receive_message);
         assert(ll);
 
         ll->receive_message = sd_event_source_unref(ll->receive_message);
-        if (ll->fd >= 0)
-                close_nointr_nofail(ll->fd);
-        ll->fd = -1;
+        ll->fd = safe_close(ll->fd);
 
         ll->timer = sd_event_source_unref(ll->timer);
 
 
         ll->timer = sd_event_source_unref(ll->timer);
 
index d330363b6f89dad199be3103ee36991e113dfcd2..2cd0e1f99ed017ef54db34ba87c9027d32f21199 100644 (file)
@@ -154,8 +154,7 @@ int bus_container_connect_kernel(sd_bus *b) {
         if (child == 0) {
                 pid_t grandchild;
 
         if (child == 0) {
                 pid_t grandchild;
 
-                close_nointr_nofail(pair[0]);
-                pair[0] = -1;
+                pair[0] = safe_close(pair[0]);
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
@@ -202,8 +201,7 @@ int bus_container_connect_kernel(sd_bus *b) {
                 _exit(si.si_status);
         }
 
                 _exit(si.si_status);
         }
 
-        close_nointr_nofail(pair[1]);
-        pair[1] = -1;
+        pair[1] = safe_close(pair[1]);
 
         r = wait_for_terminate(child, &si);
         if (r < 0)
 
         r = wait_for_terminate(child, &si);
         if (r < 0)
index 5c955f4a07b5d21e8d457f655f43446fc06c998c..e229baf2664f9d99e4b9e8cf3528d5e47850d0e8 100644 (file)
@@ -811,7 +811,7 @@ static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
                 if (d->type == KDBUS_ITEM_FDS)
                         close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
                 else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
                 if (d->type == KDBUS_ITEM_FDS)
                         close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
                 else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
-                        close_nointr_nofail(d->memfd.fd);
+                        safe_close(d->memfd.fd);
         }
 }
 
         }
 }
 
@@ -1165,7 +1165,7 @@ static void close_and_munmap(int fd, void *address, size_t size) {
         if (size > 0)
                 assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
 
         if (size > 0)
                 assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
 }
 
 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t mapped, size_t allocated) {
 }
 
 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t mapped, size_t allocated) {
@@ -1313,14 +1313,14 @@ int bus_kernel_create_bus(const char *name, bool world, char **s) {
         make->flags = world ? KDBUS_MAKE_ACCESS_WORLD : 0;
 
         if (ioctl(fd, KDBUS_CMD_BUS_MAKE, make) < 0) {
         make->flags = world ? KDBUS_MAKE_ACCESS_WORLD : 0;
 
         if (ioctl(fd, KDBUS_CMD_BUS_MAKE, make) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         /* The higher 32bit of the flags field are considered
          * 'incompatible flags'. Refuse them all for now. */
         if (make->flags > 0xFFFFFFFFULL) {
                 return -errno;
         }
 
         /* The higher 32bit of the flags field are considered
          * 'incompatible flags'. Refuse them all for now. */
         if (make->flags > 0xFFFFFFFFULL) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTSUP;
         }
 
                 return -ENOTSUP;
         }
 
@@ -1329,7 +1329,7 @@ int bus_kernel_create_bus(const char *name, bool world, char **s) {
 
                 p = strjoin("/dev/kdbus/", n->str, "/bus", NULL);
                 if (!p) {
 
                 p = strjoin("/dev/kdbus/", n->str, "/bus", NULL);
                 if (!p) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -ENOMEM;
                 }
 
                         return -ENOMEM;
                 }
 
@@ -1424,7 +1424,7 @@ int bus_kernel_create_starter(const char *bus, const char *name, BusNamePolicy *
         hello->pool_size = KDBUS_POOL_SIZE;
 
         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
         hello->pool_size = KDBUS_POOL_SIZE;
 
         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -1432,12 +1432,12 @@ int bus_kernel_create_starter(const char *bus, const char *name, BusNamePolicy *
          * 'incompatible flags'. Refuse them all for now. */
         if (hello->bus_flags > 0xFFFFFFFFULL ||
             hello->conn_flags > 0xFFFFFFFFULL) {
          * 'incompatible flags'. Refuse them all for now. */
         if (hello->bus_flags > 0xFFFFFFFFULL ||
             hello->conn_flags > 0xFFFFFFFFULL) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTSUP;
         }
 
         if (!bloom_validate_parameters((size_t) hello->bloom.size, (unsigned) hello->bloom.n_hash)) {
                 return -ENOTSUP;
         }
 
         if (!bloom_validate_parameters((size_t) hello->bloom.size, (unsigned) hello->bloom.n_hash)) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTSUP;
         }
 
                 return -ENOTSUP;
         }
 
@@ -1469,14 +1469,14 @@ int bus_kernel_create_domain(const char *name, char **s) {
         make->flags = KDBUS_MAKE_ACCESS_WORLD;
 
         if (ioctl(fd, KDBUS_CMD_DOMAIN_MAKE, make) < 0) {
         make->flags = KDBUS_MAKE_ACCESS_WORLD;
 
         if (ioctl(fd, KDBUS_CMD_DOMAIN_MAKE, make) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         /* The higher 32bit of the flags field are considered
          * 'incompatible flags'. Refuse them all for now. */
         if (make->flags > 0xFFFFFFFFULL) {
                 return -errno;
         }
 
         /* The higher 32bit of the flags field are considered
          * 'incompatible flags'. Refuse them all for now. */
         if (make->flags > 0xFFFFFFFFULL) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTSUP;
         }
 
                 return -ENOTSUP;
         }
 
@@ -1485,7 +1485,7 @@ int bus_kernel_create_domain(const char *name, char **s) {
 
                 p = strappend("/dev/kdbus/domain/", name);
                 if (!p) {
 
                 p = strappend("/dev/kdbus/domain/", name);
                 if (!p) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -ENOMEM;
                 }
 
                         return -ENOMEM;
                 }
 
@@ -1515,7 +1515,7 @@ int bus_kernel_create_monitor(const char *bus) {
         hello->pool_size = KDBUS_POOL_SIZE;
 
         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
         hello->pool_size = KDBUS_POOL_SIZE;
 
         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -1523,7 +1523,7 @@ int bus_kernel_create_monitor(const char *bus) {
          * 'incompatible flags'. Refuse them all for now. */
         if (hello->bus_flags > 0xFFFFFFFFULL ||
             hello->conn_flags > 0xFFFFFFFFULL) {
          * 'incompatible flags'. Refuse them all for now. */
         if (hello->bus_flags > 0xFFFFFFFFULL ||
             hello->conn_flags > 0xFFFFFFFFULL) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTSUP;
         }
 
                 return -ENOTSUP;
         }
 
index e32f2b827fa074fdb27a8d422ad1472ce3d856fa..4fcc693543a8d0ada16bd4a9454e5a7596cf7284 100644 (file)
@@ -70,7 +70,7 @@ static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
                         if (part->mapped > 0)
                                 assert_se(munmap(part->data, part->mapped) == 0);
 
                         if (part->mapped > 0)
                                 assert_se(munmap(part->data, part->mapped) == 0);
 
-                        close_nointr_nofail(part->memfd);
+                        safe_close(part->memfd);
                 }
 
         } else if (part->munmap_this)
                 }
 
         } else if (part->munmap_this)
@@ -1274,7 +1274,7 @@ static int message_push_fd(sd_bus_message *m, int fd) {
         f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
         if (!f) {
                 m->poisoned = true;
         f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
         if (!f) {
                 m->poisoned = true;
-                close_nointr_nofail(copy);
+                safe_close(copy);
                 return -ENOMEM;
         }
 
                 return -ENOMEM;
         }
 
index 016f8a1c9f9d34a605d6093e753fd34fd073fcd3..bccf50122213aa43509c95fe856ddcf9bbd79bec 100644 (file)
@@ -750,7 +750,7 @@ int bus_socket_exec(sd_bus *b) {
                 assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO);
 
                 if (s[1] != STDIN_FILENO && s[1] != STDOUT_FILENO)
                 assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO);
 
                 if (s[1] != STDIN_FILENO && s[1] != STDOUT_FILENO)
-                        close_nointr_nofail(s[1]);
+                        safe_close(s[1]);
 
                 fd_cloexec(STDIN_FILENO, false);
                 fd_cloexec(STDOUT_FILENO, false);
 
                 fd_cloexec(STDIN_FILENO, false);
                 fd_cloexec(STDOUT_FILENO, false);
@@ -767,7 +767,7 @@ int bus_socket_exec(sd_bus *b) {
                 _exit(EXIT_FAILURE);
         }
 
                 _exit(EXIT_FAILURE);
         }
 
-        close_nointr_nofail(s[1]);
+        safe_close(s[1]);
         b->output_fd = b->input_fd = s[0];
 
         bus_socket_setup(b);
         b->output_fd = b->input_fd = s[0];
 
         bus_socket_setup(b);
index ba8a8a244a292b73366c1436b6d5b714db4a286e..2794a4483fafeb83c98884e79761993e268960e0 100644 (file)
@@ -63,10 +63,10 @@ static void bus_close_fds(sd_bus *b) {
         detach_io_events(b);
 
         if (b->input_fd >= 0)
         detach_io_events(b);
 
         if (b->input_fd >= 0)
-                close_nointr_nofail(b->input_fd);
+                safe_close(b->input_fd);
 
         if (b->output_fd >= 0 && b->output_fd != b->input_fd)
 
         if (b->output_fd >= 0 && b->output_fd != b->input_fd)
-                close_nointr_nofail(b->output_fd);
+                safe_close(b->output_fd);
 
         b->input_fd = b->output_fd = -1;
 }
 
         b->input_fd = b->output_fd = -1;
 }
index 8f9e236392511220a62f15db89822d5bb1eedcb9..fcf3e73124ab27319eb1e5e305e8500317a97d36 100644 (file)
@@ -107,7 +107,7 @@ _public_ int sd_memfd_new(sd_memfd **m, const char *name) {
 
         n = new0(struct sd_memfd, 1);
         if (!n) {
 
         n = new0(struct sd_memfd, 1);
         if (!n) {
-                close_nointr_nofail(cmd->fd);
+                safe_close(cmd->fd);
                 return -ENOMEM;
         }
 
                 return -ENOMEM;
         }
 
@@ -144,7 +144,7 @@ _public_ void sd_memfd_free(sd_memfd *m) {
         if (m->f)
                 fclose(m->f);
         else
         if (m->f)
                 fclose(m->f);
         else
-                close_nointr_nofail(m->fd);
+                safe_close(m->fd);
 
         free(m);
 }
 
         free(m);
 }
index 113d15ba9743454e1e2e3b9e113752b3e051e77a..1b9d98fa7559b6f5bc71b549bbbad2618604b4a7 100644 (file)
@@ -232,7 +232,7 @@ static int server(sd_bus *bus) {
 
                         if (write(fd, &x, 1) < 0) {
                                 log_error("Failed to write to fd: %m");
 
                         if (write(fd, &x, 1) < 0) {
                                 log_error("Failed to write to fd: %m");
-                                close_nointr_nofail(fd);
+                                safe_close(fd);
                                 goto fail;
                         }
 
                                 goto fail;
                         }
 
index bfeee59d2fd8be18e5d6be72b21ceb5e04185611..90257d4f3c94bf3212bf50ac701acdb20a95cb44 100644 (file)
@@ -270,7 +270,7 @@ int main(int argc, char *argv[]) {
                 CPU_SET(0, &cpuset);
                 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
 
                 CPU_SET(0, &cpuset);
                 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
 
-                close_nointr_nofail(bus_ref);
+                safe_close(bus_ref);
                 sd_bus_unref(b);
 
                 switch (mode) {
                 sd_bus_unref(b);
 
                 switch (mode) {
index 2f0f5aa00b0fe392e754683b59be856805759208..34d000f1c654d31e4c022ea63a1818a79c7cfba5 100644 (file)
@@ -137,14 +137,12 @@ int main(int argc, char *argv[]) {
 
         assert_se(write(pipe_fds[1], "x", 1) == 1);
 
 
         assert_se(write(pipe_fds[1], "x", 1) == 1);
 
-        close_nointr_nofail(pipe_fds[1]);
-        pipe_fds[1] = -1;
+        pipe_fds[1] = safe_close(pipe_fds[1]);
 
         r = sd_bus_message_append(m, "h", pipe_fds[0]);
         assert_se(r >= 0);
 
 
         r = sd_bus_message_append(m, "h", pipe_fds[0]);
         assert_se(r >= 0);
 
-        close_nointr_nofail(pipe_fds[0]);
-        pipe_fds[0] = -1;
+        pipe_fds[0] = safe_close(pipe_fds[0]);
 
         r = sd_bus_send(b, m, NULL);
         assert_se(r >= 0);
 
         r = sd_bus_send(b, m, NULL);
         assert_se(r >= 0);
index db7643347f3f45c9f2e8584548a43d48c0c93088..3bda7f31adf8c97ec7b1d77705a2ad2710244f0e 100644 (file)
@@ -331,20 +331,11 @@ static void event_free(sd_event *e) {
         if (e->default_event_ptr)
                 *(e->default_event_ptr) = NULL;
 
         if (e->default_event_ptr)
                 *(e->default_event_ptr) = NULL;
 
-        if (e->epoll_fd >= 0)
-                close_nointr_nofail(e->epoll_fd);
-
-        if (e->signal_fd >= 0)
-                close_nointr_nofail(e->signal_fd);
-
-        if (e->realtime_fd >= 0)
-                close_nointr_nofail(e->realtime_fd);
-
-        if (e->monotonic_fd >= 0)
-                close_nointr_nofail(e->monotonic_fd);
-
-        if (e->watchdog_fd >= 0)
-                close_nointr_nofail(e->watchdog_fd);
+        safe_close(e->epoll_fd);
+        safe_close(e->signal_fd);
+        safe_close(e->realtime_fd);
+        safe_close(e->monotonic_fd);
+        safe_close(e->watchdog_fd);
 
         prioq_free(e->pending);
         prioq_free(e->prepare);
 
         prioq_free(e->pending);
         prioq_free(e->prepare);
@@ -673,7 +664,7 @@ static int event_setup_timer_fd(
 
         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
         if (r < 0) {
 
         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
         if (r < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -809,9 +800,7 @@ static int event_update_signal_fd(sd_event *e) {
 
         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
         if (r < 0) {
 
         r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
         if (r < 0) {
-                close_nointr_nofail(e->signal_fd);
-                e->signal_fd = -1;
-
+                e->signal_fd = safe_close(e->signal_fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -2272,8 +2261,7 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) {
         } else {
                 if (e->watchdog_fd >= 0) {
                         epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
         } else {
                 if (e->watchdog_fd >= 0) {
                         epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
-                        close_nointr_nofail(e->watchdog_fd);
-                        e->watchdog_fd = -1;
+                        e->watchdog_fd = safe_close(e->watchdog_fd);
                 }
         }
 
                 }
         }
 
@@ -2281,8 +2269,7 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) {
         return e->watchdog;
 
 fail:
         return e->watchdog;
 
 fail:
-        close_nointr_nofail(e->watchdog_fd);
-        e->watchdog_fd = -1;
+        e->watchdog_fd = safe_close(e->watchdog_fd);
         return r;
 }
 
         return r;
 }
 
index d24b2ed1fdd698f1b62bc04d5cdbe54b89c20df4..39445a191b0b7c0c2ef36f64cc5aa3545ac47361 100644 (file)
@@ -775,7 +775,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "seat")) {
                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
         if (!category || streq(category, "seat")) {
                 k = inotify_add_watch(fd, "/run/systemd/seats/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                         return -errno;
                 }
 
@@ -785,7 +785,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "session")) {
                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
         if (!category || streq(category, "session")) {
                 k = inotify_add_watch(fd, "/run/systemd/sessions/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                         return -errno;
                 }
 
@@ -795,7 +795,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "uid")) {
                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
         if (!category || streq(category, "uid")) {
                 k = inotify_add_watch(fd, "/run/systemd/users/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                         return -errno;
                 }
 
@@ -805,7 +805,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
         if (!category || streq(category, "machine")) {
                 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
         if (!category || streq(category, "machine")) {
                 k = inotify_add_watch(fd, "/run/systemd/machines/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                         return -errno;
                 }
 
index f05fe10b6ae31d236d55e261cca2512f36bf6e5c..d81012467f209b6dddcab3609e829849c887c45f 100644 (file)
@@ -593,8 +593,7 @@ _public_ sd_resolve* sd_resolve_unref(sd_resolve *resolve) {
 
         /* Close all communication channels */
         for (i = 0; i < _FD_MAX; i++)
 
         /* Close all communication channels */
         for (i = 0; i < _FD_MAX; i++)
-                if (resolve->fds[i] >= 0)
-                        close_nointr_nofail(resolve->fds[i]);
+                safe_close(resolve->fds[i]);
 
         for (i = 0; i < QUERIES_MAX && resolve->n_queries > 0; i++)
                 if (resolve->queries[i])
 
         for (i = 0; i < QUERIES_MAX && resolve->n_queries > 0; i++)
                 if (resolve->queries[i])
index 1c2c8902f95140efc2c169cdf3f1cb426b78edd2..e5610b43358d97058e340b8ae2808b0fc85268df 100644 (file)
@@ -132,9 +132,7 @@ sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
                         free(f);
                 }
 
                         free(f);
                 }
 
-                if (rtnl->fd >= 0)
-                        close_nointr_nofail(rtnl->fd);
-
+                safe_close(rtnl->fd);
                 free(rtnl);
         }
 
                 free(rtnl);
         }
 
index 060978dd349ac8b6be5df5386c9366e3745a8136..2561d13c673f55c7ab807369d01a28bbe4621316 100644 (file)
@@ -72,7 +72,7 @@ void button_free(Button *b) {
         if (b->fd >= 0) {
                 /* If the device has been unplugged close() returns
                  * ENODEV, let's ignore this, hence we don't use
         if (b->fd >= 0) {
                 /* If the device has been unplugged close() returns
                  * ENODEV, let's ignore this, hence we don't use
-                 * close_nointr_nofail() */
+                 * safe_close() */
                 close(b->fd);
         }
 
                 close(b->fd);
         }
 
index d19d648129c04792f6d6b8ec6892117f38113206..8b329abd793f39988aea17c527a58338b91d4256 100644 (file)
@@ -253,8 +253,7 @@ int inhibitor_load(Inhibitor *i) {
                 int fd;
 
                 fd = inhibitor_create_fifo(i);
                 int fd;
 
                 fd = inhibitor_create_fifo(i);
-                if (fd >= 0)
-                        close_nointr_nofail(fd);
+                safe_close(fd);
         }
 
         return 0;
         }
 
         return 0;
@@ -320,13 +319,8 @@ int inhibitor_create_fifo(Inhibitor *i) {
 void inhibitor_remove_fifo(Inhibitor *i) {
         assert(i);
 
 void inhibitor_remove_fifo(Inhibitor *i) {
         assert(i);
 
-        if (i->event_source)
-                i->event_source = sd_event_source_unref(i->event_source);
-
-        if (i->fifo_fd >= 0) {
-                close_nointr_nofail(i->fifo_fd);
-                i->fifo_fd = -1;
-        }
+        i->event_source = sd_event_source_unref(i->event_source);
+        i->fifo_fd = safe_close(i->fifo_fd);
 
         if (i->fifo_path) {
                 unlink(i->fifo_path);
 
         if (i->fifo_path) {
                 unlink(i->fifo_path);
index 33ab09ea523c0d0ded9723b2293ccfe169e486ff..8c517f46a751ca044853676ba20e289f9129f72c 100644 (file)
@@ -407,8 +407,7 @@ int session_load(Session *s) {
                    trigger the EOF. */
 
                 fd = session_create_fifo(s);
                    trigger the EOF. */
 
                 fd = session_create_fifo(s);
-                if (fd >= 0)
-                        close_nointr_nofail(fd);
+                safe_close(fd);
         }
 
         if (realtime) {
         }
 
         if (realtime) {
@@ -864,13 +863,8 @@ int session_create_fifo(Session *s) {
 static void session_remove_fifo(Session *s) {
         assert(s);
 
 static void session_remove_fifo(Session *s) {
         assert(s);
 
-        if (s->fifo_event_source)
-                s->fifo_event_source = sd_event_source_unref(s->fifo_event_source);
-
-        if (s->fifo_fd >= 0) {
-                close_nointr_nofail(s->fifo_fd);
-                s->fifo_fd = -1;
-        }
+        s->fifo_event_source = sd_event_source_unref(s->fifo_event_source);
+        s->fifo_fd = safe_close(s->fifo_fd);
 
         if (s->fifo_path) {
                 unlink(s->fifo_path);
 
         if (s->fifo_path) {
                 unlink(s->fifo_path);
@@ -950,7 +944,7 @@ static int session_open_vt(Session *s) {
         s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
         if (s->vtfd < 0) {
                 log_error("cannot open VT %s of session %s: %m", path, s->id);
         s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
         if (s->vtfd < 0) {
                 log_error("cannot open VT %s of session %s: %m", path, s->id);
-                return -1;
+                return -errno;
         }
 
         return s->vtfd;
         }
 
         return s->vtfd;
@@ -1022,13 +1016,13 @@ void session_restore_vt(Session *s) {
 
         if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && *utf8 == '1')
                 kb = K_UNICODE;
 
         if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && *utf8 == '1')
                 kb = K_UNICODE;
+
         ioctl(vt, KDSKBMODE, kb);
 
         mode.mode = VT_AUTO;
         ioctl(vt, VT_SETMODE, &mode);
 
         ioctl(vt, KDSKBMODE, kb);
 
         mode.mode = VT_AUTO;
         ioctl(vt, VT_SETMODE, &mode);
 
-        close_nointr_nofail(vt);
-        s->vtfd = -1;
+        s->vtfd = safe_close(s->vtfd);
 }
 
 bool session_is_controller(Session *s, const char *sender) {
 }
 
 bool session_is_controller(Session *s, const char *sender) {
index 8ba8a9155bdedb6d90371b0a507084f0e85cbde4..db9882323ff03a70a7a50fe7f8c9b6a55a5d3762 100644 (file)
@@ -149,8 +149,7 @@ void manager_free(Manager *m) {
         sd_event_source_unref(m->udev_button_event_source);
         sd_event_source_unref(m->lid_switch_ignore_event_source);
 
         sd_event_source_unref(m->udev_button_event_source);
         sd_event_source_unref(m->lid_switch_ignore_event_source);
 
-        if (m->console_active_fd >= 0)
-                close_nointr_nofail(m->console_active_fd);
+        safe_close(m->console_active_fd);
 
         if (m->udev_seat_monitor)
                 udev_monitor_unref(m->udev_seat_monitor);
 
         if (m->udev_seat_monitor)
                 udev_monitor_unref(m->udev_seat_monitor);
@@ -169,8 +168,7 @@ void manager_free(Manager *m) {
         sd_bus_unref(m->bus);
         sd_event_unref(m->event);
 
         sd_bus_unref(m->bus);
         sd_event_unref(m->event);
 
-        if (m->reserve_vt_fd >= 0)
-                close_nointr_nofail(m->reserve_vt_fd);
+        safe_close(m->reserve_vt_fd);
 
         strv_free(m->kill_only_users);
         strv_free(m->kill_exclude_users);
 
         strv_free(m->kill_only_users);
         strv_free(m->kill_exclude_users);
index 195d4d574e8605aa30402835e5232b51d1e5e20c..9873dd547ae166e18f279b8987c3a2184baf9d60 100644 (file)
@@ -484,7 +484,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
                 if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
                 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
                 if (r != PAM_SUCCESS) {
                         pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
-                        close_nointr_nofail(session_fd);
+                        safe_close(session_fd);
                         return r;
                 }
         }
                         return r;
                 }
         }
index 70b8314e3f8481d47600804891e89e38c5d56ef7..70780c30afc18f014a68c2b7fd29f52fa02041a1 100644 (file)
@@ -101,11 +101,11 @@ int main(int argc, char*argv[]) {
         assert(fd2 >= 0);
         print_inhibitors(bus);
 
         assert(fd2 >= 0);
         print_inhibitors(bus);
 
-        close_nointr_nofail(fd1);
+        safe_close(fd1);
         sleep(1);
         print_inhibitors(bus);
 
         sleep(1);
         print_inhibitors(bus);
 
-        close_nointr_nofail(fd2);
+        safe_close(fd2);
         sleep(1);
         print_inhibitors(bus);
 
         sleep(1);
         print_inhibitors(bus);
 
index 2183d5c397baed61b62f2fb824f8dc6d6424a753..d812ef2600dcd08e30f344f1c5f5b17c6258336a 100644 (file)
@@ -439,8 +439,7 @@ static int openpt_in_namespace(pid_t pid, int flags) {
                 return -errno;
 
         if (child == 0) {
                 return -errno;
 
         if (child == 0) {
-                close_nointr_nofail(pair[0]);
-                pair[0] = -1;
+                pair[0] = safe_close(pair[0]);
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
@@ -464,8 +463,7 @@ static int openpt_in_namespace(pid_t pid, int flags) {
                 _exit(EXIT_SUCCESS);
         }
 
                 _exit(EXIT_SUCCESS);
         }
 
-        close_nointr_nofail(pair[1]);
-        pair[1] = -1;
+        pair[1] = safe_close(pair[1]);
 
         r = wait_for_terminate(child, &si);
         if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
 
         r = wait_for_terminate(child, &si);
         if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
index 23af9409035877d8205fa951d5afb70599241950..3b541a2764e85db2b9c5f349cf42865cfbb3743f 100644 (file)
@@ -169,7 +169,7 @@ _public_ int sd_network_monitor_new(const char *category, sd_network_monitor **m
         if (!category || streq(category, "netif")) {
                 k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
         if (!category || streq(category, "netif")) {
                 k = inotify_add_watch(fd, "/run/systemd/network/links/", IN_MOVED_TO|IN_DELETE);
                 if (k < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                         return -errno;
                 }
 
index bb33de3b7fc7fbe90f42f44a16f28ec59ab59e0c..2d627db9c7fc364af4f9d2c9ab233fcfd1d4b807 100644 (file)
@@ -986,7 +986,7 @@ static int setup_kmsg(const char *dest, int kmsg_socket) {
         /* Store away the fd in the socket, so that it stays open as
          * long as we run the child */
         k = sendmsg(kmsg_socket, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
         /* Store away the fd in the socket, so that it stays open as
          * long as we run the child */
         k = sendmsg(kmsg_socket, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         if (k < 0) {
                 log_error("Failed to send FIFO fd: %m");
 
         if (k < 0) {
                 log_error("Failed to send FIFO fd: %m");
@@ -2304,8 +2304,7 @@ static void loop_remove(int nr, int *image_fd) {
 
         if (image_fd && *image_fd >= 0) {
                 ioctl(*image_fd, LOOP_CLR_FD);
 
         if (image_fd && *image_fd >= 0) {
                 ioctl(*image_fd, LOOP_CLR_FD);
-                close_nointr_nofail(*image_fd);
-                *image_fd = -1;
+                *image_fd = safe_close(*image_fd);
         }
 
         control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
         }
 
         control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
@@ -2340,9 +2339,9 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
                         _exit(EXIT_FAILURE);
 
                 if (pipe_fds[0] > 2)
                         _exit(EXIT_FAILURE);
 
                 if (pipe_fds[0] > 2)
-                        close_nointr_nofail(pipe_fds[0]);
+                        safe_close(pipe_fds[0]);
                 if (pipe_fds[1] > 2)
                 if (pipe_fds[1] > 2)
-                        close_nointr_nofail(pipe_fds[1]);
+                        safe_close(pipe_fds[1]);
 
                 nullfd = open("/dev/null", O_RDWR);
                 if (nullfd < 0)
 
                 nullfd = open("/dev/null", O_RDWR);
                 if (nullfd < 0)
@@ -2355,7 +2354,7 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
                         _exit(EXIT_FAILURE);
 
                 if (nullfd > 2)
                         _exit(EXIT_FAILURE);
 
                 if (nullfd > 2)
-                        close_nointr_nofail(nullfd);
+                        safe_close(nullfd);
 
                 reset_all_signal_handlers();
                 close_all_fds(NULL, 0);
 
                 reset_all_signal_handlers();
                 close_all_fds(NULL, 0);
@@ -2365,8 +2364,7 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
                 _exit(EXIT_FAILURE);
         }
 
                 _exit(EXIT_FAILURE);
         }
 
-        close_nointr_nofail(pipe_fds[1]);
-        pipe_fds[1] = -1;
+        pipe_fds[1] = safe_close(pipe_fds[1]);
 
         *rpid = pid;
 
 
         *rpid = pid;
 
@@ -2808,15 +2806,13 @@ int main(int argc, char *argv[]) {
                         if (envp[n_env])
                                 n_env ++;
 
                         if (envp[n_env])
                                 n_env ++;
 
-                        close_nointr_nofail(master);
-                        master = -1;
+                        master = safe_close(master);
 
                         close_nointr(STDIN_FILENO);
                         close_nointr(STDOUT_FILENO);
                         close_nointr(STDERR_FILENO);
 
 
                         close_nointr(STDIN_FILENO);
                         close_nointr(STDOUT_FILENO);
                         close_nointr(STDERR_FILENO);
 
-                        close_nointr_nofail(kmsg_socket_pair[0]);
-                        kmsg_socket_pair[0] = -1;
+                        kmsg_socket_pair[0] = safe_close(kmsg_socket_pair[0]);
 
                         reset_all_signal_handlers();
 
 
                         reset_all_signal_handlers();
 
@@ -2826,7 +2822,7 @@ int main(int argc, char *argv[]) {
                         k = open_terminal(console, O_RDWR);
                         if (k != STDIN_FILENO) {
                                 if (k >= 0) {
                         k = open_terminal(console, O_RDWR);
                         if (k != STDIN_FILENO) {
                                 if (k >= 0) {
-                                        close_nointr_nofail(k);
+                                        safe_close(k);
                                         k = -EINVAL;
                                 }
 
                                         k = -EINVAL;
                                 }
 
@@ -2899,8 +2895,7 @@ int main(int argc, char *argv[]) {
                         if (setup_kmsg(arg_directory, kmsg_socket_pair[1]) < 0)
                                 goto child_fail;
 
                         if (setup_kmsg(arg_directory, kmsg_socket_pair[1]) < 0)
                                 goto child_fail;
 
-                        close_nointr_nofail(kmsg_socket_pair[1]);
-                        kmsg_socket_pair[1] = -1;
+                        kmsg_socket_pair[1] = safe_close(kmsg_socket_pair[1]);
 
                         if (setup_boot_id(arg_directory) < 0)
                                 goto child_fail;
 
                         if (setup_boot_id(arg_directory) < 0)
                                 goto child_fail;
@@ -2927,8 +2922,7 @@ int main(int argc, char *argv[]) {
                          * it can cgroupify us to that we lack access
                          * to certain devices and resources. */
                         eventfd_write(child_ready_fd, 1);
                          * it can cgroupify us to that we lack access
                          * to certain devices and resources. */
                         eventfd_write(child_ready_fd, 1);
-                        close_nointr_nofail(child_ready_fd);
-                        child_ready_fd = -1;
+                        child_ready_fd = safe_close(child_ready_fd);
 
                         if (chdir(arg_directory) < 0) {
                                 log_error("chdir(%s) failed: %m", arg_directory);
 
                         if (chdir(arg_directory) < 0) {
                                 log_error("chdir(%s) failed: %m", arg_directory);
@@ -3029,8 +3023,7 @@ int main(int argc, char *argv[]) {
 
                         /* Wait until the parent is ready with the setup, too... */
                         eventfd_read(parent_ready_fd, &x);
 
                         /* Wait until the parent is ready with the setup, too... */
                         eventfd_read(parent_ready_fd, &x);
-                        close_nointr_nofail(parent_ready_fd);
-                        parent_ready_fd = -1;
+                        parent_ready_fd = safe_close(parent_ready_fd);
 
                         if (arg_boot) {
                                 char **a;
 
                         if (arg_boot) {
                                 char **a;
index be920062dc93cde65d617b7bfba4d3e0e02d87f5..c1afd0d605226259a74ac458601faa2f886a5544 100644 (file)
@@ -176,8 +176,7 @@ finish:
         if (start != MAP_FAILED)
                 munmap(start, l);
 
         if (start != MAP_FAILED)
                 munmap(start, l);
 
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -493,16 +492,12 @@ static int collect(const char *root) {
                                 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
 
                 next_iteration:
                                 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
 
                 next_iteration:
-                        if (m->fd >= 0)
-                                close_nointr_nofail(m->fd);
+                        safe_close(m->fd);
                 }
         }
 
 done:
                 }
         }
 
 done:
-        if (fanotify_fd >= 0) {
-                close_nointr_nofail(fanotify_fd);
-                fanotify_fd = -1;
-        }
+        fanotify_fd = safe_close(fanotify_fd);
 
         log_debug("Writing Pack File...");
 
 
         log_debug("Writing Pack File...");
 
@@ -592,14 +587,9 @@ done:
         log_debug("Done.");
 
 finish:
         log_debug("Done.");
 
 finish:
-        if (fanotify_fd >= 0)
-                close_nointr_nofail(fanotify_fd);
-
-        if (signal_fd >= 0)
-                close_nointr_nofail(signal_fd);
-
-        if (inotify_fd >= 0)
-                close_nointr_nofail(inotify_fd);
+        safe_close(fanotify_fd);
+        safe_close(signal_fd);
+        safe_close(inotify_fd);
 
         if (pack) {
                 fclose(pack);
 
         if (pack) {
                 fclose(pack);
index aea1fbeea4ff8fe02e8f226f7b5c5f25ecb65363..5ffa88b78a2f651041821099ed3bbd1331e9cc05 100644 (file)
@@ -220,7 +220,7 @@ int open_inotify(void) {
 
         if (inotify_add_watch(fd, "/run/systemd/readahead", IN_CREATE) < 0) {
                 log_error("Failed to watch /run/systemd/readahead: %m");
 
         if (inotify_add_watch(fd, "/run/systemd/readahead", IN_CREATE) < 0) {
                 log_error("Failed to watch /run/systemd/readahead: %m");
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
index cb04e5f9cde051fb2a1965be2fb9450ee39d0b90..8dc194257f07446588a28027456c0191e448ee5b 100644 (file)
@@ -67,10 +67,8 @@ static int unpack_file(FILE *pack) {
                 if (errno != ENOENT && errno != EPERM && errno != EACCES && errno != ELOOP)
                         log_warning("open(%s) failed: %m", fn);
 
                 if (errno != ENOENT && errno != EPERM && errno != EACCES && errno != ELOOP)
                         log_warning("open(%s) failed: %m", fn);
 
-        } else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
-                close_nointr_nofail(fd);
-                fd = -1;
-        }
+        } else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0)
+                fd = safe_close(fd);
 
         if (fread(&inode, sizeof(inode), 1, pack) != 1) {
                 log_error("Premature end of pack file.");
 
         if (fread(&inode, sizeof(inode), 1, pack) != 1) {
                 log_error("Premature end of pack file.");
@@ -81,10 +79,8 @@ static int unpack_file(FILE *pack) {
         if (fd >= 0) {
                 /* If the inode changed the file got deleted, so just
                  * ignore this entry */
         if (fd >= 0) {
                 /* If the inode changed the file got deleted, so just
                  * ignore this entry */
-                if (st.st_ino != (uint64_t) inode) {
-                        close_nointr_nofail(fd);
-                        fd = -1;
-                }
+                if (st.st_ino != (uint64_t) inode)
+                        fd = safe_close(fd);
         }
 
         for (;;) {
         }
 
         for (;;) {
@@ -129,8 +125,7 @@ static int unpack_file(FILE *pack) {
         }
 
 finish:
         }
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -279,8 +274,7 @@ finish:
         if (pack)
                 fclose(pack);
 
         if (pack)
                 fclose(pack);
 
-        if (inotify_fd >= 0)
-                close_nointr_nofail(inotify_fd);
+        safe_close(inotify_fd);
 
         free(pack_fn);
 
 
         free(pack_fn);
 
index 2f168985b42f620b02693810a13714a108274857..c730216b73c6aacf4d17536078357f1586d63f6e 100644 (file)
@@ -91,7 +91,8 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        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");
                 goto finish;
         }
                 log_error("socket() failed: %m");
                 goto finish;
         }
@@ -102,8 +103,7 @@ int main(int argc, char *argv[]) {
         r = EXIT_SUCCESS;
 
 finish:
         r = EXIT_SUCCESS;
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
index a328f145e9d7288a1b50c1205a74df42f7b77b89..117f0c668747c15669154e2b4a764c69a124e8d3 100644 (file)
@@ -226,8 +226,7 @@ int ask_password_tty(
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
+        safe_close(notify);
 
         if (ttyfd >= 0) {
 
 
         if (ttyfd >= 0) {
 
@@ -236,7 +235,7 @@ finish:
                         tcsetattr(ttyfd, TCSADRAIN, &old_termios);
                 }
 
                         tcsetattr(ttyfd, TCSADRAIN, &old_termios);
                 }
 
-                close_nointr_nofail(ttyfd);
+                safe_close(ttyfd);
         }
 
         return r;
         }
 
         return r;
@@ -290,7 +289,7 @@ static int create_socket(char **name) {
         return fd;
 
 fail:
         return fd;
 
 fail:
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -521,19 +520,15 @@ int ask_password_agent(
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         if (socket_name) {
                 unlink(socket_name);
                 free(socket_name);
         }
 
 
         if (socket_name) {
                 unlink(socket_name);
                 free(socket_name);
         }
 
-        if (socket_fd >= 0)
-                close_nointr_nofail(socket_fd);
-
-        if (signal_fd >= 0)
-                close_nointr_nofail(signal_fd);
+        safe_close(socket_fd);
+        safe_close(signal_fd);
 
         if (f)
                 fclose(f);
 
         if (f)
                 fclose(f);
index fd27398ebbec75f7a3554882fc7e017d1128ae5d..a2c861de3f4b0486f7fbb742e717237555b16c09 100644 (file)
@@ -82,7 +82,7 @@ int fdset_put_dup(FDSet *s, int fd) {
 
         r = fdset_put(s, copy);
         if (r < 0) {
 
         r = fdset_put(s, copy);
         if (r < 0) {
-                close_nointr_nofail(copy);
+                safe_close(copy);
                 return r;
         }
 
                 return r;
         }
 
index f57b94d599daf574fd1d38898ef267f9ddbb7847..74090463d9547a0fff77248382f13c51ef8518b4 100644 (file)
@@ -204,7 +204,7 @@ static int remove_marked_symlinks_fd(
 
         d = fdopendir(fd);
         if (!d) {
 
         d = fdopendir(fd);
         if (!d) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -244,7 +244,7 @@ static int remove_marked_symlinks_fd(
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p) {
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p) {
-                                close_nointr_nofail(nfd);
+                                safe_close(nfd);
                                 return -ENOMEM;
                         }
 
                                 return -ENOMEM;
                         }
 
@@ -344,7 +344,7 @@ static int remove_marked_symlinks(
                         r = q;
         } while (deleted);
 
                         r = q;
         } while (deleted);
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }
@@ -367,7 +367,7 @@ static int find_symlinks_fd(
 
         d = fdopendir(fd);
         if (!d) {
 
         d = fdopendir(fd);
         if (!d) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -403,7 +403,7 @@ static int find_symlinks_fd(
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p) {
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p) {
-                                close_nointr_nofail(nfd);
+                                safe_close(nfd);
                                 return -ENOMEM;
                         }
 
                                 return -ENOMEM;
                         }
 
@@ -1008,7 +1008,7 @@ static int unit_file_load(
 
         f = fdopen(fd, "re");
         if (!f) {
 
         f = fdopen(fd, "re");
         if (!f) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOMEM;
         }
 
                 return -ENOMEM;
         }
 
index 5ea1e3a0e834bfb1a264037c31d2e2e1584a5a91..a4b3b68ef181e316f74da46f18dbd9399c595784 100644 (file)
@@ -62,7 +62,7 @@ void log_close_console(void) {
 
         if (getpid() == 1) {
                 if (console_fd >= 3)
 
         if (getpid() == 1) {
                 if (console_fd >= 3)
-                        close_nointr_nofail(console_fd);
+                        safe_close(console_fd);
 
                 console_fd = -1;
         }
 
                 console_fd = -1;
         }
@@ -84,12 +84,7 @@ static int log_open_console(void) {
 }
 
 void log_close_kmsg(void) {
 }
 
 void log_close_kmsg(void) {
-
-        if (kmsg_fd < 0)
-                return;
-
-        close_nointr_nofail(kmsg_fd);
-        kmsg_fd = -1;
+        kmsg_fd = safe_close(kmsg_fd);
 }
 
 static int log_open_kmsg(void) {
 }
 
 static int log_open_kmsg(void) {
@@ -105,12 +100,7 @@ static int log_open_kmsg(void) {
 }
 
 void log_close_syslog(void) {
 }
 
 void log_close_syslog(void) {
-
-        if (syslog_fd < 0)
-                return;
-
-        close_nointr_nofail(syslog_fd);
-        syslog_fd = -1;
+        syslog_fd = safe_close(syslog_fd);
 }
 
 static int create_log_socket(int type) {
 }
 
 static int create_log_socket(int type) {
@@ -152,7 +142,7 @@ static int log_open_syslog(void) {
         }
 
         if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
         }
 
         if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
-                close_nointr_nofail(syslog_fd);
+                safe_close(syslog_fd);
 
                 /* Some legacy syslog systems still use stream
                  * sockets. They really shouldn't. But what can we
 
                 /* Some legacy syslog systems still use stream
                  * sockets. They really shouldn't. But what can we
@@ -180,12 +170,7 @@ fail:
 }
 
 void log_close_journal(void) {
 }
 
 void log_close_journal(void) {
-
-        if (journal_fd < 0)
-                return;
-
-        close_nointr_nofail(journal_fd);
-        journal_fd = -1;
+        journal_fd = safe_close(journal_fd);
 }
 
 static int log_open_journal(void) {
 }
 
 static int log_open_journal(void) {
index f7d84fc723d233bd05fde439356ac124125b4733..df49375724d5fce7f2c4a8630a817ab0d5ec7310 100644 (file)
@@ -1171,8 +1171,7 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
         if (child == 0) {
                 int fd;
 
         if (child == 0) {
                 int fd;
 
-                close_nointr_nofail(pair[0]);
-                pair[0] = -1;
+                pair[0] = safe_close(pair[0]);
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
 
                 r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
@@ -1183,7 +1182,7 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
                         _exit(EXIT_FAILURE);
 
                 k = loop_read(fd, buf, 36, false);
                         _exit(EXIT_FAILURE);
 
                 k = loop_read(fd, buf, 36, false);
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 if (k != 36)
                         _exit(EXIT_FAILURE);
 
                 if (k != 36)
                         _exit(EXIT_FAILURE);
 
@@ -1194,8 +1193,7 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
                 _exit(EXIT_SUCCESS);
         }
 
                 _exit(EXIT_SUCCESS);
         }
 
-        close_nointr_nofail(pair[1]);
-        pair[1] = -1;
+        pair[1] = safe_close(pair[1]);
 
         r = wait_for_terminate(child, &si);
         if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
 
         r = wait_for_terminate(child, &si);
         if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
index f9e52cdcbd192a6007351a66bba3c30d7fd1f4d8..fccf1e9173b709ffc6bb7fd7402ef8f9103ba1b4 100644 (file)
@@ -61,7 +61,7 @@ int polkit_agent_open(void) {
                        POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
 
         /* Close the writing side, because that's the one for the agent */
                        POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);
 
         /* Close the writing side, because that's the one for the agent */
-        close_nointr_nofail(pipe_fd[1]);
+        safe_close(pipe_fd[1]);
 
         if (r < 0)
                 log_error("Failed to fork TTY ask password agent: %s", strerror(-r));
 
         if (r < 0)
                 log_error("Failed to fork TTY ask password agent: %s", strerror(-r));
@@ -69,7 +69,7 @@ int polkit_agent_open(void) {
                 /* Wait until the agent closes the fd */
                 fd_wait_for_event(pipe_fd[0], POLLHUP, (usec_t) -1);
 
                 /* Wait until the agent closes the fd */
                 fd_wait_for_event(pipe_fd[0], POLLHUP, (usec_t) -1);
 
-        close_nointr_nofail(pipe_fd[0]);
+        safe_close(pipe_fd[0]);
 
         return r;
 }
 
         return r;
 }
index 75870fcbe2dfb7f3145764f64165d1e8b3c10d1b..a8c45239052d53df6f18f869dfe2f2b6397d0804 100644 (file)
@@ -183,13 +183,22 @@ int close_nointr(int fd) {
                 return -errno;
 }
 
                 return -errno;
 }
 
-void close_nointr_nofail(int fd) {
-        PROTECT_ERRNO;
+int safe_close(int fd) {
+
+        /*
+         * Like close_nointr() but cannot fail. Guarantees errno is
+         * unchanged. Is a NOP with negative fds passed, and returns
+         * -1, so that it can be used in this syntax:
+         *
+         * fd = safe_close(fd);
+         */
 
 
-        /* like close_nointr() but cannot fail, and guarantees errno
-         * is unchanged */
+        if (fd >= 0) {
+                PROTECT_ERRNO;
+                assert_se(close_nointr(fd) == 0);
+        }
 
 
-        assert_se(close_nointr(fd) == 0);
+        return -1;
 }
 
 void close_many(const int fds[], unsigned n_fd) {
 }
 
 void close_many(const int fds[], unsigned n_fd) {
@@ -198,7 +207,7 @@ void close_many(const int fds[], unsigned n_fd) {
         assert(fds || n_fd <= 0);
 
         for (i = 0; i < n_fd; i++)
         assert(fds || n_fd <= 0);
 
         for (i = 0; i < n_fd; i++)
-                close_nointr_nofail(fds[i]);
+                safe_close(fds[i]);
 }
 
 int unlink_noerrno(const char *path) {
 }
 
 int unlink_noerrno(const char *path) {
@@ -1693,16 +1702,13 @@ finish:
 }
 
 int reset_terminal(const char *name) {
 }
 
 int reset_terminal(const char *name) {
-        int fd, r;
+        _cleanup_close_ int fd = -1;
 
         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return fd;
 
 
         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return fd;
 
-        r = reset_terminal_fd(fd, true);
-        close_nointr_nofail(fd);
-
-        return r;
+        return reset_terminal_fd(fd, true);
 }
 
 int open_terminal(const char *name, int mode) {
 }
 
 int open_terminal(const char *name, int mode) {
@@ -1741,12 +1747,12 @@ int open_terminal(const char *name, int mode) {
 
         r = isatty(fd);
         if (r < 0) {
 
         r = isatty(fd);
         if (r < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         if (!r) {
                 return -errno;
         }
 
         if (!r) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -ENOTTY;
         }
 
                 return -ENOTTY;
         }
 
@@ -1935,11 +1941,10 @@ int acquire_terminal(
                  * ended our handle will be dead. It's important that
                  * we do this after sleeping, so that we don't enter
                  * an endless loop. */
                  * ended our handle will be dead. It's important that
                  * we do this after sleeping, so that we don't enter
                  * an endless loop. */
-                close_nointr_nofail(fd);
+                safe_close(fd);
         }
 
         }
 
-        if (notify >= 0)
-                close_nointr_nofail(notify);
+        safe_close(notify);
 
         r = reset_terminal_fd(fd, true);
         if (r < 0)
 
         r = reset_terminal_fd(fd, true);
         if (r < 0)
@@ -1948,11 +1953,8 @@ int acquire_terminal(
         return fd;
 
 fail:
         return fd;
 
 fail:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
-        if (notify >= 0)
-                close_nointr_nofail(notify);
+        safe_close(fd);
+        safe_close(notify);
 
         return r;
 }
 
         return r;
 }
@@ -2262,7 +2264,7 @@ int make_stdio(int fd) {
         t = dup3(fd, STDERR_FILENO, 0);
 
         if (fd >= 3)
         t = dup3(fd, STDERR_FILENO, 0);
 
         if (fd >= 3)
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
         if (r < 0 || s < 0 || t < 0)
                 return -errno;
 
         if (r < 0 || s < 0 || t < 0)
                 return -errno;
@@ -2640,7 +2642,7 @@ int rm_rf_children_dangerous(int fd, bool only_dirs, bool honour_sticky, struct
 
         d = fdopendir(fd);
         if (!d) {
 
         d = fdopendir(fd);
         if (!d) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
                 return errno == ENOENT ? 0 : -errno;
         }
 
                 return errno == ENOENT ? 0 : -errno;
         }
@@ -2736,7 +2738,7 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root
         assert(fd >= 0);
 
         if (fstatfs(fd, &s) < 0) {
         assert(fd >= 0);
 
         if (fstatfs(fd, &s) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
                 return -errno;
         }
 
@@ -2745,7 +2747,7 @@ int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root
          * non-state data */
         if (!is_temporary_fs(&s)) {
                 log_error("Attempted to remove disk file system, and we can't allow that.");
          * non-state data */
         if (!is_temporary_fs(&s)) {
                 log_error("Attempted to remove disk file system, and we can't allow that.");
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -EPERM;
         }
 
                 return -EPERM;
         }
 
@@ -2791,13 +2793,13 @@ static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bo
 
         if (!dangerous) {
                 if (fstatfs(fd, &s) < 0) {
 
         if (!dangerous) {
                 if (fstatfs(fd, &s) < 0) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -errno;
                 }
 
                 if (!is_temporary_fs(&s)) {
                         log_error("Attempted to remove disk file system, and we can't allow that.");
                         return -errno;
                 }
 
                 if (!is_temporary_fs(&s)) {
                         log_error("Attempted to remove disk file system, and we can't allow that.");
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return -EPERM;
                 }
         }
                         return -EPERM;
                 }
         }
@@ -3318,7 +3320,7 @@ char *ellipsize(const char *s, size_t length, unsigned percent) {
 }
 
 int touch(const char *path) {
 }
 
 int touch(const char *path) {
-        int fd;
+        _cleanup_close_ int fd;
 
         assert(path);
 
 
         assert(path);
 
@@ -3330,7 +3332,6 @@ int touch(const char *path) {
         if (fd < 0)
                 return -errno;
 
         if (fd < 0)
                 return -errno;
 
-        close_nointr_nofail(fd);
         return 0;
 }
 
         return 0;
 }
 
@@ -3493,7 +3494,7 @@ DIR *xopendirat(int fd, const char *name, int flags) {
 
         d = fdopendir(nfd);
         if (!d) {
 
         d = fdopendir(nfd);
         if (!d) {
-                close_nointr_nofail(nfd);
+                safe_close(nfd);
                 return NULL;
         }
 
                 return NULL;
         }
 
@@ -3987,16 +3988,13 @@ int terminal_vhangup_fd(int fd) {
 }
 
 int terminal_vhangup(const char *name) {
 }
 
 int terminal_vhangup(const char *name) {
-        int fd, r;
+        _cleanup_close_ int fd;
 
         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return fd;
 
 
         fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC);
         if (fd < 0)
                 return fd;
 
-        r = terminal_vhangup_fd(fd);
-        close_nointr_nofail(fd);
-
-        return r;
+        return terminal_vhangup_fd(fd);
 }
 
 int vt_disallocate(const char *name) {
 }
 
 int vt_disallocate(const char *name) {
@@ -4023,7 +4021,7 @@ int vt_disallocate(const char *name) {
                            "\033[H"    /* move home */
                            "\033[2J",  /* clear screen */
                            10, false);
                            "\033[H"    /* move home */
                            "\033[2J",  /* clear screen */
                            10, false);
-                close_nointr_nofail(fd);
+                safe_close(fd);
 
                 return 0;
         }
 
                 return 0;
         }
@@ -4044,7 +4042,7 @@ int vt_disallocate(const char *name) {
                 return fd;
 
         r = ioctl(fd, VT_DISALLOCATE, u);
                 return fd;
 
         r = ioctl(fd, VT_DISALLOCATE, u);
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         if (r >= 0)
                 return 0;
 
         if (r >= 0)
                 return 0;
@@ -4063,7 +4061,7 @@ int vt_disallocate(const char *name) {
                    "\033[H"   /* move home */
                    "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
                    10, false);
                    "\033[H"   /* move home */
                    "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */
                    10, false);
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         return 0;
 }
 
         return 0;
 }
@@ -5640,7 +5638,7 @@ int on_ac_power(void) {
                 if (n != 6 || memcmp(contents, "Mains\n", 6))
                         continue;
 
                 if (n != 6 || memcmp(contents, "Mains\n", 6))
                         continue;
 
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
                 if (fd < 0) {
                         if (errno == ENOENT)
                 fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
                 if (fd < 0) {
                         if (errno == ENOENT)
index 7752b1e3dff660e265015ecda90b2ce8c39a65a1..70c20fdcf10cb6a4ec5e2c677d9233bf314d4971 100644 (file)
@@ -149,7 +149,8 @@ char *endswith(const char *s, const char *postfix) _pure_;
 bool first_word(const char *s, const char *word) _pure_;
 
 int close_nointr(int fd);
 bool first_word(const char *s, const char *word) _pure_;
 
 int close_nointr(int fd);
-void close_nointr_nofail(int fd);
+int safe_close(int fd);
+
 void close_many(const int fds[], unsigned n_fd);
 
 int parse_size(const char *t, off_t base, off_t *size);
 void close_many(const int fds[], unsigned n_fd);
 
 int parse_size(const char *t, off_t base, off_t *size);
@@ -620,8 +621,7 @@ static inline void freep(void *p) {
         struct __useless_struct_to_allow_trailing_semicolon__
 
 static inline void closep(int *fd) {
         struct __useless_struct_to_allow_trailing_semicolon__
 
 static inline void closep(int *fd) {
-        if (*fd >= 0)
-                close_nointr_nofail(*fd);
+        safe_close(*fd);
 }
 
 static inline void umaskp(mode_t *u) {
 }
 
 static inline void umaskp(mode_t *u) {
index ddbe7afd3cd957846e1aed29d954094c3ad4eece..ba9ad9be97aab488835c3e29443c33badf75f05f 100644 (file)
@@ -164,6 +164,5 @@ void watchdog_close(bool disarm) {
                 }
         }
 
                 }
         }
 
-        close_nointr_nofail(watchdog_fd);
-        watchdog_fd = -1;
+        watchdog_fd = safe_close(watchdog_fd);
 }
 }
index fafd9ce32eca9736055fd7d69dd32400c13e8be3..578920c96dd23713ec6580a49d0d004f0a19d4f8 100644 (file)
@@ -430,8 +430,7 @@ int main(int argc, char *argv[]) {
 finish:
 
         for (i = 0; i < _FD_MAX; i++)
 finish:
 
         for (i = 0; i < _FD_MAX; i++)
-                if (pollfd[i].fd >= 0)
-                        close_nointr_nofail(pollfd[i].fd);
+                safe_close(pollfd[i].fd);
 
         if (unlink_nologin)
                 unlink("/run/nologin");
 
         if (unlink_nologin)
                 unlink("/run/nologin");
index a42e5ae27994d6c835a25792bfc45af089016d68..c172bebe8cebe8133a98577344548fbcacf11a8b 100644 (file)
@@ -76,10 +76,8 @@ static void connection_free(Connection *c) {
         sd_event_source_unref(c->server_event_source);
         sd_event_source_unref(c->client_event_source);
 
         sd_event_source_unref(c->server_event_source);
         sd_event_source_unref(c->client_event_source);
 
-        if (c->server_fd >= 0)
-                close_nointr_nofail(c->server_fd);
-        if (c->client_fd >= 0)
-                close_nointr_nofail(c->client_fd);
+        safe_close(c->server_fd);
+        safe_close(c->client_fd);
 
         close_pipe(c->server_to_client_buffer);
         close_pipe(c->client_to_server_buffer);
 
         close_pipe(c->server_to_client_buffer);
         close_pipe(c->client_to_server_buffer);
@@ -224,8 +222,7 @@ static int connection_shovel(
                                 shoveled = true;
                         } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
                                 *from_source = sd_event_source_unref(*from_source);
                                 shoveled = true;
                         } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
                                 *from_source = sd_event_source_unref(*from_source);
-                                close_nointr_nofail(*from);
-                                *from = -1;
+                                *from = safe_close(*from);
                         } else if (errno != EAGAIN && errno != EINTR) {
                                 log_error("Failed to splice: %m");
                                 return -errno;
                         } else if (errno != EAGAIN && errno != EINTR) {
                                 log_error("Failed to splice: %m");
                                 return -errno;
@@ -239,8 +236,7 @@ static int connection_shovel(
                                 shoveled = true;
                         } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
                                 *to_source = sd_event_source_unref(*to_source);
                                 shoveled = true;
                         } else if (z == 0 || errno == EPIPE || errno == ECONNRESET) {
                                 *to_source = sd_event_source_unref(*to_source);
-                                close_nointr_nofail(*to);
-                                *to = -1;
+                                *to = safe_close(*to);
                         } else if (errno != EAGAIN && errno != EINTR) {
                                 log_error("Failed to splice: %m");
                                 return -errno;
                         } else if (errno != EAGAIN && errno != EINTR) {
                                 log_error("Failed to splice: %m");
                                 return -errno;
@@ -396,7 +392,7 @@ static int add_connection_socket(Context *context, sd_event *event, int fd) {
 
         if (set_size(context->connections) > CONNECTIONS_MAX) {
                 log_warning("Hit connection limit, refusing connection.");
 
         if (set_size(context->connections) > CONNECTIONS_MAX) {
                 log_warning("Hit connection limit, refusing connection.");
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return 0;
         }
 
                 return 0;
         }
 
@@ -482,7 +478,7 @@ static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdat
                 r = add_connection_socket(context, sd_event_source_get_event(s), nfd);
                 if (r < 0) {
                         log_error("Failed to accept connection, ignoring: %s", strerror(-r));
                 r = add_connection_socket(context, sd_event_source_get_event(s), nfd);
                 if (r < 0) {
                         log_error("Failed to accept connection, ignoring: %s", strerror(-r));
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                 }
         }
 
                 }
         }
 
index a6247726b0c2bdf91025c29658a35801a0811584..1d952ebf1dafd4dea215d07b6d582d2c18f54b76 100644 (file)
@@ -67,7 +67,7 @@ static void test_close_many(void) {
         assert_se(fcntl(fds[1], F_GETFD) == -1);
         assert_se(fcntl(fds[2], F_GETFD) >= 0);
 
         assert_se(fcntl(fds[1], F_GETFD) == -1);
         assert_se(fcntl(fds[2], F_GETFD) >= 0);
 
-        close_nointr_nofail(fds[2]);
+        safe_close(fds[2]);
 
         unlink(name0);
         unlink(name1);
 
         unlink(name0);
         unlink(name1);
index 74a01271e9ed3777ebfc9e9a8010cf32735c1177..33e7cbc05d2dcab940690102c0740d2dd9502d52 100644 (file)
@@ -496,7 +496,7 @@ static int write_one_file(Item *i, const char *path) {
 
                 unescaped = cunescape(i->argument);
                 if (unescaped == NULL) {
 
                 unescaped = cunescape(i->argument);
                 if (unescaped == NULL) {
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return log_oom();
                 }
 
                         return log_oom();
                 }
 
@@ -505,12 +505,12 @@ static int write_one_file(Item *i, const char *path) {
 
                 if (n < 0 || (size_t) n < l) {
                         log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
 
                 if (n < 0 || (size_t) n < l) {
                         log_error("Failed to write file %s: %s", path, n < 0 ? strerror(-n) : "Short write");
-                        close_nointr_nofail(fd);
+                        safe_close(fd);
                         return n < 0 ? n : -EIO;
                 }
         }
 
                         return n < 0 ? n : -EIO;
                 }
         }
 
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         if (stat(path, &st) < 0) {
                 log_error("stat(%s) failed: %m", path);
 
         if (stat(path, &st) < 0) {
                 log_error("stat(%s) failed: %m", path);
index fa4d660215e72ec6eec70912e3c307fd910a05b1..1d067af2295e6d3348a7aa1e4977c8a32f099ebb 100644 (file)
@@ -234,11 +234,8 @@ static int ask_password_plymouth(
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
-
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(notify);
+        safe_close(fd);
 
         free(packet);
 
 
         free(packet);
 
@@ -372,7 +369,7 @@ static int parse_password(const char *filename, char **wall) {
                         r = ask_password_tty(message, not_after, filename, &password);
 
                         if (arg_console) {
                         r = ask_password_tty(message, not_after, filename, &password);
 
                         if (arg_console) {
-                                close_nointr_nofail(tty_fd);
+                                safe_close(tty_fd);
                                 release_terminal();
                         }
 
                                 release_terminal();
                         }
 
@@ -419,8 +416,7 @@ static int parse_password(const char *filename, char **wall) {
 finish:
         fclose(f);
 
 finish:
         fclose(f);
 
-        if (socket_fd >= 0)
-                close_nointr_nofail(socket_fd);
+        safe_close(socket_fd);
 
         free(packet);
         free(socket_name);
 
         free(packet);
         free(socket_name);
@@ -492,7 +488,7 @@ static bool wall_tty_match(const char *path) {
                 return true;
 
         /* What, we managed to open the pipe? Then this tty is filtered. */
                 return true;
 
         /* What, we managed to open the pipe? Then this tty is filtered. */
-        close_nointr_nofail(fd);
+        safe_close(fd);
         return false;
 }
 
         return false;
 }
 
@@ -614,14 +610,9 @@ static int watch_passwords(void) {
         r = 0;
 
 finish:
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
-
-        if (signal_fd >= 0)
-                close_nointr_nofail(signal_fd);
-
-        if (tty_block_fd >= 0)
-                close_nointr_nofail(tty_block_fd);
+        safe_close(notify);
+        safe_close(signal_fd);
+        safe_close(tty_block_fd);
 
         return r;
 }
 
         return r;
 }
index d3f1affffe824a9fb551eb78c8aa55430c01fc11..62439c0c71a680f858fbf48dc740da0345604a4f 100644 (file)
@@ -127,8 +127,7 @@ void link_config_ctx_free(link_config_ctx *ctx) {
         if (!ctx)
                 return;
 
         if (!ctx)
                 return;
 
-        if (ctx->ethtool_fd >= 0)
-                close_nointr_nofail(ctx->ethtool_fd);
+        safe_close(ctx->ethtool_fd);
 
         sd_rtnl_unref(ctx->rtnl);
 
 
         sd_rtnl_unref(ctx->rtnl);
 
index 1bbf737c36f9dde13d968ae07ea28babd5f21dba..0f2b7066e2bcd8279097e1751f3c4a945681323c 100644 (file)
@@ -301,8 +301,7 @@ finish:
         free(vc_font_map);
         free(vc_font_unimap);
 
         free(vc_font_map);
         free(vc_font_unimap);
 
-        if (fd >= 0)
-                close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
 
         return r;
 }