-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
+#include <unistd.h>
#include "sd-daemon.h"
#include "parse-util.h"
#include "path-util.h"
#include "set.h"
+#include "stat-util.h"
#define MAKE_SET(s) ((Set*) s)
#define MAKE_FDSET(s) ((FDSet*) s)
return MAKE_FDSET(set_new(NULL));
}
-int fdset_new_array(FDSet **ret, const int *fds, size_t n_fds) {
- size_t i;
- FDSet *s;
+static void fdset_shallow_freep(FDSet **s) {
+ /* Destroys the set, but does not free the fds inside, like fdset_free()! */
+ set_free(MAKE_SET(*ASSERT_PTR(s)));
+}
+
+int fdset_new_array(FDSet **ret, const int fds[], size_t n_fds) {
+ _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
int r;
assert(ret);
+ assert(fds || n_fds == 0);
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));
+ FOREACH_ARRAY(fd, fds, n_fds) {
+ r = fdset_put(s, *fd);
+ if (r < 0)
return r;
- }
}
- *ret = s;
+ *ret = TAKE_PTR(s);
return 0;
}
void *p;
while ((p = set_steal_first(MAKE_SET(s)))) {
+ int fd = PTR_TO_FD(p);
+
/* Valgrind's fd might have ended up in this set here, due to fdset_new_fill(). We'll ignore
* all failures here, so that the EBADFD that valgrind will return us on close() doesn't
* influence us */
* which has no effect at all, since they are only duplicates. So don't be surprised about
* these log messages. */
- log_debug("Closing set fd %i", PTR_TO_FD(p));
- (void) close_nointr(PTR_TO_FD(p));
+ if (DEBUG_LOGGING) {
+ _cleanup_free_ char *path = NULL;
+
+ (void) fd_get_path(fd, &path);
+ log_debug("Closing set fd %i (%s)", fd, strna(path));
+ }
+
+ (void) close(fd);
}
}
assert(s);
assert(fd >= 0);
+ /* Avoid integer overflow in FD_TO_PTR() */
+ if (fd == INT_MAX)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Refusing invalid fd: %d", 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;
+ _cleanup_close_ int copy = -EBADF;
+ int r;
assert(s);
assert(fd >= 0);
return -errno;
r = fdset_put(s, copy);
- if (r < 0) {
- safe_close(copy);
+ if (r < 0)
return r;
- }
- return copy;
+ return TAKE_FD(copy);
}
bool fdset_contains(FDSet *s, int fd) {
assert(s);
assert(fd >= 0);
+ /* Avoid integer overflow in FD_TO_PTR() */
+ if (fd == INT_MAX) {
+ log_debug("Refusing invalid fd: %d", fd);
+ return false;
+ }
+
return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
}
assert(s);
assert(fd >= 0);
+ /* Avoid integer overflow in FD_TO_PTR() */
+ if (fd == INT_MAX)
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Refusing invalid fd: %d", fd);
+
return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
}
-int fdset_new_fill(FDSet **_s) {
+int fdset_new_fill(
+ int filter_cloexec, /* if < 0 takes all fds, otherwise only those with O_CLOEXEC set (1) or unset (0) */
+ FDSet **ret) {
+
+ _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
_cleanup_closedir_ DIR *d = NULL;
- struct dirent *de;
- int r = 0;
- FDSet *s;
+ int r;
- assert(_s);
+ assert(ret);
- /* Creates an fdset and fills in all currently open file
- * descriptors. */
+ /* Creates an fdset and fills in all currently open file descriptors. Also set all collected fds
+ * to CLOEXEC. */
d = opendir("/proc/self/fd");
- if (!d)
+ if (!d) {
+ if (errno == ENOENT && proc_mounted() == 0)
+ return -ENOSYS;
+
return -errno;
+ }
s = fdset_new();
- if (!s) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!s)
+ return -ENOMEM;
FOREACH_DIRENT(de, d, return -errno) {
- int fd = -1;
+ int fd;
- r = safe_atoi(de->d_name, &fd);
- if (r < 0)
- goto finish;
+ if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
+ continue;
+
+ fd = parse_fd(de->d_name);
+ if (fd < 0)
+ return fd;
if (fd < 3)
continue;
-
if (fd == dirfd(d))
continue;
+ if (filter_cloexec >= 0) {
+ int fl;
+
+ /* If user asked for that filter by O_CLOEXEC. This is useful so that fds that have
+ * been passed in can be collected and fds which have been created locally can be
+ * ignored, under the assumption that only the latter have O_CLOEXEC set. */
+
+ fl = fcntl(fd, F_GETFD);
+ if (fl < 0)
+ return -errno;
+
+ if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec)
+ continue;
+ }
+
+ /* We need to set CLOEXEC manually only if we're collecting non-CLOEXEC fds. */
+ if (filter_cloexec <= 0) {
+ r = fd_cloexec(fd, true);
+ if (r < 0)
+ return r;
+ }
+
r = fdset_put(s, fd);
if (r < 0)
- goto finish;
+ return r;
}
- r = 0;
- *_s = TAKE_PTR(s);
-
-finish:
- /* We won't close the fds here! */
- if (s)
- set_free(MAKE_SET(s));
-
- return r;
+ *ret = TAKE_PTR(s);
+ return 0;
}
int fdset_cloexec(FDSet *fds, bool b) {
- Iterator i;
void *p;
int r;
assert(fds);
- SET_FOREACH(p, MAKE_SET(fds), i) {
+ SET_FOREACH(p, MAKE_SET(fds)) {
r = fd_cloexec(PTR_TO_FD(p), b);
if (r < 0)
return r;
return 0;
}
-int fdset_new_listen_fds(FDSet **_s, bool unset) {
+int fdset_new_listen_fds(FDSet **ret, bool unset) {
+ _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
int n, fd, r;
- FDSet *s;
- assert(_s);
+ assert(ret);
/* Creates an fdset and fills in all passed file descriptors */
s = fdset_new();
- if (!s) {
- r = -ENOMEM;
- goto fail;
- }
+ if (!s)
+ return -ENOMEM;
n = sd_listen_fds(unset);
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
+ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
r = fdset_put(s, fd);
if (r < 0)
- goto fail;
+ return r;
}
- *_s = s;
+ *ret = TAKE_PTR(s);
return 0;
-
-fail:
- if (s)
- set_free(MAKE_SET(s));
-
- return r;
}
-int fdset_close_others(FDSet *fds) {
+int fdset_to_array(FDSet *fds, int **ret) {
+ unsigned j = 0, m;
void *e;
- Iterator i;
- int *a = NULL;
- size_t j = 0, m;
+ int *a;
- m = fdset_size(fds);
+ assert(ret);
- if (m > 0) {
- a = newa(int, m);
- SET_FOREACH(e, MAKE_SET(fds), i)
- a[j++] = PTR_TO_FD(e);
+ m = fdset_size(fds);
+ if (m > INT_MAX) /* We want to be able to return an "int" */
+ return -ENOMEM;
+ if (m == 0) {
+ *ret = NULL; /* suppress array allocation if empty */
+ return 0;
}
+ a = new(int, m);
+ if (!a)
+ return -ENOMEM;
+
+ SET_FOREACH(e, MAKE_SET(fds))
+ a[j++] = PTR_TO_FD(e);
+
assert(j == m);
- return close_all_fds(a, j);
+ *ret = TAKE_PTR(a);
+ return (int) m;
+}
+
+int fdset_close_others(FDSet *fds) {
+ _cleanup_free_ int *a = NULL;
+ int n;
+
+ n = fdset_to_array(fds, &a);
+ if (n < 0)
+ return n;
+
+ return close_all_fds(a, n);
}
unsigned fdset_size(FDSet *fds) {