2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-01-26 05:35:28 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2014-01-26 05:35:28 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-26 18:05:03 +01:00
|
|
|
#include "fileio.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2016-01-20 01:17:01 +01:00
|
|
|
#include "fs-util.h"
|
|
|
|
#include "log.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "process-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-09-13 14:31:13 +02:00
|
|
|
#include "tests.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "util.h"
|
2014-01-26 05:35:28 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2016-04-20 19:27:32 +02:00
|
|
|
_cleanup_free_ char *cmd = NULL, *cmd2 = NULL, *ans = NULL, *ans2 = NULL, *d = NULL, *tmp = NULL, *line = NULL;
|
2016-04-23 02:49:07 +02:00
|
|
|
_cleanup_close_ int fd = -1, fd2 = -1;
|
2014-01-26 05:35:28 +01:00
|
|
|
const char *p = argv[1] ?: "/tmp";
|
2016-04-20 19:27:32 +02:00
|
|
|
char *pattern;
|
2016-01-20 01:17:01 +01:00
|
|
|
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2014-01-26 05:35:28 +01:00
|
|
|
|
2016-04-20 19:27:32 +02:00
|
|
|
pattern = strjoina(p, "/systemd-test-XXXXXX");
|
|
|
|
|
|
|
|
fd = open_tmpfile_unlinkable(p, O_RDWR|O_CLOEXEC);
|
2014-10-04 23:51:45 +02:00
|
|
|
assert_se(fd >= 0);
|
2014-01-26 05:35:28 +01:00
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
assert_se(asprintf(&cmd, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd) > 0);
|
2016-01-20 01:17:01 +01:00
|
|
|
(void) system(cmd);
|
|
|
|
assert_se(readlink_malloc(cmd + 6, &ans) >= 0);
|
|
|
|
log_debug("link1: %s", ans);
|
|
|
|
assert_se(endswith(ans, " (deleted)"));
|
2014-01-26 05:35:28 +01:00
|
|
|
|
2016-09-13 08:20:38 +02:00
|
|
|
fd2 = mkostemp_safe(pattern);
|
2014-10-04 23:51:45 +02:00
|
|
|
assert_se(fd >= 0);
|
2014-01-26 05:35:28 +01:00
|
|
|
assert_se(unlink(pattern) == 0);
|
|
|
|
|
2017-07-20 16:19:18 +02:00
|
|
|
assert_se(asprintf(&cmd2, "ls -l /proc/"PID_FMT"/fd/%d", getpid_cached(), fd2) > 0);
|
2016-01-20 01:17:01 +01:00
|
|
|
(void) system(cmd2);
|
|
|
|
assert_se(readlink_malloc(cmd2 + 6, &ans2) >= 0);
|
|
|
|
log_debug("link2: %s", ans2);
|
|
|
|
assert_se(endswith(ans2, " (deleted)"));
|
2014-01-26 05:35:28 +01:00
|
|
|
|
2016-04-20 19:27:32 +02:00
|
|
|
pattern = strjoina(p, "/tmpfiles-test");
|
|
|
|
assert_se(tempfn_random(pattern, NULL, &d) >= 0);
|
|
|
|
|
|
|
|
fd = open_tmpfile_linkable(d, O_RDWR|O_CLOEXEC, &tmp);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
assert_se(write(fd, "foobar\n", 7) == 7);
|
|
|
|
|
|
|
|
assert_se(touch(d) >= 0);
|
|
|
|
assert_se(link_tmpfile(fd, tmp, d) == -EEXIST);
|
|
|
|
assert_se(unlink(d) >= 0);
|
|
|
|
assert_se(link_tmpfile(fd, tmp, d) >= 0);
|
|
|
|
|
|
|
|
assert_se(read_one_line_file(d, &line) >= 0);
|
|
|
|
assert_se(streq(line, "foobar"));
|
|
|
|
assert_se(unlink(d) >= 0);
|
|
|
|
|
2014-01-26 05:35:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|