Debian Codegraph @

src/test/test-fs-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/file.h>
5 #include <sys/sysmacros.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "argv-util.h"
10 #include "capability-util.h"
11 #include "copy.h"
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "mkdir.h"
15 #include "path-util.h"
16 #include "pidref.h"
17 #include "process-util.h"
18 #include "random-util.h"
19 #include "rm-rf.h"
20 #include "socket-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "sync-util.h"
25 #include "tests.h"
26 #include "time-util.h"
27 #include "tmpfile-util.h"
28 #include "umask-util.h"
29 #include "virt.h"
30
31 static const char *arg_test_dir = NULL;
32
33 TEST(readlink_and_make_absolute) {
34 const char *tempdir, *name, *name2, *name_alias;
35 _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL;
36
37 tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute");
38 name = strjoina(tempdir, "/original");
39 name2 = "test-readlink_and_make_absolute/original";
40 name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias");
41
42 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
43 assert_se(touch(name) >= 0);
44
45 if (symlink(name, name_alias) < 0) {
46 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
47 log_tests_skipped_errno(errno, "symlink() not possible");
48 } else {
49 assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0);
50 ASSERT_STREQ(r1, name);
51 assert_se(unlink(name_alias) >= 0);
52
53 assert_se(safe_getcwd(&pwd) >= 0);
54
55 assert_se(chdir(tempdir) >= 0);
56 assert_se(symlink(name2, name_alias) >= 0);
57 assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0);
58 ASSERT_STREQ(r2, name);
59 assert_se(unlink(name_alias) >= 0);
60
61 assert_se(chdir(pwd) >= 0);
62 }
63
64 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
65 }
66
67 TEST(get_files_in_directory) {
68 _cleanup_strv_free_ char **l = NULL, **t = NULL;
69
70 assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0);
71 assert_se(get_files_in_directory(".", &t) >= 0);
72 assert_se(get_files_in_directory(".", NULL) >= 0);
73 }
74
75 TEST(var_tmp) {
76 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
77 const char *tmp_dir = NULL, *t;
78
79 t = getenv("TMPDIR");
80 if (t) {
81 tmpdir_backup = strdup(t);
82 assert_se(tmpdir_backup);
83 }
84
85 t = getenv("TEMP");
86 if (t) {
87 temp_backup = strdup(t);
88 assert_se(temp_backup);
89 }
90
91 t = getenv("TMP");
92 if (t) {
93 tmp_backup = strdup(t);
94 assert_se(tmp_backup);
95 }
96
97 assert_se(unsetenv("TMPDIR") >= 0);
98 assert_se(unsetenv("TEMP") >= 0);
99 assert_se(unsetenv("TMP") >= 0);
100
101 assert_se(var_tmp_dir(&tmp_dir) >= 0);
102 ASSERT_STREQ(tmp_dir, "/var/tmp");
103
104 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
105 ASSERT_STREQ(getenv("TMPDIR"), "/tmp");
106
107 assert_se(var_tmp_dir(&tmp_dir) >= 0);
108 ASSERT_STREQ(tmp_dir, "/tmp");
109
110 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
111 ASSERT_STREQ(getenv("TMPDIR"), "/88_does_not_exist_88");
112
113 assert_se(var_tmp_dir(&tmp_dir) >= 0);
114 ASSERT_STREQ(tmp_dir, "/var/tmp");
115
116 if (tmpdir_backup) {
117 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
118 ASSERT_STREQ(getenv("TMPDIR"), tmpdir_backup);
119 }
120
121 if (temp_backup) {
122 assert_se(setenv("TEMP", temp_backup, true) >= 0);
123 ASSERT_STREQ(getenv("TEMP"), temp_backup);
124 }
125
126 if (tmp_backup) {
127 assert_se(setenv("TMP", tmp_backup, true) >= 0);
128 ASSERT_STREQ(getenv("TMP"), tmp_backup);
129 }
130 }
131
132 TEST(dot_or_dot_dot) {
133 assert_se(!dot_or_dot_dot(NULL));
134 assert_se(!dot_or_dot_dot(""));
135 assert_se(!dot_or_dot_dot("xxx"));
136 assert_se(dot_or_dot_dot("."));
137 assert_se(dot_or_dot_dot(".."));
138 assert_se(!dot_or_dot_dot(".foo"));
139 assert_se(!dot_or_dot_dot("..foo"));
140 }
141
142 TEST(access_fd) {
143 _cleanup_(rmdir_and_freep) char *p = NULL;
144 _cleanup_close_ int fd = -EBADF;
145 const char *a;
146
147 a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX");
148 assert_se(mkdtemp_malloc(a, &p) >= 0);
149
150 fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
151 assert_se(fd >= 0);
152
153 assert_se(access_fd(fd, R_OK) >= 0);
154 assert_se(access_fd(fd, F_OK) >= 0);
155 assert_se(access_fd(fd, W_OK) >= 0);
156
157 assert_se(fchmod(fd, 0000) >= 0);
158
159 assert_se(access_fd(fd, F_OK) >= 0);
160
161 if (geteuid() == 0) {
162 assert_se(access_fd(fd, R_OK) >= 0);
163 assert_se(access_fd(fd, W_OK) >= 0);
164 } else {
165 assert_se(access_fd(fd, R_OK) == -EACCES);
166 assert_se(access_fd(fd, W_OK) == -EACCES);
167 }
168 }
169
170 TEST(touch_file) {
171 uid_t test_uid, test_gid;
172 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
173 struct stat st;
174 const char *a;
175 usec_t test_mtime;
176 int r;
177
178 test_uid = geteuid() == 0 ? 65534 : getuid();
179 test_gid = geteuid() == 0 ? 65534 : getgid();
180
181 test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK);
182
183 a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX");
184 assert_se(mkdtemp_malloc(a, &p) >= 0);
185
186 a = strjoina(p, "/regular");
187 r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640);
188 if (r < 0) {
189 assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM));
190 return (void) log_tests_skipped_errno(errno, "touch_file() not possible");
191 }
192
193 assert_se(lstat(a, &st) >= 0);
194 assert_se(st.st_uid == test_uid);
195 assert_se(st.st_gid == test_gid);
196 assert_se(S_ISREG(st.st_mode));
197 assert_se((st.st_mode & 0777) == 0640);
198 assert_se(timespec_load(&st.st_mtim) == test_mtime);
199
200 a = strjoina(p, "/dir");
201 assert_se(mkdir(a, 0775) >= 0);
202 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
203 assert_se(lstat(a, &st) >= 0);
204 assert_se(st.st_uid == test_uid);
205 assert_se(st.st_gid == test_gid);
206 assert_se(S_ISDIR(st.st_mode));
207 assert_se((st.st_mode & 0777) == 0640);
208 assert_se(timespec_load(&st.st_mtim) == test_mtime);
209
210 a = strjoina(p, "/fifo");
211 assert_se(mkfifo(a, 0775) >= 0);
212 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
213 assert_se(lstat(a, &st) >= 0);
214 assert_se(st.st_uid == test_uid);
215 assert_se(st.st_gid == test_gid);
216 assert_se(S_ISFIFO(st.st_mode));
217 assert_se((st.st_mode & 0777) == 0640);
218 assert_se(timespec_load(&st.st_mtim) == test_mtime);
219
220 a = strjoina(p, "/sock");
221 assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0);
222 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
223 assert_se(lstat(a, &st) >= 0);
224 assert_se(st.st_uid == test_uid);
225 assert_se(st.st_gid == test_gid);
226 assert_se(S_ISSOCK(st.st_mode));
227 assert_se((st.st_mode & 0777) == 0640);
228 assert_se(timespec_load(&st.st_mtim) == test_mtime);
229
230 if (geteuid() == 0) {
231 a = strjoina(p, "/bdev");
232 r = mknod(a, 0775 | S_IFBLK, makedev(0, 0));
233 if (r < 0 && errno == EPERM && detect_container() > 0) {
234 log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__);
235 return;
236 }
237 assert_se(r >= 0);
238 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
239 assert_se(lstat(a, &st) >= 0);
240 assert_se(st.st_uid == test_uid);
241 assert_se(st.st_gid == test_gid);
242 assert_se(S_ISBLK(st.st_mode));
243 assert_se((st.st_mode & 0777) == 0640);
244 assert_se(timespec_load(&st.st_mtim) == test_mtime);
245
246 a = strjoina(p, "/cdev");
247 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
248 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
249 assert_se(lstat(a, &st) >= 0);
250 assert_se(st.st_uid == test_uid);
251 assert_se(st.st_gid == test_gid);
252 assert_se(S_ISCHR(st.st_mode));
253 assert_se((st.st_mode & 0777) == 0640);
254 assert_se(timespec_load(&st.st_mtim) == test_mtime);
255 }
256
257 a = strjoina(p, "/lnk");
258 assert_se(symlink("target", a) >= 0);
259 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
260 assert_se(lstat(a, &st) >= 0);
261 assert_se(st.st_uid == test_uid);
262 assert_se(st.st_gid == test_gid);
263 assert_se(S_ISLNK(st.st_mode));
264 assert_se(timespec_load(&st.st_mtim) == test_mtime);
265 }
266
267 TEST(unlinkat_deallocate) {
268 _cleanup_free_ char *p = NULL;
269 _cleanup_close_ int fd = -EBADF;
270 struct stat st;
271
272 assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0);
273
274 fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600);
275 assert_se(fd >= 0);
276
277 assert_se(write(fd, "hallo\n", 6) == 6);
278
279 ASSERT_OK_ERRNO(fstat(fd, &st));
280 assert_se(st.st_size == 6);
281 assert_se(st.st_blocks > 0);
282 assert_se(st.st_nlink == 1);
283
284 assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
285
286 ASSERT_OK_ERRNO(fstat(fd, &st));
287 assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6
288 (it worked) or 0 (we had to resort to truncation) */
289 assert_se(st.st_blocks == 0);
290 assert_se(st.st_nlink == 0);
291 }
292
293 TEST(fsync_directory_of_file) {
294 _cleanup_close_ int fd = -EBADF;
295
296 fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR);
297 assert_se(fd >= 0);
298
299 assert_se(fsync_directory_of_file(fd) >= 0);
300 }
301
302 TEST(rename_noreplace) {
303 static const char* const table[] = {
304 "/reg",
305 "/dir",
306 "/fifo",
307 "/socket",
308 "/symlink",
309 NULL
310 };
311
312 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
313 const char *j = NULL;
314
315 if (arg_test_dir)
316 j = strjoina(arg_test_dir, "/testXXXXXX");
317 assert_se(mkdtemp_malloc(j, &z) >= 0);
318
319 j = strjoina(z, table[0]);
320 assert_se(touch(j) >= 0);
321
322 j = strjoina(z, table[1]);
323 assert_se(mkdir(j, 0777) >= 0);
324
325 j = strjoina(z, table[2]);
326 (void) mkfifo(j, 0777);
327
328 j = strjoina(z, table[3]);
329 (void) mknod(j, S_IFSOCK | 0777, 0);
330
331 j = strjoina(z, table[4]);
332 (void) symlink("foobar", j);
333
334 STRV_FOREACH(a, table) {
335 _cleanup_free_ char *x = NULL, *y = NULL;
336
337 x = strjoin(z, *a);
338 assert_se(x);
339
340 if (access(x, F_OK) < 0) {
341 assert_se(errno == ENOENT);
342 continue;
343 }
344
345 STRV_FOREACH(b, table) {
346 _cleanup_free_ char *w = NULL;
347
348 w = strjoin(z, *b);
349 assert_se(w);
350
351 if (access(w, F_OK) < 0) {
352 assert_se(errno == ENOENT);
353 continue;
354 }
355
356 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST);
357 }
358
359 y = strjoin(z, "/somethingelse");
360 assert_se(y);
361
362 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0);
363 assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0);
364 }
365 }
366
367 TEST(chmod_and_chown) {
368 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
369 struct stat st;
370 const char *p;
371
372 if (geteuid() != 0 || userns_has_single_user())
373 return (void) log_tests_skipped("not running as root or in userns with single user");
374
375 BLOCK_WITH_UMASK(0000);
376
377 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
378
379 p = strjoina(d, "/reg");
380 assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
381
382 assert_se(chmod_and_chown(p, S_IFREG | 0321, 1, 2) >= 0);
383 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
384
385 assert_se(lstat(p, &st) >= 0);
386 assert_se(S_ISREG(st.st_mode));
387 assert_se((st.st_mode & 07777) == 0321);
388
389 p = strjoina(d, "/dir");
390 assert_se(mkdir(p, 0123) >= 0);
391
392 assert_se(chmod_and_chown(p, S_IFDIR | 0321, 1, 2) >= 0);
393 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
394
395 assert_se(lstat(p, &st) >= 0);
396 assert_se(S_ISDIR(st.st_mode));
397 assert_se((st.st_mode & 07777) == 0321);
398
399 p = strjoina(d, "/lnk");
400 assert_se(symlink("idontexist", p) >= 0);
401
402 assert_se(chmod_and_chown(p, S_IFLNK | 0321, 1, 2) >= 0);
403 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
404 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
405
406 assert_se(lstat(p, &st) >= 0);
407 assert_se(S_ISLNK(st.st_mode));
408 }
409
410 static void create_binary_file(const char *p, const void *data, size_t l) {
411 _cleanup_close_ int fd = -EBADF;
412
413 fd = open(p, O_CREAT|O_WRONLY|O_EXCL|O_CLOEXEC, 0600);
414 assert_se(fd >= 0);
415 assert_se(write(fd, data, l) == (ssize_t) l);
416 }
417
418 TEST(conservative_rename) {
419 _cleanup_(unlink_and_freep) char *p = NULL;
420 _cleanup_free_ char *q = NULL;
421 size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */
422 uint8_t buffer[l+1];
423
424 random_bytes(buffer, l);
425
426 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
427 create_binary_file(p, buffer, l);
428
429 assert_se(tempfn_random_child(NULL, NULL, &q) >= 0);
430
431 /* Check that the hardlinked "copy" is detected */
432 assert_se(link(p, q) >= 0);
433 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
434 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
435
436 /* Check that a manual copy is detected */
437 assert_se(copy_file(p, q, 0, MODE_INVALID, COPY_REFLINK) >= 0);
438 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
439 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
440
441 /* Check that a manual new writeout is also detected */
442 create_binary_file(q, buffer, l);
443 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
444 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
445
446 /* Check that a minimally changed version is detected */
447 buffer[47] = ~buffer[47];
448 create_binary_file(q, buffer, l);
449 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
450 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
451
452 /* Check that this really is new updated version */
453 create_binary_file(q, buffer, l);
454 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
455 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
456
457 /* Make sure we detect extended files */
458 buffer[l++] = 47;
459 create_binary_file(q, buffer, l);
460 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
461 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
462
463 /* Make sure we detect truncated files */
464 l--;
465 create_binary_file(q, buffer, l);
466 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
467 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
468 }
469
470 static void test_rmdir_parents_one(
471 const char *prefix,
472 const char *path,
473 const char *stop,
474 int expected,
475 const char *test_exist,
476 const char *test_nonexist_subdir) {
477
478 const char *p, *s;
479
480 log_debug("/* %s(%s, %s) */", __func__, path, stop);
481
482 p = strjoina(prefix, path);
483 s = strjoina(prefix, stop);
484
485 if (expected >= 0)
486 assert_se(mkdir_parents(p, 0700) >= 0);
487
488 assert_se(rmdir_parents(p, s) == expected);
489
490 if (expected >= 0) {
491 const char *e, *f;
492
493 e = strjoina(prefix, test_exist);
494 f = strjoina(e, test_nonexist_subdir);
495
496 assert_se(access(e, F_OK) >= 0);
497 assert_se(access(f, F_OK) < 0);
498 }
499 }
500
501 TEST(rmdir_parents) {
502 char *temp;
503
504 temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX");
505 assert_se(mkdtemp(temp));
506
507 test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL);
508 test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL);
509
510 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee");
511 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd");
512 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc");
513 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb");
514 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa");
515
516 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb");
517 test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb");
518
519 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
520 }
521
522 static void test_parse_cifs_service_one(const char *f, const char *h, const char *s, const char *d, int ret) {
523 _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL;
524
525 assert_se(parse_cifs_service(f, &a, &b, &c) == ret);
526 ASSERT_STREQ(a, h);
527 ASSERT_STREQ(b, s);
528 ASSERT_STREQ(c, d);
529 }
530
531 TEST(parse_cifs_service) {
532 test_parse_cifs_service_one("//foo/bar/baz", "foo", "bar", "baz", 0);
533 test_parse_cifs_service_one("\\\\foo\\bar\\baz", "foo", "bar", "baz", 0);
534 test_parse_cifs_service_one("//foo/bar", "foo", "bar", NULL, 0);
535 test_parse_cifs_service_one("\\\\foo\\bar", "foo", "bar", NULL, 0);
536 test_parse_cifs_service_one("//foo/bar/baz/uuu", "foo", "bar", "baz/uuu", 0);
537 test_parse_cifs_service_one("\\\\foo\\bar\\baz\\uuu", "foo", "bar", "baz/uuu", 0);
538
539 test_parse_cifs_service_one(NULL, NULL, NULL, NULL, -EINVAL);
540 test_parse_cifs_service_one("", NULL, NULL, NULL, -EINVAL);
541 test_parse_cifs_service_one("abc", NULL, NULL, NULL, -EINVAL);
542 test_parse_cifs_service_one("abc/cde/efg", NULL, NULL, NULL, -EINVAL);
543 test_parse_cifs_service_one("//foo/bar/baz/..", NULL, NULL, NULL, -EINVAL);
544 test_parse_cifs_service_one("//foo///", NULL, NULL, NULL, -EINVAL);
545 test_parse_cifs_service_one("//foo/.", NULL, NULL, NULL, -EINVAL);
546 test_parse_cifs_service_one("//foo/a/.", NULL, NULL, NULL, -EINVAL);
547 test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL);
548 }
549
550 TEST(open_mkdir_at) {
551 _cleanup_close_ int fd = -EBADF, subdir_fd = -EBADF, subsubdir_fd = -EBADF;
552 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
553 struct stat sta, stb;
554
555 assert_se(open_mkdir_at(AT_FDCWD, "/", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
556 assert_se(open_mkdir_at(AT_FDCWD, ".", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
557
558 fd = open_mkdir_at(AT_FDCWD, "/", O_CLOEXEC, 0);
559 assert_se(fd >= 0);
560 assert_se(stat("/", &sta) >= 0);
561 ASSERT_OK_ERRNO(fstat(fd, &stb));
562 assert_se(stat_inode_same(&sta, &stb));
563 fd = safe_close(fd);
564
565 fd = open_mkdir_at(AT_FDCWD, ".", O_CLOEXEC, 0);
566 assert_se(stat(".", &sta) >= 0);
567 ASSERT_OK_ERRNO(fstat(fd, &stb));
568 assert_se(stat_inode_same(&sta, &stb));
569 fd = safe_close(fd);
570
571 assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
572
573 fd = open_mkdir_at(AT_FDCWD, "/proc", O_CLOEXEC, 0);
574 assert_se(fd >= 0);
575 fd = safe_close(fd);
576
577 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
578 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_CLOEXEC, 0) == -EEXIST);
579
580 assert_se(mkdtemp_malloc(NULL, &t) >= 0);
581
582 assert_se(open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0) == -EEXIST);
583 assert_se(open_mkdir_at(AT_FDCWD, t, O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
584
585 fd = open_mkdir_at(AT_FDCWD, t, O_CLOEXEC, 0000);
586 assert_se(fd >= 0);
587 fd = safe_close(fd);
588
589 fd = open_mkdir_at(AT_FDCWD, t, O_PATH|O_CLOEXEC, 0000);
590 assert_se(fd >= 0);
591
592 subdir_fd = open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0700);
593 assert_se(subdir_fd >= 0);
594
595 assert_se(open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
596
597 subsubdir_fd = open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0700);
598 assert_se(subsubdir_fd >= 0);
599 subsubdir_fd = safe_close(subsubdir_fd);
600
601 assert_se(open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
602
603 assert_se(open_mkdir_at(fd, "xxx/yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
604
605 subsubdir_fd = open_mkdir_at(fd, "xxx/yyy", O_CLOEXEC, 0700);
606 assert_se(subsubdir_fd >= 0);
607 }
608
609 TEST(openat_report_new) {
610 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
611 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
612 bool b;
613
614 ASSERT_OK(tfd = mkdtemp_open(NULL, 0, &t));
615
616 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
617 ASSERT_OK(fd);
618 fd = safe_close(fd);
619 ASSERT_TRUE(b);
620
621 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
622 ASSERT_OK(fd);
623 fd = safe_close(fd);
624 ASSERT_FALSE(b);
625
626 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
627 ASSERT_OK(fd);
628 fd = safe_close(fd);
629 ASSERT_FALSE(b);
630
631 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
632
633 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
634 ASSERT_OK(fd);
635 fd = safe_close(fd);
636 ASSERT_TRUE(b);
637
638 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
639 ASSERT_OK(fd);
640 fd = safe_close(fd);
641 ASSERT_FALSE(b);
642
643 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
644
645 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, NULL);
646 ASSERT_OK(fd);
647 fd = safe_close(fd);
648
649 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
650 ASSERT_OK(fd);
651 fd = safe_close(fd);
652 ASSERT_FALSE(b);
653
654 fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b);
655 ASSERT_OK(fd);
656 fd = safe_close(fd);
657 ASSERT_FALSE(b);
658
659 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b);
660 ASSERT_ERROR(fd, EEXIST);
661
662 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
663
664 fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b);
665 ASSERT_ERROR(fd, ENOENT);
666
667 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b);
668 ASSERT_OK(fd);
669 fd = safe_close(fd);
670 ASSERT_TRUE(b);
671
672 ASSERT_OK_ERRNO(symlinkat("target", tfd, "link"));
673 fd = openat_report_new(tfd, "link", O_RDWR|O_CREAT, 0666, &b);
674 ASSERT_ERROR(fd, EEXIST);
675
676 fd = openat_report_new(tfd, "target", O_RDWR|O_CREAT, 0666, &b);
677 ASSERT_OK(fd);
678 fd = safe_close(fd);
679 ASSERT_TRUE(b);
680
681 fd = openat_report_new(tfd, "link", O_RDWR|O_CREAT, 0666, &b);
682 ASSERT_OK(fd);
683 fd = safe_close(fd);
684 ASSERT_FALSE(b);
685 }
686
687 TEST(xopenat_full) {
688 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
689 _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF;
690
691 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
692
693 /* Test that xopenat_full() creates directories if O_DIRECTORY is specified. */
694
695 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755)) >= 0);
696 assert_se((fd_verify_directory(fd) >= 0));
697 fd = safe_close(fd);
698
699 assert_se(xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755) == -EEXIST);
700
701 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_CLOEXEC, 0, 0755)) >= 0);
702 assert_se((fd_verify_directory(fd) >= 0));
703 fd = safe_close(fd);
704
705 /* Test that xopenat_full() creates regular files if O_DIRECTORY is not specified. */
706
707 assert_se((fd = xopenat_full(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0, 0644)) >= 0);
708 assert_se(fd_verify_regular(fd) >= 0);
709 fd = safe_close(fd);
710
711 /* Test that we can reopen an existing fd with xopenat_full() by specifying an empty path. */
712
713 assert_se((fd = xopenat_full(tfd, "def", O_PATH|O_CLOEXEC, 0, 0)) >= 0);
714 assert_se((fd2 = xopenat_full(fd, "", O_RDWR|O_CLOEXEC, 0, 0644)) >= 0);
715 }
716
717 TEST(xopenat_regular) {
718
719 _cleanup_close_ int fd = -EBADF;
720
721 assert_se(xopenat_full(AT_FDCWD, "/dev/null", O_RDWR|O_CLOEXEC, XO_REGULAR, 0) == -EBADFD);
722 assert_se(xopenat_full(AT_FDCWD, "/proc", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0) == -EISDIR);
723 assert_se(xopenat_full(AT_FDCWD, "/proc/self", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0) == -EISDIR);
724 assert_se(xopenat_full(AT_FDCWD, "/proc/self", O_RDONLY|O_CLOEXEC|O_NOFOLLOW, XO_REGULAR, 0) == -ELOOP);
725
726 fd = xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0);
727 assert_se(fd >= 0);
728 fd = safe_close(fd);
729
730 assert_se(xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC|O_NOFOLLOW, XO_REGULAR, 0) == -ELOOP);
731
732 fd = xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC|O_PATH, XO_REGULAR, 0);
733 assert_se(fd >= 0);
734 fd = safe_close(fd);
735
736 fd = xopenat_full(AT_FDCWD, "/tmp/xopenat-regular-test", O_RDWR|O_CLOEXEC|O_CREAT, XO_REGULAR, 0600);
737 assert_se(fd >= 0);
738 fd = safe_close(fd);
739
740 assert_se(xopenat_full(AT_FDCWD, "/tmp/xopenat-regular-test", O_RDWR|O_CLOEXEC|O_CREAT|O_EXCL, XO_REGULAR, 0600) == -EEXIST);
741
742 assert_se(unlink("/tmp/xopenat-regular-test") >= 0);
743 }
744
745 TEST(xopenat_socket) {
746 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
747 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
748
749 ASSERT_OK(tfd = mkdtemp_open(NULL, 0, &t));
750
751 /* Create a Unix domain socket via bind(). */
752 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
753 ASSERT_OK(fd);
754
755 const char *sockpath = strjoina(t, "/test.sock");
756 union sockaddr_union sa = { .un.sun_family = AF_UNIX };
757 strncpy(sa.un.sun_path, sockpath, sizeof(sa.un.sun_path) - 1);
758 ASSERT_OK_ERRNO(bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sockpath) + 1));
759 fd = safe_close(fd);
760
761 /* XO_SOCKET requires O_PATH. */
762 fd = xopenat_full(tfd, "test.sock", O_PATH|O_CLOEXEC, XO_SOCKET, 0);
763 ASSERT_OK(fd);
764 fd = safe_close(fd);
765
766 /* Reopen via empty path should also work. */
767 fd = ASSERT_OK(xopenat_full(tfd, "test.sock", O_PATH|O_CLOEXEC, 0, 0));
768 _cleanup_close_ int fd2 = xopenat_full(fd, NULL, O_PATH|O_CLOEXEC, XO_SOCKET, 0);
769 ASSERT_OK(fd2);
770 fd = safe_close(fd);
771
772 /* Non-socket inodes must be rejected. */
773 ASSERT_OK_ERRNO(mkdirat(tfd, "dir", 0755));
774 ASSERT_ERROR(xopenat_full(tfd, "dir", O_PATH|O_CLOEXEC, XO_SOCKET, 0), EISDIR);
775
776 fd = ASSERT_OK_ERRNO(openat(tfd, "reg", O_CREAT|O_CLOEXEC, 0600));
777 fd = safe_close(fd);
778 ASSERT_ERROR(xopenat_full(tfd, "reg", O_PATH|O_CLOEXEC, XO_SOCKET, 0), ENOTSOCK);
779
780 /* Reopen via empty path of a non-socket fd must also be rejected. */
781 fd = ASSERT_OK(xopenat_full(tfd, "reg", O_PATH|O_CLOEXEC, 0, 0));
782 ASSERT_ERROR(xopenat_full(fd, NULL, O_PATH|O_CLOEXEC, XO_SOCKET, 0), ENOTSOCK);
783 fd = safe_close(fd);
784
785 fd = ASSERT_OK(xopenat_full(tfd, "dir", O_PATH|O_CLOEXEC, 0, 0));
786 ASSERT_ERROR(xopenat_full(fd, NULL, O_PATH|O_CLOEXEC, XO_SOCKET, 0), EISDIR);
787 fd = safe_close(fd);
788 }
789
790 TEST(xopenat_trigger_automount) {
791 _cleanup_close_ int fd = -EBADF;
792
793 /* We can't easily set up an autofs mount in a test, but we can verify that
794 * XO_TRIGGER_AUTOMOUNT works on a regular path and produces the same inode as a
795 * plain O_PATH open. */
796 fd = xopenat_full(AT_FDCWD, "/usr", O_PATH|O_CLOEXEC|O_DIRECTORY, XO_TRIGGER_AUTOMOUNT, 0);
797 ASSERT_OK(fd);
798
799 _cleanup_close_ int fd2 = xopenat_full(AT_FDCWD, "/usr", O_PATH|O_CLOEXEC|O_DIRECTORY, 0, 0);
800 ASSERT_OK(fd2);
801 ASSERT_OK_POSITIVE(fd_inode_same(fd, fd2));
802 }
803
804 TEST(xopenat_auto_rw_ro) {
805 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
806 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
807 int fl;
808
809 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
810
811 /* Regular writable file: XO_AUTO_RW_RO should end up in O_RDWR. */
812
813 fd = xopenat_full(tfd, "rw", O_CREAT|O_EXCL|O_CLOEXEC, XO_AUTO_RW_RO, 0644);
814 assert_se(fd >= 0);
815 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
816 assert_se((fl & O_ACCMODE) == O_RDWR);
817 fd = safe_close(fd);
818
819 /* Same thing, but with XO_REGULAR set too. */
820
821 fd = xopenat_full(tfd, "rw2", O_CREAT|O_EXCL|O_CLOEXEC, XO_AUTO_RW_RO|XO_REGULAR, 0644);
822 assert_se(fd >= 0);
823 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
824 assert_se((fl & O_ACCMODE) == O_RDWR);
825 fd = safe_close(fd);
826
827 /* Reopen via empty path on an O_PATH fd must also end up in O_RDWR. */
828
829 _cleanup_close_ int path_fd = xopenat_full(tfd, "rw", O_PATH|O_CLOEXEC, 0, 0);
830 assert_se(path_fd >= 0);
831 fd = xopenat_full(path_fd, "", O_CLOEXEC, XO_AUTO_RW_RO, 0);
832 assert_se(fd >= 0);
833 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
834 assert_se((fl & O_ACCMODE) == O_RDWR);
835 fd = safe_close(fd);
836
837 /* Directories can only be opened read-only: XO_AUTO_RW_RO with O_DIRECTORY must fall back to O_RDONLY. */
838
839 fd = xopenat_full(tfd, "subdir", O_DIRECTORY|O_CREAT|O_CLOEXEC, XO_AUTO_RW_RO, 0755);
840 assert_se(fd >= 0);
841 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
842 assert_se((fl & O_ACCMODE) == O_RDONLY);
843 fd = safe_close(fd);
844
845 /* Same for opening an existing directory. */
846
847 fd = xopenat_full(tfd, "subdir", O_DIRECTORY|O_CLOEXEC, XO_AUTO_RW_RO, 0);
848 assert_se(fd >= 0);
849 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
850 assert_se((fl & O_ACCMODE) == O_RDONLY);
851 fd = safe_close(fd);
852
853 /* Fallback when the inode is not writable: create a file as read-only mode and verify that
854 * XO_AUTO_RW_RO falls back to O_RDONLY. Root bypasses mode bits via CAP_DAC_OVERRIDE, so skip
855 * this when running as root, or as a user with CAP_DAC_OVERRIDE. */
856
857 if (have_effective_cap(CAP_DAC_OVERRIDE) <= 0) {
858 fd = openat(tfd, "ro", O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0444);
859 assert_se(fd >= 0);
860 fd = safe_close(fd);
861 assert_se(fchmodat(tfd, "ro", 0444, 0) >= 0);
862
863 /* Plain case: no XO_REGULAR. */
864 fd = xopenat_full(tfd, "ro", O_CLOEXEC, XO_AUTO_RW_RO, 0);
865 assert_se(fd >= 0);
866 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
867 assert_se((fl & O_ACCMODE) == O_RDONLY);
868 fd = safe_close(fd);
869
870 /* With XO_REGULAR (exercises the pin-via-O_PATH + reopen path). */
871 fd = xopenat_full(tfd, "ro", O_CLOEXEC, XO_AUTO_RW_RO|XO_REGULAR, 0);
872 assert_se(fd >= 0);
873 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
874 assert_se((fl & O_ACCMODE) == O_RDONLY);
875 fd = safe_close(fd);
876
877 /* Also exercise the empty-path/fd-reopen branch. */
878 _cleanup_close_ int ro_path_fd = xopenat_full(tfd, "ro", O_PATH|O_CLOEXEC, 0, 0);
879 assert_se(ro_path_fd >= 0);
880 fd = xopenat_full(ro_path_fd, "", O_CLOEXEC, XO_AUTO_RW_RO, 0);
881 assert_se(fd >= 0);
882 ASSERT_OK_ERRNO(fl = fcntl(fd, F_GETFL));
883 assert_se((fl & O_ACCMODE) == O_RDONLY);
884 fd = safe_close(fd);
885 }
886 }
887
888 TEST(xopenat_lock_full) {
889 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
890 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
891 siginfo_t si;
892 int r;
893
894 ASSERT_OK((tfd = mkdtemp_open(NULL, 0, &t)));
895
896 /* Test that we can acquire an exclusive lock on a directory in one process, remove the directory,
897 * and close the file descriptor and still properly create the directory and acquire the lock in
898 * another process. */
899
900 fd = ASSERT_OK(xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX));
901 ASSERT_OK_ERRNO(faccessat(tfd, "abc", F_OK, 0));
902 ASSERT_OK(fd_verify_directory(fd));
903 ASSERT_ERROR(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB), EAGAIN);
904
905 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
906 r = ASSERT_OK(pidref_safe_fork("(lock)", FORK_DEATHSIG_SIGKILL|FORK_LOG, &pidref));
907
908 if (r == 0) {
909 safe_close(fd);
910
911 fd = ASSERT_OK(xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX));
912 ASSERT_OK_ERRNO(faccessat(tfd, "abc", F_OK, 0));
913 ASSERT_OK(fd_verify_directory(fd));
914 ASSERT_ERROR(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB), EAGAIN);
915
916 _exit(EXIT_SUCCESS);
917 }
918
919 /* We need to give the child process some time to get past the xopenat() call in xopenat_lock_full()
920 * and block in the call to lock_generic() waiting for the lock to become free. We can't modify
921 * xopenat_lock_full() to signal an eventfd to let us know when that has happened, so we just sleep
922 * for a little and assume that's enough time for the child process to get along far enough. It
923 * doesn't matter if it doesn't get far enough, in that case we just won't trigger the fallback logic
924 * in xopenat_lock_full(), but the test will still succeed. */
925 ASSERT_OK(usleep_safe(20 * USEC_PER_MSEC));
926
927 ASSERT_OK(unlinkat(tfd, "abc", AT_REMOVEDIR));
928 fd = safe_close(fd);
929
930 ASSERT_OK(pidref_wait_for_terminate(&pidref, &si));
931 ASSERT_EQ(si.si_code, CLD_EXITED);
932
933 ASSERT_ERROR(xopenat_lock_full(tfd, "abc", 0, 0, 0755, LOCK_POSIX, LOCK_EX), EBADF);
934 ASSERT_ERROR(xopenat_lock_full(tfd, "def", O_DIRECTORY, 0, 0755, LOCK_POSIX, LOCK_EX), EBADF);
935 }
936
937 TEST(linkat_replace) {
938 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
939 _cleanup_close_ int tfd = -EBADF;
940
941 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
942
943 _cleanup_close_ int fd1 = openat(tfd, "foo", O_CREAT|O_RDWR|O_CLOEXEC, 0600);
944 assert_se(fd1 >= 0);
945
946 assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0);
947 assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0);
948
949 _cleanup_close_ int fd1_check = openat(tfd, "bar", O_RDWR|O_CLOEXEC);
950 assert_se(fd1_check >= 0);
951
952 assert_se(inode_same_at(fd1, NULL, fd1_check, NULL, AT_EMPTY_PATH) > 0);
953
954 _cleanup_close_ int fd2 = openat(tfd, "baz", O_CREAT|O_RDWR|O_CLOEXEC, 0600);
955 assert_se(fd2 >= 0);
956
957 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH) == 0);
958
959 assert_se(linkat_replace(tfd, "foo", tfd, "baz") >= 0);
960
961 _cleanup_close_ int fd2_check = openat(tfd, "baz", O_RDWR|O_CLOEXEC);
962
963 assert_se(inode_same_at(fd2, NULL, fd2_check, NULL, AT_EMPTY_PATH) == 0);
964 assert_se(inode_same_at(fd1, NULL, fd2_check, NULL, AT_EMPTY_PATH) > 0);
965 }
966
967 TEST(readlinkat_malloc) {
968 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
969 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
970 _cleanup_free_ char *p = NULL, *q = NULL;
971 const char *expect = "hgoehogefoobar";
972
973 tfd = mkdtemp_open(NULL, O_PATH, &t);
974 assert_se(tfd >= 0);
975
976 assert_se(symlinkat(expect, tfd, "linkname") >= 0);
977
978 assert_se(readlinkat_malloc(tfd, "linkname", &p) >= 0);
979 ASSERT_STREQ(p, expect);
980 p = mfree(p);
981
982 fd = openat(tfd, "linkname", O_PATH | O_NOFOLLOW | O_CLOEXEC);
983 assert_se(fd >= 0);
984 assert_se(readlinkat_malloc(fd, NULL, &p) >= 0);
985 ASSERT_STREQ(p, expect);
986 p = mfree(p);
987 assert_se(readlinkat_malloc(fd, "", &p) >= 0);
988 ASSERT_STREQ(p, expect);
989 p = mfree(p);
990 fd = safe_close(fd);
991
992 assert_se(q = path_join(t, "linkname"));
993 assert_se(readlinkat_malloc(AT_FDCWD, q, &p) >= 0);
994 ASSERT_STREQ(p, expect);
995 p = mfree(p);
996 assert_se(readlinkat_malloc(INT_MAX, q, &p) >= 0);
997 ASSERT_STREQ(p, expect);
998 p = mfree(p);
999 q = mfree(q);
1000 }
1001
1002 TEST(xat_fdroot) {
1003 _cleanup_free_ char *p = NULL;
1004 ASSERT_OK(fd_get_path(XAT_FDROOT, &p));
1005 ASSERT_STREQ(p, "/");
1006
1007 _cleanup_close_ int fd = -EBADF;
1008 fd = fd_reopen(XAT_FDROOT, O_CLOEXEC);
1009 ASSERT_OK(fd);
1010
1011 ASSERT_OK_POSITIVE(path_is_root_at(XAT_FDROOT, NULL));
1012 ASSERT_OK_POSITIVE(path_is_root_at(XAT_FDROOT, "."));
1013 ASSERT_OK_POSITIVE(path_is_root_at(XAT_FDROOT, "/"));
1014
1015 ASSERT_OK_POSITIVE(path_is_root_at(fd, NULL));
1016 ASSERT_OK_POSITIVE(path_is_root_at(fd, "."));
1017 ASSERT_OK_POSITIVE(path_is_root_at(fd, "/"));
1018
1019 ASSERT_OK_POSITIVE(fds_inode_and_mount_same(fd, fd));
1020 ASSERT_OK_POSITIVE(fds_inode_and_mount_same(XAT_FDROOT, XAT_FDROOT));
1021 ASSERT_OK_POSITIVE(fds_inode_and_mount_same(fd, XAT_FDROOT));
1022 ASSERT_OK_POSITIVE(fds_inode_and_mount_same(XAT_FDROOT, fd));
1023
1024 ASSERT_OK_POSITIVE(dir_fd_is_root(XAT_FDROOT));
1025 ASSERT_OK_POSITIVE(dir_fd_is_root(fd));
1026
1027 ASSERT_OK_POSITIVE(dir_fd_is_root_or_cwd(XAT_FDROOT));
1028 ASSERT_OK_POSITIVE(dir_fd_is_root_or_cwd(AT_FDCWD));
1029 ASSERT_OK_POSITIVE(dir_fd_is_root_or_cwd(fd));
1030
1031 ASSERT_OK(access_fd(XAT_FDROOT, F_OK));
1032 ASSERT_OK(access_fd(fd, F_OK));
1033
1034 fd = safe_close(fd);
1035 fd = xopenat(XAT_FDROOT, ".", O_RDONLY);
1036 ASSERT_OK(fd);
1037
1038 fd = safe_close(fd);
1039 fd = xopenat(XAT_FDROOT, "/", O_RDONLY);
1040 ASSERT_OK(fd);
1041
1042 ASSERT_OK(fd_verify_directory(fd));
1043 ASSERT_OK(fd_verify_directory(XAT_FDROOT));
1044
1045 ASSERT_OK(fd_verify_linked(fd));
1046 ASSERT_OK(fd_verify_linked(XAT_FDROOT));
1047
1048 int a = fd_is_read_only_fs(fd);
1049 ASSERT_OK(a);
1050 int b = fd_is_read_only_fs(XAT_FDROOT);
1051 ASSERT_OK(b);
1052 ASSERT_EQ(a, b);
1053
1054 a = fd_is_temporary_fs(fd);
1055 ASSERT_OK(a);
1056 b = fd_is_temporary_fs(XAT_FDROOT);
1057 ASSERT_OK(b);
1058 ASSERT_EQ(a, b);
1059 }
1060
1061 static int intro(void) {
1062 arg_test_dir = saved_argv[1];
1063 return EXIT_SUCCESS;
1064 }
1065
1066 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);