]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: introduce new TAKE_FD() macro
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Mar 2018 16:04:29 +0000 (17:04 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 22 Mar 2018 19:30:40 +0000 (20:30 +0100)
This is similar to TAKE_PTR() but operates on file descriptors, and thus
assigns -1 to the fd parameter after returning it.

Removes 60 lines from our codebase. Pretty good too I think.

25 files changed:
coccinelle/take-fd.cocci [new file with mode: 0644]
src/basic/fd-util.c
src/basic/fd-util.h
src/basic/fs-util.c
src/basic/memfd-util.c
src/basic/terminal-util.c
src/core/dynamic-user.c
src/core/manager.c
src/core/socket.c
src/import/export-raw.c
src/import/import-common.c
src/import/import-raw.c
src/import/importd.c
src/import/pull-raw.c
src/journal/journal-send.c
src/libsystemd-network/arp-util.c
src/libsystemd-network/dhcp-network.c
src/libsystemd-network/dhcp6-network.c
src/libsystemd-network/icmp6-util.c
src/libsystemd-network/lldp-network.c
src/login/logind-session.c
src/resolve/resolved-dns-scope.c
src/resolve/resolved-dns-stub.c
src/shared/machine-pool.c
src/vconsole/vconsole-setup.c

diff --git a/coccinelle/take-fd.cocci b/coccinelle/take-fd.cocci
new file mode 100644 (file)
index 0000000..ba24248
--- /dev/null
@@ -0,0 +1,14 @@
+@@
+local idexpression p;
+expression q;
+@@
+- p = q;
+- q = -1;
+- return p;
++ return TAKE_FD(q);
+@@
+expression p, q;
+@@
+- p = q;
+- q = -1;
++ p = TAKE_FD(q);
index 6d7875361ce1b090ca8cc83889fa87438af2d8b7..0726aac38cb124064bbc71a24466d40028b4505b 100644 (file)
@@ -484,10 +484,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags) {
                 if (r < 0)
                         return r;
 
-                r = fd;
-                fd = -1;
-
-                return r;
+                return TAKE_FD(fd);
         }
 
 try_pipe:
@@ -524,10 +521,7 @@ try_pipe:
 
                 (void) fd_nonblock(pipefds[0], false);
 
-                r = pipefds[0];
-                pipefds[0] = -1;
-
-                return r;
+                return TAKE_FD(pipefds[0]);
         }
 
 try_dev_shm:
index 635a538b5ae097392668d17a1848920304b150d4..007580b48f4ac3d740e20c830483d075c99737e2 100644 (file)
@@ -105,3 +105,11 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_
 static inline int make_null_stdio(void) {
         return rearrange_stdio(-1, -1, -1);
 }
+
+/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
+#define TAKE_FD(fd)                             \
+        ({                                      \
+                int _fd_ = (fd);                \
+                (fd) = -1;                      \
+                _fd_;                           \
+        })
index aec8b007445226d4475d56b4ce5fc101558b0819..150c47a7199ff72feca0dece73b0231cbefb910d 100644 (file)
@@ -730,8 +730,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                         }
 
                         safe_close(fd);
-                        fd = fd_parent;
-                        fd_parent = -1;
+                        fd = TAKE_FD(fd_parent);
 
                         continue;
                 }
@@ -849,8 +848,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
 
                 /* And iterate again, but go one directory further down. */
                 safe_close(fd);
