| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/wait.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "async.h" |
| 8 |
#include "fs-util.h" |
| 9 |
#include "path-util.h" |
| 10 |
#include "pidref.h" |
| 11 |
#include "process-util.h" |
| 12 |
#include "rm-rf.h" |
| 13 |
#include "signal-util.h" |
| 14 |
#include "tests.h" |
| 15 |
#include "time-util.h" |
| 16 |
#include "tmpfile-util.h" |
| 17 |
|
| 18 |
TEST(asynchronous_sync) { |
| 19 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 20 |
ASSERT_OK(asynchronous_sync(&pidref)); |
| 21 |
ASSERT_OK(pidref_wait_for_terminate(&pidref, NULL)); |
| 22 |
} |
| 23 |
|
| 24 |
static void wait_fd_closed(int fd) { |
| 25 |
for (unsigned trial = 0; trial < 100; trial++) { |
| 26 |
usleep_safe(100 * USEC_PER_MSEC); |
| 27 |
if (fcntl(fd, F_GETFD) < 0) { |
| 28 |
assert_se(errno == EBADF); |
| 29 |
return; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
assert_not_reached(); |
| 34 |
} |
| 35 |
|
| 36 |
TEST(asynchronous_close) { |
| 37 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-asynchronous_close.XXXXXX"; |
| 38 |
int fd, r; |
| 39 |
|
| 40 |
fd = mkostemp_safe(name); |
| 41 |
ASSERT_OK(fd); |
| 42 |
asynchronous_close(fd); |
| 43 |
wait_fd_closed(fd); |
| 44 |
|
| 45 |
r = ASSERT_OK(pidref_safe_fork( |
| 46 |
"(subreaper)", |
| 47 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_LOG|FORK_WAIT, |
| 48 |
NULL)); |
| 49 |
|
| 50 |
if (r == 0) { |
| 51 |
/* child */ |
| 52 |
|
| 53 |
ASSERT_OK(make_reaper_process(true)); |
| 54 |
|
| 55 |
fd = open("/dev/null", O_RDONLY|O_CLOEXEC); |
| 56 |
ASSERT_OK(fd); |
| 57 |
asynchronous_close(fd); |
| 58 |
wait_fd_closed(fd); |
| 59 |
|
| 60 |
_exit(EXIT_SUCCESS); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static void wait_rm_rf(const char *path) { |
| 65 |
for (unsigned trial = 0; trial < 100; trial++) { |
| 66 |
usleep_safe(100 * USEC_PER_MSEC); |
| 67 |
if (access(path, F_OK) < 0) { |
| 68 |
assert_se(errno == ENOENT); |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
assert_not_reached(); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(asynchronous_rm_rf) { |
| 77 |
_cleanup_free_ char *t = NULL, *k = NULL; |
| 78 |
int r; |
| 79 |
|
| 80 |
ASSERT_OK(mkdtemp_malloc(NULL, &t)); |
| 81 |
assert_se(k = path_join(t, "somefile")); |
| 82 |
ASSERT_OK(touch(k)); |
| 83 |
ASSERT_OK(asynchronous_rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL)); |
| 84 |
wait_rm_rf(t); |
| 85 |
|
| 86 |
/* Do this once more, from a subreaper. Which is nice, because we can watch the async child even |
| 87 |
* though detached */ |
| 88 |
|
| 89 |
r = ASSERT_OK(pidref_safe_fork( |
| 90 |
"(subreaper)", |
| 91 |
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_WAIT, |
| 92 |
NULL)); |
| 93 |
|
| 94 |
if (r == 0) { |
| 95 |
_cleanup_free_ char *tt = NULL, *kk = NULL; |
| 96 |
|
| 97 |
/* child */ |
| 98 |
|
| 99 |
ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD)); |
| 100 |
ASSERT_OK(make_reaper_process(true)); |
| 101 |
|
| 102 |
ASSERT_OK(mkdtemp_malloc(NULL, &tt)); |
| 103 |
assert_se(kk = path_join(tt, "somefile")); |
| 104 |
ASSERT_OK(touch(kk)); |
| 105 |
ASSERT_OK(asynchronous_rm_rf(tt, REMOVE_ROOT|REMOVE_PHYSICAL)); |
| 106 |
|
| 107 |
for (;;) { |
| 108 |
siginfo_t si = {}; |
| 109 |
|
| 110 |
ASSERT_OK_ERRNO(waitid(P_ALL, 0, &si, WEXITED)); |
| 111 |
|
| 112 |
if (access(tt, F_OK) < 0) { |
| 113 |
assert_se(errno == ENOENT); |
| 114 |
break; |
| 115 |
} |
| 116 |
|
| 117 |
/* wasn't the rm_rf() call. let's wait longer */ |
| 118 |
} |
| 119 |
|
| 120 |
_exit(EXIT_SUCCESS); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
DEFINE_TEST_MAIN(LOG_DEBUG); |