blob: e3e3d1933da8ecb76ba75719aaddf61812de81f6 [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: nanosleep01
22 *
23 * Test Description:
24 * Verify that nanosleep() will be successful to suspend the execution
25 * of a process for a specified time.
26 *
27 * Expected Result:
28 * nanosleep() should return with value 0 and the process should be
29 * suspended for time specified by timespec structure.
30 *
31 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Pause for SIGUSR1 if option specified.
35 *
36 * Test:
37 * Loop if the proper options are given.
38 * Execute system call
39 * Check return code, if system call failed (return=-1)
robbiew2d950e42003-03-28 18:57:31 +000040 * Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000041 * Otherwise,
subrata_modak4bb656a2009-02-26 12:02:09 +000042 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000043 * if successful,
robbiew2d950e42003-03-28 18:57:31 +000044 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000045 * Otherwise,
robbiew2d950e42003-03-28 18:57:31 +000046 * Issue Functionality-Fail message.
plars865695b2001-08-27 22:15:12 +000047 * Cleanup:
48 * Print errno log and/or timing stats if options given
49 *
50 * Usage: <for command-line>
51 * nanosleep01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
52 * where, -c n : Run n copies concurrently.
53 * -f : Turn off functionality Testing.
robbiew2d950e42003-03-28 18:57:31 +000054 * -i n : Execute test n times.
55 * -I x : Execute test for x seconds.
56 * -P x : Pause for x seconds between iterations.
57 * -t : Turn on syscall timing.
plars865695b2001-08-27 22:15:12 +000058 *
59 * HISTORY
robbiew2d950e42003-03-28 18:57:31 +000060 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000061 *
62 * RESTRICTIONS:
63 * None.
64 *
65 */
66#include <errno.h>
67#include <unistd.h>
68#include <fcntl.h>
69#include <time.h>
robbiew2d950e42003-03-28 18:57:31 +000070#include <sys/time.h>
plars865695b2001-08-27 22:15:12 +000071#include <sys/wait.h>
subrata_modak923b23f2009-11-02 13:57:16 +000072#include <stdint.h>
plars865695b2001-08-27 22:15:12 +000073
74#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020076char *TCID = "nanosleep01";
77int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079struct timespec timereq; /* time struct. buffer for nanosleep() */
robbiew2d950e42003-03-28 18:57:31 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081void setup(); /* Main setup function of test */
82void cleanup(); /* cleanup function for the test */
plars865695b2001-08-27 22:15:12 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000085{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000088 pid_t cpid; /* Child process id */
89 struct timeval otime; /* time before child execution suspended */
90 struct timeval ntime; /* time after child resumes execution */
91 int retval = 0, e_code, status;
plars865695b2001-08-27 22:15:12 +000092
Garrett Cooper45e285d2010-11-22 12:19:25 -080093 msg = parse_opts(ac, av, NULL, NULL);
94 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +000095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080096
subrata_modak56207ce2009-03-23 13:35:39 +000097 }
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 setup();
plars865695b2001-08-27 22:15:12 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000102
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000104
subrata_modak56207ce2009-03-23 13:35:39 +0000105 /*
106 * Creat a child process and suspend it till
107 * time specified by timespec struct element
108 * time_t tv_sec.
109 */
110 cpid = FORK_OR_VFORK();
111 if (cpid == -1) {
112 tst_brkm(TBROK, cleanup, "fork() failed");
113 }
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 if (cpid == 0) { /* Child process */
116 /* Note down the current time */
117 gettimeofday(&otime, 0);
118 /*
119 * Call nanosleep() to suspend child process
120 * for specified time.
121 */
122 TEST(nanosleep(&timereq, NULL));
plars865695b2001-08-27 22:15:12 +0000123
subrata_modak56207ce2009-03-23 13:35:39 +0000124 /* time after child resumes execution */
125 gettimeofday(&ntime, 0);
plars865695b2001-08-27 22:15:12 +0000126
subrata_modak56207ce2009-03-23 13:35:39 +0000127 if (TEST_RETURN == -1) {
128 retval = 1;
129 tst_resm(TFAIL,
130 "nanosleep() failed, errno=%d : %s",
131 TEST_ERRNO, strerror(TEST_ERRNO));
132 continue;
133 }
plars865695b2001-08-27 22:15:12 +0000134
subrata_modak56207ce2009-03-23 13:35:39 +0000135 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200136 * Verify whether child execution was
137 * actually suspended to desired interval.
subrata_modak56207ce2009-03-23 13:35:39 +0000138 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200139 long want_ms, got_ms;
140 want_ms =
141 timereq.tv_sec * 1000 +
142 timereq.tv_nsec / 1000000;
143 got_ms =
144 ntime.tv_sec * 1000 + ntime.tv_usec / 1000;
145 got_ms -=
146 otime.tv_sec * 1000 + otime.tv_usec / 1000;
147 if (got_ms < want_ms) {
148 retval = 1;
149 tst_resm(TFAIL, "Child execution not "
150 "suspended for %jd seconds. (Wanted %ld ms, got %ld ms)",
151 (intmax_t) timereq.tv_sec,
152 want_ms, got_ms);
subrata_modak56207ce2009-03-23 13:35:39 +0000153 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200154 tst_resm(TPASS, "nanosleep "
155 "functionality is correct");
subrata_modak56207ce2009-03-23 13:35:39 +0000156 }
157 exit(retval);
158 } else { /* parent process */
159 /* wait for the child to finish */
160 wait(&status);
161 /* make sure the child returned a good exit status */
162 e_code = status >> 8;
163 if (e_code != 0) {
164 tst_resm(TFAIL, "Failures reported above");
165 }
166 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800167 }
plars865695b2001-08-27 22:15:12 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800170 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000171
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
plars865695b2001-08-27 22:15:12 +0000173
174/*
175 * setup() - performs all ONE TIME setup for this test.
robbiew2d950e42003-03-28 18:57:31 +0000176 * Initialize time structure elements.
plars865695b2001-08-27 22:15:12 +0000177 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400178void setup(void)
plars865695b2001-08-27 22:15:12 +0000179{
Garrett Cooper2c282152010-12-16 00:55:50 -0800180
subrata_modak56207ce2009-03-23 13:35:39 +0000181 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 /* Initialise time variables which used to suspend child execution */
186 timereq.tv_sec = 2;
187 timereq.tv_nsec = 9999;
plars865695b2001-08-27 22:15:12 +0000188}
189
plars865695b2001-08-27 22:15:12 +0000190/*
191 * cleanup() - performs all ONE TIME cleanup for this test at
192 * completion or premature exit.
193 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400194void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000195{
plars865695b2001-08-27 22:15:12 +0000196
Chris Dearmanec6edca2012-10-17 19:54:01 -0700197}