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 invoke a signal handling function
     10  * is received for an absolute nanosleep (same as 1-3.c with
     11  * TIMER_ABSTIME set).
     12  *
     13  * Note:  This test case has some limitations:
     14  * - clock_nanosleep() could not be executed at all, and the test would
     15  *   pass [Hopefully, the sleep() in the parent ensures clock_nanosleep()
     16  *   will be executed.]
     17  * - There is no way of knowing for sure that it was the signal that
     18  *   stopped clock_nanosleep().
     19  */
     20 #include <stdio.h>
     21 #include <time.h>
     22 #include <signal.h>
     23 #include <unistd.h>
     24 #include <sys/wait.h>
     25 #include "posixtest.h"
     26 
     27 #define SLEEPSEC 30
     28 
     29 void handler(int signo)
     30 {
     31 	(void) signo;
     32 
     33 	printf("In handler\n");
     34 }
     35 
     36 int main(void)
     37 {
     38 	struct timespec tssleep, tsbefore, tsafter;
     39 	int pid, sleepuntilsec;
     40 	struct sigaction act;
     41 
     42 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
     43 		perror("clock_gettime() did not return success\n");
     44 		return PTS_UNRESOLVED;
     45 	}
     46 
     47 	sleepuntilsec = tsbefore.tv_sec + SLEEPSEC;
     48 
     49 	if ((pid = fork()) == 0) {
     50 		/* child here */
     51 		int flags = 0;
     52 
     53 		act.sa_handler = handler;
     54 		act.sa_flags = 0;
     55 		if (sigemptyset(&act.sa_mask) != 0) {
     56 			perror("sigemptyset() did not return success\n");
     57 			return PTS_UNRESOLVED;
     58 		}
     59 		if (sigaction(SIGABRT, &act, 0) != 0) {
     60 			perror("sigaction() did not return success\n");
     61 			return PTS_UNRESOLVED;
     62 		}
     63 
     64 		tssleep.tv_sec = sleepuntilsec;
     65 		tssleep.tv_nsec = tsbefore.tv_nsec;
     66 
     67 		flags |= TIMER_ABSTIME;
     68 		clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL);
     69 	} else {
     70 		/* parent here */
     71 		int i;
     72 
     73 		sleep(1);
     74 
     75 		if (kill(pid, SIGABRT) != 0) {
     76 			printf("Could not raise signal being tested\n");
     77 			return PTS_UNRESOLVED;
     78 		}
     79 
     80 		if (wait(&i) == -1) {
     81 			perror("Error waiting for child to exit\n");
     82 			return PTS_UNRESOLVED;
     83 		}
     84 		if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
     85 			perror("clock_gettime() did not return success\n");
     86 			return PTS_UNRESOLVED;
     87 		}
     88 
     89 		/*
     90 		 * pass if we slept for less than the (large) sleep time
     91 		 * allotted
     92 		 */
     93 
     94 		if (tsafter.tv_sec < sleepuntilsec) {
     95 			printf("Test PASSED\n");
     96 			return PTS_PASS;
     97 		} else {
     98 			printf("Slept for too long: %d >= %d\n",
     99 			       (int)tsafter.tv_sec, sleepuntilsec);
    100 			printf("Test FAILED\n");
    101 			return PTS_FAIL;
    102 		}
    103 	}
    104 
    105 	return PTS_UNRESOLVED;
    106 }
    107