]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fdset: add new fdset_consume() helper
authorLennart Poettering <lennart@poettering.net>
Wed, 29 Mar 2023 16:52:25 +0000 (18:52 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Mar 2023 17:09:10 +0000 (19:09 +0200)
src/shared/fdset.c
src/shared/fdset.h
src/udev/udevadm-lock.c

index 49928b66d6e749869eb4a5d9483225b846ebf17b..d816a3e4efb9640dcbfeec8be3244a054466f95b 100644 (file)
@@ -80,6 +80,19 @@ int fdset_put(FDSet *s, int fd) {
         return set_put(MAKE_SET(s), FD_TO_PTR(fd));
 }
 
+int fdset_consume(FDSet *s, int fd) {
+        int r;
+
+        assert(s);
+        assert(fd >= 0);
+
+        r = fdset_put(s, fd);
+        if (r < 0)
+                safe_close(fd);
+
+        return r;
+}
+
 int fdset_put_dup(FDSet *s, int fd) {
         _cleanup_close_ int copy = -EBADF;
         int r;
index 0e55db81b432507ff9c24b11e8f0eb8a1fcd87c2..9700cdbbca8b6d468b3e85aa5c80e6bf53cd275c 100644 (file)
@@ -13,6 +13,7 @@ FDSet* fdset_new(void);
 FDSet* fdset_free(FDSet *s);
 
 int fdset_put(FDSet *s, int fd);
+int fdset_consume(FDSet *s, int fd);
 int fdset_put_dup(FDSet *s, int fd);
 
 bool fdset_contains(FDSet *s, int fd);
index d19e7561f81f166cabefc3007f732d8ee3d56e6c..9a4a8adbc321a2dcadbc41f54e0af50e4aa4d69f 100644 (file)
@@ -334,11 +334,9 @@ int lock_main(int argc, char *argv[], void *userdata) {
                         if (fd < 0)
                                 return fd;
 
-                        r = fdset_put(fds, fd);
+                        r = fdset_consume(fds, TAKE_FD(fd));
                         if (r < 0)
                                 return log_oom();
-
-                        TAKE_FD(fd);
                 }
         }