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