]>
Commit | Line | Data |
---|---|---|
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 | 23 | FDSet *fdset_new(void) { |
d5099efc | 24 | return MAKE_FDSET(set_new(NULL)); |
a16e1123 LP |
25 | } |
26 | ||
08a0ebc6 LP |
27 | static 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 | ||
32 | int 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 | 53 | void 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 |
70 | FDSet* fdset_free(FDSet *s) { |
71 | fdset_close(s); | |
a16e1123 | 72 | set_free(MAKE_SET(s)); |
a354329f | 73 | return NULL; |
a16e1123 LP |
74 | } |
75 | ||
76 | int fdset_put(FDSet *s, int fd) { | |
77 | assert(s); | |
78 | assert(fd >= 0); | |
79 | ||
cc938f1c FS |
80 | /* Avoid integer overflow in FD_TO_PTR() */ |
81 | if (fd == INT_MAX) | |
82 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Refusing invalid fd: %d", fd); | |
83 | ||
a16e1123 LP |
84 | return set_put(MAKE_SET(s), FD_TO_PTR(fd)); |
85 | } | |
86 | ||
e829f28c LP |
87 | int fdset_consume(FDSet *s, int fd) { |
88 | int r; | |
89 | ||
90 | assert(s); | |
91 | assert(fd >= 0); | |
92 | ||
93 | r = fdset_put(s, fd); | |
94 | if (r < 0) | |
95 | safe_close(fd); | |
96 | ||
97 | return r; | |
98 | } | |
99 | ||
a16e1123 | 100 | int fdset_put_dup(FDSet *s, int fd) { |
08a0ebc6 LP |
101 | _cleanup_close_ int copy = -EBADF; |
102 | int r; | |
a16e1123 LP |
103 | |
104 | assert(s); | |
105 | assert(fd >= 0); | |
106 | ||
e83c7163 LP |
107 | copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
108 | if (copy < 0) | |
a16e1123 LP |
109 | return -errno; |
110 | ||
e83c7163 | 111 | r = fdset_put(s, copy); |
08a0ebc6 | 112 | if (r < 0) |
a16e1123 | 113 | return r; |
a16e1123 | 114 | |
08a0ebc6 | 115 | return TAKE_FD(copy); |
a16e1123 LP |
116 | } |
117 | ||
118 | bool fdset_contains(FDSet *s, int fd) { | |
119 | assert(s); | |
120 | assert(fd >= 0); | |
121 | ||
cc938f1c FS |
122 | /* Avoid integer overflow in FD_TO_PTR() */ |
123 | if (fd == INT_MAX) { | |
124 | log_debug("Refusing invalid fd: %d", fd); | |
125 | return false; | |
126 | } | |
127 | ||
a16e1123 LP |
128 | return !!set_get(MAKE_SET(s), FD_TO_PTR(fd)); |
129 | } | |
130 | ||
131 | int fdset_remove(FDSet *s, int fd) { | |
132 | assert(s); | |
133 | assert(fd >= 0); | |
134 | ||
cc938f1c FS |
135 | /* Avoid integer overflow in FD_TO_PTR() */ |
136 | if (fd == INT_MAX) | |
137 | return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Refusing invalid fd: %d", fd); | |
138 | ||
a16e1123 LP |
139 | return set_remove(MAKE_SET(s), FD_TO_PTR(fd)) ? fd : -ENOENT; |
140 | } | |
141 | ||
08a0ebc6 LP |
142 | int fdset_new_fill(FDSet **ret) { |
143 | _cleanup_(fdset_shallow_freep) FDSet *s = NULL; | |
e1d75803 | 144 | _cleanup_closedir_ DIR *d = NULL; |
08a0ebc6 | 145 | int r; |
a16e1123 | 146 | |
08a0ebc6 | 147 | assert(ret); |
a16e1123 | 148 | |
08a0ebc6 | 149 | /* Creates an fdset and fills in all currently open file descriptors. */ |
a16e1123 | 150 | |
e83c7163 | 151 | d = opendir("/proc/self/fd"); |
08a0ebc6 LP |
152 | if (!d) { |
153 | if (errno == ENOENT && proc_mounted() == 0) | |
154 | return -ENOSYS; | |
155 | ||
a16e1123 | 156 | return -errno; |
08a0ebc6 | 157 | } |
a16e1123 | 158 | |
e83c7163 | 159 | s = fdset_new(); |
08a0ebc6 LP |
160 | if (!s) |
161 | return -ENOMEM; | |
a16e1123 | 162 | |
8fb3f009 | 163 | FOREACH_DIRENT(de, d, return -errno) { |
254d1313 | 164 | int fd = -EBADF; |
a16e1123 | 165 | |
e83c7163 LP |
166 | r = safe_atoi(de->d_name, &fd); |
167 | if (r < 0) | |
08a0ebc6 | 168 | return r; |
a16e1123 LP |
169 | |
170 | if (fd < 3) | |
171 | continue; | |
a16e1123 LP |
172 | if (fd == dirfd(d)) |
173 | continue; | |
174 | ||
e83c7163 LP |
175 | r = fdset_put(s, fd); |
176 | if (r < 0) | |
08a0ebc6 | 177 | return r; |
a16e1123 LP |
178 | } |
179 | ||
08a0ebc6 LP |
180 | *ret = TAKE_PTR(s); |
181 | return 0; | |
a16e1123 LP |
182 | } |
183 | ||
184 | int fdset_cloexec(FDSet *fds, bool b) { | |
a16e1123 LP |
185 | void *p; |
186 | int r; | |
187 | ||
188 | assert(fds); | |
189 | ||
90e74a66 | 190 | SET_FOREACH(p, MAKE_SET(fds)) { |
3cc2aff1 LP |
191 | r = fd_cloexec(PTR_TO_FD(p), b); |
192 | if (r < 0) | |
a16e1123 | 193 | return r; |
3cc2aff1 | 194 | } |
a16e1123 LP |
195 | |
196 | return 0; | |
197 | } | |
e83c7163 | 198 | |
08a0ebc6 LP |
199 | int fdset_new_listen_fds(FDSet **ret, bool unset) { |
200 | _cleanup_(fdset_shallow_freep) FDSet *s = NULL; | |
e83c7163 | 201 | int n, fd, r; |
e83c7163 | 202 | |
08a0ebc6 | 203 | assert(ret); |
e83c7163 LP |
204 | |
205 | /* Creates an fdset and fills in all passed file descriptors */ | |
206 | ||
207 | s = fdset_new(); | |
08a0ebc6 LP |
208 | if (!s) |
209 | return -ENOMEM; | |
e83c7163 LP |
210 | |
211 | n = sd_listen_fds(unset); | |
212 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { | |
213 | r = fdset_put(s, fd); | |
214 | if (r < 0) | |
08a0ebc6 | 215 | return r; |
e83c7163 LP |
216 | } |
217 | ||
08a0ebc6 | 218 | *ret = TAKE_PTR(s); |
e83c7163 | 219 | return 0; |
e83c7163 LP |
220 | } |
221 | ||
bdcad22e LP |
222 | int fdset_to_array(FDSet *fds, int **ret) { |
223 | unsigned j = 0, m; | |
08a0ebc6 | 224 | void *e; |
bdcad22e | 225 | int *a; |
e83c7163 | 226 | |
bdcad22e | 227 | assert(ret); |
08a0ebc6 | 228 | |
bdcad22e LP |
229 | m = fdset_size(fds); |
230 | if (m > INT_MAX) /* We want to be able to return an "int" */ | |
231 | return -ENOMEM; | |
232 | if (m == 0) { | |
233 | *ret = NULL; /* suppress array allocation if empty */ | |
234 | return 0; | |
8192548e | 235 | } |
e83c7163 | 236 | |
bdcad22e LP |
237 | a = new(int, m); |
238 | if (!a) | |
239 | return -ENOMEM; | |
240 | ||
241 | SET_FOREACH(e, MAKE_SET(fds)) | |
242 | a[j++] = PTR_TO_FD(e); | |
243 | ||
e83c7163 LP |
244 | assert(j == m); |
245 | ||
bdcad22e LP |
246 | *ret = TAKE_PTR(a); |
247 | return (int) m; | |
248 | } | |
249 | ||
250 | int fdset_close_others(FDSet *fds) { | |
251 | _cleanup_free_ int *a = NULL; | |
252 | int n; | |
253 | ||
254 | n = fdset_to_array(fds, &a); | |
255 | if (n < 0) | |
256 | return n; | |
257 | ||
258 | return close_all_fds(a, n); | |
e83c7163 LP |
259 | } |
260 | ||
261 | unsigned fdset_size(FDSet *fds) { | |
262 | return set_size(MAKE_SET(fds)); | |
263 | } | |
264 | ||
a354329f LP |
265 | bool fdset_isempty(FDSet *fds) { |
266 | return set_isempty(MAKE_SET(fds)); | |
267 | } | |
268 | ||
e83c7163 LP |
269 | int fdset_iterate(FDSet *s, Iterator *i) { |
270 | void *p; | |
271 | ||
8927b1da | 272 | if (!set_iterate(MAKE_SET(s), i, &p)) |
e83c7163 LP |
273 | return -ENOENT; |
274 | ||
275 | return PTR_TO_FD(p); | |
276 | } | |
a354329f LP |
277 | |
278 | int fdset_steal_first(FDSet *fds) { | |
279 | void *p; | |
280 | ||
281 | p = set_steal_first(MAKE_SET(fds)); | |
282 | if (!p) | |
283 | return -ENOENT; | |
284 | ||
285 | return PTR_TO_FD(p); | |
286 | } |