]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-tmpfiles.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-tmpfiles.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "format-util.h"
12 #include "fs-util.h"
13 #include "log.h"
14 #include "process-util.h"
15 #include "string-util.h"
16 #include "tests.h"
17 #include "util.h"
18
19 int main(int argc, char** argv) {
20 _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
21 _cleanup_close_ int fd = -1, fd2 = -1;
22 const char *p = argv[1] ?: "/tmp";
23 char *pattern;
24
25 test_setup_logging(LOG_DEBUG);
26
27 pattern = strjoina(p, "/systemd-test-XXXXXX");
28
29 fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
30 assert_se(fd >= 0);
31
32 assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
33 (void) system(cmd);
34 assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
35 log_debug("link1: %s", ans);
36 assert_se(endswith(ans, " (deleted)"));
37
38 fd2 = mkostemp_safe(pattern);
39 assert_se(fd >= 0);
40 assert_se(unlink(pattern) == 0);
41
42 assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
43 (void) system(cmd2);
44 assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
45 log_debug("link2: %s", ans2);
46 assert_se(endswith(ans2, " (deleted)"));
47
48 pattern = strjoina(p, "/tmpfiles-test");
49 assert_se(tempfn_random(pattern, NULL, &d) >= 0);
50
51 fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
52 assert_se(fd >= 0);
53 assert_se(write(fd, "foobar\n", 7) == 7);
54
55 assert_se(touch(d) >= 0);
56 assert_se(link_tmpfile(fd, tmp, d) == -EEXIST);
57 assert_se(unlink(d) >= 0);
58 assert_se(link_tmpfile(fd, tmp, d) >= 0);
59
60 assert_se(read_one_line_file(d, &line) >= 0);
61 assert_se(streq(line, "foobar"));
62 assert_se(unlink(d) >= 0);
63
64 return 0;
65 }