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