Home | History | Annotate | Download | only in sigtimedwait
      1 /*
      2  * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
      3  * Created by:  salwan.searty 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 if the signal specified by set does not become pending,
      9     and the timespec structure pointed to by timeout is zero-valued,
     10     the sigtimedwait() function shall return immediately with an error.
     11 
     12     NOTE: This program has commented out areas. The commented out functionality
     13     sets a timer in case sigtimedwait() never returns, to help the program
     14     from hanging. To make this program
     15     runnable on a typical system, I've commented out the timer functionality
     16     by default. However, if you do have a timers implementation on your
     17     system, then it is recommened that you uncomment the timers-related lines
     18     of code in this program.
     19 
     20     Steps:
     21     1. Register signal TIMERSIGNAL with the handler myhandler
     22    (2.)Create and set a timer that expires in TIMERSEC seconds incase sigtimedwait()
     23        never returns.
     24     3. Obtain time1.
     25     4. Call sigtimedwait() to wait for non-pending signal SIGTOTEST for SIGTIMEDWAITSEC
     26        seconds.
     27     5. Obtain time2, and find the difference between time2 and time1.
     28     6. Verify that (time2-time1) is equal to SIGTIMEDWAITSEC within a reasonable
     29        error margin.
     30  */
     31 
     32 #define _XOPEN_SOURCE 600
     33 #define _XOPEN_REALTIME 1
     34 
     35 #define TIMERSIGNAL SIGUSR1
     36 #define SIGTOTEST SIGUSR2
     37 #define TIMERSEC 2
     38 #define SIGTIMEDWAITSEC 0
     39 #define ERRORMARGIN 0.1
     40 
     41 #include <signal.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <time.h>
     45 #include <unistd.h>
     46 #include <sys/time.h>
     47 #include <sys/wait.h>
     48 #include "posixtest.h"
     49 
     50 void myhandler(int signo)
     51 {
     52 	printf
     53 	    ("Test FAILED: %d seconds have elapsed and sigtimedwait() has not yet returned.\n",
     54 	     TIMERSEC);
     55 	exit(PTS_FAIL);
     56 }
     57 
     58 int main(void)
     59 {
     60 	struct sigaction act;
     61 
     62 	struct timeval time1, time2;
     63 	double time_elapsed;
     64 
     65 	sigset_t selectset;
     66 	struct timespec ts;
     67 /*
     68 	struct sigevent ev;
     69 	timer_t tid;
     70 	struct itimerspec its;
     71 
     72         its.it_interval.tv_sec = 0;
     73         its.it_interval.tv_nsec = 0;
     74         its.it_value.tv_sec = TIMERSEC;
     75         its.it_value.tv_nsec = 0;
     76 
     77         ev.sigev_notify = SIGEV_SIGNAL;
     78         ev.sigev_signo = TIMERSIGNAL;
     79 */
     80 	act.sa_flags = 0;
     81 	act.sa_handler = myhandler;
     82 	sigemptyset(&act.sa_mask);
     83 	sigaction(TIMERSIGNAL, &act, 0);
     84 
     85 	sigemptyset(&selectset);
     86 	sigaddset(&selectset, SIGTOTEST);
     87 
     88 	ts.tv_sec = SIGTIMEDWAITSEC;
     89 	ts.tv_nsec = 0;
     90 /*
     91         if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     92                 perror("timer_create() did not return success\n");
     93                 return PTS_UNRESOLVED;
     94         }
     95 
     96         if (timer_settime(tid, 0, &its, NULL) != 0) {
     97                 perror("timer_settime() did not return success\n");
     98                 return PTS_UNRESOLVED;
     99         }
    100 */
    101 	if (gettimeofday(&time1, NULL) == -1) {
    102 		perror("gettimeofday()");
    103 		return PTS_UNRESOLVED;
    104 	}
    105 	if (sigtimedwait(&selectset, NULL, &ts) != -1) {
    106 		printf
    107 		    ("Test FAILED: sigtimedwait() did not return with an error\n");
    108 		return PTS_FAIL;
    109 	}
    110 	if (gettimeofday(&time2, NULL) == -1) {
    111 		perror("gettimeofday()");
    112 		return PTS_UNRESOLVED;
    113 	}
    114 
    115 	time_elapsed = (time2.tv_sec - time1.tv_sec
    116 		+ (time2.tv_usec - time1.tv_usec) / 1000000.0);
    117 
    118 	if ((time_elapsed > SIGTIMEDWAITSEC + ERRORMARGIN)
    119 	    || (time_elapsed < SIGTIMEDWAITSEC - ERRORMARGIN)) {
    120 		printf
    121 		    ("Test FAILED: sigtimedwait() did not return immediately\n");
    122 		return PTS_FAIL;
    123 	}
    124 
    125 	printf("Test PASSED\n");
    126 	return PTS_PASS;
    127 }
    128