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 when clock_nanosleep() is interrupted by a signal, rmtp
      9  * contains the amount of time remaining (test with relative sleep).
     10  *
     11  */
     12 #include <stdio.h>
     13 #include <time.h>
     14 #include <signal.h>
     15 #include <unistd.h>
     16 #include <sys/wait.h>
     17 #include <stdlib.h>
     18 #include <errno.h>
     19 #include "posixtest.h"
     20 
     21 #define SLEEPSEC 30
     22 
     23 #define CHILDPASS 1
     24 #define CHILDFAIL 0
     25 
     26 #define OKDELTA 1
     27 
     28 void handler(int signo)
     29 {
     30 	(void) signo;
     31 
     32 	printf("In handler\n");
     33 }
     34 
     35 int main(void)
     36 {
     37 	struct timespec tssleep, tsbefore, tsafter, tsremain;
     38 	int pid;
     39 	struct sigaction act;
     40 
     41 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
     42 		perror("clock_gettime() did not return success\n");
     43 		return PTS_UNRESOLVED;
     44 	}
     45 
     46 	if ((pid = fork()) == 0) {
     47 		/* child here */
     48 		int sleptplusremaining;
     49 
     50 		act.sa_handler = handler;
     51 		act.sa_flags = 0;
     52 		if (sigemptyset(&act.sa_mask) != 0) {
     53 			perror("sigemptyset() did not return success\n");
     54 			return CHILDFAIL;
     55 		}
     56 		if (sigaction(SIGABRT, &act, 0) != 0) {
     57 			perror("sigaction() did not return success\n");
     58 			return CHILDFAIL;
     59 		}
     60 		tssleep.tv_sec = SLEEPSEC;
     61 		tssleep.tv_nsec = 0;
     62 		if (clock_nanosleep(CLOCK_REALTIME, 0,
     63 				    &tssleep, &tsremain) == EINTR) {
     64 			if (clock_gettime(CLOCK_REALTIME, &tsafter) != 0) {
     65 				perror("clock_gettime() failed\n");
     66 				return CHILDFAIL;
     67 			}
     68 			sleptplusremaining =
     69 			    (tsafter.tv_sec - tsbefore.tv_sec) +
     70 			    tsremain.tv_sec;
     71 
     72 			if (abs(sleptplusremaining - SLEEPSEC) <= OKDELTA) {
     73 				printf("PASS - within %d difference\n",
     74 				       abs(sleptplusremaining - SLEEPSEC));
     75 				return CHILDPASS;
     76 			} else {
     77 				printf("FAIL - within %d difference\n",
     78 				       abs(sleptplusremaining - SLEEPSEC));
     79 				return CHILDFAIL;
     80 			}
     81 
     82 			return CHILDFAIL;
     83 		} else {
     84 			printf("clock_nanosleep() did not return EINTR\n");
     85 			return CHILDFAIL;
     86 		}
     87 	} else {
     88 		/* parent here */
     89 		int i;
     90 
     91 		sleep(1);
     92 
     93 		if (kill(pid, SIGABRT) != 0) {
     94 			printf("Could not raise signal being tested\n");
     95 			return PTS_UNRESOLVED;
     96 		}
     97 
     98 		if (wait(&i) == -1) {
     99 			perror("Error waiting for child to exit\n");
    100 			return PTS_UNRESOLVED;
    101 		}
    102 
    103 		if (WIFEXITED(i) && WEXITSTATUS(i)) {
    104 			printf("Child exited normally\n");
    105 			printf("Test PASSED\n");
    106 			return PTS_PASS;
    107 		} else {
    108 			printf("Child did not exit normally.\n");
    109 			printf("Test FAILED\n");
    110 			return PTS_FAIL;
    111 		}
    112 	}
    113 
    114 	return PTS_UNRESOLVED;
    115 }
    116