1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Linux Test Project, 2003-2024
5 */
6
7 /*\
8 * [Description]
9 *
10 * Verify the basic sigsuspend(2) syscall functionality:
11 *
12 * - sigsuspend(2) can replace process's current signal mask by the specified
13 * signal mask and suspend the process execution until the delivery of a
14 * signal.
15 * - sigsuspend(2) should return after the execution of signal handler and
16 * restore the previous signal mask.
17 */
18
19 #include <errno.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <signal.h>
23
24 #include "tst_test.h"
25
26 static sigset_t signalset, sigset1, sigset2;
27 static volatile sig_atomic_t alarm_num;
28
sig_handler(int sig)29 static void sig_handler(int sig)
30 {
31 alarm_num = sig;
32 }
33
verify_sigsuspend(void)34 static void verify_sigsuspend(void)
35 {
36 alarm_num = 0;
37
38 SAFE_SIGFILLSET(&sigset2);
39
40 alarm(1);
41
42 /* Unblock SIGALRM */
43 TEST(sigsuspend(&signalset));
44
45 alarm(0);
46
47 if (TST_RET != -1 || TST_ERR != EINTR) {
48 tst_res(TFAIL | TTERRNO,
49 "sigsuspend() returned value %ld", TST_RET);
50 return;
51 }
52
53 if (alarm_num != SIGALRM) {
54 tst_res(TFAIL, "sigsuspend() didn't unblock SIGALRM");
55 return;
56 }
57
58 SAFE_SIGPROCMASK(0, NULL, &sigset2);
59 if (memcmp(&sigset1, &sigset2, sizeof(unsigned long))) {
60 tst_res(TFAIL, "sigsuspend() failed to "
61 "restore the previous signal mask");
62 return;
63 }
64
65 tst_res(TPASS, "sigsuspend() succeeded");
66 }
67
setup(void)68 static void setup(void)
69 {
70 SAFE_SIGEMPTYSET(&signalset);
71 SAFE_SIGEMPTYSET(&sigset1);
72 SAFE_SIGADDSET(&sigset1, SIGALRM);
73
74 struct sigaction sa_new = {
75 .sa_handler = sig_handler,
76 };
77
78 SAFE_SIGACTION(SIGALRM, &sa_new, 0);
79
80 /* Block SIGALRM */
81 SAFE_SIGPROCMASK(SIG_SETMASK, &sigset1, NULL);
82 }
83
84 static struct tst_test test = {
85 .setup = setup,
86 .test_all = verify_sigsuspend,
87 };
88