Home | History | Annotate | Download | only in 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 nanosleep() causes the current thread to be suspended
      9  * until a signal whose action is to invoke a signal handling function
     10  * is received.
     11  */
     12 #include <stdio.h>
     13 #include <time.h>
     14 #include <signal.h>
     15 #include <unistd.h>
     16 #include <sys/wait.h>
     17 #include "posixtest.h"
     18 
     19 void handler(int signo)
     20 {
     21 	printf("In handler\n");
     22 }
     23 
     24 int main(void)
     25 {
     26 	struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
     27 	int sleepsec = 30;
     28 	int pid;
     29 	struct sigaction act;
     30 
     31 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
     32 		perror("Error in clock_gettime()\n");
     33 		return PTS_UNRESOLVED;
     34 	}
     35 
     36 	if ((pid = fork()) == 0) {
     37 		/* child here */
     38 
     39 		act.sa_handler = handler;
     40 		act.sa_flags = 0;
     41 		if (sigemptyset(&act.sa_mask) == -1) {
     42 			perror("Error calling sigemptyset\n");
     43 			return PTS_UNRESOLVED;
     44 		}
     45 		if (sigaction(SIGABRT, &act, 0) == -1) {
     46 			perror("Error calling sigaction\n");
     47 			return PTS_UNRESOLVED;
     48 		}
     49 		tssleepfor.tv_sec = sleepsec;
     50 		tssleepfor.tv_nsec = 0;
     51 		nanosleep(&tssleepfor, &tsstorage);
     52 	} else {
     53 		/* parent here */
     54 		int i;
     55 
     56 		sleep(1);
     57 
     58 		if (kill(pid, SIGABRT) != 0) {
     59 			printf("Could not raise signal being tested\n");
     60 			return PTS_UNRESOLVED;
     61 		}
     62 
     63 		if (wait(&i) == -1) {
     64 			perror("Error waiting for child to exit\n");
     65 			return PTS_UNRESOLVED;
     66 		}
     67 		if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     68 			perror("Error in clock_gettime()\n");
     69 			return PTS_UNRESOLVED;
     70 		}
     71 
     72 		/*
     73 		 * pass if we slept for less than the (large) sleep time
     74 		 * allotted
     75 		 */
     76 		if ((tsafter.tv_sec - tsbefore.tv_sec) < sleepsec) {
     77 			printf("Test PASSED\n");
     78 			return PTS_PASS;
     79 		} else {
     80 			printf("Slept for too long: %d >= %d\n",
     81 			       (int)tsafter.tv_sec - (int)tsbefore.tv_sec,
     82 			       sleepsec);
     83 			printf("Test FAILED\n");
     84 			return PTS_FAIL;
     85 		}
     86 	}
     87 	return PTS_UNRESOLVED;
     88 }
     89