1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2016 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Basic test for epoll_pwait() and epoll_pwait2().
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - With a sigmask a signal is ignored and the syscall safely waits until
14f08c3bdfSopenharmony_ci *   either a file descriptor becomes ready or the timeout expires.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci * - Without sigmask if signal arrives a syscall is iterrupted by a signal.
17f08c3bdfSopenharmony_ci *   The call should return -1 and set errno to EINTR.
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <stdlib.h>
21f08c3bdfSopenharmony_ci#include <sys/epoll.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "epoll_pwait_var.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic int efd, sfd[2];
27f08c3bdfSopenharmony_cistatic struct epoll_event e;
28f08c3bdfSopenharmony_cistatic sigset_t signalset;
29f08c3bdfSopenharmony_cistatic struct sigaction sa;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void sighandler(int sig LTP_ATTRIBUTE_UNUSED) {}
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void verify_sigmask(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	TEST(do_epoll_pwait(efd, &e, 1, -1, &signalset));
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (TST_RET != 1) {
38f08c3bdfSopenharmony_ci		tst_res(TFAIL, "do_epoll_pwait() returned %li, expected 1",
39f08c3bdfSopenharmony_ci			TST_RET);
40f08c3bdfSopenharmony_ci		return;
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	tst_res(TPASS, "do_epoll_pwait() with sigmask blocked signal");
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void verify_nonsigmask(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	TST_EXP_FAIL(do_epoll_pwait(efd, &e, 1, -1, NULL), EINTR,
49f08c3bdfSopenharmony_ci		     "do_epoll_pwait() without sigmask");
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void (*testcase_list[])(void) = {verify_sigmask, verify_nonsigmask};
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic void run(unsigned int n)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	char b;
57f08c3bdfSopenharmony_ci	pid_t pid;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if (!SAFE_FORK()) {
60f08c3bdfSopenharmony_ci		pid = getppid();
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci		TST_PROCESS_STATE_WAIT(pid, 'S', 0);
63f08c3bdfSopenharmony_ci		SAFE_KILL(pid, SIGUSR1);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci		usleep(10000);
66f08c3bdfSopenharmony_ci		SAFE_WRITE(SAFE_WRITE_ALL, sfd[1], "w", 1);
67f08c3bdfSopenharmony_ci		exit(0);
68f08c3bdfSopenharmony_ci	}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	testcase_list[n]();
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	SAFE_READ(1, sfd[0], &b, 1);
73f08c3bdfSopenharmony_ci	tst_reap_children();
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic void setup(void)
77f08c3bdfSopenharmony_ci{
78f08c3bdfSopenharmony_ci	epoll_pwait_init();
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	SAFE_SIGEMPTYSET(&signalset);
81f08c3bdfSopenharmony_ci	SAFE_SIGADDSET(&signalset, SIGUSR1);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	sa.sa_flags = 0;
84f08c3bdfSopenharmony_ci	sa.sa_handler = sighandler;
85f08c3bdfSopenharmony_ci	SAFE_SIGEMPTYSET(&sa.sa_mask);
86f08c3bdfSopenharmony_ci	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sfd);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	efd = epoll_create(1);
91f08c3bdfSopenharmony_ci	if (efd == -1)
92f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_create()");
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	e.events = EPOLLIN;
95f08c3bdfSopenharmony_ci	if (epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e))
96f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
97f08c3bdfSopenharmony_ci}
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_cistatic void cleanup(void)
100f08c3bdfSopenharmony_ci{
101f08c3bdfSopenharmony_ci	if (efd > 0)
102f08c3bdfSopenharmony_ci		SAFE_CLOSE(efd);
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	if (sfd[0] > 0)
105f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd[0]);
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	if (sfd[1] > 0)
108f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd[1]);
109f08c3bdfSopenharmony_ci}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_cistatic struct tst_test test = {
112f08c3bdfSopenharmony_ci	.test = run,
113f08c3bdfSopenharmony_ci	.setup = setup,
114f08c3bdfSopenharmony_ci	.cleanup = cleanup,
115f08c3bdfSopenharmony_ci	.forks_child = 1,
116f08c3bdfSopenharmony_ci	.test_variants = TEST_VARIANTS,
117f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(testcase_list),
118f08c3bdfSopenharmony_ci};
119