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