blob: 8ee22b49e4a38a778eac9636e14cbb8ce69196b6 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis6febe052015-02-19 12:45:27 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
Cyril Hrubis7b858682015-02-24 16:08:44 +01004 * Copyright (C) Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00005 *
Cyril Hrubis6febe052015-02-19 12:45:27 +01006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +000010 *
Cyril Hrubis6febe052015-02-19 12:45:27 +010011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000015 *
Cyril Hrubis6febe052015-02-19 12:45:27 +010016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
plars865695b2001-08-27 22:15:12 +000022 * Test Description:
23 * Verify that nanosleep() will be successful to suspend the execution
24 * of a process, returns after the receipt of a signal and writes the
25 * remaining sleep time into the structure.
plars865695b2001-08-27 22:15:12 +000026 */
subrata_modak56207ce2009-03-23 13:35:39 +000027
plars865695b2001-08-27 22:15:12 +000028#include <errno.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <time.h>
Steven Jackson249f4052016-12-13 16:16:00 +000033#include <sys/wait.h>
robbiew01be3332003-03-26 23:48:41 +000034#include <sys/time.h>
subrata_modak923b23f2009-11-02 13:57:16 +000035#include <stdint.h>
yaberauneyaf00ed7a2009-12-22 17:35:13 +000036#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000037
38#include "test.h"
plars865695b2001-08-27 22:15:12 +000039
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020040char *TCID = "nanosleep02";
41int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000042
Cyril Hrubis6febe052015-02-19 12:45:27 +010043static void do_child(void);
44static void setup(void);
45static void sig_handler();
plars865695b2001-08-27 22:15:12 +000046
robbiew9b919472002-09-10 15:11:44 +000047/*
48 * Define here the "rem" precision in microseconds,
49 * Various implementations will provide different
50 * precisions. The -aa tree provides up to usec precision.
51 * NOTE: all the trees that don't provide a precision of
52 * the order of the microseconds are subject to an userspace
53 * live lock condition with glibc under a flood of signals,
54 * the "rem" field would never change without the increased
55 * usec precision in the -aa tree.
56 */
subrata_modak56207ce2009-03-23 13:35:39 +000057#define USEC_PRECISION 250000 /* Error margin allowed in usec */
robbiew9b919472002-09-10 15:11:44 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000060{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020061 int lc;
Cyril Hrubis6febe052015-02-19 12:45:27 +010062 pid_t cpid;
subrata_modak56207ce2009-03-23 13:35:39 +000063
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010064 tst_parse_opts(ac, av, NULL, NULL);
Cyril Hrubis6febe052015-02-19 12:45:27 +010065
robbiewd34d5812005-07-11 22:28:09 +000066#ifdef UCLINUX
Cyril Hrubis7b858682015-02-24 16:08:44 +010067 maybe_run_child(&do_child, "");
robbiewd34d5812005-07-11 22:28:09 +000068#endif
69
plars865695b2001-08-27 22:15:12 +000070 setup();
71
plars865695b2001-08-27 22:15:12 +000072 for (lc = 0; TEST_LOOPING(lc); lc++) {
73
Caspar Zhangd59a6592013-03-07 14:59:12 +080074 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000075
robbiewd34d5812005-07-11 22:28:09 +000076 if ((cpid = FORK_OR_VFORK()) == -1) {
Cyril Hrubis6febe052015-02-19 12:45:27 +010077 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +000078 "fork() failed to create child process");
79 }
80
Cyril Hrubis6febe052015-02-19 12:45:27 +010081 if (cpid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000082#ifdef UCLINUX
Cyril Hrubis7b858682015-02-24 16:08:44 +010083 if (self_exec(av[0], "")) {
Cyril Hrubis6febe052015-02-19 12:45:27 +010084 tst_brkm(TBROK, NULL, "self_exec failed");
plars865695b2001-08-27 22:15:12 +000085 }
robbiewd34d5812005-07-11 22:28:09 +000086#else
87 do_child();
88#endif
plars865695b2001-08-27 22:15:12 +000089 }
90
91 /* wait for child to time slot for execution */
92 sleep(1);
93
94 /* Now send signal to child */
95 if (kill(cpid, SIGINT) < 0) {
Cyril Hrubis6febe052015-02-19 12:45:27 +010096 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +000097 "kill() fails send signal to child");
98 }
99
Cyril Hrubis7b858682015-02-24 16:08:44 +0100100 tst_record_childstatus(NULL, cpid);
Garrett Cooper2c282152010-12-16 00:55:50 -0800101 }
plars865695b2001-08-27 22:15:12 +0000102
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800103 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800104}
plars865695b2001-08-27 22:15:12 +0000105
Cyril Hrubis6febe052015-02-19 12:45:27 +0100106static void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000107{
Cyril Hrubis6febe052015-02-19 12:45:27 +0100108 struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999};
Cyril Hrubis7b858682015-02-24 16:08:44 +0100109 struct timespec timerem, exp_rem;
Cyril Hrubis6febe052015-02-19 12:45:27 +0100110
Cyril Hrubis7b858682015-02-24 16:08:44 +0100111 tst_timer_start(CLOCK_MONOTONIC);
robbiewd34d5812005-07-11 22:28:09 +0000112 TEST(nanosleep(&timereq, &timerem));
Cyril Hrubis7b858682015-02-24 16:08:44 +0100113 tst_timer_stop();
robbiewd34d5812005-07-11 22:28:09 +0000114
Cyril Hrubis7b858682015-02-24 16:08:44 +0100115 if (tst_timespec_lt(timereq, tst_timer_elapsed())) {
116 tst_resm(TFAIL, "nanosleep() slept more than timereq");
117 return;
118 }
robbiewd34d5812005-07-11 22:28:09 +0000119
Cyril Hrubis7b858682015-02-24 16:08:44 +0100120 exp_rem = tst_timespec_diff(timereq, tst_timer_elapsed());
121
122 if (tst_timespec_abs_diff_us(timerem, exp_rem) > USEC_PRECISION) {
123 tst_resm(TFAIL,
124 "nanosleep() remaining time %llius, expected %llius, diff %llius",
125 tst_timespec_to_us(timerem), tst_timespec_to_us(exp_rem),
126 tst_timespec_abs_diff_us(timerem, exp_rem));
robbiewd34d5812005-07-11 22:28:09 +0000127 } else {
Cyril Hrubis7b858682015-02-24 16:08:44 +0100128 tst_resm(TPASS,
129 "nanosleep() slept for %llius, remaining time difference %llius",
130 tst_timer_elapsed_us(),
131 tst_timespec_abs_diff_us(timerem, exp_rem));
robbiewd34d5812005-07-11 22:28:09 +0000132 }
yaberauneyacba23872010-01-06 06:06:15 +0000133
134 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000135}
136
Cyril Hrubis6febe052015-02-19 12:45:27 +0100137static void setup(void)
plars865695b2001-08-27 22:15:12 +0000138{
Cyril Hrubis6febe052015-02-19 12:45:27 +0100139 tst_sig(FORK, DEF_HANDLER, NULL);
plars865695b2001-08-27 22:15:12 +0000140
Cyril Hrubis7b858682015-02-24 16:08:44 +0100141 tst_timer_check(CLOCK_MONOTONIC);
142
plars865695b2001-08-27 22:15:12 +0000143 TEST_PAUSE;
144
plars865695b2001-08-27 22:15:12 +0000145 if (signal(SIGINT, sig_handler) == SIG_ERR) {
Cyril Hrubis6febe052015-02-19 12:45:27 +0100146 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +0000147 "signal() fails to setup signal handler");
148 }
plars865695b2001-08-27 22:15:12 +0000149}
150
Cyril Hrubis6febe052015-02-19 12:45:27 +0100151static void sig_handler(void)
plars865695b2001-08-27 22:15:12 +0000152{
153}