]> git.ipfire.org Git - thirdparty/glibc.git/blob - rt/tst-mqueue2.c
Remove "Contributed by" lines
[thirdparty/glibc.git] / rt / tst-mqueue2.c
1 /* Test message queue passing.
2 Copyright (C) 2004-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <mqueue.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <sys/wait.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "tst-mqueue.h"
31
32 static void
33 alrm_handler (int sig)
34 {
35 }
36
37 #define TEST_FUNCTION do_test ()
38 static int
39 do_test (void)
40 {
41 int result = 0;
42
43 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
44 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
45
46 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
47 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
48
49 if (q == (mqd_t) -1)
50 {
51 printf ("mq_open failed with: %m\n");
52 return result;
53 }
54 else
55 add_temp_mq (name);
56
57 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
58 if (q2 != (mqd_t) -1)
59 {
60 puts ("mq_open with O_EXCL unexpectedly succeeded");
61 result = 1;
62 }
63 else if (errno != EEXIST)
64 {
65 printf ("mq_open did not fail with EEXIST: %m\n");
66 result = 1;
67 }
68
69 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
70 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
71
72 attr.mq_maxmsg = -2;
73 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
74 if (q2 != (mqd_t) -1)
75 {
76 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
77 add_temp_mq (name2);
78 result = 1;
79 }
80 else if (errno != EINVAL)
81 {
82 printf ("mq_open with invalid mq_maxmsg did not fail with "
83 "EINVAL: %m\n");
84 result = 1;
85 }
86
87 attr.mq_maxmsg = 2;
88 attr.mq_msgsize = -56;
89 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
90 if (q2 != (mqd_t) -1)
91 {
92 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
93 add_temp_mq (name2);
94 result = 1;
95 }
96 else if (errno != EINVAL)
97 {
98 printf ("mq_open with invalid mq_msgsize did not fail with "
99 "EINVAL: %m\n");
100 result = 1;
101 }
102
103 char buf[3];
104 struct timespec ts;
105 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
106 ts.tv_sec += 10;
107 else
108 {
109 ts.tv_sec = time (NULL) + 10;
110 ts.tv_nsec = 0;
111 }
112
113 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
114 {
115 puts ("mq_timedreceive with too small msg_len did not fail");
116 result = 1;
117 }
118 else if (errno != EMSGSIZE)
119 {
120 printf ("mq_timedreceive with too small msg_len did not fail with "
121 "EMSGSIZE: %m\n");
122 result = 1;
123 }
124
125 ts.tv_nsec = -1;
126 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
127 {
128 puts ("mq_timedreceive with negative tv_nsec did not fail");
129 result = 1;
130 }
131 else if (errno != EINVAL)
132 {
133 printf ("mq_timedreceive with negative tv_nsec did not fail with "
134 "EINVAL: %m\n");
135 result = 1;
136 }
137
138 ts.tv_nsec = 1000000000;
139 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
140 {
141 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
142 result = 1;
143 }
144 else if (errno != EINVAL)
145 {
146 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
147 "EINVAL: %m\n");
148 result = 1;
149 }
150
151 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
152 sigemptyset (&sa.sa_mask);
153 sigaction (SIGALRM, &sa, NULL);
154
155 struct itimerval it = { .it_value = { .tv_sec = 1 } };
156 setitimer (ITIMER_REAL, &it, NULL);
157
158 if (mq_receive (q, buf, 2, NULL) == 0)
159 {
160 puts ("mq_receive on empty queue did not block");
161 result = 1;
162 }
163 else if (errno != EINTR)
164 {
165 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
166 result = 1;
167 }
168
169 setitimer (ITIMER_REAL, &it, NULL);
170
171 ts.tv_nsec = 0;
172 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
173 {
174 puts ("mq_timedreceive on empty queue did not block");
175 result = 1;
176 }
177 else if (errno != EINTR)
178 {
179 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
180 result = 1;
181 }
182
183 buf[0] = '6';
184 buf[1] = '7';
185 if (mq_send (q, buf, 2, 3) != 0
186 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
187 {
188 printf ("mq_send failed: %m\n");
189 result = 1;
190 }
191
192 memset (buf, ' ', sizeof (buf));
193
194 unsigned int prio;
195 ssize_t rets = mq_receive (q, buf, 3, &prio);
196 if (rets != 1)
197 {
198 if (rets == -1)
199 printf ("mq_receive failed: %m\n");
200 else
201 printf ("mq_receive returned %zd != 1\n", rets);
202 result = 1;
203 }
204 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
205 {
206 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
207 prio, buf[0], buf[1], buf[2]);
208 result = 1;
209 }
210
211 rets = mq_receive (q, buf, 2, NULL);
212 if (rets != 2)
213 {
214 if (rets == -1)
215 printf ("mq_receive failed: %m\n");
216 else
217 printf ("mq_receive returned %zd != 2\n", rets);
218 result = 1;
219 }
220 else if (memcmp (buf, "67 ", 3) != 0)
221 {
222 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
223 buf[0], buf[1], buf[2]);
224 result = 1;
225 }
226
227 buf[0] = '2';
228 buf[1] = '1';
229 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
230 ts.tv_sec = time (NULL);
231 ts.tv_nsec = -1000000001;
232 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
233 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
234 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
235 (mq_timedsend (q, buf, 1, 4, &ts) != 0
236 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
237 {
238 printf ("mq_timedsend failed: %m\n");
239 result = 1;
240 }
241
242 buf[0] = '-';
243 ts.tv_nsec = 1000000001;
244 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
245 {
246 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
247 result = 1;
248 }
249 else if (errno != EINVAL)
250 {
251 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
252 "EINVAL: %m\n");
253 result = 1;
254 }
255
256 ts.tv_nsec = -2;
257 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
258 {
259 puts ("mq_timedsend with negative tv_nsec did not fail");
260 result = 1;
261 }
262 else if (errno != EINVAL)
263 {
264 printf ("mq_timedsend with megatove tv_nsec did not fail with "
265 "EINVAL: %m\n");
266 result = 1;
267 }
268
269 setitimer (ITIMER_REAL, &it, NULL);
270
271 if (mq_send (q, buf, 2, 8) == 0)
272 {
273 puts ("mq_send on full queue did not block");
274 result = 1;
275 }
276 else if (errno != EINTR)
277 {
278 printf ("mq_send on full queue did not fail with EINTR: %m\n");
279 result = 1;
280 }
281
282 setitimer (ITIMER_REAL, &it, NULL);
283
284 ts.tv_sec += 10;
285 ts.tv_nsec = 0;
286 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
287 {
288 puts ("mq_timedsend on full queue did not block");
289 result = 1;
290 }
291 else if (errno != EINTR)
292 {
293 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
294 result = 1;
295 }
296
297 memset (buf, ' ', sizeof (buf));
298
299 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
300 ts.tv_sec = time (NULL);
301 ts.tv_nsec = -1000000001;
302 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
303 if (rets == -1 && errno == EINVAL)
304 rets = mq_receive (q, buf, 2, &prio);
305 if (rets != 2)
306 {
307 if (rets == -1)
308 printf ("mq_timedreceive failed: %m\n");
309 else
310 printf ("mq_timedreceive returned %zd != 2\n", rets);
311 result = 1;
312 }
313 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
314 {
315 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
316 prio, buf[0], buf[1], buf[2]);
317 result = 1;
318 }
319
320 if (mq_receive (q, buf, 1, NULL) == 0)
321 {
322 puts ("mq_receive with too small msg_len did not fail");
323 result = 1;
324 }
325 else if (errno != EMSGSIZE)
326 {
327 printf ("mq_receive with too small msg_len did not fail with "
328 "EMSGSIZE: %m\n");
329 result = 1;
330 }
331
332 ts.tv_nsec = -ts.tv_nsec;
333 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
334 if (rets == -1 && errno == EINVAL)
335 rets = mq_receive (q, buf, 2, NULL);
336 if (rets != 1)
337 {
338 if (rets == -1)
339 printf ("mq_timedreceive failed: %m\n");
340 else
341 printf ("mq_timedreceive returned %zd != 1\n", rets);
342 result = 1;
343 }
344 else if (memcmp (buf, "31 ", 3) != 0)
345 {
346 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
347 buf[0], buf[1], buf[2]);
348 result = 1;
349 }
350
351 if (mq_send (q, "", 0, 2) != 0)
352 {
353 printf ("mq_send with msg_len 0 failed: %m\n");
354 result = 1;
355 }
356
357 rets = mq_receive (q, buf, 2, &prio);
358 if (rets)
359 {
360 if (rets == -1)
361 printf ("mq_receive failed: %m\n");
362 else
363 printf ("mq_receive returned %zd != 0\n", rets);
364 result = 1;
365 }
366
367 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
368 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
369 {
370 if (mq_send (q, buf, 1, mq_prio_max) == 0)
371 {
372 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
373 result = 1;
374 }
375 else if (errno != EINVAL)
376 {
377 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
378 "EINVAL: %m\n");
379 result = 1;
380 }
381
382 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
383 {
384 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
385 result = 1;
386 }
387 }
388
389 if (mq_unlink (name) != 0)
390 {
391 printf ("mq_unlink failed: %m\n");
392 result = 1;
393 }
394
395 q2 = mq_open (name, O_RDWR);
396 if (q2 != (mqd_t) -1)
397 {
398 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
399 "succeeded\n", name);
400 result = 1;
401 }
402 else if (errno != ENOENT)
403 {
404 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
405 "ENOENT: %m\n", name);
406 result = 1;
407 }
408
409 if (mq_close (q) != 0)
410 {
411 printf ("mq_close in parent failed: %m\n");
412 result = 1;
413 }
414
415 if (mq_receive (q, buf, 2, NULL) == 0)
416 {
417 puts ("mq_receive on invalid mqd_t did not fail");
418 result = 1;
419 }
420 else if (errno != EBADF)
421 {
422 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
423 result = 1;
424 }
425
426 if (mq_send (q, buf, 1, 2) == 0)
427 {
428 puts ("mq_send on invalid mqd_t did not fail");
429 result = 1;
430 }
431 else if (errno != EBADF)
432 {
433 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
434 result = 1;
435 }
436
437 if (mq_getattr (q, &attr) == 0)
438 {
439 puts ("mq_getattr on invalid mqd_t did not fail");
440 result = 1;
441 }
442 else if (errno != EBADF)
443 {
444 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
445 result = 1;
446 }
447
448 memset (&attr, 0, sizeof (attr));
449 if (mq_setattr (q, &attr, NULL) == 0)
450 {
451 puts ("mq_setattr on invalid mqd_t did not fail");
452 result = 1;
453 }
454 else if (errno != EBADF)
455 {
456 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
457 result = 1;
458 }
459
460 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
461 {
462 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
463 result = 1;
464 }
465 else if (errno != ENOENT)
466 {
467 printf ("mq_unlink of non-existant message queue did not fail with "
468 "ENOENT: %m\n");
469 result = 1;
470 }
471 return result;
472 }
473
474 #include "../test-skeleton.c"