]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-async.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "process-util.h"
13 #include "signal-util.h"
15 #include "time-util.h"
16 #include "tmpfile-util.h"
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
, /* ret= */ NULL
));
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
);
36 TEST(asynchronous_close
) {
37 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-asynchronous_close.XXXXXX";
40 fd
= mkostemp_safe(name
);
42 asynchronous_close(fd
);
45 r
= safe_fork("(subreaper)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGKILL
|FORK_LOG
|FORK_WAIT
, NULL
);
51 ASSERT_OK(make_reaper_process(true));
53 fd
= open("/dev/null", O_RDONLY
|O_CLOEXEC
);
55 asynchronous_close(fd
);
62 static void wait_rm_rf(const char *path
) {
63 for (unsigned trial
= 0; trial
< 100; trial
++) {
64 usleep_safe(100 * USEC_PER_MSEC
);
65 if (access(path
, F_OK
) < 0) {
66 assert_se(errno
== ENOENT
);
74 TEST(asynchronous_rm_rf
) {
75 _cleanup_free_
char *t
= NULL
, *k
= NULL
;
78 ASSERT_OK(mkdtemp_malloc(NULL
, &t
));
79 assert_se(k
= path_join(t
, "somefile"));
81 ASSERT_OK(asynchronous_rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
));
84 /* Do this once more, from a subreaper. Which is nice, because we can watch the async child even
87 r
= safe_fork("(subreaper)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGTERM
|FORK_REOPEN_LOG
|FORK_LOG
|FORK_WAIT
, NULL
);
91 _cleanup_free_
char *tt
= NULL
, *kk
= NULL
;
95 ASSERT_OK(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
));
96 ASSERT_OK(make_reaper_process(true));
98 ASSERT_OK(mkdtemp_malloc(NULL
, &tt
));
99 assert_se(kk
= path_join(tt
, "somefile"));
100 ASSERT_OK(touch(kk
));
101 ASSERT_OK(asynchronous_rm_rf(tt
, REMOVE_ROOT
|REMOVE_PHYSICAL
));
106 ASSERT_OK_ERRNO(waitid(P_ALL
, 0, &si
, WEXITED
));
108 if (access(tt
, F_OK
) < 0) {
109 assert_se(errno
== ENOENT
);
113 /* wasn't the rm_rf() call. let's wait longer */
120 DEFINE_TEST_MAIN(LOG_DEBUG
);