From 3d94f76c99da13e5603831d0b278f8c8c21bcb02 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 24 Mar 2014 03:22:44 +0100 Subject: 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... --- src/shared/logs-show.c | 2 +- src/shared/pager.c | 6 +++--- src/shared/util.c | 20 ++++++++------------ src/shared/util.h | 8 ++++---- 4 files changed, 16 insertions(+), 20 deletions(-) (limited to 'src/shared') 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)) -- cgit v1.2.3-54-g00ecf