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