Skip to content

Commit

Permalink
test: add test case for self-destroy inotify handler
Browse files Browse the repository at this point in the history
(cherry picked from commit 035daf7)

Related: #2211358
  • Loading branch information
poettering authored and systemd-rhel-bot committed Aug 4, 2023
1 parent a78186f commit 2fcb710
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/libsystemd/sd-event/test-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,40 @@ static void test_ratelimit(void) {
assert_se(expired == 0);
}

static int inotify_self_destroy_handler(sd_event_source *s, const struct inotify_event *ev, void *userdata) {
sd_event_source **p = userdata;

assert_se(ev);
assert_se(p);
assert_se(*p == s);

assert_se(FLAGS_SET(ev->mask, IN_ATTRIB));

assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);

*p = sd_event_source_unref(*p); /* here's what we actually intend to test: we destroy the event
* source from inside the event source handler */
return 1;
}

static void test_inotify_self_destroy(void) {
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
char path[] = "/tmp/inotifyXXXXXX";
_cleanup_close_ int fd = -1;

/* Tests that destroying an inotify event source from its own handler is safe */

assert_se(sd_event_default(&e) >= 0);

fd = mkostemp_safe(path);
assert_se(fd >= 0);
assert_se(sd_event_add_inotify_fd(e, &s, fd, IN_ATTRIB, inotify_self_destroy_handler, &s) >= 0);
fd = safe_close(fd);
assert_se(unlink(path) >= 0); /* This will trigger IN_ATTRIB because link count goes to zero */
assert_se(sd_event_loop(e) >= 0);
}

int main(int argc, char *argv[]) {

log_set_max_level(LOG_DEBUG);
Expand All @@ -602,5 +636,7 @@ int main(int argc, char *argv[]) {

test_ratelimit();

test_inotify_self_destroy();

return 0;
}

0 comments on commit 2fcb710

Please sign in to comment.