aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/sigfd-deadlock.c
blob: a2a9ad948830d3b728e07dbc645e28772aa8cfde (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "../config-host.h"
/* 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"
#include "helpers.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;

	ret = io_uring_queue_init(32, &ring, 0);
	if (ret)
		return T_EXIT_FAIL;

	sqe = io_uring_get_sqe(&ring);
	io_uring_prep_poll_add(sqe, sfd, POLLIN);
	ret = io_uring_submit(&ring);
	if (ret < 0) {
		ret = T_EXIT_FAIL;
		goto err_exit;
	}

	kill(getpid(), SIGINT);

	io_uring_wait_cqe(&ring, &cqe);
	if (cqe->res == -EOPNOTSUPP) {
		fprintf(stderr, "signalfd poll not supported\n");
		ret = T_EXIT_SKIP;
	} else if (cqe->res < 0) {
		fprintf(stderr, "poll failed: %d\n", cqe->res);
		ret = T_EXIT_FAIL;
	} else if (cqe->res & POLLIN) {
		ret = T_EXIT_PASS;
	} else {
		fprintf(stderr, "Unexpected poll mask %x\n", cqe->res);
		ret = T_EXIT_FAIL;
	}
	io_uring_cqe_seen(&ring, cqe);
err_exit:
	io_uring_queue_exit(&ring);
	return ret;
}

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

	if (argc > 1)
		return T_EXIT_PASS;

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

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

	close(sfd);
	return ret;
}