xref: /aosp_15_r20/external/ltp/testcases/open_posix_testsuite/functional/timers/timers/twoevtimers.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 having two timers in different processes set to expire at the
9  * same time, and ensure they both expire at the same time.
10  */
11 #include <stdio.h>
12 #include <time.h>
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include "posixtest.h"
19 
20 #define EXPIREDELTA 3
21 #define LONGTIME 5
22 
23 #define CHILDPASS 1
24 
25 static int caughtabort = 0;
26 static int caughtalarm = 0;
27 
handler_abrt(int signo)28 static void handler_abrt(int signo)
29 {
30 	printf("Caught abort signal\n");
31 	caughtabort++;
32 }
33 
handler_alrm(int signo)34 static void handler_alrm(int signo)
35 {
36 	printf("Caught alarm signal\n");
37 	caughtalarm++;
38 }
39 
main(int argc,char * argv[])40 int main(int argc, char *argv[])
41 {
42 	struct sigaction act1, act2;
43 	struct sigevent ev1, ev2;
44 	timer_t tid1, tid2;
45 	struct timespec ts;
46 	struct itimerspec its;
47 	int flags = 0;
48 
49 	act1.sa_handler = handler_abrt;
50 	act1.sa_flags = 0;
51 	act2.sa_handler = handler_alrm;
52 	act2.sa_flags = 0;
53 
54 	if ((sigemptyset(&act1.sa_mask) != 0) ||
55 	    (sigemptyset(&act2.sa_mask) != 0)) {
56 		perror("sigemptyset() did not return success\n");
57 		return PTS_UNRESOLVED;
58 	}
59 	if ((sigaction(SIGABRT, &act1, 0) != 0) ||
60 	    (sigaction(SIGALRM, &act2, 0) != 0)) {
61 		perror("sigaction() did not return success\n");
62 		return PTS_UNRESOLVED;
63 	}
64 
65 	ev1.sigev_notify = SIGEV_SIGNAL;
66 	ev1.sigev_signo = SIGABRT;
67 	ev2.sigev_notify = SIGEV_SIGNAL;
68 	ev2.sigev_signo = SIGALRM;
69 	if ((timer_create(CLOCK_REALTIME, &ev1, &tid1) != 0) ||
70 	    (timer_create(CLOCK_REALTIME, &ev2, &tid2) != 0)) {
71 		perror("timer_create() did not return success\n");
72 		return PTS_UNRESOLVED;
73 	}
74 
75 	if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
76 		perror("clock_gettime() did not return success\n");
77 		return PTS_UNRESOLVED;
78 	}
79 
80 	its.it_value.tv_sec = ts.tv_sec + EXPIREDELTA;
81 	its.it_value.tv_nsec = ts.tv_nsec;
82 	its.it_interval.tv_sec = 0;
83 	its.it_interval.tv_nsec = 0;
84 
85 	flags |= TIMER_ABSTIME;
86 	if (timer_settime(tid1, flags, &its, NULL) != 0) {
87 		perror("timer_settime() did not return success\n");
88 		return PTS_UNRESOLVED;
89 	}
90 
91 	if (timer_settime(tid2, flags, &its, NULL) != 0) {
92 		perror("timer_settime() did not return success\n");
93 		return PTS_UNRESOLVED;
94 	}
95 
96 	sleep(EXPIREDELTA + 1);
97 
98 	if ((caughtalarm == 1) && (caughtabort == 1)) {
99 		printf("Test PASSED\n");
100 		return PTS_PASS;
101 	} else {
102 		printf("Test FAILED\n");
103 		return PTS_FAIL;
104 	}
105 
106 	printf("This code should not be executed.\n");
107 	return PTS_UNRESOLVED;
108 }
109