plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * setitimer01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * setitimer01 - check that a resonable setitimer() call succeeds. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * loop if that option was specified |
| 29 | * allocate needed space and set up needed values |
| 30 | * issue the system call |
| 31 | * check the errno value |
| 32 | * issue a PASS message if we get zero |
| 33 | * otherwise, the tests fails |
| 34 | * issue a FAIL message |
| 35 | * break any remaining tests |
| 36 | * call cleanup |
| 37 | * |
| 38 | * USAGE: <for command-line> |
| 39 | * setitimer01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 40 | * where, -c n : Run n copies concurrently. |
| 41 | * -f : Turn off functionality Testing. |
| 42 | * -i n : Execute test n times. |
| 43 | * -I x : Execute test for x seconds. |
| 44 | * -P x : Pause for x seconds between iterations. |
| 45 | * -t : Turn on syscall timing. |
| 46 | * |
| 47 | * HISTORY |
| 48 | * 03/2001 - Written by Wayne Boyer |
| 49 | * |
| 50 | * RESTRICTIONS |
| 51 | * none |
| 52 | */ |
| 53 | |
| 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #include <errno.h> |
| 57 | #include <sys/time.h> |
| 58 | |
| 59 | void cleanup(void); |
| 60 | void setup(void); |
| 61 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | char *TCID = "setitimer01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | #define SEC0 0 |
| 66 | #define SEC1 20 |
| 67 | #define SEC2 40 |
| 68 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | struct itimerval *value, *ovalue; |
| 74 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 75 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 76 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | /* The following loop checks looping state if -i option given */ |
| 82 | |
| 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 84 | /* reset tst_count in case we are looping */ |
| 85 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | /* allocate some space for the timer structures */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 88 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 89 | if ((value = malloc((size_t)sizeof(struct itimerval))) == |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | tst_brkm(TBROK, cleanup, "value malloc failed"); |
| 92 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 93 | |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 94 | if ((ovalue = malloc((size_t)sizeof(struct itimerval))) == |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | tst_brkm(TBROK, cleanup, "ovalue malloc failed"); |
| 97 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | /* set up some reasonable values */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | value->it_value.tv_sec = SEC1; |
| 102 | value->it_value.tv_usec = SEC0; |
mreed10 | 41509cd | 2006-03-22 01:32:44 +0000 | [diff] [blame] | 103 | value->it_interval.tv_sec = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 104 | value->it_interval.tv_usec = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | /* |
| 106 | * issue the system call with the TEST() macro |
| 107 | * ITIMER_REAL = 0, ITIMER_VIRTUAL = 1 and ITIMER_PROF = 2 |
| 108 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | TEST(setitimer(ITIMER_REAL, value, ovalue)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | if (TEST_RETURN != 0) { |
| 113 | tst_resm(TFAIL, "call failed - errno = %d - %s", |
| 114 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 115 | continue; |
| 116 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 117 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 118 | /* |
| 119 | * call setitimer again with new values. |
| 120 | * the old values should be stored in ovalue |
| 121 | */ |
| 122 | value->it_value.tv_sec = SEC2; |
| 123 | value->it_value.tv_usec = SEC0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | if ((setitimer(ITIMER_REAL, value, ovalue)) == -1) { |
| 126 | tst_brkm(TBROK, cleanup, "second setitimer " |
| 127 | "call failed"); |
| 128 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | if (ovalue->it_value.tv_sec <= SEC1) { |
| 131 | tst_resm(TPASS, "functionality is correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | tst_brkm(TFAIL, cleanup, "old timer value is " |
| 134 | "not equal to expected value"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 139 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
| 143 | * setup() - performs all the ONE TIME setup for this test. |
| 144 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 149 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | TEST_PAUSE; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 155 | * or premature exit. |
| 156 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 160 | } |