]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/fdset.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / shared / fdset.c
index a2c861de3f4b0486f7fbb742e717237555b16c09..e9802bb82caadde96c44e3246f582449aaa6e301 100644 (file)
@@ -1,47 +1,59 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
   Copyright 2010 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <alloca.h>
 #include <errno.h>
-#include <dirent.h>
 #include <fcntl.h>
-#include <unistd.h>
+#include <stddef.h>
 
-#include "set.h"
-#include "util.h"
-#include "macro.h"
-#include "fdset.h"
 #include "sd-daemon.h"
 
+#include "alloc-util.h"
+#include "dirent-util.h"
+#include "fd-util.h"
+#include "fdset.h"
+#include "log.h"
+#include "macro.h"
+#include "parse-util.h"
+#include "path-util.h"
+#include "set.h"
+
 #define MAKE_SET(s) ((Set*) s)
 #define MAKE_FDSET(s) ((FDSet*) s)
 
-/* Make sure we can distuingish fd 0 and NULL */
-#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
-#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
-
 FDSet *fdset_new(void) {
-        return MAKE_FDSET(set_new(trivial_hash_func, trivial_compare_func));
+        return MAKE_FDSET(set_new(NULL));
+}
+
+int fdset_new_array(FDSet **ret, const int *fds, unsigned n_fds) {
+        unsigned i;
+        FDSet *s;
+        int r;
+
+        assert(ret);
+
+        s = fdset_new();
+        if (!s)
+                return -ENOMEM;
+
+        for (i = 0; i < n_fds; i++) {
+
+                r = fdset_put(s, fds[i]);
+                if (r < 0) {
+                        set_free(MAKE_SET(s));
+                        return r;
+                }
+        }
+
+        *ret = s;
+        return 0;
 }
 
-void fdset_free(FDSet *s) {
+FDSet* fdset_free(FDSet *s) {
         void *p;
 
         while ((p = set_steal_first(MAKE_SET(s)))) {
@@ -61,6 +73,7 @@ void fdset_free(FDSet *s) {
         }
 
         set_free(MAKE_SET(s));
+        return NULL;
 }
 
 int fdset_put(FDSet *s, int fd) {
@@ -104,7 +117,7 @@ int fdset_remove(FDSet *s, int fd) {
 }
 
 int fdset_new_fill(FDSet **_s) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
         FDSet *s;
@@ -124,12 +137,9 @@ int fdset_new_fill(FDSet **_s) {
                 goto finish;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 int fd = -1;
 
-                if (ignore_file(de->d_name))
-                        continue;
-
                 r = safe_atoi(de->d_name, &fd);
                 if (r < 0)
                         goto finish;
@@ -146,12 +156,9 @@ int fdset_new_fill(FDSet **_s) {
         }
 
         r = 0;
-        *_s = s;
-        s = NULL;
+        *_s = TAKE_PTR(s);
 
 finish:
-        closedir(d);
-
         /* We won't close the fds here! */
         if (s)
                 set_free(MAKE_SET(s));
@@ -166,9 +173,11 @@ int fdset_cloexec(FDSet *fds, bool b) {
 
         assert(fds);
 
-        SET_FOREACH(p, MAKE_SET(fds), i)
-                if ((r = fd_cloexec(PTR_TO_FD(p), b)) < 0)
+        SET_FOREACH(p, MAKE_SET(fds), i) {
+                r = fd_cloexec(PTR_TO_FD(p), b);
+                if (r < 0)
                         return r;
+        }
 
         return 0;
 }
@@ -225,10 +234,23 @@ unsigned fdset_size(FDSet *fds) {
         return set_size(MAKE_SET(fds));
 }
 
+bool fdset_isempty(FDSet *fds) {
+        return set_isempty(MAKE_SET(fds));
+}
+
 int fdset_iterate(FDSet *s, Iterator *i) {
         void *p;
 
-        p = set_iterate(MAKE_SET(s), i);
+        if (!set_iterate(MAKE_SET(s), i, &p))
+                return -ENOENT;
+
+        return PTR_TO_FD(p);
+}
+
+int fdset_steal_first(FDSet *fds) {
+        void *p;
+
+        p = set_steal_first(MAKE_SET(fds));
         if (!p)
                 return -ENOENT;