Home | History | Annotate | Download | only in timer_getoverrun
      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 only a single timer expiration signal can be sent to
      9  * the process at a time.
     10  *
     11  * - Block signal SIGTOTEST.
     12  * - Set up a repeating timer to expire with signal SIGTOTEST.
     13  * - Sleep for enough time for > 2 signals to be sent.
     14  * - After the signals are unblocked, ensure only one signal is sent.
     15  */
     16 
     17 #include <signal.h>
     18 #include <time.h>
     19 #include <unistd.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include "posixtest.h"
     23 
     24 #define SIGTOTEST SIGALRM
     25 #define TIMERVAL 2
     26 #define TIMERINTERVAL 3
     27 
     28 int madeit = 0;
     29 
     30 void handler(int signo)
     31 {
     32 	madeit++;
     33 	if (madeit > 1) {
     34 		printf(">1 signal made it through\n");
     35 		exit(PTS_FAIL);
     36 	}
     37 }
     38 
     39 int main(void)
     40 {
     41 	struct sigevent ev;
     42 	struct sigaction act;
     43 	timer_t tid;
     44 	struct itimerspec its;
     45 	int overruns;
     46 	sigset_t set;
     47 
     48 	ev.sigev_notify = SIGEV_SIGNAL;
     49 	ev.sigev_signo = SIGTOTEST;
     50 
     51 	act.sa_handler = handler;
     52 	act.sa_flags = 0;
     53 
     54 	/*
     55 	 * set up handler for SIGTOTEST
     56 	 */
     57 	if (sigemptyset(&act.sa_mask) != 0) {
     58 		perror("sigemptyset() did not return success\n");
     59 		return PTS_UNRESOLVED;
     60 	}
     61 	if (sigaction(SIGTOTEST, &act, 0) != 0) {
     62 		perror("sigaction() did not return success\n");
     63 		return PTS_UNRESOLVED;
     64 	}
     65 
     66 	/*
     67 	 * set up timer to send SIGTOTEST
     68 	 */
     69 
     70 	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     71 		perror("timer_create() did not return success\n");
     72 		return PTS_UNRESOLVED;
     73 	}
     74 	its.it_interval.tv_sec = TIMERINTERVAL;
     75 	its.it_interval.tv_nsec = 0;
     76 	its.it_value.tv_sec = TIMERVAL;
     77 	its.it_value.tv_nsec = 0;
     78 
     79 	if (timer_settime(tid, 0, &its, NULL) != 0) {
     80 		perror("timer_settime() did not return success\n");
     81 		return PTS_UNRESOLVED;
     82 	}
     83 
     84 	/*
     85 	 * block signal SIGTOTEST
     86 	 */
     87 	if (sigemptyset(&set) != 0) {
     88 		perror("sigemptyset() did not return success\n");
     89 		return PTS_UNRESOLVED;
     90 	}
     91 	if (sigaddset(&set, SIGTOTEST) != 0) {
     92 		perror("sigaddset() did not return success\n");
     93 		return PTS_UNRESOLVED;
     94 	}
     95 	if (sigprocmask(SIG_SETMASK, &set, NULL) != 0) {
     96 		perror("sigprocmask() did not return success\n");
     97 		return PTS_UNRESOLVED;
     98 	}
     99 
    100 	if (sleep(2 * TIMERINTERVAL + TIMERVAL) != 0) {
    101 		perror("Could not sleep for correct amount of time\n");
    102 		return PTS_UNRESOLVED;
    103 	}
    104 
    105 	if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) {
    106 		perror("sigprocmask() did not return success\n");
    107 		return PTS_UNRESOLVED;
    108 	}
    109 
    110 	overruns = timer_getoverrun(tid);
    111 	printf("Total overruns: %d\n", overruns);
    112 	if (madeit == 1) {
    113 		printf("Test PASSED\n");
    114 		return PTS_PASS;
    115 	}
    116 
    117 	printf("FAIL:  %d signals sent\n", madeit);
    118 	return PTS_FAIL;
    119 }
    120