Home | History | Annotate | Download | only in timer_create
      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 a timer is in the disarmed state when it is first created.
      9  *
     10  * Test by creating a timer, sleeping for SLEEPTIME, and ensuring
     11  * the sleep was not interrupted.
     12  *
     13  * For this test, CLOCK_REALTIME will be used.  Signal SIGTOTEST will be
     14  * used.
     15  */
     16 
     17 #include <time.h>
     18 #include <signal.h>
     19 #include <stdio.h>
     20 #include <unistd.h>
     21 #include <stdlib.h>
     22 #include "posixtest.h"
     23 
     24 #define SIGTOTEST SIGALRM
     25 #define SLEEPTIME 3
     26 
     27 void handler(int signo)
     28 {
     29 	printf("Caught signal\n");
     30 }
     31 
     32 int main(void)
     33 {
     34 	struct sigevent ev;
     35 	struct sigaction act;
     36 	timer_t tid;
     37 	struct timespec ts;
     38 
     39 	act.sa_handler = handler;
     40 	act.sa_flags = 0;
     41 	sigemptyset(&act.sa_mask);
     42 	if (sigaction(SIGTOTEST, &act, 0) == -1) {
     43 		perror("Error calling sigaction\n");
     44 		return PTS_UNRESOLVED;
     45 	}
     46 
     47 	ev.sigev_notify = SIGEV_SIGNAL;
     48 	ev.sigev_signo = SIGTOTEST;
     49 	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     50 		perror("timer_create() did not return success\n");
     51 		return PTS_FAIL;
     52 	}
     53 
     54 	ts.tv_sec = SLEEPTIME;
     55 	ts.tv_nsec = 0;
     56 	if (nanosleep(&ts, NULL) == -1) {
     57 		perror("nanosleep() interrupted\n");
     58 		return PTS_FAIL;
     59 	}
     60 	//Sleep not interrupted
     61 	printf("Test PASSED\n");
     62 	return PTS_PASS;
     63 }
     64