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