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...
This commit is contained in:
Lennart Poettering 2014-03-24 03:22:44 +01:00
parent 6a0f1f6d5a
commit 3d94f76c99
18 changed files with 37 additions and 43 deletions

View 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);

View 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] = {

View 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) {

View 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;
}

View 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)

View 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;

View 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) {

View File

@ -356,7 +356,7 @@ finish:
sd_bus_error_free(&error);
close_pipe(pp);
safe_close_pair(pp);
return INT_TO_PTR(r);
}

View 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;
}

View 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;

View 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;

View 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;

View 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;

View 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;
}

View 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[0] == p[1]) {
/* Special case pairs which use the same fd in both
* directions... */
p[0] = p[1] = safe_close(p[0]);
return;
}
if (p[1] >= 0) {
b = close_nointr(p[1]);
p[1] = -1;
}
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) {

View 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))

View 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);
}

View 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;