xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sigprocmask/sigprocmask01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * Test Name: sigprocmask01
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that sigprocmask() succeeds to examine and change the calling
25*49cdfc7eSAndroid Build Coastguard Worker  *  process's signal mask.
26*49cdfc7eSAndroid Build Coastguard Worker  *  Also, verify that sigpending() succeeds to store signal mask that are
27*49cdfc7eSAndroid Build Coastguard Worker  *  blocked from delivery and pending for the calling process.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
30*49cdfc7eSAndroid Build Coastguard Worker  *  - sigprocmask() should return value 0 on successs and succeed to change
31*49cdfc7eSAndroid Build Coastguard Worker  *    calling process's set of blocked/unblocked signals.
32*49cdfc7eSAndroid Build Coastguard Worker  *  - sigpending() should succeed to store the signal mask of pending signal.
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
35*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
36*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
37*49cdfc7eSAndroid Build Coastguard Worker  *   Create temporary directory.
38*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
39*49cdfc7eSAndroid Build Coastguard Worker  *
40*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
41*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
42*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
43*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
44*49cdfc7eSAndroid Build Coastguard Worker  *   	Log the errno and Issue a FAIL message.
45*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
46*49cdfc7eSAndroid Build Coastguard Worker  *   	Verify the Functionality of system call
47*49cdfc7eSAndroid Build Coastguard Worker  *      if successful,
48*49cdfc7eSAndroid Build Coastguard Worker  *      	Issue Functionality-Pass message.
49*49cdfc7eSAndroid Build Coastguard Worker  *      Otherwise,
50*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Fail message.
51*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
52*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
53*49cdfc7eSAndroid Build Coastguard Worker  *   Delete the temporary directory created.
54*49cdfc7eSAndroid Build Coastguard Worker  *
55*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
56*49cdfc7eSAndroid Build Coastguard Worker  *  sigprocmask01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
57*49cdfc7eSAndroid Build Coastguard Worker  *	where,  -c n : Run n copies concurrently.
58*49cdfc7eSAndroid Build Coastguard Worker  *		-e   : Turn on errno logging.
59*49cdfc7eSAndroid Build Coastguard Worker  *		-f   : Turn off functionality Testing.
60*49cdfc7eSAndroid Build Coastguard Worker  *		-i n : Execute test n times.
61*49cdfc7eSAndroid Build Coastguard Worker  *		-I x : Execute test for x seconds.
62*49cdfc7eSAndroid Build Coastguard Worker  *		-P x : Pause for x seconds between iterations.
63*49cdfc7eSAndroid Build Coastguard Worker  *		-t   : Turn on syscall timing.
64*49cdfc7eSAndroid Build Coastguard Worker  *
65*49cdfc7eSAndroid Build Coastguard Worker  * History
66*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 John George
67*49cdfc7eSAndroid Build Coastguard Worker  *		-Ported
68*49cdfc7eSAndroid Build Coastguard Worker  *
69*49cdfc7eSAndroid Build Coastguard Worker  * Restrictions:
70*49cdfc7eSAndroid Build Coastguard Worker  *  None.
71*49cdfc7eSAndroid Build Coastguard Worker  */
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
75*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
76*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
77*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
78*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
79*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
80*49cdfc7eSAndroid Build Coastguard Worker #include <ucontext.h>
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* Main setup function of test */
85*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
86*49cdfc7eSAndroid Build Coastguard Worker void sig_handler(int sig);	/* signal catching function */
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "sigprocmask01";
89*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker int sig_catch = 0;		/* variable to blocked/unblocked signals */
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker struct sigaction sa_new;	/* struct to hold signal info */
94*49cdfc7eSAndroid Build Coastguard Worker sigset_t set;		/* signal set to hold signal lists */
95*49cdfc7eSAndroid Build Coastguard Worker sigset_t sigset2;
96*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)97*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
98*49cdfc7eSAndroid Build Coastguard Worker {
99*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
100*49cdfc7eSAndroid Build Coastguard Worker 	pid_t my_pid;		/* test process id */
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	setup();
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 		/*
111*49cdfc7eSAndroid Build Coastguard Worker 		 * Call sigprocmask() to block (SIGINT) signal
112*49cdfc7eSAndroid Build Coastguard Worker 		 * so that, signal will not be delivered to
113*49cdfc7eSAndroid Build Coastguard Worker 		 * the test process.
114*49cdfc7eSAndroid Build Coastguard Worker 		 */
115*49cdfc7eSAndroid Build Coastguard Worker 		TEST(sigprocmask(SIG_BLOCK, &set, 0));
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 		/* Get the process id of test process */
118*49cdfc7eSAndroid Build Coastguard Worker 		my_pid = getpid();
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 		/* Send SIGINT signal to the process */
121*49cdfc7eSAndroid Build Coastguard Worker 		kill(my_pid, SIGINT);
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
124*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
125*49cdfc7eSAndroid Build Coastguard Worker 				 "sigprocmask() Failed, errno=%d : %s",
126*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO, strerror(TEST_ERRNO));
127*49cdfc7eSAndroid Build Coastguard Worker 		} else {
128*49cdfc7eSAndroid Build Coastguard Worker 			/*
129*49cdfc7eSAndroid Build Coastguard Worker 			 * Check whether process received the signal.
130*49cdfc7eSAndroid Build Coastguard Worker 			 * If yes! signal handler was executed and
131*49cdfc7eSAndroid Build Coastguard Worker 			 * incremented 'sig_catch' variable.
132*49cdfc7eSAndroid Build Coastguard Worker 			 */
133*49cdfc7eSAndroid Build Coastguard Worker 			if (sig_catch) {
134*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "sigprocmask fails to "
135*49cdfc7eSAndroid Build Coastguard Worker 					 "change process's signal mask");
136*49cdfc7eSAndroid Build Coastguard Worker 			} else {
137*49cdfc7eSAndroid Build Coastguard Worker 				/*
138*49cdfc7eSAndroid Build Coastguard Worker 				 * Check whether specified signal
139*49cdfc7eSAndroid Build Coastguard Worker 				 * 'SIGINT' is pending for the process.
140*49cdfc7eSAndroid Build Coastguard Worker 				 */
141*49cdfc7eSAndroid Build Coastguard Worker 				errno = 0;
142*49cdfc7eSAndroid Build Coastguard Worker 				if (sigpending(&sigset2) == -1) {
143*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL, cleanup,
144*49cdfc7eSAndroid Build Coastguard Worker 						 "blocked signal not "
145*49cdfc7eSAndroid Build Coastguard Worker 						 "in pending state, "
146*49cdfc7eSAndroid Build Coastguard Worker 						 "error:%d", errno);
147*49cdfc7eSAndroid Build Coastguard Worker 				}
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 				/*
150*49cdfc7eSAndroid Build Coastguard Worker 				 * Check whether specified signal
151*49cdfc7eSAndroid Build Coastguard Worker 				 * is the member of signal set.
152*49cdfc7eSAndroid Build Coastguard Worker 				 */
153*49cdfc7eSAndroid Build Coastguard Worker 				errno = 0;
154*49cdfc7eSAndroid Build Coastguard Worker 				if (!sigismember(&sigset2, SIGINT)) {
155*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL, cleanup,
156*49cdfc7eSAndroid Build Coastguard Worker 						 "sigismember() failed, "
157*49cdfc7eSAndroid Build Coastguard Worker 						 "error:%d", errno);
158*49cdfc7eSAndroid Build Coastguard Worker 				}
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 				/*
161*49cdfc7eSAndroid Build Coastguard Worker 				 * Invoke sigprocmask() again to
162*49cdfc7eSAndroid Build Coastguard Worker 				 * unblock the specified signal.
163*49cdfc7eSAndroid Build Coastguard Worker 				 * so that, signal is delivered and
164*49cdfc7eSAndroid Build Coastguard Worker 				 * signal handler executed.
165*49cdfc7eSAndroid Build Coastguard Worker 				 */
166*49cdfc7eSAndroid Build Coastguard Worker 				errno = 0;
167*49cdfc7eSAndroid Build Coastguard Worker 				if (sigprocmask(SIG_UNBLOCK,
168*49cdfc7eSAndroid Build Coastguard Worker 						&set, 0) == -1) {
169*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL, cleanup,
170*49cdfc7eSAndroid Build Coastguard Worker 						 "sigprocmask() failed "
171*49cdfc7eSAndroid Build Coastguard Worker 						 "to unblock signal, "
172*49cdfc7eSAndroid Build Coastguard Worker 						 "error=%d", errno);
173*49cdfc7eSAndroid Build Coastguard Worker 				}
174*49cdfc7eSAndroid Build Coastguard Worker 				if (sig_catch) {
175*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS, "Functionality "
176*49cdfc7eSAndroid Build Coastguard Worker 						 "of sigprocmask() "
177*49cdfc7eSAndroid Build Coastguard Worker 						 "Successful");
178*49cdfc7eSAndroid Build Coastguard Worker 				} else {
179*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL, "Functionality "
180*49cdfc7eSAndroid Build Coastguard Worker 						 "of sigprocmask() "
181*49cdfc7eSAndroid Build Coastguard Worker 						 "Failed");
182*49cdfc7eSAndroid Build Coastguard Worker 				}
183*49cdfc7eSAndroid Build Coastguard Worker 				/* set sig_catch back to 0 */
184*49cdfc7eSAndroid Build Coastguard Worker 				sig_catch = 0;
185*49cdfc7eSAndroid Build Coastguard Worker 			}
186*49cdfc7eSAndroid Build Coastguard Worker 		}
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 		tst_count++;	/* incr TEST_LOOP counter */
189*49cdfc7eSAndroid Build Coastguard Worker 	}
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
192*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker /*
196*49cdfc7eSAndroid Build Coastguard Worker  * void
197*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
198*49cdfc7eSAndroid Build Coastguard Worker  * Initialise signal set with the list that includes/excludes
199*49cdfc7eSAndroid Build Coastguard Worker  * all system-defined signals.
200*49cdfc7eSAndroid Build Coastguard Worker  * Set the signal handler to catch SIGINT signal.
201*49cdfc7eSAndroid Build Coastguard Worker  * Add the signal SIGINT to the exclude list of system-defined
202*49cdfc7eSAndroid Build Coastguard Worker  * signals for the test process.
203*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)204*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
205*49cdfc7eSAndroid Build Coastguard Worker {
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	/*
212*49cdfc7eSAndroid Build Coastguard Worker 	 * Initialise the signal sets with the list that
213*49cdfc7eSAndroid Build Coastguard Worker 	 * excludes/includes  all system-defined signals.
214*49cdfc7eSAndroid Build Coastguard Worker 	 */
215*49cdfc7eSAndroid Build Coastguard Worker 	if (sigemptyset(&set) == -1) {
216*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup,
217*49cdfc7eSAndroid Build Coastguard Worker 			 "sigemptyset() failed, errno=%d : %s",
218*49cdfc7eSAndroid Build Coastguard Worker 			 errno, strerror(errno));
219*49cdfc7eSAndroid Build Coastguard Worker 	}
220*49cdfc7eSAndroid Build Coastguard Worker 	if (sigfillset(&sigset2) == -1) {
221*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup,
222*49cdfc7eSAndroid Build Coastguard Worker 			 "sigfillset() failed, errno=%d : %s",
223*49cdfc7eSAndroid Build Coastguard Worker 			 errno, strerror(errno));
224*49cdfc7eSAndroid Build Coastguard Worker 	}
225*49cdfc7eSAndroid Build Coastguard Worker 
226*49cdfc7eSAndroid Build Coastguard Worker 	/* Set the signal handler function to catch the signal */
227*49cdfc7eSAndroid Build Coastguard Worker 	sa_new.sa_handler = sig_handler;
228*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGINT, &sa_new, 0) == -1) {
229*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup,
230*49cdfc7eSAndroid Build Coastguard Worker 			 "sigaction() failed, errno=%d : %s",
231*49cdfc7eSAndroid Build Coastguard Worker 			 errno, strerror(errno));
232*49cdfc7eSAndroid Build Coastguard Worker 	}
233*49cdfc7eSAndroid Build Coastguard Worker 
234*49cdfc7eSAndroid Build Coastguard Worker 	/*
235*49cdfc7eSAndroid Build Coastguard Worker 	 * Add specified signal (SIGINT) to the signal set
236*49cdfc7eSAndroid Build Coastguard Worker 	 * which excludes system-defined signals.
237*49cdfc7eSAndroid Build Coastguard Worker 	 */
238*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaddset(&set, SIGINT) == -1) {
239*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, cleanup,
240*49cdfc7eSAndroid Build Coastguard Worker 			 "sigaddset() failed, errno=%d : %s",
241*49cdfc7eSAndroid Build Coastguard Worker 			 errno, strerror(errno));
242*49cdfc7eSAndroid Build Coastguard Worker 	}
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker 
245*49cdfc7eSAndroid Build Coastguard Worker /*
246*49cdfc7eSAndroid Build Coastguard Worker  * void
247*49cdfc7eSAndroid Build Coastguard Worker  * sig_handler(int sig) - Signal catching function.
248*49cdfc7eSAndroid Build Coastguard Worker  *   This function gets executed when the signal SIGINT is delivered
249*49cdfc7eSAndroid Build Coastguard Worker  *   to the test process and the signal was trapped by sigaction()
250*49cdfc7eSAndroid Build Coastguard Worker  *   to execute this function.
251*49cdfc7eSAndroid Build Coastguard Worker  *   This function when executed, increments a global variable value
252*49cdfc7eSAndroid Build Coastguard Worker  *   which will be accessed in the test.
253*49cdfc7eSAndroid Build Coastguard Worker  */
sig_handler(int sig)254*49cdfc7eSAndroid Build Coastguard Worker void sig_handler(int sig)
255*49cdfc7eSAndroid Build Coastguard Worker {
256*49cdfc7eSAndroid Build Coastguard Worker 	/* Increment the sig_catch variable */
257*49cdfc7eSAndroid Build Coastguard Worker 	sig_catch++;
258*49cdfc7eSAndroid Build Coastguard Worker }
259*49cdfc7eSAndroid Build Coastguard Worker 
260*49cdfc7eSAndroid Build Coastguard Worker /*
261*49cdfc7eSAndroid Build Coastguard Worker  * void
262*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
263*49cdfc7eSAndroid Build Coastguard Worker  *             completion or premature exit.
264*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)265*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
266*49cdfc7eSAndroid Build Coastguard Worker {
267*49cdfc7eSAndroid Build Coastguard Worker 
268*49cdfc7eSAndroid Build Coastguard Worker }
269