plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 4 | * Copyright (C) Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 6 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 11 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 16 | * 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 27 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <unistd.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <signal.h> |
| 32 | #include <time.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 33 | #include <sys/wait.h> |
robbiew | 01be333 | 2003-03-26 23:48:41 +0000 | [diff] [blame] | 34 | #include <sys/time.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 36 | #include <inttypes.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
| 38 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 40 | char *TCID = "nanosleep02"; |
| 41 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 43 | static void do_child(void); |
| 44 | static void setup(void); |
| 45 | static void sig_handler(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
robbiew | 9b91947 | 2002-09-10 15:11:44 +0000 | [diff] [blame] | 47 | /* |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | #define USEC_PRECISION 250000 /* Error margin allowed in usec */ |
robbiew | 9b91947 | 2002-09-10 15:11:44 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 61 | int lc; |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 62 | pid_t cpid; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 64 | tst_parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 65 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 66 | #ifdef UCLINUX |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 67 | maybe_run_child(&do_child, ""); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | setup(); |
| 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 73 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 74 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | if ((cpid = FORK_OR_VFORK()) == -1) { |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | "fork() failed to create child process"); |
| 79 | } |
| 80 | |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 81 | if (cpid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 82 | #ifdef UCLINUX |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 83 | if (self_exec(av[0], "")) { |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 84 | tst_brkm(TBROK, NULL, "self_exec failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 86 | #else |
| 87 | do_child(); |
| 88 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | } |
| 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 Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 96 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | "kill() fails send signal to child"); |
| 98 | } |
| 99 | |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 100 | tst_record_childstatus(NULL, cpid); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 103 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 104 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 106 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 107 | { |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 108 | struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999}; |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 109 | struct timespec timerem, exp_rem; |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 110 | |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 111 | tst_timer_start(CLOCK_MONOTONIC); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 112 | TEST(nanosleep(&timereq, &timerem)); |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 113 | tst_timer_stop(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 115 | if (tst_timespec_lt(timereq, tst_timer_elapsed())) { |
| 116 | tst_resm(TFAIL, "nanosleep() slept more than timereq"); |
| 117 | return; |
| 118 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 119 | |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 120 | 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)); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 127 | } else { |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 128 | 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)); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 132 | } |
yaberauneya | cba2387 | 2010-01-06 06:06:15 +0000 | [diff] [blame] | 133 | |
| 134 | tst_exit(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 137 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | { |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 139 | tst_sig(FORK, DEF_HANDLER, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
Cyril Hrubis | 7b85868 | 2015-02-24 16:08:44 +0100 | [diff] [blame] | 141 | tst_timer_check(CLOCK_MONOTONIC); |
| 142 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | TEST_PAUSE; |
| 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | if (signal(SIGINT, sig_handler) == SIG_ERR) { |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 146 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | "signal() fails to setup signal handler"); |
| 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Cyril Hrubis | 6febe05 | 2015-02-19 12:45:27 +0100 | [diff] [blame] | 151 | static void sig_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
| 153 | } |