Home | History | Annotate | Download | only in clock_settime
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  *
      8  * Test that clock_settime() does not affect clock_nanosleep()
      9  * relative timers expiration times.
     10  *
     11  * Steps:
     12  * - get time T0
     13  * - in child:  set clock_nanosleep() to sleep for SLEEPSEC seconds
     14  * - in parent:  sleep SMALLTIME (< SLEEPSEC)
     15  * - in parent:  set time back to T0
     16  * - in child:  ensure time when clock_nanosleep() expires is within
     17  *   ACCEPTABLEDELTA of T0+(SLEEPSEC-SMALLTIME)
     18  */
     19 #include <stdio.h>
     20 #include <time.h>
     21 #include <signal.h>
     22 #include <unistd.h>
     23 #include <sys/wait.h>
     24 #include "posixtest.h"
     25 #include "helpers.h"
     26 
     27 #define SLEEPSEC 5
     28 #define SMALLTIME 2
     29 #define ACCEPTABLEDELTA 1
     30 
     31 #define CHILDPASS 1
     32 #define CHILDFAIL 0
     33 
     34 int main(void)
     35 {
     36 	struct timespec tsT0, tssleep;
     37 	int pid;
     38 
     39 	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
     40 	if (getuid() != 0) {
     41 		printf("Run this test as ROOT, not as a Regular User\n");
     42 		return PTS_UNTESTED;
     43 	}
     44 
     45 	if (clock_gettime(CLOCK_REALTIME, &tsT0) != 0) {
     46 		perror("clock_gettime() did not return success\n");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if ((pid = fork()) == 0) {
     51 		/* child here */
     52 		struct timespec tsend;
     53 		int expectedsec;
     54 
     55 		tssleep.tv_sec = SLEEPSEC;
     56 		tssleep.tv_nsec = 0;
     57 
     58 		if (clock_nanosleep(CLOCK_REALTIME, 0, &tssleep, NULL) != 0) {
     59 			printf("clock_nanosleep() did not return success\n");
     60 			return CHILDFAIL;
     61 		}
     62 
     63 		if (clock_gettime(CLOCK_REALTIME, &tsend) != 0) {
     64 			perror("clock_gettime() did not return success\n");
     65 			return CHILDFAIL;
     66 		}
     67 
     68 		expectedsec = tsT0.tv_sec + (SLEEPSEC - SMALLTIME);
     69 
     70 		if (tsend.tv_sec >= expectedsec) {
     71 			if ((tsend.tv_sec - expectedsec) <= ACCEPTABLEDELTA) {
     72 				return CHILDPASS;
     73 			} else {
     74 				printf("Ended too late.  %d >> %d\n",
     75 				       (int)tsend.tv_sec, (int)expectedsec);
     76 				return CHILDFAIL;
     77 			}
     78 		} else {
     79 			printf("Did not sleep for long enough %d < %d\n",
     80 			       (int)tsend.tv_sec, (int)expectedsec);
     81 			return CHILDFAIL;
     82 		}
     83 
     84 		return CHILDFAIL;
     85 	} else {
     86 		/* parent here */
     87 		int i;
     88 		struct timespec tsreset;
     89 
     90 		sleep(SMALLTIME);
     91 
     92 		if (clock_settime(CLOCK_REALTIME, &tsT0) != 0) {
     93 			printf("clock_settime() did not return success\n");
     94 			return PTS_UNRESOLVED;
     95 		}
     96 
     97 		if (wait(&i) == -1) {
     98 			perror("Error waiting for child to exit\n");
     99 			return PTS_UNRESOLVED;
    100 		}
    101 
    102 		getBeforeTime(&tsreset);	// get current time
    103 		tsreset.tv_sec += SMALLTIME;
    104 		setBackTime(tsreset);
    105 
    106 		if (WIFEXITED(i) && WEXITSTATUS(i)) {
    107 			printf("Test PASSED\n");
    108 			return PTS_PASS;
    109 		} else {
    110 			printf("Test FAILED\n");
    111 			return PTS_FAIL;
    112 		}
    113 	}
    114 
    115 	return PTS_UNRESOLVED;
    116 }
    117