-                fd = child;
-                child = -1;
+                fd = TAKE_FD(child);
         }
 
         if (!done) {
@@ -864,16 +862,11 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                 *ret = TAKE_PTR(done);
 
         if (flags & CHASE_OPEN) {
-                int q;
-
                 /* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by
                  * opening /proc/self/fd/xyz. */
 
                 assert(fd >= 0);
-                q = fd;
-                fd = -1;
-
-                return q;
+                return TAKE_FD(fd);
         }
 
         return exists;
index e7eb895462382d14d89ac279cc13fd3bba2a8456..758ba6458afe951ae853a3c023b27afec35a4803 100644 (file)
@@ -168,8 +168,5 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) {
         if (r < 0)
                 return r;
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
index 87691d1d382e1bdbdef31856a502ab3f91e21745..c7e2d2abb75ab77d6c0e496a5e5ca5cf03636580 100644 (file)
@@ -496,10 +496,7 @@ int acquire_terminal(
                 fd = safe_close(fd);
         }
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
 
 int release_terminal(void) {
index f87a5a20d47a2117e9f26234c5e5c990a5607143..7236dcfabd4f20faea949006982aca044bd958ef 100644 (file)
@@ -319,10 +319,7 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) {
                 (void) make_uid_symlinks(candidate, name, true); /* also add direct lookup symlinks */
 
                 *ret_uid = candidate;
-                r = lock_fd;
-                lock_fd = -1;
-
-                return r;
+                return TAKE_FD(lock_fd);
 
         next:
                 ;
index 7026a58d7348159a3e5c33d42b40a3ab21063449..9a71f5f2e7e83e72eb6a846c70b35b4c4880ffc8 100644 (file)
@@ -844,8 +844,7 @@ static int manager_setup_notify(Manager *m) {
                 if (r < 0)
                         return log_error_errno(errno, "SO_PASSCRED failed: %m");
 
-                m->notify_fd = fd;
-                fd = -1;
+                m->notify_fd = TAKE_FD(fd);
 
                 log_debug("Using notification socket %s", m->notify_socket);
         }
index b3c09f67c027918e4019387174b8c29302fe0054..eecf6fbe18bb64738d8f36e723874627d7288b37 100644 (file)
@@ -1238,10 +1238,7 @@ static int fifo_address_create(
                 goto fail;
         }
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 
 fail:
         mac_selinux_create_file_clear();
@@ -1251,7 +1248,6 @@ fail:
 static int special_address_create(const char *path, bool writable) {
         _cleanup_close_ int fd = -1;
         struct stat st;
-        int r;
 
         assert(path);
 
@@ -1266,16 +1262,12 @@ static int special_address_create(const char *path, bool writable) {
         if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
                 return -EEXIST;
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
 
 static int usbffs_address_create(const char *path) {
         _cleanup_close_ int fd = -1;
         struct stat st;
-        int r;
 
         assert(path);
 
@@ -1290,10 +1282,7 @@ static int usbffs_address_create(const char *path) {
         if (!S_ISREG(st.st_mode))
                 return -EEXIST;
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
 
 static int mq_address_create(
@@ -1306,7 +1295,6 @@ static int mq_address_create(
         struct stat st;
         mode_t old_mask;
         struct mq_attr _attr, *attr = NULL;
-        int r;
 
         assert(path);
 
@@ -1338,10 +1326,7 @@ static int mq_address_create(
             st.st_gid != getgid())
                 return -EEXIST;
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
 
 static int socket_symlink(Socket *s) {
index eaa6d109157537e550d689c9f2d770a3b3660156..9cab838b9654ec694299a4e7ebf649c8eb5ed4da 100644 (file)
@@ -326,13 +326,10 @@ int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType
 
         /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
         tfd = reflink_snapshot(sfd, path);
-        if (tfd >= 0) {
-                e->input_fd = tfd;
-                tfd = -1;
-        } else {
-                e->input_fd = sfd;
-                sfd = -1;
-        }
+        if (tfd >= 0)
+                e->input_fd = TAKE_FD(tfd);
+        else
+                e->input_fd = TAKE_FD(sfd);
 
         r = import_compress_init(&e->compress, compress);
         if (r < 0)
index a3dc1dde8cb07e0f60cd0dade9ab52a425216f40..b9d8c23905f15bf6987264816ce54e593e7ad81f 100644 (file)
@@ -117,13 +117,9 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
                 _exit(EXIT_FAILURE);
         }
 
-        pipefd[0] = safe_close(pipefd[0]);
-        r = pipefd[1];
-        pipefd[1] = -1;
-
         *ret = pid;
 
-        return r;
+        return TAKE_FD(pipefd[1]);
 }
 
 int import_fork_tar_c(const char *path, pid_t *ret) {
@@ -165,11 +161,7 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
                 _exit(EXIT_FAILURE);
         }
 
-        pipefd[1] = safe_close(pipefd[1]);
-        r = pipefd[0];
-        pipefd[0] = -1;
-
         *ret = pid;
 
-        return r;
+        return TAKE_FD(pipefd[0]);
 }
index f117c94da23880e6812d00e367342efea43fff87..032b3dcda2ee41943bcf3151a57ede1a58c43466 100644 (file)
@@ -210,8 +210,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) {
         free_and_replace(i->temp_path, t);
 
         safe_close(i->output_fd);
-        i->output_fd = converted_fd;
-        converted_fd = -1;
+        i->output_fd = TAKE_FD(converted_fd);
 
         return 1;
 }
index a0c02b26abd2b4627f2513dd8926aef239ae4887..55e61b485ff675f6150b8b2c82ba443bd551b115 100644 (file)
@@ -453,8 +453,7 @@ static int transfer_start(Transfer *t) {
         }
 
         pipefd[1] = safe_close(pipefd[1]);
-        t->log_fd = pipefd[0];
-        pipefd[0] = -1;
+        t->log_fd = TAKE_FD(pipefd[0]);
 
         t->stdin_fd = safe_close(t->stdin_fd);
 
index 880cb84ba845f09b65fb5f2c53e670cbeb0e7d59..9784141871a1b8a52dd776dcb7a4955a784135bf 100644 (file)
@@ -272,8 +272,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) {
         free_and_replace(i->temp_path, t);
 
         safe_close(i->raw_job->disk_fd);
-        i->raw_job->disk_fd = converted_fd;
-        converted_fd = -1;
+        i->raw_job->disk_fd = TAKE_FD(converted_fd);
 
         return 1;
 }
index 73329ba0248bb07dac5b53f10000f54d508c4be1..b6adf64c2723cc6af3bda8007bab001a9b330413 100644 (file)
@@ -441,9 +441,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
         if (r < 0)
                 return r;
 
-        r = fd;
-        fd = -1;
-        return r;
+        return TAKE_FD(fd);
 }
 
 _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
index b8e9b2e496307a9115ab7156283298afd13c1fe3..3217ac0d1f16fb7aca5fd82646273c22222c5e22 100644 (file)
@@ -103,10 +103,7 @@ int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_
         if (r < 0)
                 return -errno;
 
-        r = s;
-        s = -1;
-
-        return r;
+        return TAKE_FD(s);
 }
 
 static int arp_send_packet(int fd, int ifindex,
index 602bf08a6033d7baa7edcb83154c1b7fb830822a..ac9a7a31b6883e67fca33a502a05f192ec987d6e 100644 (file)
@@ -123,10 +123,7 @@ static int _bind_raw_socket(int ifindex, union sockaddr_union *link,
         if (r < 0)
                 return -errno;
 
-        r = s;
-        s = -1;
-
-        return r;
+        return TAKE_FD(s);
 }
 
 int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link,
@@ -211,10 +208,7 @@ int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) {
         if (r < 0)
                 return -errno;
 
-        r = s;
-        s = -1;
-
-        return r;
+        return TAKE_FD(s);
 }
 
 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
index b3b8fddbcb94f1bc15665dd2aedc1db46bcc5ce2..70f590930428c95a70632fee6940338ed063e2cc 100644 (file)
@@ -67,9 +67,7 @@ int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) {
         if (r < 0)
                 return -errno;
 
-        r = s;
-        s = -1;
-        return r;
+        return TAKE_FD(s);
 }
 
 int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address,
index dd4e28bfd999d47467e8d986166437d4d6aa0d6c..2347251572753533cd31ba6b041637a4fc49c52f 100644 (file)
@@ -98,9 +98,7 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter,
         if (r < 0)
                 return -errno;
 
-        r = s;
-        s = -1;
-        return r;
+        return TAKE_FD(s);
 }
 
 int icmp6_bind_router_solicitation(int index) {
index cb3841ef225223ebaf35ab775a7c26b92c552517..c653813cfb12e321a2b335eb453c843dcd566fe1 100644 (file)
@@ -92,8 +92,5 @@ int lldp_network_bind_raw_socket(int ifindex) {
         if (r < 0)
                 return -errno;
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 }
index 7aeeafd8e149d2202ef61de07de8b7b2e510f6dd..5673ea4f9e6f0ea786586a1bac88e60918d61987 100644 (file)
@@ -1145,8 +1145,7 @@ void session_restore_vt(Session *s) {
          * little dance to avoid having the terminal be available
          * for reuse before we've cleaned it up.
          */
-        old_fd = s->vtfd;
-        s->vtfd = -1;
+        old_fd = TAKE_FD(s->vtfd);
 
         vt = session_open_vt(s);
         safe_close(old_fd);
index bf6aac8300cd598e4d2c89424cd23d2f5d73270a..305bb6afe88750ce25be71009219e1db51a0d493 100644 (file)
@@ -325,7 +325,7 @@ static int dns_scope_socket(
         union sockaddr_union sa = {};
         socklen_t salen;
         static const int one = 1;
-        int ret, r, ifindex;
+        int r, ifindex;
 
         assert(s);
 
@@ -409,10 +409,7 @@ static int dns_scope_socket(
         if (r < 0 && errno != EINPROGRESS)
                 return -errno;
 
-        ret = fd;
-        fd = -1;
-
-        return ret;
+        return TAKE_FD(fd);
 }
 
 int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
index 5ce8d240ddc3167d0a248857f4601be0ed65ebb1..20ff8a1d3ba8eb1b01ac10f9d1f0aaa07458ff5f 100644 (file)
@@ -447,10 +447,8 @@ static int manager_dns_stub_udp_fd(Manager *m) {
                 return r;
 
         (void) sd_event_source_set_description(m->dns_stub_udp_event_source, "dns-stub-udp");
-        m->dns_stub_udp_fd = fd;
-        fd = -1;
 
-        return m->dns_stub_udp_fd;
+        return m->dns_stub_udp_fd = TAKE_FD(fd);
 }
 
 static int on_dns_stub_stream_packet(DnsStream *s) {
@@ -542,10 +540,8 @@ static int manager_dns_stub_tcp_fd(Manager *m) {
                 return r;
 
         (void) sd_event_source_set_description(m->dns_stub_tcp_event_source, "dns-stub-tcp");
-        m->dns_stub_tcp_fd = fd;
-        fd = -1;
 
-        return m->dns_stub_tcp_fd;
+        return m->dns_stub_tcp_fd = TAKE_FD(fd);
 }
 
 int manager_dns_stub_start(Manager *m) {
index 031d443e9e06100c85a46a9859210ce885f186cc..68e5bd19a08e006d3fafccd152e9eaf807d87a5b 100644 (file)
@@ -89,11 +89,8 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
          * /var/lib/machines. */
 
         fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-        if (fd >= 0) {
-                r = fd;
-                fd = -1;
-                return r;
-        }
+        if (fd >= 0)
+                return TAKE_FD(fd);
 
         if (errno != ENOENT)
                 return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m");
@@ -162,10 +159,7 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
                 goto fail;
         }
 
-        r = fd;
-        fd = -1;
-
-        return r;
+        return TAKE_FD(fd);
 
 fail:
         unlink_noerrno(tmp);
index 6bde3da1c8d9c00b6ef6aa46faeceb841e7902e3..92656c63833b7496057268ebb14e7d57c79ce5a2 100644 (file)
@@ -325,8 +325,8 @@ static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
 
 static int find_source_vc(char **ret_path, unsigned *ret_idx) {
         _cleanup_free_ char *path = NULL;
+        int r, err = 0;
         unsigned i;
-        int ret_fd, r, err = 0;
 
         path = new(char, sizeof("/dev/tty63"));
         if (!path)
@@ -359,18 +359,16 @@ static int find_source_vc(char **ret_path, unsigned *ret_idx) {
                 /* all checks passed, return this one as a source console */
                 *ret_idx = i;
                 *ret_path = TAKE_PTR(path);
-                ret_fd = fd;
-                fd = -1;
-                return ret_fd;
+                return TAKE_FD(fd);
         }
 
         return log_error_errno(err, "No usable source console found: %m");
 }
 
 static int verify_source_vc(char **ret_path, const char *src_vc) {
-        char *path;
         _cleanup_close_ int fd = -1;
-        int ret_fd, r;
+        char *path;
+        int r;
 
         fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY);
         if (fd < 0)
@@ -393,9 +391,7 @@ static int verify_source_vc(char **ret_path, const char *src_vc) {
                 return log_oom();
 
         *ret_path = path;
-        ret_fd = fd;
-        fd = -1;
-        return ret_fd;
+        return TAKE_FD(fd);
 }
 
 int main(int argc, char **argv) {