blob: 53827af3572209b221143335cd0b3cd524bd14a5 [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: nanosleep03
22 *
23 * Test Description:
24 * Verify that nanosleep() will fail to suspend the execution
25 * of a process for a specified time if interrupted by a non-blocked signal.
26 *
27 * Expected Result:
28 * nanosleep() should return with -1 value and sets errno to EINTR.
29 *
30 * Algorithm:
31 * Setup:
32 * Setup signal handling.
33 * Pause for SIGUSR1 if option specified.
34 *
35 * Test:
36 * Loop if the proper options are given.
37 * Execute system call
38 * Check return code, if system call failed (return=-1)
39 * if errno set == expected errno
40 * Issue sys call fails with expected return value and errno.
41 * Otherwise,
42 * Issue sys call fails with unexpected errno.
43 * Otherwise,
44 * Issue sys call returns unexpected value.
45 *
46 * Cleanup:
47 * Print errno log and/or timing stats if options given
48 *
49 * Usage: <for command-line>
50 * nanosleep03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
51 * where, -c n : Run n copies concurrently.
52 * -e : Turn on errno logging.
53 * -i n : Execute test n times.
54 * -I x : Execute test for x seconds.
55 * -P x : Pause for x seconds between iterations.
56 * -t : Turn on syscall timing.
57 *
58 * HISTORY
59 * 07/2001 Ported by Wayne Boyer
60 *
61 * RESTRICTIONS:
62 * None.
63 */
64#include <errno.h>
65#include <unistd.h>
66#include <fcntl.h>
robbiew01be3332003-03-26 23:48:41 +000067#include <time.h>
68#include <wait.h>
plars865695b2001-08-27 22:15:12 +000069
70#include "test.h"
plars865695b2001-08-27 22:15:12 +000071
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020072char *TCID = "nanosleep03";
73int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000074
Cyril Hrubis605fa332015-02-04 13:11:20 +010075struct timespec timereq;
76struct timespec timerem;
plars865695b2001-08-27 22:15:12 +000077
robbiewd34d5812005-07-11 22:28:09 +000078void do_child(); /* Child process */
plars865695b2001-08-27 22:15:12 +000079void setup(); /* Main setup function of test */
80void cleanup(); /* cleanup function for the test */
81void sig_handler(); /* signal catching function */
82
subrata_modak56207ce2009-03-23 13:35:39 +000083int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
plars865695b2001-08-27 22:15:12 +000087 pid_t cpid; /* Child process id */
88 int status; /* child exit status */
subrata_modak56207ce2009-03-23 13:35:39 +000089
Garrett Coopera9e49f12010-12-16 10:54:03 -080090 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +000092#ifdef UCLINUX
93 maybe_run_child(&do_child, "dddd", &timereq.tv_sec, &timereq.tv_nsec,
94 &timerem.tv_sec, &timerem.tv_nsec);
95#endif
96
plars865695b2001-08-27 22:15:12 +000097 setup();
98
plars865695b2001-08-27 22:15:12 +000099 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000102
103 /*
104 * Creat a child process and suspend its
105 * execution using nanosleep()
106 */
vapierb5ed1f62006-08-24 04:16:32 +0000107 if ((cpid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000108 tst_brkm(TBROK, cleanup, "fork() failed");
109 }
110
subrata_modak56207ce2009-03-23 13:35:39 +0000111 if (cpid == 0) { /* Child process */
robbiewd34d5812005-07-11 22:28:09 +0000112#ifdef UCLINUX
113 if (self_exec(av[0], "dddd",
114 timereq.tv_sec, timereq.tv_nsec,
115 timerem.tv_sec, timerem.tv_nsec) < 0) {
116 tst_brkm(TBROK, cleanup, "self_exec failed");
plars865695b2001-08-27 22:15:12 +0000117 }
robbiewd34d5812005-07-11 22:28:09 +0000118#else
119 do_child();
120#endif
plars865695b2001-08-27 22:15:12 +0000121 }
122
123 /* wait for child to time slot for execution */
124 sleep(1);
125
126 /* Now send signal to child */
127 if (kill(cpid, SIGINT) < 0) {
128 tst_brkm(TBROK, cleanup,
129 "kill() fails send signal to child");
130 }
131
132 /* Wait for child to execute */
133 wait(&status);
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000134 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
135 tst_resm(TPASS, "nanosleep() failed, interrupted"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 " by signal (%d) as expected", EINTR);
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000137 } else {
138 tst_resm(TFAIL, "child process exited abnormally; "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 "status = %d", status);
plars865695b2001-08-27 22:15:12 +0000140 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800141 }
plars865695b2001-08-27 22:15:12 +0000142
plars865695b2001-08-27 22:15:12 +0000143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
plars865695b2001-08-27 22:15:12 +0000145
Garrett Cooper2c282152010-12-16 00:55:50 -0800146}
plars865695b2001-08-27 22:15:12 +0000147
148/*
robbiewd34d5812005-07-11 22:28:09 +0000149 * do_child()
150 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400151void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000152{
subrata_modak4bb656a2009-02-26 12:02:09 +0000153 /*
robbiewd34d5812005-07-11 22:28:09 +0000154 * Call nanosleep() to suspend child process
155 * for specified time 'tv_sec'.
156 * Call should return before suspending execution
157 * for the specified time due to receipt of signal
158 * from Parent.
159 */
160 TEST(nanosleep(&timereq, &timerem));
161
robbiewd34d5812005-07-11 22:28:09 +0000162 if (TEST_RETURN == -1) {
163
robbiewd34d5812005-07-11 22:28:09 +0000164 /* Check for expected errno is set */
165 if (TEST_ERRNO != EINTR) {
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000166 tst_resm(TFAIL | TTERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 "nanosleep() failed; expected errno: %d",
168 EINTR);
robbiewd34d5812005-07-11 22:28:09 +0000169 exit(1);
170 }
171 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000172 tst_resm(TFAIL, "nanosleep() returns %ld, "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 "expected -1, errno:%d", TEST_RETURN, EINTR);
robbiewd34d5812005-07-11 22:28:09 +0000174 exit(1);
175 }
176
177 /* Everything is fine, exit normally */
178 exit(0);
179}
180
181/*
plars865695b2001-08-27 22:15:12 +0000182 * setup() - performs all ONE TIME setup for this test.
183 * Setup signal handler to catch the interrupt signal sent by parent
184 * to child process.
185 * Initialise time structure elements.
186 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400187void setup(void)
plars865695b2001-08-27 22:15:12 +0000188{
Garrett Cooper2c282152010-12-16 00:55:50 -0800189
plars865695b2001-08-27 22:15:12 +0000190 tst_sig(FORK, DEF_HANDLER, cleanup);
191
plars865695b2001-08-27 22:15:12 +0000192 TEST_PAUSE;
193
194 /* Setup signal handler */
195 if (signal(SIGINT, sig_handler) == SIG_ERR) {
196 tst_brkm(TBROK, cleanup,
197 "signal() fails to setup signal handler");
198 }
199
200 /* Initialise time variables which used to suspend child execution */
201 timereq.tv_sec = 5;
202 timereq.tv_nsec = 9999;
203
204}
205
206/*
207 * sig_handler() - signal catching function.
208 * This function gets executed when a parnet sends a signal 'SIGINT'
subrata_modak4bb656a2009-02-26 12:02:09 +0000209 * to child to awake it from sleep.
plars865695b2001-08-27 22:15:12 +0000210 * This function just returns without doing anything.
211 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400212void sig_handler(void)
plars865695b2001-08-27 22:15:12 +0000213{
214}
215
216/*
217 * cleanup() - performs all ONE TIME cleanup for this test at
218 * completion or premature exit.
219 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400220void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000221{
plars865695b2001-08-27 22:15:12 +0000222
Chris Dearmanec6edca2012-10-17 19:54:01 -0700223}