]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-tmpfiles.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
11 #include "format-util.h"
14 #include "process-util.h"
15 #include "string-util.h"
17 #include "tmpfile-util.h"
20 int main(int argc
, char** argv
) {
21 _cleanup_free_
char *cmd
= NULL
, *cmd2
= NULL
, *ans
= NULL
, *ans2
= NULL
, *d
= NULL
, *tmp
= NULL
, *line
= NULL
;
22 _cleanup_close_
int fd
= -1, fd2
= -1;
23 const char *p
= argv
[1] ?: "/tmp";
26 test_setup_logging(LOG_DEBUG
);
28 pattern
= strjoina(p
, "/systemd-test-XXXXXX");
30 fd
= open_tmpfile_unlinkable(p
, O_RDWR
|O_CLOEXEC
);
33 assert_se(asprintf(&cmd
, "ls -l /proc/"PID_FMT
"/fd/%d", getpid_cached(), fd
) > 0);
35 assert_se(readlink_malloc(cmd
+ 6, &ans
) >= 0);
36 log_debug("link1: %s", ans
);
37 assert_se(endswith(ans
, " (deleted)"));
39 fd2
= mkostemp_safe(pattern
);
41 assert_se(unlink(pattern
) == 0);
43 assert_se(asprintf(&cmd2
, "ls -l /proc/"PID_FMT
"/fd/%d", getpid_cached(), fd2
) > 0);
45 assert_se(readlink_malloc(cmd2
+ 6, &ans2
) >= 0);
46 log_debug("link2: %s", ans2
);
47 assert_se(endswith(ans2
, " (deleted)"));
49 pattern
= strjoina(p
, "/tmpfiles-test");
50 assert_se(tempfn_random(pattern
, NULL
, &d
) >= 0);
52 fd
= open_tmpfile_linkable(d
, O_RDWR
|O_CLOEXEC
, &tmp
);
54 assert_se(write(fd
, "foobar\n", 7) == 7);
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);
61 assert_se(read_one_line_file(d
, &line
) >= 0);
62 assert_se(streq(line
, "foobar"));
63 assert_se(unlink(d
) >= 0);