]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-tmpfiles.c
2 This file is part of systemd.
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
25 #include "alloc-util.h"
28 #include "formats-util.h"
31 #include "string-util.h"
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";
40 log_set_max_level(LOG_DEBUG
);
41 log_parse_environment();
43 pattern
= strjoina(p
, "/systemd-test-XXXXXX");
45 fd
= open_tmpfile_unlinkable(p
, O_RDWR
|O_CLOEXEC
);
48 assert_se(asprintf(&cmd
, "ls -l /proc/"PID_FMT
"/fd/%d", getpid(), fd
) > 0);
50 assert_se(readlink_malloc(cmd
+ 6, &ans
) >= 0);
51 log_debug("link1: %s", ans
);
52 assert_se(endswith(ans
, " (deleted)"));
54 fd2
= mkostemp_safe(pattern
);
56 assert_se(unlink(pattern
) == 0);
58 assert_se(asprintf(&cmd2
, "ls -l /proc/"PID_FMT
"/fd/%d", getpid(), fd2
) > 0);
60 assert_se(readlink_malloc(cmd2
+ 6, &ans2
) >= 0);
61 log_debug("link2: %s", ans2
);
62 assert_se(endswith(ans2
, " (deleted)"));
64 pattern
= strjoina(p
, "/tmpfiles-test");
65 assert_se(tempfn_random(pattern
, NULL
, &d
) >= 0);
67 fd
= open_tmpfile_linkable(d
, O_RDWR
|O_CLOEXEC
, &tmp
);
69 assert_se(write(fd
, "foobar\n", 7) == 7);
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);
76 assert_se(read_one_line_file(d
, &line
) >= 0);
77 assert_se(streq(line
, "foobar"));
78 assert_se(unlink(d
) >= 0);