]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-tmpfiles.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-tmpfiles.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
65b3903f 2/***
96b2fb93 3 Copyright © 2014 Zbigniew Jędrzejewski-Szmek
65b3903f
ZJS
4***/
5
65b3903f 6#include <fcntl.h>
65b3903f 7#include <stdio.h>
07630cea
LP
8#include <stdlib.h>
9#include <unistd.h>
65b3903f 10
b5efdb8a 11#include "alloc-util.h"
3ffd4af2 12#include "fd-util.h"
0d39fa9c 13#include "fileio.h"
f97b34a6 14#include "format-util.h"
dbb471ac
ZJS
15#include "fs-util.h"
16#include "log.h"
dccca82b 17#include "process-util.h"
07630cea
LP
18#include "string-util.h"
19#include "util.h"
65b3903f
ZJS
20
21int main(int argc, char** argv) {
03532f0a 22 _cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
0f5e1382 23 _cleanup_close_ int fd = -1, fd2 = -1;
65b3903f 24 const char *p = argv[1] ?: "/tmp";
03532f0a 25 char *pattern;
dbb471ac
ZJS
26
27 log_set_max_level(LOG_DEBUG);
28 log_parse_environment();
65b3903f 29
03532f0a
LP
30 pattern = strjoina(p, "/systemd-test-XXXXXX");
31
32 fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
bdf7026e 33 assert_se(fd >= 0);
65b3903f 34
df0ff127 35 assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
dbb471ac
ZJS
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)"));
65b3903f 40
646853bd 41 fd2 = mkostemp_safe(pattern);
bdf7026e 42 assert_se(fd >= 0);
65b3903f
ZJS
43 assert_se(unlink(pattern) == 0);
44
df0ff127 45 assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
dbb471ac
ZJS
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)"));
65b3903f 50
03532f0a
LP
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
65b3903f
ZJS
67 return 0;
68}