]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-fs-util.c
Merge pull request #31777 from keszybz/unit-retitling-and-comments
[thirdparty/systemd.git] / src / test / test-fs-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/file.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "copy.h"
8 #include "dirent-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "fs-util.h"
12 #include "macro.h"
13 #include "mkdir.h"
14 #include "path-util.h"
15 #include "process-util.h"
16 #include "random-util.h"
17 #include "rm-rf.h"
18 #include "stat-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "sync-util.h"
23 #include "tests.h"
24 #include "tmpfile-util.h"
25 #include "umask-util.h"
26 #include "user-util.h"
27 #include "virt.h"
28
29 static const char *arg_test_dir = NULL;
30
31 TEST(readlink_and_make_absolute) {
32 const char *tempdir, *name, *name2, *name_alias;
33 _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL;
34
35 tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute");
36 name = strjoina(tempdir, "/original");
37 name2 = "test-readlink_and_make_absolute/original";
38 name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias");
39
40 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
41 assert_se(touch(name) >= 0);
42
43 if (symlink(name, name_alias) < 0) {
44 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
45 log_tests_skipped_errno(errno, "symlink() not possible");
46 } else {
47 assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0);
48 assert_se(streq(r1, name));
49 assert_se(unlink(name_alias) >= 0);
50
51 assert_se(safe_getcwd(&pwd) >= 0);
52
53 assert_se(chdir(tempdir) >= 0);
54 assert_se(symlink(name2, name_alias) >= 0);
55 assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0);
56 assert_se(streq(r2, name));
57 assert_se(unlink(name_alias) >= 0);
58
59 assert_se(chdir(pwd) >= 0);
60 }
61
62 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
63 }
64
65 TEST(get_files_in_directory) {
66 _cleanup_strv_free_ char **l = NULL, **t = NULL;
67
68 assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0);
69 assert_se(get_files_in_directory(".", &t) >= 0);
70 assert_se(get_files_in_directory(".", NULL) >= 0);
71 }
72
73 TEST(var_tmp) {
74 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
75 const char *tmp_dir = NULL, *t;
76
77 t = getenv("TMPDIR");
78 if (t) {
79 tmpdir_backup = strdup(t);
80 assert_se(tmpdir_backup);
81 }
82
83 t = getenv("TEMP");
84 if (t) {
85 temp_backup = strdup(t);
86 assert_se(temp_backup);
87 }
88
89 t = getenv("TMP");
90 if (t) {
91 tmp_backup = strdup(t);
92 assert_se(tmp_backup);
93 }
94
95 assert_se(unsetenv("TMPDIR") >= 0);
96 assert_se(unsetenv("TEMP") >= 0);
97 assert_se(unsetenv("TMP") >= 0);
98
99 assert_se(var_tmp_dir(&tmp_dir) >= 0);
100 assert_se(streq(tmp_dir, "/var/tmp"));
101
102 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
103 assert_se(streq(getenv("TMPDIR"), "/tmp"));
104
105 assert_se(var_tmp_dir(&tmp_dir) >= 0);
106 assert_se(streq(tmp_dir, "/tmp"));
107
108 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
109 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
110
111 assert_se(var_tmp_dir(&tmp_dir) >= 0);
112 assert_se(streq(tmp_dir, "/var/tmp"));
113
114 if (tmpdir_backup) {
115 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
116 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
117 }
118
119 if (temp_backup) {
120 assert_se(setenv("TEMP", temp_backup, true) >= 0);
121 assert_se(streq(getenv("TEMP"), temp_backup));
122 }
123
124 if (tmp_backup) {
125 assert_se(setenv("TMP", tmp_backup, true) >= 0);
126 assert_se(streq(getenv("TMP"), tmp_backup));
127 }
128 }
129
130 TEST(dot_or_dot_dot) {
131 assert_se(!dot_or_dot_dot(NULL));
132 assert_se(!dot_or_dot_dot(""));
133 assert_se(!dot_or_dot_dot("xxx"));
134 assert_se(dot_or_dot_dot("."));
135 assert_se(dot_or_dot_dot(".."));
136 assert_se(!dot_or_dot_dot(".foo"));
137 assert_se(!dot_or_dot_dot("..foo"));
138 }
139
140 TEST(access_fd) {
141 _cleanup_(rmdir_and_freep) char *p = NULL;
142 _cleanup_close_ int fd = -EBADF;
143 const char *a;
144
145 a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX");
146 assert_se(mkdtemp_malloc(a, &p) >= 0);
147
148 fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
149 assert_se(fd >= 0);
150
151 assert_se(access_fd(fd, R_OK) >= 0);
152 assert_se(access_fd(fd, F_OK) >= 0);
153 assert_se(access_fd(fd, W_OK) >= 0);
154
155 assert_se(fchmod(fd, 0000) >= 0);
156
157 assert_se(access_fd(fd, F_OK) >= 0);
158
159 if (geteuid() == 0) {
160 assert_se(access_fd(fd, R_OK) >= 0);
161 assert_se(access_fd(fd, W_OK) >= 0);
162 } else {
163 assert_se(access_fd(fd, R_OK) == -EACCES);
164 assert_se(access_fd(fd, W_OK) == -EACCES);
165 }
166 }
167
168 TEST(touch_file) {
169 uid_t test_uid, test_gid;
170 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
171 struct stat st;
172 const char *a;
173 usec_t test_mtime;
174 int r;
175
176 test_uid = geteuid() == 0 ? 65534 : getuid();
177 test_gid = geteuid() == 0 ? 65534 : getgid();
178
179 test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK);
180
181 a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX");
182 assert_se(mkdtemp_malloc(a, &p) >= 0);
183
184 a = strjoina(p, "/regular");
185 r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640);
186 if (r < 0) {
187 assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM));
188 log_tests_skipped_errno(errno, "touch_file() not possible");
189 return;
190 }
191
192 assert_se(lstat(a, &st) >= 0);
193 assert_se(st.st_uid == test_uid);
194 assert_se(st.st_gid == test_gid);
195 assert_se(S_ISREG(st.st_mode));
196 assert_se((st.st_mode & 0777) == 0640);
197 assert_se(timespec_load(&st.st_mtim) == test_mtime);
198
199 a = strjoina(p, "/dir");
200 assert_se(mkdir(a, 0775) >= 0);
201 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
202 assert_se(lstat(a, &st) >= 0);
203 assert_se(st.st_uid == test_uid);
204 assert_se(st.st_gid == test_gid);
205 assert_se(S_ISDIR(st.st_mode));
206 assert_se((st.st_mode & 0777) == 0640);
207 assert_se(timespec_load(&st.st_mtim) == test_mtime);
208
209 a = strjoina(p, "/fifo");
210 assert_se(mkfifo(a, 0775) >= 0);
211 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
212 assert_se(lstat(a, &st) >= 0);
213 assert_se(st.st_uid == test_uid);
214 assert_se(st.st_gid == test_gid);
215 assert_se(S_ISFIFO(st.st_mode));
216 assert_se((st.st_mode & 0777) == 0640);
217 assert_se(timespec_load(&st.st_mtim) == test_mtime);
218
219 a = strjoina(p, "/sock");
220 assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0);
221 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
222 assert_se(lstat(a, &st) >= 0);
223 assert_se(st.st_uid == test_uid);
224 assert_se(st.st_gid == test_gid);
225 assert_se(S_ISSOCK(st.st_mode));
226 assert_se((st.st_mode & 0777) == 0640);
227 assert_se(timespec_load(&st.st_mtim) == test_mtime);
228
229 if (geteuid() == 0) {
230 a = strjoina(p, "/bdev");
231 r = mknod(a, 0775 | S_IFBLK, makedev(0, 0));
232 if (r < 0 && errno == EPERM && detect_container() > 0) {
233 log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__);
234 return;
235 }
236 assert_se(r >= 0);
237 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
238 assert_se(lstat(a, &st) >= 0);
239 assert_se(st.st_uid == test_uid);
240 assert_se(st.st_gid == test_gid);
241 assert_se(S_ISBLK(st.st_mode));
242 assert_se((st.st_mode & 0777) == 0640);
243 assert_se(timespec_load(&st.st_mtim) == test_mtime);
244
245 a = strjoina(p, "/cdev");
246 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
247 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
248 assert_se(lstat(a, &st) >= 0);
249 assert_se(st.st_uid == test_uid);
250 assert_se(st.st_gid == test_gid);
251 assert_se(S_ISCHR(st.st_mode));
252 assert_se((st.st_mode & 0777) == 0640);
253 assert_se(timespec_load(&st.st_mtim) == test_mtime);
254 }
255
256 a = strjoina(p, "/lnk");
257 assert_se(symlink("target", a) >= 0);
258 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
259 assert_se(lstat(a, &st) >= 0);
260 assert_se(st.st_uid == test_uid);
261 assert_se(st.st_gid == test_gid);
262 assert_se(S_ISLNK(st.st_mode));
263 assert_se(timespec_load(&st.st_mtim) == test_mtime);
264 }
265
266 TEST(unlinkat_deallocate) {
267 _cleanup_free_ char *p = NULL;
268 _cleanup_close_ int fd = -EBADF;
269 struct stat st;
270
271 assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0);
272
273 fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600);
274 assert_se(fd >= 0);
275
276 assert_se(write(fd, "hallo\n", 6) == 6);
277
278 assert_se(fstat(fd, &st) >= 0);
279 assert_se(st.st_size == 6);
280 assert_se(st.st_blocks > 0);
281 assert_se(st.st_nlink == 1);
282
283 assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
284
285 assert_se(fstat(fd, &st) >= 0);
286 assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6
287 (it worked) or 0 (we had to resort to truncation) */
288 assert_se(st.st_blocks == 0);
289 assert_se(st.st_nlink == 0);
290 }
291
292 TEST(fsync_directory_of_file) {
293 _cleanup_close_ int fd = -EBADF;
294
295 fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR);
296 assert_se(fd >= 0);
297
298 assert_se(fsync_directory_of_file(fd) >= 0);
299 }
300
301 TEST(rename_noreplace) {
302 static const char* const table[] = {
303 "/reg",
304 "/dir",
305 "/fifo",
306 "/socket",
307 "/symlink",
308 NULL
309 };
310
311 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
312 const char *j = NULL;
313
314 if (arg_test_dir)
315 j = strjoina(arg_test_dir, "/testXXXXXX");
316 assert_se(mkdtemp_malloc(j, &z) >= 0);
317
318 j = strjoina(z, table[0]);
319 assert_se(touch(j) >= 0);
320
321 j = strjoina(z, table[1]);
322 assert_se(mkdir(j, 0777) >= 0);
323
324 j = strjoina(z, table[2]);
325 (void) mkfifo(j, 0777);
326
327 j = strjoina(z, table[3]);
328 (void) mknod(j, S_IFSOCK | 0777, 0);
329
330 j = strjoina(z, table[4]);
331 (void) symlink("foobar", j);
332
333 STRV_FOREACH(a, table) {
334 _cleanup_free_ char *x = NULL, *y = NULL;
335
336 x = strjoin(z, *a);
337 assert_se(x);
338
339 if (access(x, F_OK) < 0) {
340 assert_se(errno == ENOENT);
341 continue;
342 }
343
344 STRV_FOREACH(b, table) {
345 _cleanup_free_ char *w = NULL;
346
347 w = strjoin(z, *b);
348 assert_se(w);
349
350 if (access(w, F_OK) < 0) {
351 assert_se(errno == ENOENT);
352 continue;
353 }
354
355 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST);
356 }
357
358 y = strjoin(z, "/somethingelse");
359 assert_se(y);
360
361 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0);
362 assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0);
363 }
364 }
365
366 TEST(chmod_and_chown) {
367 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
368 struct stat st;
369 const char *p;
370
371 if (geteuid() != 0)
372 return;
373
374 BLOCK_WITH_UMASK(0000);
375
376 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
377
378 p = strjoina(d, "/reg");
379 assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
380
381 assert_se(chmod_and_chown(p, S_IFREG | 0321, 1, 2) >= 0);
382 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
383
384 assert_se(lstat(p, &st) >= 0);
385 assert_se(S_ISREG(st.st_mode));
386 assert_se((st.st_mode & 07777) == 0321);
387
388 p = strjoina(d, "/dir");
389 assert_se(mkdir(p, 0123) >= 0);
390
391 assert_se(chmod_and_chown(p, S_IFDIR | 0321, 1, 2) >= 0);
392 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
393
394 assert_se(lstat(p, &st) >= 0);
395 assert_se(S_ISDIR(st.st_mode));
396 assert_se((st.st_mode & 07777) == 0321);
397
398 p = strjoina(d, "/lnk");
399 assert_se(symlink("idontexist", p) >= 0);
400
401 assert_se(chmod_and_chown(p, S_IFLNK | 0321, 1, 2) >= 0);
402 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
403 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
404
405 assert_se(lstat(p, &st) >= 0);
406 assert_se(S_ISLNK(st.st_mode));
407 }
408
409 static void create_binary_file(const char *p, const void *data, size_t l) {
410 _cleanup_close_ int fd = -EBADF;
411
412 fd = open(p, O_CREAT|O_WRONLY|O_EXCL|O_CLOEXEC, 0600);
413 assert_se(fd >= 0);
414 assert_se(write(fd, data, l) == (ssize_t) l);
415 }
416
417 TEST(conservative_rename) {
418 _cleanup_(unlink_and_freep) char *p = NULL;
419 _cleanup_free_ char *q = NULL;
420 size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */
421 uint8_t buffer[l+1];
422
423 random_bytes(buffer, l);
424
425 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
426 create_binary_file(p, buffer, l);
427
428 assert_se(tempfn_random_child(NULL, NULL, &q) >= 0);
429
430 /* Check that the hardlinked "copy" is detected */
431 assert_se(link(p, q) >= 0);
432 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
433 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
434
435 /* Check that a manual copy is detected */
436 assert_se(copy_file(p, q, 0, MODE_INVALID, COPY_REFLINK) >= 0);
437 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
438 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
439
440 /* Check that a manual new writeout is also detected */
441 create_binary_file(q, buffer, l);
442 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
443 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
444
445 /* Check that a minimally changed version is detected */
446 buffer[47] = ~buffer[47];
447 create_binary_file(q, buffer, l);
448 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
449 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
450
451 /* Check that this really is new updated version */
452 create_binary_file(q, buffer, l);
453 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
454 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
455
456 /* Make sure we detect extended files */
457 buffer[l++] = 47;
458 create_binary_file(q, buffer, l);
459 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
460 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
461
462 /* Make sure we detect truncated files */
463 l--;
464 create_binary_file(q, buffer, l);
465 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
466 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
467 }
468
469 static void test_rmdir_parents_one(
470 const char *prefix,
471 const char *path,
472 const char *stop,
473 int expected,
474 const char *test_exist,
475 const char *test_nonexist_subdir) {
476
477 const char *p, *s;
478
479 log_debug("/* %s(%s, %s) */", __func__, path, stop);
480
481 p = strjoina(prefix, path);
482 s = strjoina(prefix, stop);
483
484 if (expected >= 0)
485 assert_se(mkdir_parents(p, 0700) >= 0);
486
487 assert_se(rmdir_parents(p, s) == expected);
488
489 if (expected >= 0) {
490 const char *e, *f;
491
492 e = strjoina(prefix, test_exist);
493 f = strjoina(e, test_nonexist_subdir);
494
495 assert_se(access(e, F_OK) >= 0);
496 assert_se(access(f, F_OK) < 0);
497 }
498 }
499
500 TEST(rmdir_parents) {
501 char *temp;
502
503 temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX");
504 assert_se(mkdtemp(temp));
505
506 test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL);
507 test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL);
508
509 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee");
510 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd");
511 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc");
512 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb");
513 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa");
514
515 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb");
516 test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb");
517
518 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
519 }
520
521 static void test_parse_cifs_service_one(const char *f, const char *h, const char *s, const char *d, int ret) {
522 _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL;
523
524 assert_se(parse_cifs_service(f, &a, &b, &c) == ret);
525 assert_se(streq_ptr(a, h));
526 assert_se(streq_ptr(b, s));
527 assert_se(streq_ptr(c, d));
528 }
529
530 TEST(parse_cifs_service) {
531 test_parse_cifs_service_one("//foo/bar/baz", "foo", "bar", "baz", 0);
532 test_parse_cifs_service_one("\\\\foo\\bar\\baz", "foo", "bar", "baz", 0);
533 test_parse_cifs_service_one("//foo/bar", "foo", "bar", NULL, 0);
534 test_parse_cifs_service_one("\\\\foo\\bar", "foo", "bar", NULL, 0);
535 test_parse_cifs_service_one("//foo/bar/baz/uuu", "foo", "bar", "baz/uuu", 0);
536 test_parse_cifs_service_one("\\\\foo\\bar\\baz\\uuu", "foo", "bar", "baz/uuu", 0);
537
538 test_parse_cifs_service_one(NULL, NULL, NULL, NULL, -EINVAL);
539 test_parse_cifs_service_one("", NULL, NULL, NULL, -EINVAL);
540 test_parse_cifs_service_one("abc", NULL, NULL, NULL, -EINVAL);
541 test_parse_cifs_service_one("abc/cde/efg", NULL, NULL, NULL, -EINVAL);
542 test_parse_cifs_service_one("//foo/bar/baz/..", NULL, NULL, NULL, -EINVAL);
543 test_parse_cifs_service_one("//foo///", NULL, NULL, NULL, -EINVAL);
544 test_parse_cifs_service_one("//foo/.", NULL, NULL, NULL, -EINVAL);
545 test_parse_cifs_service_one("//foo/a/.", NULL, NULL, NULL, -EINVAL);
546 test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL);
547 }
548
549 TEST(open_mkdir_at) {
550 _cleanup_close_ int fd = -EBADF, subdir_fd = -EBADF, subsubdir_fd = -EBADF;
551 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
552 struct stat sta, stb;
553
554 assert_se(open_mkdir_at(AT_FDCWD, "/", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
555 assert_se(open_mkdir_at(AT_FDCWD, ".", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
556
557 fd = open_mkdir_at(AT_FDCWD, "/", O_CLOEXEC, 0);
558 assert_se(fd >= 0);
559 assert_se(stat("/", &sta) >= 0);
560 assert_se(fstat(fd, &stb) >= 0);
561 assert_se(stat_inode_same(&sta, &stb));
562 fd = safe_close(fd);
563
564 fd = open_mkdir_at(AT_FDCWD, ".", O_CLOEXEC, 0);
565 assert_se(stat(".", &sta) >= 0);
566 assert_se(fstat(fd, &stb) >= 0);
567 assert_se(stat_inode_same(&sta, &stb));
568 fd = safe_close(fd);
569
570 assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
571
572 fd = open_mkdir_at(AT_FDCWD, "/proc", O_CLOEXEC, 0);
573 assert_se(fd >= 0);
574 fd = safe_close(fd);
575
576 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
577 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_CLOEXEC, 0) == -EEXIST);
578
579 assert_se(mkdtemp_malloc(NULL, &t) >= 0);
580
581 assert_se(open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0) == -EEXIST);
582 assert_se(open_mkdir_at(AT_FDCWD, t, O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
583
584 fd = open_mkdir_at(AT_FDCWD, t, O_CLOEXEC, 0000);
585 assert_se(fd >= 0);
586 fd = safe_close(fd);
587
588 fd = open_mkdir_at(AT_FDCWD, t, O_PATH|O_CLOEXEC, 0000);
589 assert_se(fd >= 0);
590
591 subdir_fd = open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0700);
592 assert_se(subdir_fd >= 0);
593
594 assert_se(open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
595
596 subsubdir_fd = open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0700);
597 assert_se(subsubdir_fd >= 0);
598 subsubdir_fd = safe_close(subsubdir_fd);
599
600 assert_se(open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
601
602 assert_se(open_mkdir_at(fd, "xxx/yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
603
604 subsubdir_fd = open_mkdir_at(fd, "xxx/yyy", O_CLOEXEC, 0700);
605 assert_se(subsubdir_fd >= 0);
606 }
607
608 TEST(openat_report_new) {
609 _cleanup_free_ char *j = NULL;
610 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
611 _cleanup_close_ int fd = -EBADF;
612 bool b;
613
614 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
615
616 j = path_join(d, "test");
617 assert_se(j);
618
619 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
620 assert_se(fd >= 0);
621 fd = safe_close(fd);
622 assert_se(b);
623
624 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
625 assert_se(fd >= 0);
626 fd = safe_close(fd);
627 assert_se(!b);
628
629 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
630 assert_se(fd >= 0);
631 fd = safe_close(fd);
632 assert_se(!b);
633
634 assert_se(unlink(j) >= 0);
635
636 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
637 assert_se(fd >= 0);
638 fd = safe_close(fd);
639 assert_se(b);
640
641 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
642 assert_se(fd >= 0);
643 fd = safe_close(fd);
644 assert_se(!b);
645
646 assert_se(unlink(j) >= 0);
647
648 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, NULL);
649 assert_se(fd >= 0);
650 fd = safe_close(fd);
651
652 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
653 assert_se(fd >= 0);
654 fd = safe_close(fd);
655 assert_se(!b);
656
657 fd = openat_report_new(AT_FDCWD, j, O_RDWR, 0666, &b);
658 assert_se(fd >= 0);
659 fd = safe_close(fd);
660 assert_se(!b);
661
662 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT|O_EXCL, 0666, &b);
663 assert_se(fd == -EEXIST);
664
665 assert_se(unlink(j) >= 0);
666
667 fd = openat_report_new(AT_FDCWD, j, O_RDWR, 0666, &b);
668 assert_se(fd == -ENOENT);
669
670 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT|O_EXCL, 0666, &b);
671 assert_se(fd >= 0);
672 fd = safe_close(fd);
673 assert_se(b);
674 }
675
676 TEST(xopenat_full) {
677 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
678 _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF;
679
680 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
681
682 /* Test that xopenat_full() creates directories if O_DIRECTORY is specified. */
683
684 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755)) >= 0);
685 assert_se((fd_verify_directory(fd) >= 0));
686 fd = safe_close(fd);
687
688 assert_se(xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755) == -EEXIST);
689
690 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_CLOEXEC, 0, 0755)) >= 0);
691 assert_se((fd_verify_directory(fd) >= 0));
692 fd = safe_close(fd);
693
694 /* Test that xopenat_full() creates regular files if O_DIRECTORY is not specified. */
695
696 assert_se((fd = xopenat_full(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0, 0644)) >= 0);
697 assert_se(fd_verify_regular(fd) >= 0);
698 fd = safe_close(fd);
699
700 /* Test that we can reopen an existing fd with xopenat_full() by specifying an empty path. */
701
702 assert_se((fd = xopenat_full(tfd, "def", O_PATH|O_CLOEXEC, 0, 0)) >= 0);
703 assert_se((fd2 = xopenat_full(fd, "", O_RDWR|O_CLOEXEC, 0, 0644)) >= 0);
704 }
705
706 TEST(xopenat_lock_full) {
707 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
708 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
709 siginfo_t si;
710
711 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
712
713 /* Test that we can acquire an exclusive lock on a directory in one process, remove the directory,
714 * and close the file descriptor and still properly create the directory and acquire the lock in
715 * another process. */
716
717 fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX);
718 assert_se(fd >= 0);
719 assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0);
720 assert_se(fd_verify_directory(fd) >= 0);
721 assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
722
723 pid_t pid = fork();
724 assert_se(pid >= 0);
725
726 if (pid == 0) {
727 safe_close(fd);
728
729 fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX);
730 assert_se(fd >= 0);
731 assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0);
732 assert_se(fd_verify_directory(fd) >= 0);
733 assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
734
735 _exit(EXIT_SUCCESS);
736 }
737
738 /* We need to give the child process some time to get past the xopenat() call in xopenat_lock_full()
739 * and block in the call to lock_generic() waiting for the lock to become free. We can't modify
740 * xopenat_lock_full() to signal an eventfd to let us know when that has happened, so we just sleep
741 * for a little and assume that's enough time for the child process to get along far enough. It
742 * doesn't matter if it doesn't get far enough, in that case we just won't trigger the fallback logic
743 * in xopenat_lock_full(), but the test will still succeed. */
744 assert_se(usleep_safe(20 * USEC_PER_MSEC) >= 0);
745
746 assert_se(unlinkat(tfd, "abc", AT_REMOVEDIR) >= 0);
747 fd = safe_close(fd);
748
749 assert_se(wait_for_terminate(pid, &si) >= 0);
750 assert_se(si.si_code == CLD_EXITED);
751
752 assert_se(xopenat_lock_full(tfd, "abc", 0, 0, 0755, LOCK_POSIX, LOCK_EX) == -EBADF);
753 assert_se(xopenat_lock_full(tfd, "def", O_DIRECTORY, 0, 0755, LOCK_POSIX, LOCK_EX) == -EBADF);
754 }
755
756 TEST(linkat_replace) {
757 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
758 _cleanup_close_ int tfd = -EBADF;
759
760 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
761
762 _cleanup_close_ int fd1 = openat(tfd, "foo", O_CREAT|O_RDWR|O_CLOEXEC, 0600);
763 assert_se(fd1 >= 0);
764
765 assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0);
766 assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0);
767
768 _cleanup_close_ int fd1_check = openat(tfd, "bar", O_RDWR|O_CLOEXEC);
769 assert_se(fd1_check >= 0);
770
771 assert_se(inode_same_at(fd1, NULL, fd1_check, NULL, AT_EMPTY_PATH) > 0);
772
773 _cleanup_close_ int fd2 = openat(tfd, "baz", O_CREAT|O_RDWR|O_CLOEXEC, 0600);
774 assert_se(fd2 >= 0);
775
776 assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH) == 0);
777
778 assert_se(linkat_replace(tfd, "foo", tfd, "baz") >= 0);
779
780 _cleanup_close_ int fd2_check = openat(tfd, "baz", O_RDWR|O_CLOEXEC);
781
782 assert_se(inode_same_at(fd2, NULL, fd2_check, NULL, AT_EMPTY_PATH) == 0);
783 assert_se(inode_same_at(fd1, NULL, fd2_check, NULL, AT_EMPTY_PATH) > 0);
784 }
785
786 static int intro(void) {
787 arg_test_dir = saved_argv[1];
788 return EXIT_SUCCESS;
789 }
790
791 TEST(readlinkat_malloc) {
792 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
793 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
794 _cleanup_free_ char *p = NULL, *q = NULL;
795 const char *expect = "hgoehogefoobar";
796
797 tfd = mkdtemp_open(NULL, O_PATH, &t);
798 assert_se(tfd >= 0);
799
800 assert_se(symlinkat(expect, tfd, "linkname") >= 0);
801
802 assert_se(readlinkat_malloc(tfd, "linkname", &p) >= 0);
803 assert_se(streq(p, expect));
804 p = mfree(p);
805
806 fd = openat(tfd, "linkname", O_PATH | O_NOFOLLOW | O_CLOEXEC);
807 assert_se(fd >= 0);
808 assert_se(readlinkat_malloc(fd, NULL, &p) >= 0);
809 assert_se(streq(p, expect));
810 p = mfree(p);
811 assert_se(readlinkat_malloc(fd, "", &p) >= 0);
812 assert_se(streq(p, expect));
813 p = mfree(p);
814 fd = safe_close(fd);
815
816 assert_se(q = path_join(t, "linkname"));
817 assert_se(readlinkat_malloc(AT_FDCWD, q, &p) >= 0);
818 assert_se(streq(p, expect));
819 p = mfree(p);
820 assert_se(readlinkat_malloc(INT_MAX, q, &p) >= 0);
821 assert_se(streq(p, expect));
822 p = mfree(p);
823 q = mfree(q);
824 }
825
826 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);