12c28215423293e443469a07ae7011135d058b671Garrett Cooper/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2002, Intel Corporation. All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
40dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license.  For the full content
52c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this
60dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree.
70dc076565f772bb1953209fb69ea150b494aaa40robbiew
80dc076565f772bb1953209fb69ea150b494aaa40robbiew * pt:MON
90dc076565f772bb1953209fb69ea150b494aaa40robbiew *
100dc076565f772bb1953209fb69ea150b494aaa40robbiew * Test that CLOCK_MONOTONIC is supported by timer_create().
110dc076565f772bb1953209fb69ea150b494aaa40robbiew *
120dc076565f772bb1953209fb69ea150b494aaa40robbiew * Same test as 1-1.c with CLOCK_MONOTONIC.
130dc076565f772bb1953209fb69ea150b494aaa40robbiew */
140dc076565f772bb1953209fb69ea150b494aaa40robbiew
150dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <time.h>
160dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <signal.h>
170dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
180dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
190dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
200dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
210dc076565f772bb1953209fb69ea150b494aaa40robbiew
220dc076565f772bb1953209fb69ea150b494aaa40robbiew#define SIGTOTEST SIGALRM
230dc076565f772bb1953209fb69ea150b494aaa40robbiew#define TIMERSEC 2
240dc076565f772bb1953209fb69ea150b494aaa40robbiew#define SLEEPDELTA 3
250dc076565f772bb1953209fb69ea150b494aaa40robbiew#define ACCEPTABLEDELTA 1
260dc076565f772bb1953209fb69ea150b494aaa40robbiew
270dc076565f772bb1953209fb69ea150b494aaa40robbiewvoid handler(int signo)
280dc076565f772bb1953209fb69ea150b494aaa40robbiew{
290dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("Caught signal\n");
300dc076565f772bb1953209fb69ea150b494aaa40robbiew}
310dc076565f772bb1953209fb69ea150b494aaa40robbiew
324ca2bbdcd3003f3c8df4e6129e9c7b2bd1514f87Cyril Hrubisint main(void)
330dc076565f772bb1953209fb69ea150b494aaa40robbiew{
340dc076565f772bb1953209fb69ea150b494aaa40robbiew#ifdef CLOCK_MONOTONIC
35354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (sysconf(_SC_MONOTONIC_CLOCK) == -1) {
360dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("CLOCK_MONOTONIC unsupported\n");
370dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNSUPPORTED;
380dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
390dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sigevent ev;
400dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sigaction act;
410dc076565f772bb1953209fb69ea150b494aaa40robbiew	timer_t tid;
420dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct itimerspec its;
430dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct timespec ts, tsleft;
440dc076565f772bb1953209fb69ea150b494aaa40robbiew
450dc076565f772bb1953209fb69ea150b494aaa40robbiew	ev.sigev_notify = SIGEV_SIGNAL;
460dc076565f772bb1953209fb69ea150b494aaa40robbiew	ev.sigev_signo = SIGTOTEST;
470dc076565f772bb1953209fb69ea150b494aaa40robbiew
48354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	act.sa_handler = handler;
49354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	act.sa_flags = 0;
500dc076565f772bb1953209fb69ea150b494aaa40robbiew
510dc076565f772bb1953209fb69ea150b494aaa40robbiew	its.it_interval.tv_sec = 0;
520dc076565f772bb1953209fb69ea150b494aaa40robbiew	its.it_interval.tv_nsec = 0;
530dc076565f772bb1953209fb69ea150b494aaa40robbiew	its.it_value.tv_sec = TIMERSEC;
540dc076565f772bb1953209fb69ea150b494aaa40robbiew	its.it_value.tv_nsec = 0;
550dc076565f772bb1953209fb69ea150b494aaa40robbiew
56354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	ts.tv_sec = TIMERSEC + SLEEPDELTA;
57354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	ts.tv_nsec = 0;
580dc076565f772bb1953209fb69ea150b494aaa40robbiew
590dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (sigemptyset(&act.sa_mask) == -1) {
600dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("Error calling sigemptyset\n");
610dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
620dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
630dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (sigaction(SIGTOTEST, &act, 0) == -1) {
640dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("Error calling sigaction\n");
650dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
660dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
670dc076565f772bb1953209fb69ea150b494aaa40robbiew
680dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (timer_create(CLOCK_MONOTONIC, &ev, &tid) != 0) {
690dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("timer_create() did not return success\n");
700dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
710dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
720dc076565f772bb1953209fb69ea150b494aaa40robbiew
730dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (timer_settime(tid, 0, &its, NULL) != 0) {
740dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("timer_settime() did not return success\n");
750dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_UNRESOLVED;
760dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
770dc076565f772bb1953209fb69ea150b494aaa40robbiew
780dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (nanosleep(&ts, &tsleft) != -1) {
790dc076565f772bb1953209fb69ea150b494aaa40robbiew		perror("nanosleep() not interrupted\n");
800dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
810dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
820dc076565f772bb1953209fb69ea150b494aaa40robbiew
83354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (abs(tsleft.tv_sec - SLEEPDELTA) <= ACCEPTABLEDELTA) {
840dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Test PASSED\n");
850dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_PASS;
860dc076565f772bb1953209fb69ea150b494aaa40robbiew	} else {
870dc076565f772bb1953209fb69ea150b494aaa40robbiew		printf("Timer did not last for correct amount of time\n");
882c28215423293e443469a07ae7011135d058b671Garrett Cooper		printf("timer: %d != correct %d\n",
89354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       (int)ts.tv_sec - (int)tsleft.tv_sec, TIMERSEC);
900dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
910dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
920dc076565f772bb1953209fb69ea150b494aaa40robbiew
930dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_UNRESOLVED;
940dc076565f772bb1953209fb69ea150b494aaa40robbiew#else
950dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("CLOCK_MONOTONIC unsupported\n");
960dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_UNSUPPORTED;
970dc076565f772bb1953209fb69ea150b494aaa40robbiew#endif
980dc076565f772bb1953209fb69ea150b494aaa40robbiew
99ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
100