xref: /aosp_15_r20/external/ltp/testcases/open_posix_testsuite/functional/timers/timers/twoptimers.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 2
21 
22 #define CHILDPASS 1
23 
main(int argc,char * argv[])24 int main(int argc, char *argv[])
25 {
26 	int pid;
27 	struct timespec ts;
28 
29 	if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
30 		perror("clock_gettime() did not return success\n");
31 		return PTS_UNRESOLVED;
32 	}
33 
34 	if ((pid = fork()) == 0) {
35 		/*child */
36 		struct sigevent ev;
37 		timer_t tid;
38 		struct itimerspec its;
39 		sigset_t set;
40 		int sig;
41 		int flags = 0;
42 
43 		if (sigemptyset(&set) == -1) {
44 			perror("sigemptyset() failed\n");
45 			return PTS_UNRESOLVED;
46 		}
47 
48 		if (sigaddset(&set, SIGABRT) == -1) {
49 			perror("sigaddset() failed\n");
50 			return PTS_UNRESOLVED;
51 		}
52 
53 		if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
54 			perror("sigprocmask() failed\n");
55 			return PTS_UNRESOLVED;
56 		}
57 		ev.sigev_notify = SIGEV_SIGNAL;
58 		ev.sigev_signo = SIGABRT;
59 		if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
60 			perror("timer_create() did not return success\n");
61 			return PTS_UNRESOLVED;
62 		}
63 
64 		its.it_value.tv_sec = ts.tv_sec + EXPIREDELTA;
65 		its.it_value.tv_nsec = ts.tv_nsec;
66 		its.it_interval.tv_sec = 0;
67 		its.it_interval.tv_nsec = 0;
68 
69 		flags |= TIMER_ABSTIME;
70 		if (timer_settime(tid, flags, &its, NULL) != 0) {
71 			perror("timer_settime() did not return success\n");
72 			return PTS_UNRESOLVED;
73 		}
74 
75 		if (sigwait(&set, &sig) == -1) {
76 			perror("sigwait() failed\n");
77 			return PTS_UNRESOLVED;
78 		}
79 
80 		if (sig == SIGABRT) {
81 			printf("Got it! Child\n");
82 			return CHILDPASS;
83 		}
84 
85 		printf("Got another signal! Child\n");
86 		return PTS_FAIL;
87 	} else {
88 		/*parent */
89 		struct sigevent ev;
90 		timer_t tid;
91 		struct itimerspec its;
92 		sigset_t set;
93 		int sig;
94 		int flags = 0;
95 		int i;
96 
97 		if (sigemptyset(&set) == -1) {
98 			perror("sigemptyset() failed\n");
99 			return PTS_UNRESOLVED;
100 		}
101 
102 		if (sigaddset(&set, SIGALRM) == -1) {
103 			perror("sigaddset() failed\n");
104 			return PTS_UNRESOLVED;
105 		}
106 
107 		if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
108 			perror("sigaprocmask() failed\n");
109 			return PTS_UNRESOLVED;
110 		}
111 
112 		ev.sigev_notify = SIGEV_SIGNAL;
113 		ev.sigev_signo = SIGALRM;
114 		if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
115 			perror("timer_create() did not return success\n");
116 			return PTS_UNRESOLVED;
117 		}
118 
119 		its.it_value.tv_sec = ts.tv_sec + EXPIREDELTA;
120 		its.it_value.tv_nsec = ts.tv_nsec;
121 		its.it_interval.tv_sec = 0;
122 		its.it_interval.tv_nsec = 0;
123 
124 		flags |= TIMER_ABSTIME;
125 		if (timer_settime(tid, flags, &its, NULL) != 0) {
126 			perror("timer_settime() did not return success\n");
127 			return PTS_UNRESOLVED;
128 		}
129 
130 		if (sigwait(&set, &sig) == -1) {
131 			perror("sigwait() failed\n");
132 			return PTS_UNRESOLVED;
133 		}
134 
135 		if (sig != SIGALRM) {
136 			printf("Got another signal! Parent\n");
137 			return PTS_FAIL;
138 		}
139 
140 		printf("Got it! Parent\n");
141 
142 		if (wait(&i) == -1) {
143 			perror("Error waiting for child to exit\n");
144 			return PTS_UNRESOLVED;
145 		}
146 		if (WEXITSTATUS(i)) {
147 			printf("Test PASSED\n");
148 			return PTS_PASS;
149 		} else {
150 			printf("Test FAILED\n");
151 			return PTS_FAIL;
152 		}
153 	}
154 	return PTS_UNRESOLVED;
155 }
156