summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-03-24 03:22:44 +0100
committerLennart Poettering <lennart@poettering.net>2014-03-24 03:22:44 +0100
commit3d94f76c99da13e5603831d0b278f8c8c21bcb02 (patch)
tree15b0ccaa3006d76d28b4f23412c5c35ec6494f8e
parent6a0f1f6d5af7c7300d3db7a0ba2b068f8abd222b (diff)
downloadsystemd-3d94f76c99da13e5603831d0b278f8c8c21bcb02.tar.gz
util: replace close_pipe() with new safe_close_pair()
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...
-rw-r--r--src/core/automount.c2
-rw-r--r--src/core/execute.c4
-rw-r--r--src/core/manager.c4
-rw-r--r--src/fsck/fsck.c2
-rw-r--r--src/journal/journal-remote.c6
-rw-r--r--src/libsystemd/sd-bus/bus-container.c2
-rw-r--r--src/libsystemd/sd-bus/bus-socket.c2
-rw-r--r--src/libsystemd/sd-bus/test-bus-chat.c2
-rw-r--r--src/libsystemd/sd-event/test-event.c8
-rw-r--r--src/libsystemd/sd-login/test-login.c2
-rw-r--r--src/machine/machinectl.c2
-rw-r--r--src/nspawn/nspawn.c2
-rw-r--r--src/shared/logs-show.c2
-rw-r--r--src/shared/pager.c6
-rw-r--r--src/shared/util.c20
-rw-r--r--src/shared/util.h8
-rw-r--r--src/socket-proxy/socket-proxyd.c4
-rw-r--r--src/test/test-namespace.c2
18 files changed, 37 insertions, 43 deletions
diff --git a/src/core/automount.c b/src/core/automount.c
index 77d80b2789..65e6d6f179 100644
--- a/src/core/automount.c
+++ b/src/core/automount.c
@@ -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);
diff --git a/src/core/execute.c b/src/core/execute.c
index a46f25de3b..353f2d1297 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -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] = {
diff --git a/src/core/manager.c b/src/core/manager.c
index 360a65a492..a3ff85c86e 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -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) {
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index f60cc1d912..18f2acaa49 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -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;
}
diff --git a/src/journal/journal-remote.c b/src/journal/journal-remote.c
index 469735663c..4ece14ee77 100644
--- a/src/journal/journal-remote.c
+++ b/src/journal/journal-remote.c
@@ -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)
diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
index 2cd0e1f99e..6bd7ad6318 100644
--- a/src/libsystemd/sd-bus/bus-container.c
+++ b/src/libsystemd/sd-bus/bus-container.c
@@ -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;
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
index bccf501222..2e8f008be4 100644
--- a/src/libsystemd/sd-bus/bus-socket.c
+++ b/src/libsystemd/sd-bus/bus-socket.c
@@ -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) {
diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c
index 1b9d98fa75..c0abc7a697 100644
--- a/src/libsystemd/sd-bus/test-bus-chat.c
+++ b/src/libsystemd/sd-bus/test-bus-chat.c
@@ -356,7 +356,7 @@ finish:
sd_bus_error_free(&error);
- close_pipe(pp);
+ safe_close_pair(pp);
return INT_TO_PTR(r);
}
diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c
index 68bb2b0ca8..3342ec6968 100644
--- a/src/libsystemd/sd-event/test-event.c
+++ b/src/libsystemd/sd-event/test-event.c
@@ -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;
}
diff --git a/src/libsystemd/sd-login/test-login.c b/src/libsystemd/sd-login/test-login.c
index 2ab083bb71..9e326de5b4 100644
--- a/src/libsystemd/sd-login/test-login.c
+++ b/src/libsystemd/sd-login/test-login.c
@@ -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;
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index d812ef2600..6337aa274f 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -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;
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 2d627db9c7..9a9ed9dc6e 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -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;
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index df49375724..9d14933bc1 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -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;
diff --git a/src/shared/pager.c b/src/shared/pager.c
index 55b13d6ff6..002e3aa373 100644
--- a/src/shared/pager.c
+++ b/src/shared/pager.c
@@ -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;
}
diff --git a/src/shared/util.c b/src/shared/util.c
index a8c4523905..dd67c22641 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 70c20fdcf1..90464c940b 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -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))
diff --git a/src/socket-proxy/socket-proxyd.c b/src/socket-proxy/socket-proxyd.c
index c172bebe8c..ac47c85150 100644
--- a/src/socket-proxy/socket-proxyd.c
+++ b/src/socket-proxy/socket-proxyd.c
@@ -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);
}
diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c
index 5b76b9e73f..e74fd0c88a 100644
--- a/src/test/test-namespace.c
+++ b/src/test/test-namespace.c
@@ -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;