aboutsummaryrefslogtreecommitdiff
path: root/test/sigfd-deadlock.c
blob: 277b34288d25c9132911eacb605377e37af05346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* SPDX-License-Identifier: MIT */
/*
 * Description: test that sigfd reading/polling works. A regression test for
 * the upstream commit:
 *
 * fd7d6de22414 ("io_uring: don't recurse on tsk->sighand->siglock with signalfd")
 */
#include <unistd.h>
#include <sys/signalfd.h>
#include <sys/epoll.h>
#include <poll.h>
#include <stdio.h>
#include "liburing.h"

static int setup_signal(void)
{
	sigset_t mask;
	int sfd;

	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);

	sigprocmask(SIG_BLOCK, &mask, NULL);
	sfd = signalfd(-1, &mask, SFD_NONBLOCK);
	if (sfd < 0)
		perror("signalfd");
	return sfd;
}

static int test_uring(int sfd)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	struct io_uring ring;
	int ret;

	io_uring_queue_init(32, &ring, 0);

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_poll_add(sqe, sfd, POLLIN);
	io_uring_submit(&ring);

	kill(getpid(), SIGINT);

	io_uring_wait_cqe(&ring, &cqe);
	if (cqe->res & POLLIN) {
		ret = 0;
	} else {
		fprintf(stderr, "Unexpected poll mask %x\n", cqe->res);
		ret = 1;
	}
	io_uring_cqe_seen(&ring, cqe);
	io_uring_queue_exit(&ring);
	return ret;
}

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

	if (argc > 1)
		return 0;

	sfd = setup_signal();
	if (sfd < 0)
		return 1;

	ret = test_uring(sfd);
	if (ret)
		fprintf(stderr, "test_uring signalfd failed\n");

	close(sfd);
	return ret;
}