]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-async.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "path-util.h"
11 #include "process-util.h"
12 #include "signal-util.h"
13 #include "time-util.h"
15 #include "tmpfile-util.h"
17 TEST(asynchronous_sync
) {
18 ASSERT_OK(asynchronous_sync(NULL
));
21 static void wait_fd_closed(int fd
) {
22 for (unsigned trial
= 0; trial
< 100; trial
++) {
23 usleep_safe(100 * USEC_PER_MSEC
);
24 if (fcntl(fd
, F_GETFD
) < 0) {
25 assert_se(errno
== EBADF
);
33 TEST(asynchronous_close
) {
34 _cleanup_(unlink_tempfilep
) char name
[] = "/tmp/test-asynchronous_close.XXXXXX";
37 fd
= mkostemp_safe(name
);
39 asynchronous_close(fd
);
42 r
= safe_fork("(subreaper)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGKILL
|FORK_LOG
|FORK_WAIT
, NULL
);
48 ASSERT_OK(make_reaper_process(true));
50 fd
= open("/dev/null", O_RDONLY
|O_CLOEXEC
);
52 asynchronous_close(fd
);
59 static void wait_rm_rf(const char *path
) {
60 for (unsigned trial
= 0; trial
< 100; trial
++) {
61 usleep_safe(100 * USEC_PER_MSEC
);
62 if (access(path
, F_OK
) < 0) {
63 assert_se(errno
== ENOENT
);
71 TEST(asynchronous_rm_rf
) {
72 _cleanup_free_
char *t
= NULL
, *k
= NULL
;
75 ASSERT_OK(mkdtemp_malloc(NULL
, &t
));
76 assert_se(k
= path_join(t
, "somefile"));
78 ASSERT_OK(asynchronous_rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
));
81 /* Do this once more, from a subreaper. Which is nice, because we can watch the async child even
84 r
= safe_fork("(subreaper)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG_SIGTERM
|FORK_LOG
|FORK_WAIT
, NULL
);
88 _cleanup_free_
char *tt
= NULL
, *kk
= NULL
;
92 ASSERT_OK(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
));
93 ASSERT_OK(make_reaper_process(true));
95 ASSERT_OK(mkdtemp_malloc(NULL
, &tt
));
96 assert_se(kk
= path_join(tt
, "somefile"));
98 ASSERT_OK(asynchronous_rm_rf(tt
, REMOVE_ROOT
|REMOVE_PHYSICAL
));
103 ASSERT_OK(waitid(P_ALL
, 0, &si
, WEXITED
));
105 if (access(tt
, F_OK
) < 0) {
106 assert_se(errno
== ENOENT
);
110 /* wasn't the rm_rf() call. let's wait longer */
118 DEFINE_TEST_MAIN(LOG_DEBUG
);