Home | History | Annotate | Download | only in setitimer
      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
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     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"
     55 
     56 #include <errno.h>
     57 #include <sys/time.h>
     58 
     59 void cleanup(void);
     60 void setup(void);
     61 
     62 char *TCID = "setitimer01";
     63 int TST_TOTAL = 1;
     64 
     65 #define SEC0	0
     66 #define SEC1	20
     67 #define SEC2	40
     68 
     69 int main(int ac, char **av)
     70 {
     71 	int lc;
     72 	struct itimerval *value, *ovalue;
     73 
     74 	tst_parse_opts(ac, av, NULL, NULL);
     75 
     76 	setup();		/* global setup */
     77 
     78 	/* The following loop checks looping state if -i option given */
     79 
     80 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     81 		/* reset tst_count in case we are looping */
     82 		tst_count = 0;
     83 
     84 		/* allocate some space for the timer structures */
     85 
     86 		if ((value = malloc((size_t)sizeof(struct itimerval))) ==
     87 		    NULL) {
     88 			tst_brkm(TBROK, cleanup, "value malloc failed");
     89 		}
     90 
     91 		if ((ovalue = malloc((size_t)sizeof(struct itimerval))) ==
     92 		    NULL) {
     93 			tst_brkm(TBROK, cleanup, "ovalue malloc failed");
     94 		}
     95 
     96 		/* set up some reasonable values */
     97 
     98 		value->it_value.tv_sec = SEC1;
     99 		value->it_value.tv_usec = SEC0;
    100 		value->it_interval.tv_sec = 0;
    101 		value->it_interval.tv_usec = 0;
    102 		/*
    103 		 * issue the system call with the TEST() macro
    104 		 * ITIMER_REAL = 0, ITIMER_VIRTUAL = 1 and ITIMER_PROF = 2
    105 		 */
    106 
    107 		TEST(setitimer(ITIMER_REAL, value, ovalue));
    108 
    109 		if (TEST_RETURN != 0) {
    110 			tst_resm(TFAIL, "call failed - errno = %d - %s",
    111 				 TEST_ERRNO, strerror(TEST_ERRNO));
    112 			continue;
    113 		}
    114 
    115 		/*
    116 		 * call setitimer again with new values.
    117 		 * the old values should be stored in ovalue
    118 		 */
    119 		value->it_value.tv_sec = SEC2;
    120 		value->it_value.tv_usec = SEC0;
    121 
    122 		if ((setitimer(ITIMER_REAL, value, ovalue)) == -1) {
    123 			tst_brkm(TBROK, cleanup, "second setitimer "
    124 				 "call failed");
    125 		}
    126 
    127 		if (ovalue->it_value.tv_sec <= SEC1) {
    128 			tst_resm(TPASS, "functionality is correct");
    129 		} else {
    130 			tst_brkm(TFAIL, cleanup, "old timer value is "
    131 				 "not equal to expected value");
    132 		}
    133 	}
    134 
    135 	cleanup();
    136 	tst_exit();
    137 }
    138 
    139 /*
    140  * setup() - performs all the ONE TIME setup for this test.
    141  */
    142 void setup(void)
    143 {
    144 
    145 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    146 
    147 	TEST_PAUSE;
    148 }
    149 
    150 /*
    151  * cleanup() - performs all the ONE TIME cleanup for this test at completion
    152  * 	       or premature exit.
    153  */
    154 void cleanup(void)
    155 {
    156 
    157 }
    158