]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fd-util.c
util-lib: split out all temporary file related calls into tmpfiles-util.c
[thirdparty/systemd.git] / src / test / test-fd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "macro.h"
10 #include "path-util.h"
11 #include "process-util.h"
12 #include "random-util.h"
13 #include "string-util.h"
14 #include "tests.h"
15 #include "tmpfile-util.h"
16 #include "util.h"
17
18 static void test_close_many(void) {
19 int fds[3];
20 char name0[] = "/tmp/test-close-many.XXXXXX";
21 char name1[] = "/tmp/test-close-many.XXXXXX";
22 char name2[] = "/tmp/test-close-many.XXXXXX";
23
24 fds[0] = mkostemp_safe(name0);
25 fds[1] = mkostemp_safe(name1);
26 fds[2] = mkostemp_safe(name2);
27
28 close_many(fds, 2);
29
30 assert_se(fcntl(fds[0], F_GETFD) == -1);
31 assert_se(fcntl(fds[1], F_GETFD) == -1);
32 assert_se(fcntl(fds[2], F_GETFD) >= 0);
33
34 safe_close(fds[2]);
35
36 unlink(name0);
37 unlink(name1);
38 unlink(name2);
39 }
40
41 static void test_close_nointr(void) {
42 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
43 int fd;
44
45 fd = mkostemp_safe(name);
46 assert_se(fd >= 0);
47 assert_se(close_nointr(fd) >= 0);
48 assert_se(close_nointr(fd) < 0);
49
50 unlink(name);
51 }
52
53 static void test_same_fd(void) {
54 _cleanup_close_pair_ int p[2] = { -1, -1 };
55 _cleanup_close_ int a = -1, b = -1, c = -1;
56
57 assert_se(pipe2(p, O_CLOEXEC) >= 0);
58 assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
59 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
60 assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0);
61
62 assert_se(same_fd(p[0], p[0]) > 0);
63 assert_se(same_fd(p[1], p[1]) > 0);
64 assert_se(same_fd(a, a) > 0);
65 assert_se(same_fd(b, b) > 0);
66
67 assert_se(same_fd(a, p[0]) > 0);
68 assert_se(same_fd(p[0], a) > 0);
69 assert_se(same_fd(c, p[0]) > 0);
70 assert_se(same_fd(p[0], c) > 0);
71 assert_se(same_fd(a, c) > 0);
72 assert_se(same_fd(c, a) > 0);
73
74 assert_se(same_fd(p[0], p[1]) == 0);
75 assert_se(same_fd(p[1], p[0]) == 0);
76 assert_se(same_fd(p[0], b) == 0);
77 assert_se(same_fd(b, p[0]) == 0);
78 assert_se(same_fd(p[1], a) == 0);
79 assert_se(same_fd(a, p[1]) == 0);
80 assert_se(same_fd(p[1], b) == 0);
81 assert_se(same_fd(b, p[1]) == 0);
82
83 assert_se(same_fd(a, b) == 0);
84 assert_se(same_fd(b, a) == 0);
85 }
86
87 static void test_open_serialization_fd(void) {
88 _cleanup_close_ int fd = -1;
89
90 fd = open_serialization_fd("test");
91 assert_se(fd >= 0);
92
93 assert_se(write(fd, "test\n", 5) == 5);
94 }
95
96 static void test_acquire_data_fd_one(unsigned flags) {
97 char wbuffer[196*1024 - 7];
98 char rbuffer[sizeof(wbuffer)];
99 int fd;
100
101 fd = acquire_data_fd("foo", 3, flags);
102 assert_se(fd >= 0);
103
104 zero(rbuffer);
105 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3);
106 assert_se(streq(rbuffer, "foo"));
107
108 fd = safe_close(fd);
109
110 fd = acquire_data_fd("", 0, flags);
111 assert_se(fd >= 0);
112
113 zero(rbuffer);
114 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0);
115 assert_se(streq(rbuffer, ""));
116
117 fd = safe_close(fd);
118
119 random_bytes(wbuffer, sizeof(wbuffer));
120
121 fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
122 assert_se(fd >= 0);
123
124 zero(rbuffer);
125 assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer));
126 assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0);
127
128 fd = safe_close(fd);
129 }
130
131 static void test_acquire_data_fd(void) {
132
133 test_acquire_data_fd_one(0);
134 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL);
135 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD);
136 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD);
137 test_acquire_data_fd_one(ACQUIRE_NO_PIPE);
138 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE);
139 test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
140 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
141 test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
142 }
143
144 static void test_fd_move_above_stdio(void) {
145 int original_stdin, new_fd;
146
147 original_stdin = fcntl(0, F_DUPFD, 3);
148 assert_se(original_stdin >= 3);
149 assert_se(close_nointr(0) != EBADF);
150
151 new_fd = open("/dev/null", O_RDONLY);
152 assert_se(new_fd == 0);
153
154 new_fd = fd_move_above_stdio(new_fd);
155 assert_se(new_fd >= 3);
156
157 assert_se(dup(original_stdin) == 0);
158 assert_se(close_nointr(original_stdin) != EBADF);
159 assert_se(close_nointr(new_fd) != EBADF);
160 }
161
162 static void test_rearrange_stdio(void) {
163 pid_t pid;
164 int r;
165
166 r = safe_fork("rearrange", FORK_WAIT|FORK_LOG, &pid);
167 assert_se(r >= 0);
168
169 if (r == 0) {
170 _cleanup_free_ char *path = NULL;
171 char buffer[10];
172
173 /* Child */
174
175 safe_close(STDERR_FILENO); /* Let's close an fd < 2, to make it more interesting */
176
177 assert_se(rearrange_stdio(-1, -1, -1) >= 0);
178
179 assert_se(fd_get_path(STDIN_FILENO, &path) >= 0);
180 assert_se(path_equal(path, "/dev/null"));
181 path = mfree(path);
182
183 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0);
184 assert_se(path_equal(path, "/dev/null"));
185 path = mfree(path);
186
187 assert_se(fd_get_path(STDOUT_FILENO, &path) >= 0);
188 assert_se(path_equal(path, "/dev/null"));
189 path = mfree(path);
190
191 safe_close(STDIN_FILENO);
192 safe_close(STDOUT_FILENO);
193 safe_close(STDERR_FILENO);
194
195 {
196 int pair[2];
197 assert_se(pipe(pair) >= 0);
198 assert_se(pair[0] == 0);
199 assert_se(pair[1] == 1);
200 assert_se(fd_move_above_stdio(0) == 3);
201 }
202 assert_se(open("/dev/full", O_WRONLY|O_CLOEXEC) == 0);
203 assert_se(acquire_data_fd("foobar", 6, 0) == 2);
204
205 assert_se(rearrange_stdio(2, 0, 1) >= 0);
206
207 assert_se(write(1, "x", 1) < 0 && errno == ENOSPC);
208 assert_se(write(2, "z", 1) == 1);
209 assert_se(read(3, buffer, sizeof(buffer)) == 1);
210 assert_se(buffer[0] == 'z');
211 assert_se(read(0, buffer, sizeof(buffer)) == 6);
212 assert_se(memcmp(buffer, "foobar", 6) == 0);
213
214 assert_se(rearrange_stdio(-1, 1, 2) >= 0);
215 assert_se(write(1, "a", 1) < 0 && errno == ENOSPC);
216 assert_se(write(2, "y", 1) == 1);
217 assert_se(read(3, buffer, sizeof(buffer)) == 1);
218 assert_se(buffer[0] == 'y');
219
220 assert_se(fd_get_path(0, &path) >= 0);
221 assert_se(path_equal(path, "/dev/null"));
222 path = mfree(path);
223
224 _exit(EXIT_SUCCESS);
225 }
226 }
227
228 static void assert_equal_fd(int fd1, int fd2) {
229
230 for (;;) {
231 uint8_t a[4096], b[4096];
232 ssize_t x, y;
233
234 x = read(fd1, a, sizeof(a));
235 assert(x >= 0);
236
237 y = read(fd2, b, sizeof(b));
238 assert(y >= 0);
239
240 assert(x == y);
241
242 if (x == 0)
243 break;
244
245 assert(memcmp(a, b, x) == 0);
246 }
247 }
248
249 static void test_fd_duplicate_data_fd(void) {
250 _cleanup_close_ int fd1 = -1, fd2 = -1;
251 _cleanup_(close_pairp) int sfd[2] = { -1, -1 };
252 _cleanup_(sigkill_waitp) pid_t pid = -1;
253 uint64_t i, j;
254 int r;
255
256 fd1 = open("/etc/fstab", O_RDONLY|O_CLOEXEC);
257 if (fd1 >= 0) {
258
259 fd2 = fd_duplicate_data_fd(fd1);
260 assert_se(fd2 >= 0);
261
262 assert_se(lseek(fd1, 0, SEEK_SET) == 0);
263 assert_equal_fd(fd1, fd2);
264 }
265
266 fd1 = safe_close(fd1);
267 fd2 = safe_close(fd2);
268
269 fd1 = acquire_data_fd("hallo", 6, 0);
270 assert_se(fd1 >= 0);
271
272 fd2 = fd_duplicate_data_fd(fd1);
273 assert_se(fd2 >= 0);
274
275 safe_close(fd1);
276 fd1 = acquire_data_fd("hallo", 6, 0);
277 assert_se(fd1 >= 0);
278
279 assert_equal_fd(fd1, fd2);
280
281 fd1 = safe_close(fd1);
282 fd2 = safe_close(fd2);
283
284 assert_se(socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sfd) >= 0);
285
286 r = safe_fork("(sd-pipe)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
287 assert_se(r >= 0);
288
289 if (r == 0) {
290 /* child */
291
292 sfd[0] = safe_close(sfd[0]);
293
294 for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++)
295 assert_se(write(sfd[1], &i, sizeof(i)) == sizeof(i));
296
297 sfd[1] = safe_close(sfd[1]);
298
299 _exit(EXIT_SUCCESS);
300 }
301
302 sfd[1] = safe_close(sfd[1]);
303
304 fd2 = fd_duplicate_data_fd(sfd[0]);
305 assert_se(fd2 >= 0);
306
307 for (i = 0; i < 1536*1024 / sizeof(uint64_t); i++) {
308 assert_se(read(fd2, &j, sizeof(j)) == sizeof(j));
309 assert_se(i == j);
310 }
311
312 assert_se(read(fd2, &j, sizeof(j)) == 0);
313 }
314
315 static void test_read_nr_open(void) {
316 log_info("nr-open: %i", read_nr_open());
317 }
318
319 int main(int argc, char *argv[]) {
320
321 test_setup_logging(LOG_DEBUG);
322
323 test_close_many();
324 test_close_nointr();
325 test_same_fd();
326 test_open_serialization_fd();
327 test_acquire_data_fd();
328 test_fd_move_above_stdio();
329 test_rearrange_stdio();
330 test_fd_duplicate_data_fd();
331 test_read_nr_open();
332
333 return 0;
334 }