From e829f28c1bc6e6865261bfb3bc26089f50e0c7bd Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 29 Mar 2023 18:52:25 +0200
Subject: [PATCH] fdset: add new fdset_consume() helper

Conflict:Context Adaptation. Delete the code that is not involved.
Reference:https://github.com/systemd/systemd/commit/e829f28c1bc6e6865261bfb3bc26089f50e0c7bd

---
 src/shared/fdset.c | 13 +++++++++++++
 src/shared/fdset.h |  1 +
 2 files changed, 14 insertions(+)

diff --git a/src/shared/fdset.c b/src/shared/fdset.c
index 443aa7f..68a465f 100644
--- a/src/shared/fdset.c
+++ b/src/shared/fdset.c
@@ -81,6 +81,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) {
         int copy, r;
 
diff --git a/src/shared/fdset.h b/src/shared/fdset.h
index e8a6b48..ac5ec76 100644
--- a/src/shared/fdset.h
+++ b/src/shared/fdset.h
@@ -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);
-- 
2.33.0

