chiark / gitweb /
util: replace close_pipe() with new safe_close_pair()
authorLennart Poettering <lennart@poettering.net>
Mon, 24 Mar 2014 02:22:44 +0000 (03:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Mar 2014 02:22:44 +0000 (03:22 +0100)
safe_close_pair() is more like safe_close(), except that it handles
pairs of fds, and doesn't make and misleading allusion, as it works
similarly well for socketpairs() as for pipe()s...

18 files changed:
src/core/automount.c
src/core/execute.c
src/core/manager.c
src/fsck/fsck.c
src/journal/journal-remote.c
src/libsystemd/sd-bus/bus-container.c
src/libsystemd/sd-bus/bus-socket.c
src/libsystemd/sd-bus/test-bus-chat.c
src/libsystemd/sd-event/test-event.c
src/libsystemd/sd-login/test-login.c
src/machine/machinectl.c
src/nspawn/nspawn.c
src/shared/logs-show.c
src/shared/pager.c
src/shared/util.c
src/shared/util.h
src/socket-proxy/socket-proxyd.c
src/test/test-namespace.c

index 77d80b2789e49bc6bbd8dcc1c5d9d7dbd73f5d3b..65e6d6f179219aba14996e61b9f59c226e9e021b 100644 (file)
@@ -532,7 +532,7 @@ static void automount_enter_waiting(Automount *a) {
         return;
 
 fail:
-        close_pipe(p);
+        safe_close_pair(p);
 
         if (mounted)
                 repeat_unmount(a->where);
index a46f25de3bb49e167700c8844327814cb2886e5e..353f2d1297619f9ca4e6b4077d5ea85cf664fbfe 100644 (file)
@@ -2629,7 +2629,7 @@ ExecRuntime *exec_runtime_unref(ExecRuntime *r) {
         if (r->n_ref <= 0) {
                 free(r->tmp_dir);
                 free(r->var_tmp_dir);
-                close_pipe(r->netns_storage_socket);
+                safe_close_pair(r->netns_storage_socket);
                 free(r);
         }
 
@@ -2781,7 +2781,7 @@ void exec_runtime_destroy(ExecRuntime *rt) {
                 rt->var_tmp_dir = NULL;
         }
 
-        close_pipe(rt->netns_storage_socket);
+        safe_close_pair(rt->netns_storage_socket);
 }
 
 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
index 360a65a49219da98b49f5a2fc3dd8278b7291237..a3ff85c86e3237da1a90a0dbf4b36209b257a9cb 100644 (file)
@@ -225,8 +225,8 @@ static int manager_watch_idle_pipe(Manager *m) {
 static void manager_close_idle_pipe(Manager *m) {
         assert(m);
 
-        close_pipe(m->idle_pipe);
-        close_pipe(m->idle_pipe + 2);
+        safe_close_pair(m->idle_pipe);
+        safe_close_pair(m->idle_pipe + 2);
 }
 
 static int manager_setup_time_change(Manager *m) {
index f60cc1d9126ce4ebca93c70bd6081c95e3dcef5c..18f2acaa490afd7bdda27938602763cef076f55a 100644 (file)
@@ -374,7 +374,7 @@ int main(int argc, char *argv[]) {
                 touch("/run/systemd/quotacheck");
 
 finish:
-        close_pipe(progress_pipe);
+        safe_close_pair(progress_pipe);
 
         return r;
 }
index 469735663cf85b5b79d1e7164b34bb80d7fc2510..4ece14ee777b9ce9138cc6bfc0fabf851c2ea827 100644 (file)
@@ -89,7 +89,7 @@ static int spawn_child(const char* child, char** argv) {
         if (child_pid < 0) {
                 r = -errno;
                 log_error("Failed to fork: %m");
-                close_pipe(fd);
+                safe_close_pair(fd);
                 return r;
         }
 
@@ -101,9 +101,7 @@ static int spawn_child(const char* child, char** argv) {
                         _exit(EXIT_FAILURE);
                 }
 
-                r = close_pipe(fd);
-                if (r < 0)
-                        log_warning("Failed to close pipe fds: %m");
+                safe_close_pair(fd);
 
                 /* Make sure the child goes away when the parent dies */
                 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
index 2cd0e1f99ed017ef54db34ba87c9027d32f21199..6bd7ad6318dd732c47b9e1ab684e4600e05344cf 100644 (file)
@@ -116,7 +116,7 @@ int bus_container_connect_socket(sd_bus *b) {
 }
 
 int bus_container_connect_kernel(sd_bus *b) {
-        _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
         _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
         union {
                 struct cmsghdr cmsghdr;
index bccf50122213aa43509c95fe856ddcf9bbd79bec..2e8f008be47d2b6b40dccfef91bb6592ee12d501 100644 (file)
@@ -736,7 +736,7 @@ int bus_socket_exec(sd_bus *b) {
 
         pid = fork();
         if (pid < 0) {
-                close_pipe(s);
+                safe_close_pair(s);
                 return -errno;
         }
         if (pid == 0) {
index 1b9d98fa7559b6f5bc71b549bbbad2618604b4a7..c0abc7a69795864a8ba34b16980928a4970efa60 100644 (file)
@@ -356,7 +356,7 @@ finish:
 
         sd_bus_error_free(&error);
 
-        close_pipe(pp);
+        safe_close_pair(pp);
 
         return INT_TO_PTR(r);
 }
index 68bb2b0ca8c750647a6b5258dbb4f788411409b8..3342ec696841ed1b801d001b3d0be9e9159b26b5 100644 (file)
@@ -239,10 +239,10 @@ int main(int argc, char *argv[]) {
 
         sd_event_unref(e);
 
-        close_pipe(a);
-        close_pipe(b);
-        close_pipe(d);
-        close_pipe(k);
+        safe_close_pair(a);
+        safe_close_pair(b);
+        safe_close_pair(d);
+        safe_close_pair(k);
 
         return 0;
 }
index 2ab083bb71960b447acda01b082fdd3a67fb72ae..9e326de5b43ed4776dac6c496da2533443ecee9b 100644 (file)
@@ -28,7 +28,7 @@
 #include "strv.h"
 
 static void test_login(void) {
-        _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
         _cleanup_free_ char *pp = NULL, *qq = NULL;
         int r, k;
         uid_t u, u2;
index d812ef2600dcd08e30f344f1c5f5b17c6258336a..6337aa274fd5ada55986abecc00e3b053e43fb9a 100644 (file)
@@ -412,7 +412,7 @@ static int terminate_machine(sd_bus *bus, char **args, unsigned n) {
 }
 
 static int openpt_in_namespace(pid_t pid, int flags) {
-        _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
         _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
         union {
                 struct cmsghdr cmsghdr;
index 2d627db9c7fc364af4f9d2c9ab233fcfd1d4b807..9a9ed9dc6e0f7e3e6d2759b6ce50711697c194d9 100644 (file)
@@ -2582,7 +2582,7 @@ int main(int argc, char *argv[]) {
         _cleanup_free_ char *kdbus_domain = NULL, *device_path = NULL, *root_device = NULL, *home_device = NULL, *srv_device = NULL;
         bool root_device_rw = true, home_device_rw = true, srv_device_rw = true;
         _cleanup_close_ int master = -1, kdbus_fd = -1, image_fd = -1;
-        _cleanup_close_pipe_ int kmsg_socket_pair[2] = { -1, -1 };
+        _cleanup_close_pair_ int kmsg_socket_pair[2] = { -1, -1 };
         _cleanup_fdset_free_ FDSet *fds = NULL;
         int r = EXIT_FAILURE, k, n_fd_passed, loop_nr = -1;
         const char *console = NULL;
index df49375724d5fce7f2c4a8630a817ab0d5ec7310..9d14933bc11c0439e4cf697bb32f221b3d6eb675 100644 (file)
@@ -1139,7 +1139,7 @@ int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
 }
 
 static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
-        _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
         _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
         pid_t pid, child;
         siginfo_t si;
index 55b13d6ff63ecbf80198b5d9144c12fa5474320d..002e3aa37356c7b0f556dd635bcacade31c7d502 100644 (file)
@@ -78,7 +78,7 @@ int pager_open(bool jump_to_end) {
         if (pager_pid < 0) {
                 r = -errno;
                 log_error("Failed to fork pager: %m");
-                close_pipe(fd);
+                safe_close_pair(fd);
                 return r;
         }
 
@@ -87,7 +87,7 @@ int pager_open(bool jump_to_end) {
                 const char* less_opts;
 
                 dup2(fd[0], STDIN_FILENO);
-                close_pipe(fd);
+                safe_close_pair(fd);
 
                 less_opts = getenv("SYSTEMD_LESS");
                 if (!less_opts)
@@ -131,7 +131,7 @@ int pager_open(bool jump_to_end) {
                 return -errno;
         }
 
-        close_pipe(fd);
+        safe_close_pair(fd);
         return 1;
 }
 
index a8c45239052d53df6f18f869dfe2f2b6397d0804..dd67c2264164b0c04c32a630e11cc872e162245e 100644 (file)
@@ -2037,22 +2037,18 @@ int default_signals(int sig, ...) {
         return r;
 }
 
-int close_pipe(int p[]) {
-        int a = 0, b = 0;
-
+void safe_close_pair(int p[]) {
         assert(p);
 
-        if (p[0] >= 0) {
-                a = close_nointr(p[0]);
-                p[0] = -1;
-        }
-
-        if (p[1] >= 0) {
-                b = close_nointr(p[1]);
-                p[1] = -1;
+        if (p[0] == p[1]) {
+                /* Special case pairs which use the same fd in both
+                 * directions... */
+                p[0] = p[1] = safe_close(p[0]);
+                return;
         }
 
-        return a < 0 ? a : b;
+        p[0] = safe_close(p[0]);
+        p[1] = safe_close(p[1]);
 }
 
 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
index 70c20fdcf10cb6a4ec5e2c677d9233bf314d4971..90464c940be37b0eeb64e3300729b050cb68da4e 100644 (file)
@@ -150,6 +150,7 @@ bool first_word(const char *s, const char *word) _pure_;
 
 int close_nointr(int fd);
 int safe_close(int fd);
+void safe_close_pair(int p[]);
 
 void close_many(const int fds[], unsigned n_fd);
 
@@ -378,7 +379,6 @@ int ignore_signals(int sig, ...);
 int default_signals(int sig, ...);
 int sigaction_many(const struct sigaction *sa, ...);
 
-int close_pipe(int p[]);
 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
 
 ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
@@ -628,8 +628,8 @@ static inline void umaskp(mode_t *u) {
         umask(*u);
 }
 
-static inline void close_pipep(int (*p)[2]) {
-        close_pipe(*p);
+static inline void close_pairp(int (*p)[2]) {
+        safe_close_pair(*p);
 }
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
@@ -645,7 +645,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
 #define _cleanup_pclose_ _cleanup_(pclosep)
 #define _cleanup_closedir_ _cleanup_(closedirp)
 #define _cleanup_endmntent_ _cleanup_(endmntentp)
-#define _cleanup_close_pipe_ _cleanup_(close_pipep)
+#define _cleanup_close_pair_ _cleanup_(close_pairp)
 
 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
         if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
index c172bebe8cebe8133a98577344548fbcacf11a8b..ac47c851507b2516e01835bdac6dd32d675189c1 100644 (file)
@@ -79,8 +79,8 @@ static void connection_free(Connection *c) {
         safe_close(c->server_fd);
         safe_close(c->client_fd);
 
-        close_pipe(c->server_to_client_buffer);
-        close_pipe(c->client_to_server_buffer);
+        safe_close_pair(c->server_to_client_buffer);
+        safe_close_pair(c->client_to_server_buffer);
 
         free(c);
 }
index 5b76b9e73ffddaaafe2f3a4f9dc6a4db885a838b..e74fd0c88a53251cd7c0bef76752ac9475db403a 100644 (file)
@@ -63,7 +63,7 @@ static void test_tmpdir(const char *id, const char *A, const char *B) {
 }
 
 static void test_netns(void) {
-        _cleanup_close_pipe_ int s[2] = { -1, -1 };
+        _cleanup_close_pair_ int s[2] = { -1, -1 };
         pid_t pid1, pid2, pid3;
         int r, n = 0;
         siginfo_t si;