1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project., 2007
4f08c3bdfSopenharmony_ci * Porting from Crackerjack to LTP is done by
5f08c3bdfSopenharmony_ci * Manas Kumar Nayak maknayak@in.ibm.com>
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * Waits for SIGALRM in rt_sigsuspend() then checks that process mask wasn't
8f08c3bdfSopenharmony_ci * modified.
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#include <signal.h>
12f08c3bdfSopenharmony_ci#include <errno.h>
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
16f08c3bdfSopenharmony_ci#include "lapi/safe_rt_signal.h"
17f08c3bdfSopenharmony_ci#include "lapi/signal.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void sig_handler(int sig)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	(void) sig;
22f08c3bdfSopenharmony_ci}
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void verify_rt_sigsuspend(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	int i;
27f08c3bdfSopenharmony_ci	sigset_t set, set1, set2;
28f08c3bdfSopenharmony_ci	struct sigaction act = {.sa_handler = sig_handler};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (sigemptyset(&set) < 0)
31f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TERRNO, "sigemptyset failed");
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	SAFE_RT_SIGACTION(SIGALRM, &act, NULL, SIGSETSIZE);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	SAFE_RT_SIGPROCMASK(0, NULL, &set1, SIGSETSIZE);
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	alarm(1);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	TEST(tst_syscall(__NR_rt_sigsuspend, &set, SIGSETSIZE));
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	alarm(0);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	if (TST_RET != -1)
44f08c3bdfSopenharmony_ci		tst_brk(TFAIL, "rt_sigsuspend returned %ld", TST_RET);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (TST_ERR != EINTR)
47f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "rt_sigsuspend() failed unexpectedly");
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	tst_res(TPASS, "rt_sigsuspend() returned with -1 and EINTR");
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	SAFE_RT_SIGPROCMASK(0, NULL, &set2, SIGSETSIZE);
52f08c3bdfSopenharmony_ci	for (i = 1; i < SIGRTMAX; i++) {
53f08c3bdfSopenharmony_ci		if (i >= __SIGRTMIN && i < SIGRTMIN)
54f08c3bdfSopenharmony_ci			continue;
55f08c3bdfSopenharmony_ci		if (sigismember(&set1, i) != sigismember(&set2, i))
56f08c3bdfSopenharmony_ci			tst_brk(TFAIL, "signal mask not preserved");
57f08c3bdfSopenharmony_ci	}
58f08c3bdfSopenharmony_ci	tst_res(TPASS, "signal mask preserved");
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic struct tst_test test = {
62f08c3bdfSopenharmony_ci	.test_all = verify_rt_sigsuspend,
63f08c3bdfSopenharmony_ci};
64