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