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