]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/fdset.c
fdset: minor modernizations
[thirdparty/systemd.git] / src / shared / fdset.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a16e1123 2
cf0fbc49 3#include <errno.h>
a16e1123 4#include <fcntl.h>
11c3a366 5#include <stddef.h>
a16e1123 6
07630cea
LP
7#include "sd-daemon.h"
8
ae2a15bc 9#include "alloc-util.h"
8fb3f009 10#include "dirent-util.h"
3ffd4af2
LP
11#include "fd-util.h"
12#include "fdset.h"
93cc7779 13#include "log.h"
3ffd4af2 14#include "macro.h"
6bedfcbb 15#include "parse-util.h"
11c3a366 16#include "path-util.h"
93cc7779 17#include "set.h"
08a0ebc6 18#include "stat-util.h"
a16e1123
LP
19
20#define MAKE_SET(s) ((Set*) s)
21#define MAKE_FDSET(s) ((FDSet*) s)
22
a16e1123 23FDSet *fdset_new(void) {
d5099efc 24 return MAKE_FDSET(set_new(NULL));
a16e1123
LP
25}
26
08a0ebc6
LP
27static inline void fdset_shallow_freep(FDSet **s) {
28 /* Destroys the set, but does not free the fds inside, like fdset_free()! */
29 set_free(MAKE_SET(*ASSERT_PTR(s)));
30}
31
32int fdset_new_array(FDSet **ret, const int fds[], size_t n_fds) {
33 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
a354329f
LP
34 int r;
35
36 assert(ret);
08a0ebc6 37 assert(fds || n_fds == 0);
a354329f
LP
38
39 s = fdset_new();
40 if (!s)
41 return -ENOMEM;
42
08a0ebc6 43 for (size_t i = 0; i < n_fds; i++) {
a354329f 44 r = fdset_put(s, fds[i]);
08a0ebc6 45 if (r < 0)
a354329f 46 return r;
a354329f
LP
47 }
48
08a0ebc6 49 *ret = TAKE_PTR(s);
a354329f
LP
50 return 0;
51}
52
e4077ff6 53void fdset_close(FDSet *s) {
a16e1123
LP
54 void *p;
55
56 while ((p = set_steal_first(MAKE_SET(s)))) {
e4077ff6
LP
57 /* Valgrind's fd might have ended up in this set here, due to fdset_new_fill(). We'll ignore
58 * all failures here, so that the EBADFD that valgrind will return us on close() doesn't
59 * influence us */
60
61 /* When reloading duplicates of the private bus connection fds and suchlike are closed here,
62 * which has no effect at all, since they are only duplicates. So don't be surprised about
63 * these log messages. */
64
65 log_debug("Closing set fd %i", PTR_TO_FD(p));
66 (void) close_nointr(PTR_TO_FD(p));
a16e1123 67 }
e4077ff6 68}
a16e1123 69
e4077ff6
LP
70FDSet* fdset_free(FDSet *s) {
71 fdset_close(s);
a16e1123 72 set_free(MAKE_SET(s));
a354329f 73 return NULL;
a16e1123
LP
74}
75
76int fdset_put(FDSet *s, int fd) {
77 assert(s);
78 assert(fd >= 0);
79
80 return set_put(MAKE_SET(s), FD_TO_PTR(fd));
81}
82
83int fdset_put_dup(FDSet *s, int fd) {
08a0ebc6
LP
84 _cleanup_close_ int copy = -EBADF;
85 int r;
a16e1123
LP
86
87 assert(s);
88 assert(fd >= 0);
89
e83c7163
LP
90 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
91 if (copy < 0)
a16e1123
LP
92 return -errno;
93
e83c7163 94 r = fdset_put(s, copy);
08a0ebc6 95 if (r < 0)
a16e1123 96 return r;
a16e1123 97
08a0ebc6 98 return TAKE_FD(copy);
a16e1123
LP
99}
100
101bool fdset_contains(FDSet *s, int fd) {
102 assert(s);
103 assert(fd >= 0);
104
105 return !!set_get(MAKE_SET(s), FD_TO_PTR(fd));
106}
107
108int fdset_remove(FDSet *s, int fd) {
109 assert(s);
110 assert(fd >= 0);
111
112 return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT;
113}
114
08a0ebc6
LP
115int fdset_new_fill(FDSet **ret) {
116 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e1d75803 117 _cleanup_closedir_ DIR *d = NULL;
08a0ebc6 118 int r;
a16e1123 119
08a0ebc6 120 assert(ret);
a16e1123 121
08a0ebc6 122 /* Creates an fdset and fills in all currently open file descriptors. */
a16e1123 123
e83c7163 124 d = opendir("/proc/self/fd");
08a0ebc6
LP
125 if (!d) {
126 if (errno == ENOENT && proc_mounted() == 0)
127 return -ENOSYS;
128
a16e1123 129 return -errno;
08a0ebc6 130 }
a16e1123 131
e83c7163 132 s = fdset_new();
08a0ebc6
LP
133 if (!s)
134 return -ENOMEM;
a16e1123 135
8fb3f009 136 FOREACH_DIRENT(de, d, return -errno) {
254d1313 137 int fd = -EBADF;
a16e1123 138
e83c7163
LP
139 r = safe_atoi(de->d_name, &fd);
140 if (r < 0)
08a0ebc6 141 return r;
a16e1123
LP
142
143 if (fd < 3)
144 continue;
a16e1123
LP
145 if (fd == dirfd(d))
146 continue;
147
e83c7163
LP
148 r = fdset_put(s, fd);
149 if (r < 0)
08a0ebc6 150 return r;
a16e1123
LP
151 }
152
08a0ebc6
LP
153 *ret = TAKE_PTR(s);
154 return 0;
a16e1123
LP
155}
156
157int fdset_cloexec(FDSet *fds, bool b) {
a16e1123
LP
158 void *p;
159 int r;
160
161 assert(fds);
162
90e74a66 163 SET_FOREACH(p, MAKE_SET(fds)) {
3cc2aff1
LP
164 r = fd_cloexec(PTR_TO_FD(p), b);
165 if (r < 0)
a16e1123 166 return r;
3cc2aff1 167 }
a16e1123
LP
168
169 return 0;
170}
e83c7163 171
08a0ebc6
LP
172int fdset_new_listen_fds(FDSet **ret, bool unset) {
173 _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
e83c7163 174 int n, fd, r;
e83c7163 175
08a0ebc6 176 assert(ret);
e83c7163
LP
177
178 /* Creates an fdset and fills in all passed file descriptors */
179
180 s = fdset_new();
08a0ebc6
LP
181 if (!s)
182 return -ENOMEM;
e83c7163
LP
183
184 n = sd_listen_fds(unset);
185 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
186 r = fdset_put(s, fd);
187 if (r < 0)
08a0ebc6 188 return r;
e83c7163
LP
189 }
190
08a0ebc6 191 *ret = TAKE_PTR(s);
e83c7163 192 return 0;
e83c7163
LP
193}
194
195int fdset_close_others(FDSet *fds) {
08a0ebc6 196 _cleanup_free_ int *a = NULL;
da6053d0 197 size_t j = 0, m;
08a0ebc6 198 void *e;
e83c7163 199
da6053d0 200 m = fdset_size(fds);
8192548e 201 if (m > 0) {
08a0ebc6
LP
202 a = new(int, m);
203 if (!a)
204 return -ENOMEM;
205
90e74a66 206 SET_FOREACH(e, MAKE_SET(fds))
8192548e
DT
207 a[j++] = PTR_TO_FD(e);
208 }
e83c7163
LP
209
210 assert(j == m);
211
212 return close_all_fds(a, j);
213}
214
215unsigned fdset_size(FDSet *fds) {
216 return set_size(MAKE_SET(fds));
217}
218
a354329f
LP
219bool fdset_isempty(FDSet *fds) {
220 return set_isempty(MAKE_SET(fds));
221}
222
e83c7163
LP
223int fdset_iterate(FDSet *s, Iterator *i) {
224 void *p;
225
8927b1da 226 if (!set_iterate(MAKE_SET(s), i, &p))
e83c7163
LP
227 return -ENOENT;
228
229 return PTR_TO_FD(p);
230}
a354329f
LP
231
232int fdset_steal_first(FDSet *fds) {
233 void *p;
234
235 p = set_steal_first(MAKE_SET(fds));
236 if (!p)
237 return -ENOENT;
238
239 return PTR_TO_FD(p);
240}