diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-09-16 15:36:44 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-09-18 15:28:48 +0200 |
commit | 0706c012598eee8372fb5d4ca6688ac5fb2380a4 (patch) | |
tree | c7764980023b427bcabbafa4813ae272681ffdb9 /src | |
parent | 831d57953e8e688128bc0f871d4b4213459842ba (diff) | |
download | systemd-0706c012598eee8372fb5d4ca6688ac5fb2380a4.tar.gz |
Add CLOSE_AND_REPLACE helper
Similar to free_and_replace. I think this should be uppercase to make it
clear that this is a macro. free_and_replace should probably be uppercased
too.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fd-util.c | 6 | ||||
-rw-r--r-- | src/basic/fd-util.h | 10 | ||||
-rw-r--r-- | src/core/execute.c | 3 | ||||
-rw-r--r-- | src/import/import-raw.c | 4 | ||||
-rw-r--r-- | src/import/pull-raw.c | 4 | ||||
-rw-r--r-- | src/libsystemd-network/sd-ipv4acd.c | 3 | ||||
-rw-r--r-- | src/login/logind-session-device.c | 3 |
7 files changed, 17 insertions, 16 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 14b12b2706..3b2f93a073 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -731,8 +731,7 @@ int fd_duplicate_data_fd(int fd) { if (f != 0) return -errno; - safe_close(copy_fd); - copy_fd = TAKE_FD(tmp_fd); + CLOSE_AND_REPLACE(copy_fd, tmp_fd); remains = mfree(remains); remains_size = 0; @@ -865,8 +864,7 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_ goto finish; } - safe_close(null_fd); - null_fd = copy; + CLOSE_AND_REPLACE(null_fd, copy); } } diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index e490753caf..93ce95cd03 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -93,6 +93,16 @@ static inline int make_null_stdio(void) { _fd_; \ }) +/* Like free_and_replace(), but for file descriptors */ +#define CLOSE_AND_REPLACE(a, b) \ + ({ \ + int *_fdp_ = &(a); \ + safe_close(*_fdp_); \ + *_fdp_ = TAKE_FD(b); \ + 0; \ + }) + + int fd_reopen(int fd, int flags); int read_nr_open(void); diff --git a/src/core/execute.c b/src/core/execute.c index be35093969..3b5ccc890e 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -4217,8 +4217,7 @@ static int exec_child( return log_unit_error_errno(unit, errno, "Couldn't move exec fd up: %m"); } - safe_close(exec_fd); - exec_fd = moved_fd; + CLOSE_AND_REPLACE(exec_fd, moved_fd); } else { /* This fd should be FD_CLOEXEC already, but let's make sure. */ r = fd_cloexec(exec_fd, true); diff --git a/src/import/import-raw.c b/src/import/import-raw.c index 0606cf5406..7e9f3441f4 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -185,9 +185,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) { (void) unlink(i->temp_path); free_and_replace(i->temp_path, t); - - safe_close(i->output_fd); - i->output_fd = TAKE_FD(converted_fd); + CLOSE_AND_REPLACE(i->output_fd, converted_fd); return 1; } diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 0183c13f46..c32b77f14b 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -253,9 +253,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) { (void) unlink(i->temp_path); free_and_replace(i->temp_path, t); - - safe_close(i->raw_job->disk_fd); - i->raw_job->disk_fd = TAKE_FD(converted_fd); + CLOSE_AND_REPLACE(i->raw_job->disk_fd, converted_fd); return 1; } diff --git a/src/libsystemd-network/sd-ipv4acd.c b/src/libsystemd-network/sd-ipv4acd.c index af2b98f591..ecd91cd927 100644 --- a/src/libsystemd-network/sd-ipv4acd.c +++ b/src/libsystemd-network/sd-ipv4acd.c @@ -471,8 +471,7 @@ int sd_ipv4acd_start(sd_ipv4acd *acd, bool reset_conflicts) { if (r < 0) return r; - safe_close(acd->fd); - acd->fd = r; + CLOSE_AND_REPLACE(acd->fd, r); acd->defend_window = 0; if (reset_conflicts) diff --git a/src/login/logind-session-device.c b/src/login/logind-session-device.c index 5cef5169b9..1a266605be 100644 --- a/src/login/logind-session-device.c +++ b/src/login/logind-session-device.c @@ -195,8 +195,7 @@ static int session_device_start(SessionDevice *sd) { /* For evdev devices, the file descriptor might be left uninitialized. This might happen while resuming * into a session and logind has been restarted right before. */ - safe_close(sd->fd); - sd->fd = r; + CLOSE_AND_REPLACE(sd->fd, r); break; case DEVICE_TYPE_UNKNOWN: |