Home | History | Annotate | Download | only in timer_settime
      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 timer_settime() sets the time until the next timer
      9  * expiration to value.it_value and arms the timer for a variety
     10  * of value.it_value values.
     11  *
     12  * Same steps as in 1-1.c; however, this time for multiple
     13  * value.it_value values.
     14  *
     15  * For this test, signal SIGTOTEST will be used, clock CLOCK_REALTIME
     16  * will be used.
     17  */
     18 
     19 #include <time.h>
     20 #include <signal.h>
     21 #include <stdio.h>
     22 #include <unistd.h>
     23 #include <stdlib.h>
     24 #include "posixtest.h"
     25 
     26 #define SIGTOTEST SIGALRM
     27 #define SLEEPDELTA 3
     28 #define ACCEPTABLEDELTA 1
     29 #define NUMTESTS 6
     30 
     31 void handler(int signo)
     32 {
     33 	printf("Caught signal\n");
     34 }
     35 
     36 int main(void)
     37 {
     38 	struct sigevent ev;
     39 	struct sigaction act;
     40 	timer_t tid;
     41 	struct itimerspec its;
     42 	struct timespec ts, tsleft;
     43 	int timervals[NUMTESTS][2] = {
     44 		{0, 30000000}, {1, 0},
     45 		{1, 30000000}, {2, 0},
     46 		{10, 5000}, {13, 5}
     47 	};
     48 	int i;
     49 	int failure = 0;
     50 
     51 	ev.sigev_notify = SIGEV_SIGNAL;
     52 	ev.sigev_signo = SIGTOTEST;
     53 
     54 	act.sa_handler = handler;
     55 	act.sa_flags = 0;
     56 
     57 	its.it_interval.tv_sec = 0;
     58 	its.it_interval.tv_nsec = 0;
     59 
     60 	if (sigemptyset(&act.sa_mask) == -1) {
     61 		perror("Error calling sigemptyset\n");
     62 		return PTS_UNRESOLVED;
     63 	}
     64 	if (sigaction(SIGTOTEST, &act, 0) == -1) {
     65 		perror("Error calling sigaction\n");
     66 		return PTS_UNRESOLVED;
     67 	}
     68 
     69 	for (i = 0; i < NUMTESTS; i++) {
     70 		its.it_value.tv_sec = timervals[i][0];
     71 		its.it_value.tv_nsec = timervals[i][1];
     72 
     73 		ts.tv_sec = timervals[i][0] + SLEEPDELTA;
     74 		ts.tv_nsec = timervals[i][1];
     75 
     76 		if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     77 			perror("timer_create() did not return success\n");
     78 			return PTS_UNRESOLVED;
     79 		}
     80 
     81 		if (timer_settime(tid, 0, &its, NULL) != 0) {
     82 			perror("timer_settime() did not return success\n");
     83 			failure = 1;
     84 		}
     85 
     86 		if (nanosleep(&ts, &tsleft) != -1) {
     87 			perror("nanosleep() not interrupted\n");
     88 			failure = 1;
     89 		}
     90 
     91 		if (abs(tsleft.tv_sec - SLEEPDELTA) > ACCEPTABLEDELTA) {
     92 			printf("Timer did not last correct amount of time\n");
     93 			failure = 1;
     94 		}
     95 
     96 		if (timer_delete(tid) == -1) {
     97 			perror("timer_delete() returned failure\n");
     98 			/*
     99 			 * do not continue
    100 			 */
    101 			return PTS_UNRESOLVED;
    102 		}
    103 	}
    104 
    105 	if (failure) {
    106 		printf("At least one test FAILED\n");
    107 		return PTS_FAIL;
    108 	}
    109 
    110 	printf("All tests PASSED\n");
    111 	return PTS_PASS;
    112 }
    113