blob: a0441577ba0a4b333679af849ac2563965e36a54 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: sigsuspend01
22 *
23 * Test Description:
24 * Verify that sigsuspend() succeeds to change process's current signal
25 * mask with the specified signal mask and suspends the process execution
26 * until the delivery of a signal.
27 *
28 * Expected Result:
29 * sigsuspend() should return after the execution of signal catching
30 * function and the previous signal mask should be restored.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Create temporary directory.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * Log the errno and Issue a FAIL message.
43 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * if successful,
46 * Issue Functionality-Pass message.
47 * Otherwise,
48 * Issue Functionality-Fail message.
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory created.
52 *
53 * Usage: <for command-line>
54 * sigsuspend01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -e : Turn on errno logging.
57 * -f : Turn off functionality Testing.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -P x : Pause for x seconds between iterations.
61 * -t : Turn on syscall timing.
62 *
63 * History
64 * 07/2001 John George
65 * -Ported
66 *
67 * Restrictions:
68 * None.
69 */
70
71#include <stdio.h>
72#include <unistd.h>
73#include <sys/types.h>
74#include <errno.h>
75#include <fcntl.h>
76#include <string.h>
77#include <signal.h>
78#include <ucontext.h>
79
80#include "test.h"
plars865695b2001-08-27 22:15:12 +000081
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020082char *TCID = "sigsuspend01";
83int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000084
85struct sigaction sa_new; /* struct to hold signal info */
86sigset_t sigset; /* signal set to hold signal lists */
87sigset_t sigset1;
88sigset_t sigset2;
89
90void setup(); /* Main setup function of test */
91void cleanup(); /* cleanup function for the test */
92void sig_handler(int sig); /* signal catching function */
93
subrata_modak56207ce2009-03-23 13:35:39 +000094int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000095{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020096 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020097 const char *msg;
plars865695b2001-08-27 22:15:12 +000098
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000100 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000101
plars865695b2001-08-27 22:15:12 +0000102 setup();
103
plars865695b2001-08-27 22:15:12 +0000104 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000107
108 /* Set the alarm timer */
109 alarm(5);
110
subrata_modak4bb656a2009-02-26 12:02:09 +0000111 /*
plars865695b2001-08-27 22:15:12 +0000112 * Call sigsuspend() to replace current signal mask
113 * of the process and suspend process execution till
114 * receipt of a signal 'SIGALRM'.
115 */
116 TEST(sigsuspend(&sigset));
117
118 /* Reset the alarm timer */
119 alarm(0);
120
plars865695b2001-08-27 22:15:12 +0000121 if ((TEST_RETURN == -1) && (TEST_ERRNO == EINTR)) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200122 if (sigprocmask(SIG_UNBLOCK, 0, &sigset2) == -1) {
123 tst_resm(TFAIL, "sigprocmask() Failed "
124 "to get previous signal mask "
125 "of process");
126 } else if (sigset2.__val[0] != sigset1.__val[0]) {
127 tst_resm(TFAIL, "sigsuspend failed to "
128 "preserve signal mask");
plars865695b2001-08-27 22:15:12 +0000129 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 tst_resm(TPASS, "Functionality of "
131 "sigsuspend() successful");
plars865695b2001-08-27 22:15:12 +0000132 }
133 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_resm(TFAIL | TTERRNO,
subrata_modak923b23f2009-11-02 13:57:16 +0000135 "sigsuspend() returned value %ld",
136 TEST_RETURN);
plars865695b2001-08-27 22:15:12 +0000137 }
138
Caspar Zhangd59a6592013-03-07 14:59:12 +0800139 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800140 }
plars865695b2001-08-27 22:15:12 +0000141
plars865695b2001-08-27 22:15:12 +0000142 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800143 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800144}
plars865695b2001-08-27 22:15:12 +0000145
146/*
147 * void
148 * setup() - performs all ONE TIME setup for this test.
149 * Initialise signal set with the list that includes/excludes
150 * all system-defined signals.
151 * Set the signal handler to catch SIGALRM signal.
152 * Get the current signal mask of test process using sigprocmask().
153 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400154void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
Garrett Cooper2c282152010-12-16 00:55:50 -0800156
plars865695b2001-08-27 22:15:12 +0000157 tst_sig(FORK, DEF_HANDLER, cleanup);
158
plars865695b2001-08-27 22:15:12 +0000159 TEST_PAUSE;
160
161 /*
162 * Initialise the signal sets with the list that
163 * excludes/includes all system-defined signals.
164 */
165 if (sigemptyset(&sigset) == -1) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000166 tst_brkm(TFAIL, cleanup,
plars865695b2001-08-27 22:15:12 +0000167 "sigemptyset() failed, errno=%d : %s",
168 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 }
plars865695b2001-08-27 22:15:12 +0000170 if (sigfillset(&sigset2) == -1) {
171 tst_brkm(TFAIL, cleanup,
172 "sigfillset() failed, errno=%d : %s",
173 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 }
plars865695b2001-08-27 22:15:12 +0000175
176 /* Set the signal handler function to catch the signal */
177 sa_new.sa_handler = sig_handler;
178 if (sigaction(SIGALRM, &sa_new, 0) == -1) {
179 tst_brkm(TFAIL, cleanup,
180 "sigaction() failed, errno=%d : %s",
181 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 }
plars865695b2001-08-27 22:15:12 +0000183
184 /* Read the test process's current signal mask. */
185 if (sigprocmask(SIG_UNBLOCK, 0, &sigset1) == -1) {
186 tst_brkm(TFAIL, cleanup,
187 "sigprocmask() Failed, errno=%d : %s",
188 errno, strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800190}
plars865695b2001-08-27 22:15:12 +0000191
192/*
193 * void
194 * sig_handler(int sig) - Signal catching function.
195 * This function gets executed when the signal SIGALRM is delivered
subrata_modak4bb656a2009-02-26 12:02:09 +0000196 * to the test process after the expiry of alarm time and the signal was
plars865695b2001-08-27 22:15:12 +0000197 * trapped by sigaction() to execute this function.
198 *
199 * This function simply returns without doing anything.
200 */
subrata_modak56207ce2009-03-23 13:35:39 +0000201void sig_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000202{
203}
204
205/*
206 * void
207 * cleanup() - performs all ONE TIME cleanup for this test at
208 * completion or premature exit.
209 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400210void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000211{
plars865695b2001-08-27 22:15:12 +0000212
Chris Dearmanec6edca2012-10-17 19:54:01 -0700213}