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 terminate the process is received.
     10  */
     11 #include <stdio.h>
     12 #include <time.h>
     13 #include <signal.h>
     14 #include <unistd.h>
     15 #include <sys/wait.h>
     16 #include "posixtest.h"
     17 
     18 int main(void)
     19 {
     20 	struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
     21 	int sleepsec = 30;
     22 	int pid;
     23 
     24 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
     25 		perror("Error in clock_gettime()\n");
     26 		return PTS_UNRESOLVED;
     27 	}
     28 
     29 	if ((pid = fork()) == 0) {
     30 		/* child here */
     31 		tssleepfor.tv_sec = sleepsec;
     32 		tssleepfor.tv_nsec = 0;
     33 		nanosleep(&tssleepfor, &tsstorage);
     34 	} else {
     35 		/* parent here */
     36 		int i;
     37 
     38 		sleep(1);
     39 
     40 		if (kill(pid, SIGABRT) != 0) {
     41 			printf("Could not raise signal being tested\n");
     42 			return PTS_UNRESOLVED;
     43 		}
     44 
     45 		if (wait(&i) == -1) {
     46 			perror("Error waiting for child to exit\n");
     47 			return PTS_UNRESOLVED;
     48 		}
     49 		if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     50 			perror("Error in clock_gettime()\n");
     51 			return PTS_UNRESOLVED;
     52 		}
     53 
     54 		/*
     55 		 * pass if we slept for less than the (large) sleep time
     56 		 * allotted
     57 		 */
     58 		if ((tsafter.tv_sec - tsbefore.tv_sec) < sleepsec) {
     59 			printf("Test PASSED\n");
     60 			return PTS_PASS;
     61 		} else {
     62 			printf("Slept for too long: %d >= %d\n",
     63 			       (int)tsafter.tv_sec - (int)tsbefore.tv_sec,
     64 			       sleepsec);
     65 			printf("Test FAILED\n");
     66 			return PTS_FAIL;
     67 		}
     68 	}
     69 	return PTS_UNRESOLVED;
     70 }
     71