]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add CLOSE_AND_REPLACE helper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Sep 2020 13:36:44 +0000 (15:36 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 18 Sep 2020 13:28:48 +0000 (15:28 +0200)
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.

src/basic/fd-util.c
src/basic/fd-util.h
src/core/execute.c
src/import/import-raw.c
src/import/pull-raw.c
src/libsystemd-network/sd-ipv4acd.c
src/login/logind-session-device.c

index 14b12b27061ac987901e7b06704341b6efd7f2eb..3b2f93a073655b31e1c26531c2562995fea3ceeb 100644 (file)
@@ -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);
                 }
         }
 
index e490753caf23a5c9559cd9bd72bd5023bad6745b..93ce95cd036768c3084fd548fe1e457914476059 100644 (file)
@@ -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);
index be3509396922648fe4e5d5c81d7cc0ed814870b6..3b5ccc890e23c66e3dbe838d56acaa7cfad070bc 100644 (file)
@@ -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);
index 0606cf5406b38defd706c7759b135881301b0fa0..7e9f3441f4da67ed8dea23c240cdccda08382db8 100644 (file)
@@ -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;
 }
index 0183c13f461112aeb1c6af9aeb0ce542255918f6..c32b77f14b84e6497a0b355e70b1f1f5be4f8243 100644 (file)
@@ -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;
 }
index af2b98f5914ef25ad6037f3975ac0c1478466be0..ecd91cd927cd1f412ccd68c7f888492eaf5d5e59 100644 (file)
@@ -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)
index 5cef5169b94ae489ffa25f47b9a2a87618a21329..1a266605be6350c6b35fcd568928f5b9dc2bc720 100644 (file)
@@ -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: