Home | History | Annotate | Download | only in clock_nanosleep
      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_nanosleep() causes the current thread to be suspended
      9  * until a signal whose action is to terminate the process is received
     10  * for an absolute clock.  [Same as test 1-4.c except with TIMER_ABSTIME
     11  * set.]
     12  */
     13 #include <stdio.h>
     14 #include <time.h>
     15 #include <signal.h>
     16 #include <unistd.h>
     17 #include <sys/wait.h>
     18 #include "posixtest.h"
     19 
     20 #define SLEEPSEC 30
     21 
     22 int main(void)
     23 {
     24 	struct timespec tssleep, tsbefore, tsafter;
     25 	int pid, sleepuntilsec;
     26 
     27 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
     28 		perror("clock_gettime() did not return success\n");
     29 		return PTS_UNRESOLVED;
     30 	}
     31 
     32 	sleepuntilsec = tsbefore.tv_sec + SLEEPSEC;
     33 
     34 	if ((pid = fork()) == 0) {
     35 		int flags = 0;
     36 
     37 		/* child here */
     38 		tssleep.tv_sec = sleepuntilsec;
     39 		tssleep.tv_nsec = tsbefore.tv_nsec;
     40 
     41 		flags |= TIMER_ABSTIME;
     42 		clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL);
     43 	} else {
     44 		/* parent here */
     45 		int i;
     46 
     47 		sleep(1);
     48 
     49 		if (kill(pid, SIGABRT) != 0) {
     50 			printf("Could not raise signal being tested\n");
     51 			return PTS_UNRESOLVED;
     52 		}
     53 
     54 		if (wait(&i) == -1) {
     55 			perror("Error waiting for child to exit\n");
     56 			return PTS_UNRESOLVED;
     57 		}
     58 		if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
     59 			perror("clock_gettime() did not return success\n");
     60 			return PTS_UNRESOLVED;
     61 		}
     62 
     63 		/*
     64 		 * pass if we slept for less than the (large) sleep time
     65 		 * allotted
     66 		 */
     67 		if (tsafter.tv_sec < sleepuntilsec) {
     68 			printf("Test PASSED\n");
     69 			return PTS_PASS;
     70 		} else {
     71 			printf("Slept for too long: %d >= %d\n",
     72 			       (int)tsafter.tv_sec, sleepuntilsec);
     73 			printf("Test FAILED\n");
     74 			return PTS_FAIL;
     75 		}
     76 	}
     77 
     78 	return PTS_UNRESOLVED;
     79 }
     80