blob: f109f8b63a875b58ed556f5e20349f6a259caf41 [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: nanosleep04
22 *
23 * Test Description:
24 * Verify that nanosleep() will fail to suspend the execution
25 * of a process if the specified pause time is invalid.
26 *
27 * Expected Result:
28 * nanosleep() should return with -1 value and sets errno to EINVAL.
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 * nanosleep04 [-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 <wait.h>
68#include <time.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 = "nanosleep04";
73int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000074
Cyril Hrubis605fa332015-02-04 13:11:20 +010075struct timespec timereq;
plars865695b2001-08-27 22:15:12 +000076
77void setup(); /* Main setup function of test */
78void cleanup(); /* cleanup function for the test */
79
subrata_modak56207ce2009-03-23 13:35:39 +000080int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000081{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020082 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020083 const char *msg;
plars865695b2001-08-27 22:15:12 +000084 pid_t cpid; /* Child process id */
85 int status; /* child exit status */
subrata_modak56207ce2009-03-23 13:35:39 +000086
Garrett Coopera9e49f12010-12-16 10:54:03 -080087 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080088 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000089
plars865695b2001-08-27 22:15:12 +000090 setup();
91
plars865695b2001-08-27 22:15:12 +000092 for (lc = 0; TEST_LOOPING(lc); lc++) {
93
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000095
96 /*
97 * Creat a child process and suspend its
98 * execution using nanosleep()
99 */
robbiewd34d5812005-07-11 22:28:09 +0000100 if ((cpid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, cleanup,
102 "fork() fails to create child process");
103 }
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 if (cpid == 0) { /* Child process */
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 /*
plars865695b2001-08-27 22:15:12 +0000107 * Call nanosleep() to suspend child process
108 * for specified time 'tv_sec'.
109 */
110 TEST(nanosleep(&timereq, NULL));
111
plars865695b2001-08-27 22:15:12 +0000112 if (TEST_RETURN == -1) {
113
plars865695b2001-08-27 22:15:12 +0000114 /* Check for expected errno is set */
115 if (TEST_ERRNO != EINVAL) {
116 tst_resm(TFAIL, "nanosleep() failed, "
117 "errno:%d, expected:%d",
118 TEST_ERRNO, EINVAL);
119 exit(1);
120 }
121 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000122 tst_resm(TFAIL, "nanosleep() returns %ld, "
plars865695b2001-08-27 22:15:12 +0000123 "expected -1, errno:%d",
124 TEST_RETURN, EINVAL);
125 exit(1);
126 }
127
128 /* Everything is fine, exit normally */
129 exit(0);
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, when provided "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 "invalid pause time, with expected "
137 "errno: %d", EINVAL);
plars865695b2001-08-27 22:15:12 +0000138 } else if (WEXITSTATUS(status) == 1) {
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000139 tst_resm(TFAIL, "child process exited abnormally; "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140 "status = %d", status);
plars865695b2001-08-27 22:15:12 +0000141 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800142 }
plars865695b2001-08-27 22:15:12 +0000143
plars865695b2001-08-27 22:15:12 +0000144 cleanup();
145
Garrett Coopera9e49f12010-12-16 10:54:03 -0800146 tst_exit();
147
Garrett Cooper2c282152010-12-16 00:55:50 -0800148}
plars865695b2001-08-27 22:15:12 +0000149
150/*
151 * setup() - performs all ONE TIME setup for this test.
subrata_modak4bb656a2009-02-26 12:02:09 +0000152 * Initialise time structure elements, such that pause time
plars865695b2001-08-27 22:15:12 +0000153 * points to -ve value.
154 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155void setup(void)
plars865695b2001-08-27 22:15:12 +0000156{
Garrett Cooper2c282152010-12-16 00:55:50 -0800157
plars865695b2001-08-27 22:15:12 +0000158 tst_sig(FORK, DEF_HANDLER, cleanup);
159
plars865695b2001-08-27 22:15:12 +0000160 TEST_PAUSE;
161
162 /* Initialise time variables which used to suspend child execution */
163 timereq.tv_sec = -5;
164 timereq.tv_nsec = 9999;
165
166}
167
168/*
169 * cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
plars865695b2001-08-27 22:15:12 +0000174
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}