]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 2004-2024 Free Software Foundation, Inc. |
1683daeb | 2 | This file is part of the GNU C Library. |
1683daeb UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
1683daeb UD |
17 | |
18 | #include <errno.h> | |
19 | #include <mqueue.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
24 | #if _POSIX_THREADS | |
25 | # include <pthread.h> | |
9702a41c | 26 | # include <support/check.h> |
1683daeb UD |
27 | |
28 | static pthread_barrier_t b; | |
29 | ||
30 | /* Cleanup handling test. */ | |
31 | static int cl_called; | |
32 | ||
33 | static void | |
34 | cl (void *arg) | |
35 | { | |
36 | ++cl_called; | |
37 | } | |
38 | ||
39 | #define TF_MQ_RECEIVE 0L | |
40 | #define TF_MQ_TIMEDRECEIVE 1L | |
41 | #define TF_MQ_SEND 2L | |
42 | #define TF_MQ_TIMEDSEND 3L | |
43 | ||
44 | static const char *names[] | |
45 | = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" }; | |
46 | ||
47 | static mqd_t q; | |
48 | static struct timespec never; | |
49 | ||
50 | static void * | |
51 | tf (void *arg) | |
52 | { | |
53 | int r = pthread_barrier_wait (&b); | |
54 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) | |
55 | { | |
56 | puts ("tf: barrier_wait failed"); | |
57 | exit (1); | |
58 | } | |
59 | ||
60 | pthread_cleanup_push (cl, NULL); | |
61 | ||
62 | char c = ' '; | |
63 | ||
64 | switch ((long) arg) | |
65 | { | |
66 | case TF_MQ_SEND: | |
67 | TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1)); | |
68 | break; | |
69 | case TF_MQ_TIMEDSEND: | |
70 | TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never)); | |
71 | break; | |
72 | case TF_MQ_RECEIVE: | |
73 | TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL)); | |
74 | break; | |
75 | case TF_MQ_TIMEDRECEIVE: | |
76 | TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never)); | |
77 | break; | |
78 | } | |
79 | ||
80 | pthread_cleanup_pop (0); | |
81 | ||
82 | printf ("tf: %s returned\n", names[(long) arg]); | |
83 | ||
84 | exit (1); | |
85 | } | |
86 | ||
87 | #define TEST_FUNCTION do_test () | |
88 | static int | |
89 | do_test (void) | |
90 | { | |
91 | char name[sizeof "/tst-mqueue8-" + sizeof (pid_t) * 3]; | |
92 | snprintf (name, sizeof (name), "/tst-mqueue8-%u", getpid ()); | |
93 | ||
94 | struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 }; | |
95 | q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); | |
96 | ||
97 | if (q == (mqd_t) -1) | |
98 | { | |
9702a41c ST |
99 | if (errno == ENOSYS) |
100 | FAIL_UNSUPPORTED ("mq_open not supported"); | |
101 | ||
1683daeb | 102 | printf ("mq_open failed with: %m\n"); |
9702a41c | 103 | return 1; |
1683daeb UD |
104 | } |
105 | ||
106 | if (mq_unlink (name) != 0) | |
107 | { | |
108 | printf ("mq_unlink failed with: %m\n"); | |
109 | return 1; | |
110 | } | |
111 | ||
112 | if (pthread_barrier_init (&b, NULL, 2) != 0) | |
113 | { | |
114 | puts ("barrier_init failed"); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | if (clock_gettime (CLOCK_REALTIME, &never) == 0) | |
119 | never.tv_sec += 100; | |
120 | else | |
121 | { | |
122 | never.tv_sec = time (NULL) + 100; | |
123 | never.tv_nsec = 0; | |
124 | } | |
125 | ||
126 | int result = 0; | |
127 | for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l) | |
128 | { | |
129 | cl_called = 0; | |
130 | ||
131 | pthread_t th; | |
132 | if (pthread_create (&th, NULL, tf, (void *) l) != 0) | |
133 | { | |
134 | printf ("1st %s create failed\n", names[l]); | |
135 | result = 1; | |
136 | continue; | |
137 | } | |
138 | ||
139 | int r = pthread_barrier_wait (&b); | |
140 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) | |
141 | { | |
142 | puts ("barrier_wait failed"); | |
143 | result = 1; | |
144 | continue; | |
145 | } | |
146 | ||
147 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 }; | |
148 | while (nanosleep (&ts, &ts) != 0) | |
149 | continue; | |
150 | ||
151 | printf ("going to cancel %s in-time\n", names[l]); | |
152 | if (pthread_cancel (th) != 0) | |
153 | { | |
154 | printf ("1st cancel of %s failed\n", names[l]); | |
155 | result = 1; | |
156 | continue; | |
157 | } | |
158 | ||
159 | void *status; | |
160 | if (pthread_join (th, &status) != 0) | |
161 | { | |
162 | printf ("1st join of %s failed\n", names[l]); | |
163 | result = 1; | |
164 | continue; | |
165 | } | |
166 | if (status != PTHREAD_CANCELED) | |
167 | { | |
168 | printf ("1st %s thread not canceled\n", names[l]); | |
169 | result = 1; | |
170 | continue; | |
171 | } | |
172 | ||
173 | if (cl_called == 0) | |
174 | { | |
175 | printf ("%s cleanup handler not called\n", names[l]); | |
176 | result = 1; | |
177 | continue; | |
178 | } | |
179 | if (cl_called > 1) | |
180 | { | |
181 | printf ("%s cleanup handler called more than once\n", names[l]); | |
182 | result = 1; | |
183 | continue; | |
184 | } | |
185 | ||
186 | printf ("in-time %s cancellation succeeded\n", names[l]); | |
187 | ||
188 | cl_called = 0; | |
189 | ||
190 | if (pthread_create (&th, NULL, tf, (void *) l) != 0) | |
191 | { | |
192 | printf ("2nd %s create failed\n", names[l]); | |
193 | result = 1; | |
194 | continue; | |
195 | } | |
196 | ||
197 | printf ("going to cancel %s early\n", names[l]); | |
198 | if (pthread_cancel (th) != 0) | |
199 | { | |
200 | printf ("2nd cancel of %s failed\n", names[l]); | |
201 | result = 1; | |
202 | continue; | |
203 | } | |
204 | ||
205 | r = pthread_barrier_wait (&b); | |
206 | if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) | |
207 | { | |
208 | puts ("barrier_wait failed"); | |
209 | result = 1; | |
210 | continue; | |
211 | } | |
212 | ||
213 | if (pthread_join (th, &status) != 0) | |
214 | { | |
215 | printf ("2nd join of %s failed\n", names[l]); | |
216 | result = 1; | |
217 | continue; | |
218 | } | |
219 | if (status != PTHREAD_CANCELED) | |
220 | { | |
221 | printf ("2nd %s thread not canceled\n", names[l]); | |
222 | result = 1; | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (cl_called == 0) | |
227 | { | |
228 | printf ("%s cleanup handler not called\n", names[l]); | |
229 | result = 1; | |
230 | continue; | |
231 | } | |
232 | if (cl_called > 1) | |
233 | { | |
234 | printf ("%s cleanup handler called more than once\n", names[l]); | |
235 | result = 1; | |
236 | continue; | |
237 | } | |
238 | ||
239 | printf ("early %s cancellation succeeded\n", names[l]); | |
240 | ||
241 | if (l == TF_MQ_TIMEDRECEIVE) | |
242 | { | |
243 | /* mq_receive and mq_timedreceive are tested on empty mq. | |
244 | For mq_send and mq_timedsend we need to make it full. | |
245 | If this fails, there is no point in doing further testing. */ | |
246 | char c = ' '; | |
247 | if (mq_send (q, &c, 1, 1) != 0) | |
248 | { | |
249 | printf ("mq_send failed: %m\n"); | |
250 | result = 1; | |
251 | break; | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | if (mq_close (q) != 0) | |
257 | { | |
258 | printf ("mq_close failed: %m\n"); | |
259 | result = 1; | |
260 | } | |
261 | ||
262 | return result; | |
263 | } | |
264 | #else | |
265 | # define TEST_FUNCTION 0 | |
266 | #endif | |
267 | ||
268 | #include "../test-skeleton.c" |