2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <errno.h>
|
2010-04-21 03:27:44 +02:00
|
|
|
#include <fcntl.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stddef.h>
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-daemon.h"
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
#include "alloc-util.h"
|
2016-12-09 10:04:30 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fdset.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "log.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "macro.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "path-util.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "set.h"
|
2010-04-21 03:27:44 +02:00
|
|
|
|
|
|
|
#define MAKE_SET(s) ((Set*) s)
|
|
|
|
#define MAKE_FDSET(s) ((FDSet*) s)
|
|
|
|
|
|
|
|
FDSet *fdset_new(void) {
|
2014-08-13 01:00:18 +02:00
|
|
|
return MAKE_FDSET(set_new(NULL));
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
int fdset_new_array(FDSet **ret, const int *fds, size_t n_fds) {
|
|
|
|
size_t i;
|
2015-01-06 00:26:25 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:22:45 +01:00
|
|
|
void fdset_close(FDSet *s) {
|
2010-04-21 03:27:44 +02:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
while ((p = set_steal_first(MAKE_SET(s)))) {
|
2019-03-22 15:22:45 +01:00
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
/* When reloading duplicates of the private bus connection fds and suchlike are closed here,
|
|
|
|
* 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));
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
2019-03-22 15:22:45 +01:00
|
|
|
}
|
2010-04-21 03:27:44 +02:00
|
|
|
|
2019-03-22 15:22:45 +01:00
|
|
|
FDSet* fdset_free(FDSet *s) {
|
|
|
|
fdset_close(s);
|
2010-04-21 03:27:44 +02:00
|
|
|
set_free(MAKE_SET(s));
|
2015-01-06 00:26:25 +01:00
|
|
|
return NULL;
|
2010-04-21 03:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_put(FDSet *s, int fd) {
|
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
return set_put(MAKE_SET(s), FD_TO_PTR(fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_put_dup(FDSet *s, int fd) {
|
|
|
|
int copy, r;
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
|
|
|
|
if (copy < 0)
|
2010-04-21 03:27:44 +02:00
|
|
|
return -errno;
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
r = fdset_put(s, copy);
|
|
|
|
if (r < 0) {
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(copy);
|
2010-04-21 03:27:44 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fdset_contains(FDSet *s, int fd) {
|
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_remove(FDSet *s, int fd) {
|
|
|
|
assert(s);
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_new_fill(FDSet **_s) {
|
2014-06-24 19:00:32 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2010-04-21 03:27:44 +02:00
|
|
|
struct dirent *de;
|
|
|
|
int r = 0;
|
|
|
|
FDSet *s;
|
|
|
|
|
|
|
|
assert(_s);
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
/* Creates an fdset and fills in all currently open file
|
2010-04-21 03:27:44 +02:00
|
|
|
* descriptors. */
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
d = opendir("/proc/self/fd");
|
|
|
|
if (!d)
|
2010-04-21 03:27:44 +02:00
|
|
|
return -errno;
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
s = fdset_new();
|
|
|
|
if (!s) {
|
2010-04-21 03:27:44 +02:00
|
|
|
r = -ENOMEM;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2016-12-09 10:04:30 +01:00
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
2010-04-21 03:27:44 +02:00
|
|
|
int fd = -1;
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
r = safe_atoi(de->d_name, &fd);
|
|
|
|
if (r < 0)
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (fd < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fd == dirfd(d))
|
|
|
|
continue;
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
r = fdset_put(s, fd);
|
|
|
|
if (r < 0)
|
2010-04-21 03:27:44 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
2018-03-22 16:53:26 +01:00
|
|
|
*_s = TAKE_PTR(s);
|
2010-04-21 03:27:44 +02:00
|
|
|
|
|
|
|
finish:
|
|
|
|
/* We won't close the fds here! */
|
|
|
|
if (s)
|
|
|
|
set_free(MAKE_SET(s));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_cloexec(FDSet *fds, bool b) {
|
|
|
|
void *p;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(fds);
|
|
|
|
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(p, MAKE_SET(fds)) {
|
2015-09-08 19:14:10 +02:00
|
|
|
r = fd_cloexec(PTR_TO_FD(p), b);
|
|
|
|
if (r < 0)
|
2010-04-21 03:27:44 +02:00
|
|
|
return r;
|
2015-09-08 19:14:10 +02:00
|
|
|
}
|
2010-04-21 03:27:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-22 19:28:19 +01:00
|
|
|
|
|
|
|
int fdset_new_listen_fds(FDSet **_s, bool unset) {
|
|
|
|
int n, fd, r;
|
|
|
|
FDSet *s;
|
|
|
|
|
|
|
|
assert(_s);
|
|
|
|
|
|
|
|
/* Creates an fdset and fills in all passed file descriptors */
|
|
|
|
|
|
|
|
s = fdset_new();
|
|
|
|
if (!s) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = sd_listen_fds(unset);
|
|
|
|
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
|
|
|
|
r = fdset_put(s, fd);
|
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_s = s;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (s)
|
|
|
|
set_free(MAKE_SET(s));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdset_close_others(FDSet *fds) {
|
|
|
|
void *e;
|
2018-10-12 14:48:41 +02:00
|
|
|
int *a = NULL;
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t j = 0, m;
|
2012-12-22 19:28:19 +01:00
|
|
|
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
m = fdset_size(fds);
|
2018-10-12 14:48:41 +02:00
|
|
|
|
|
|
|
if (m > 0) {
|
|
|
|
a = newa(int, m);
|
2020-09-08 11:58:29 +02:00
|
|
|
SET_FOREACH(e, MAKE_SET(fds))
|
2018-10-12 14:48:41 +02:00
|
|
|
a[j++] = PTR_TO_FD(e);
|
|
|
|
}
|
2012-12-22 19:28:19 +01:00
|
|
|
|
|
|
|
assert(j == m);
|
|
|
|
|
|
|
|
return close_all_fds(a, j);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned fdset_size(FDSet *fds) {
|
|
|
|
return set_size(MAKE_SET(fds));
|
|
|
|
}
|
|
|
|
|
2015-01-06 00:26:25 +01:00
|
|
|
bool fdset_isempty(FDSet *fds) {
|
|
|
|
return set_isempty(MAKE_SET(fds));
|
|
|
|
}
|
|
|
|
|
2012-12-22 19:28:19 +01:00
|
|
|
int fdset_iterate(FDSet *s, Iterator *i) {
|
|
|
|
void *p;
|
|
|
|
|
2015-06-14 16:51:35 +02:00
|
|
|
if (!set_iterate(MAKE_SET(s), i, &p))
|
2012-12-22 19:28:19 +01:00
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
return PTR_TO_FD(p);
|
|
|
|
}
|
2015-01-06 00:26:25 +01:00
|
|
|
|
|
|
|
int fdset_steal_first(FDSet *fds) {
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = set_steal_first(MAKE_SET(fds));
|
|
|
|
if (!p)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
return PTR_TO_FD(p);
|
|
|
|
}
|