blob: 50200235c027e40a67a6d22f1bef86db7e96cb95 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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"
71#include "usctest.h"
72
subrata_modak56207ce2009-03-23 13:35:39 +000073char *TCID = "nanosleep04"; /* Test program identifier. */
74int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000075extern int Tst_count; /* Test Case counter for tst_* routines */
76
77struct timespec timereq; /* time struct. buffer for nanosleep() */
78
subrata_modak56207ce2009-03-23 13:35:39 +000079int exp_enos[] = { EINVAL, 0 };
plars865695b2001-08-27 22:15:12 +000080
81void setup(); /* Main setup function of test */
82void cleanup(); /* cleanup function for the test */
83
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000085{
86 int lc; /* loop counter */
87 char *msg; /* message returned from parse_opts */
88 pid_t cpid; /* Child process id */
89 int status; /* child exit status */
subrata_modak56207ce2009-03-23 13:35:39 +000090
plars865695b2001-08-27 22:15:12 +000091 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -080092 msg = parse_opts(ac, av, NULL, NULL);
93 if (msg != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000095 }
96
97 /* Perform global setup for test */
98 setup();
99
100 /* set the expected errnos... */
101 TEST_EXP_ENOS(exp_enos);
102
103 /* Check looping state if -i option given */
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
106 /* Reset Tst_count in case we are looping. */
subrata_modak56207ce2009-03-23 13:35:39 +0000107 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
109 /*
110 * Creat a child process and suspend its
111 * execution using nanosleep()
112 */
robbiewd34d5812005-07-11 22:28:09 +0000113 if ((cpid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000114 tst_brkm(TBROK, cleanup,
115 "fork() fails to create child process");
116 }
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118 if (cpid == 0) { /* Child process */
subrata_modak4bb656a2009-02-26 12:02:09 +0000119 /*
plars865695b2001-08-27 22:15:12 +0000120 * Call nanosleep() to suspend child process
121 * for specified time 'tv_sec'.
122 */
123 TEST(nanosleep(&timereq, NULL));
124
125 /* check return code of nanosleep() */
126 if (TEST_RETURN == -1) {
127
128 TEST_ERROR_LOG(TEST_ERRNO);
129
130 /* Check for expected errno is set */
131 if (TEST_ERRNO != EINVAL) {
132 tst_resm(TFAIL, "nanosleep() failed, "
133 "errno:%d, expected:%d",
134 TEST_ERRNO, EINVAL);
135 exit(1);
136 }
137 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000138 tst_resm(TFAIL, "nanosleep() returns %ld, "
plars865695b2001-08-27 22:15:12 +0000139 "expected -1, errno:%d",
140 TEST_RETURN, EINVAL);
141 exit(1);
142 }
143
144 /* Everything is fine, exit normally */
145 exit(0);
146 }
147
148 /* Wait for child to execute */
149 wait(&status);
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000150 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
151 tst_resm(TPASS, "nanosleep() failed, when provided "
152 "invalid pause time, with expected "
153 "errno: %d", EINVAL);
plars865695b2001-08-27 22:15:12 +0000154 } else if (WEXITSTATUS(status) == 1) {
yaberauneyaf00ed7a2009-12-22 17:35:13 +0000155 tst_resm(TFAIL, "child process exited abnormally; "
156 "status = %d", status);
plars865695b2001-08-27 22:15:12 +0000157 }
subrata_modak56207ce2009-03-23 13:35:39 +0000158 } /* End for TEST_LOOPING */
plars865695b2001-08-27 22:15:12 +0000159
160 /* Call cleanup() to undo setup done for the test. */
161 cleanup();
162
subrata_modak56207ce2009-03-23 13:35:39 +0000163 /*NOTREACHED*/ return 0;
164} /* End main */
plars865695b2001-08-27 22:15:12 +0000165
166/*
167 * setup() - performs all ONE TIME setup for this test.
subrata_modak4bb656a2009-02-26 12:02:09 +0000168 * Initialise time structure elements, such that pause time
plars865695b2001-08-27 22:15:12 +0000169 * points to -ve value.
170 */
subrata_modak56207ce2009-03-23 13:35:39 +0000171void setup()
plars865695b2001-08-27 22:15:12 +0000172{
173 /* capture signals */
174 tst_sig(FORK, DEF_HANDLER, cleanup);
175
176 /* Pause if that option was specified */
177 TEST_PAUSE;
178
179 /* Initialise time variables which used to suspend child execution */
180 timereq.tv_sec = -5;
181 timereq.tv_nsec = 9999;
182
183}
184
185/*
186 * cleanup() - performs all ONE TIME cleanup for this test at
187 * completion or premature exit.
188 */
subrata_modak56207ce2009-03-23 13:35:39 +0000189void cleanup()
plars865695b2001-08-27 22:15:12 +0000190{
191 /*
192 * print timing stats if that option was specified.
193 * print errno log if that option was specified.
194 */
195 TEST_CLEANUP;
196
197 /* exit with return code appropriate for results */
198 tst_exit();
199}