Home | History | Annotate | Download | only in settimeofday
      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  * 	settimeofday01.c
     23  *
     24  * DESCRIPTION
     25  *	Testcase to check the basic functionality of settimeofday().
     26  *
     27  * ALGORITHM
     28  *	Setup:
     29  *	  Setup signal handling.
     30  *	  Check that we are the proper user.
     31  *	  Setup expected errnos.
     32  *	  Pause for SIGUSER1 if option specified.
     33  *	  Save the current time values.
     34  *	Loop if the proper options are given.
     35  *	  Call settimeofday and verify the time was changed.
     36  *	  Call settimeofday with invalid Args and verify that the call fails.
     37  *	Cleanup:
     38  *	  Restore the original time values.
     39  *	  Print errno log and/or timing stats if options given.
     40  *
     41  * USAGE:  <for command-line>
     42  *	settimeofday01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     43  *	where,  -c n : Run n copies concurrently.
     44  *		-e   : Turn on errno logging.
     45  *		-i n : Execute test n times.
     46  *		-I x : Execute test for x seconds.
     47  *		-P x : Pause for x seconds between iterations.
     48  *		-t   : Turn on syscall timing.
     49  *
     50  * History
     51  *	07/2001 John George
     52  *		-Ported
     53  *
     54  * Restrictions
     55  * 	Must be run as root.
     56  */
     57 
     58 #include <sys/time.h>
     59 #include <errno.h>
     60 #include <unistd.h>
     61 #include "test.h"
     62 
     63 #define	FAILED		1
     64 #define	VAL_SEC		100
     65 #define	VAL_MSEC	100
     66 #define ACCEPTABLE_DELTA	500	/* in milli-seconds */
     67 
     68 char *TCID = "settimeofday01";
     69 int TST_TOTAL = 1;
     70 time_t save_tv_sec, save_tv_usec;
     71 struct timeval tp, tp1, tp2;
     72 
     73 void setup(void);
     74 void cleanup(void);
     75 
     76 #if !defined(UCLINUX)
     77 
     78 int main(int argc, char **argv)
     79 {
     80 	int lc;
     81 	suseconds_t delta;
     82 
     83 	tst_parse_opts(argc, argv, NULL, NULL);
     84 
     85 	setup();
     86 
     87 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     88 		int condition_number = 1;
     89 		/* reset tst_count in case we are looping */
     90 		tst_count = 0;
     91 
     92 		gettimeofday(&tp, NULL);
     93 		tp.tv_sec += VAL_SEC;
     94 		tp.tv_usec += VAL_MSEC;
     95 
     96 		TEST(settimeofday(&tp, NULL));
     97 		if (TEST_RETURN == -1) {
     98 			tst_resm(TFAIL, "Error Setting Time, errno=%d",
     99 				 TEST_ERRNO);
    100 		}
    101 
    102 		if ((gettimeofday(&tp2, (struct timezone *)&tp1)) == -1) {
    103 			tst_resm(TBROK, "Error Getting Time, errno=%d", errno);
    104 		}
    105 
    106 		if (tp2.tv_sec > tp.tv_sec) {
    107 			delta =
    108 			    (suseconds_t) (tp2.tv_sec - tp.tv_sec) * 1000 +
    109 			    (tp2.tv_usec - tp.tv_usec) / 1000;
    110 		} else {
    111 			delta =
    112 			    (suseconds_t) (tp.tv_sec - tp2.tv_sec) * 1000 +
    113 			    (tp.tv_usec - tp2.tv_usec) / 1000;
    114 		}
    115 
    116 		if (delta > -ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA) {
    117 			tst_resm(TPASS, "Test condition %d successful",
    118 				 condition_number++);
    119 		} else {
    120 			tst_resm(TFAIL, "Test condition %d failed",
    121 				 condition_number++);
    122 		}
    123 
    124 		/* Invalid Args : Error Condition where tp = NULL */
    125 		TEST(settimeofday((struct timeval *)-1, NULL));
    126 		if (TEST_RETURN == -1) {
    127 			tst_resm(TPASS, "Test condition %d successful",
    128 				 condition_number++);
    129 		} else {
    130 			tst_resm(TFAIL, "Test condition %d failed",
    131 				 condition_number);
    132 		}
    133 
    134 	}
    135 	cleanup();
    136 	tst_exit();
    137 
    138 }
    139 
    140 #else
    141 
    142 int main(void)
    143 {
    144 	tst_resm(TINFO, "test is not available on uClinux");
    145 	tst_exit();
    146 }
    147 
    148 #endif /* if !defined(UCLINUX) */
    149 
    150 /*
    151  * setup()
    152  *	performs all ONE TIME setup for this test
    153  */
    154 void setup(void)
    155 {
    156 	tst_require_root();
    157 
    158 	tst_sig(FORK, DEF_HANDLER, cleanup);
    159 
    160 	/* Pause if that option was specified
    161 	 * TEST_PAUSE contains the code to fork the test with the -c option.
    162 	 */
    163 	TEST_PAUSE;
    164 
    165 	/* Save the current time values */
    166 	if ((gettimeofday(&tp, (struct timezone *)&tp1)) == -1) {
    167 		tst_brkm(TBROK, cleanup, "gettimeofday failed. "
    168 			 "errno=%d", errno);
    169 	}
    170 	save_tv_sec = tp.tv_sec;
    171 	save_tv_usec = tp.tv_usec;
    172 }
    173 
    174 /*
    175  * cleanup()
    176  *	performs all ONE TIME cleanup for this test at
    177  *	completion or premature exit
    178  */
    179 void cleanup(void)
    180 {
    181 	/* restore the original time values. */
    182 	tp.tv_sec = save_tv_sec;
    183 	tp.tv_usec = save_tv_usec;
    184 	if ((settimeofday(&tp, NULL)) == -1) {
    185 		tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
    186 		tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
    187 	}
    188 
    189 }
    190