]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/fdset.c
Merge pull request #13554 from keur/systemctl_status_timer
[thirdparty/systemd.git] / src / shared / fdset.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stddef.h>
6
7 #include "sd-daemon.h"
8
9 #include "alloc-util.h"
10 #include "dirent-util.h"
11 #include "fd-util.h"
12 #include "fdset.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "parse-util.h"
16 #include "path-util.h"
17 #include "set.h"
18
19 #define MAKE_SET(s) ((Set*) s)
20 #define MAKE_FDSET(s) ((FDSet*) s)
21
22 FDSet *fdset_new(void) {
23 return MAKE_FDSET(set_new(NULL));
24 }
25
26 int fdset_new_array(FDSet **ret, const int *fds, size_t n_fds) {
27 size_t i;
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
50 void fdset_close(FDSet *s) {
51 void *p;
52
53 while ((p = set_steal_first(MAKE_SET(s)))) {
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));
64 }
65 }
66
67 FDSet* fdset_free(FDSet *s) {
68 fdset_close(s);
69 set_free(MAKE_SET(s));
70 return NULL;
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
86 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
87 if (copy < 0)
88 return -errno;
89
90 r = fdset_put(s, copy);
91 if (r < 0) {
92 safe_close(copy);
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) {
114 _cleanup_closedir_ DIR *d = NULL;
115 struct dirent *de;
116 int r = 0;
117 FDSet *s;
118
119 assert(_s);
120
121 /* Creates an fdset and fills in all currently open file
122 * descriptors. */
123
124 d = opendir("/proc/self/fd");
125 if (!d)
126 return -errno;
127
128 s = fdset_new();
129 if (!s) {
130 r = -ENOMEM;
131 goto finish;
132 }
133
134 FOREACH_DIRENT(de, d, return -errno) {
135 int fd = -1;
136
137 r = safe_atoi(de->d_name, &fd);
138 if (r < 0)
139 goto finish;
140
141 if (fd < 3)
142 continue;
143
144 if (fd == dirfd(d))
145 continue;
146
147 r = fdset_put(s, fd);
148 if (r < 0)
149 goto finish;
150 }
151
152 r = 0;
153 *_s = TAKE_PTR(s);
154
155 finish:
156 /* We won't close the fds here! */
157 if (s)
158 set_free(MAKE_SET(s));
159
160 return r;
161 }
162
163 int fdset_cloexec(FDSet *fds, bool b) {
164 Iterator i;
165 void *p;
166 int r;
167
168 assert(fds);
169
170 SET_FOREACH(p, MAKE_SET(fds), i) {
171 r = fd_cloexec(PTR_TO_FD(p), b);
172 if (r < 0)
173 return r;
174 }
175
176 return 0;
177 }
178
179 int fdset_new_listen_fds(FDSet **_s, bool unset) {
180 int n, fd, r;
181 FDSet *s;
182
183 assert(_s);
184
185 /* Creates an fdset and fills in all passed file descriptors */
186
187 s = fdset_new();
188 if (!s) {
189 r = -ENOMEM;
190 goto fail;
191 }
192
193 n = sd_listen_fds(unset);
194 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
195 r = fdset_put(s, fd);
196 if (r < 0)
197 goto fail;
198 }
199
200 *_s = s;
201 return 0;
202
203 fail:
204 if (s)
205 set_free(MAKE_SET(s));
206
207 return r;
208 }
209
210 int fdset_close_others(FDSet *fds) {
211 void *e;
212 Iterator i;
213 int *a = NULL;
214 size_t j = 0, m;
215
216 m = fdset_size(fds);
217
218 if (m > 0) {
219 a = newa(int, m);
220 SET_FOREACH(e, MAKE_SET(fds), i)
221 a[j++] = PTR_TO_FD(e);
222 }
223
224 assert(j == m);
225
226 return close_all_fds(a, j);
227 }
228
229 unsigned fdset_size(FDSet *fds) {
230 return set_size(MAKE_SET(fds));
231 }
232
233 bool fdset_isempty(FDSet *fds) {
234 return set_isempty(MAKE_SET(fds));
235 }
236
237 int fdset_iterate(FDSet *s, Iterator *i) {
238 void *p;
239
240 if (!set_iterate(MAKE_SET(s), i, &p))
241 return -ENOENT;
242
243 return PTR_TO_FD(p);
244 }
245
246 int fdset_steal_first(FDSet *fds) {
247 void *p;
248
249 p = set_steal_first(MAKE_SET(fds));
250 if (!p)
251 return -ENOENT;
252
253 return PTR_TO_FD(p);
254 }