]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-tmpfiles.c
Merge pull request #4131 from intelfx/update-done-timestamps-precision
[thirdparty/systemd.git] / src / test / test-tmpfiles.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "formats-util.h"
29 #include "fs-util.h"
30 #include "log.h"
31 #include "string-util.h"
32 #include "util.h"
33
34 int main(int argc, char** argv) {
35 _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
36 _cleanup_close_ int fd = -1, fd2 = -1;
37 const char *p = argv[1] ?: "/tmp";
38 char *pattern;
39
40 log_set_max_level(LOG_DEBUG);
41 log_parse_environment();
42
43 pattern = strjoina(p, "/systemd-test-XXXXXX");
44
45 fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
46 assert_se(fd >= 0);
47
48 assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd) > 0);
49 (void) system(cmd);
50 assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
51 log_debug("link1: %s", ans);
52 assert_se(endswith(ans, " (deleted)"));
53
54 fd2 = mkostemp_safe(pattern);
55 assert_se(fd >= 0);
56 assert_se(unlink(pattern) == 0);
57
58 assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid(), fd2) > 0);
59 (void) system(cmd2);
60 assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
61 log_debug("link2: %s", ans2);
62 assert_se(endswith(ans2, " (deleted)"));
63
64 pattern = strjoina(p, "/tmpfiles-test");
65 assert_se(tempfn_random(pattern, NULL, &d) >= 0);
66
67 fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
68 assert_se(fd >= 0);
69 assert_se(write(fd, "foobar\n", 7) == 7);
70
71 assert_se(touch(d) >= 0);
72 assert_se(link_tmpfile(fd, tmp, d) == -EEXIST);
73 assert_se(unlink(d) >= 0);
74 assert_se(link_tmpfile(fd, tmp, d) >= 0);
75
76 assert_se(read_one_line_file(d, &line) >= 0);
77 assert_se(streq(line, "foobar"));
78 assert_se(unlink(d) >= 0);
79
80 return 0;
81 }