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 the sigtimedwait() function shall wait for the time interval specified
10 in the timespec structure referenced by timeout.
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 signal SIGTOTEST that will never be pending
26 5. Obtain time2, and find the difference between time2 and time1.
27 6. Verify that (time2-time1) is equal to SIGTIMEDWAITSEC within a reasonable
28 error margin.
29 */
30
31 #define _XOPEN_REALTIME 1
32
33 #define TIMERSIGNAL SIGUSR1
34 #define SIGTOTEST SIGUSR2
35 #define TIMERSEC 2
36 #define SIGTIMEDWAITSEC 1
37 #define ERRORMARGIN 0.1
38
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/time.h>
45 #include <sys/wait.h>
46 #include "posixtest.h"
47
myhandler(int signo PTS_ATTRIBUTE_UNUSED)48 static void myhandler(int signo PTS_ATTRIBUTE_UNUSED)
49 {
50 printf
51 ("Test FAILED: %d seconds have elapsed and sigtimedwait() has not yet returned.\n",
52 TIMERSEC);
53 exit(PTS_FAIL);
54 }
55
main(void)56 int main(void)
57 {
58 struct sigaction act;
59
60 struct timeval time1, time2;
61 double time_elapsed;
62
63 sigset_t selectset;
64 struct timespec ts;
65 /*
66 struct sigevent ev;
67 timer_t tid;
68 struct itimerspec its;
69
70 its.it_interval.tv_sec = 0;
71 its.it_interval.tv_nsec = 0;
72 its.it_value.tv_sec = TIMERSEC;
73 its.it_value.tv_nsec = 0;
74
75 ev.sigev_notify = SIGEV_SIGNAL;
76 ev.sigev_signo = TIMERSIGNAL;
77 */
78 act.sa_flags = 0;
79 act.sa_handler = myhandler;
80 sigemptyset(&act.sa_mask);
81 sigaction(TIMERSIGNAL, &act, 0);
82
83 sigemptyset(&selectset);
84 sigaddset(&selectset, SIGTOTEST);
85
86 ts.tv_sec = SIGTIMEDWAITSEC;
87 ts.tv_nsec = 0;
88 /*
89 if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
90 perror("timer_create() did not return success\n");
91 return PTS_UNRESOLVED;
92 }
93
94 if (timer_settime(tid, 0, &its, NULL) != 0) {
95 perror("timer_settime() did not return success\n");
96 return PTS_UNRESOLVED;
97 }
98 */
99 if (gettimeofday(&time1, NULL) == -1) {
100 perror("gettimeofday()");
101 return PTS_UNRESOLVED;
102 }
103 if (sigtimedwait(&selectset, NULL, &ts) != -1) {
104 perror
105 ("sigtimedwait() did not return -1 even though signal was not pending\n");
106 return PTS_UNRESOLVED;
107 }
108 if (gettimeofday(&time2, NULL) == -1) {
109 perror("gettimeofday()");
110 return PTS_UNRESOLVED;
111 }
112
113 time_elapsed = (time2.tv_sec - time1.tv_sec
114 + (time2.tv_usec - time1.tv_usec) / 1000000.0);
115
116 if ((time_elapsed > SIGTIMEDWAITSEC + ERRORMARGIN)
117 || (time_elapsed < SIGTIMEDWAITSEC - ERRORMARGIN)) {
118 printf("Test FAILED: sigtimedwait() did not return in "
119 "the required time\n");
120 printf("time_elapsed: %lf\n", time_elapsed);
121 return PTS_FAIL;
122 }
123
124 printf("Test PASSED\n");
125 return PTS_PASS;
126 }
127