nis: Fix nis_print_directory
[glibc.git] / rt / tst-mqueue2.c
blobb355d22474a5d11535ed1361b47f1f99b8fa47e9
1 /* Test message queue passing.
2 Copyright (C) 2004-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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 <support/check.h>
31 #include "tst-mqueue.h"
33 static void
34 alrm_handler (int sig)
38 #define TEST_FUNCTION do_test ()
39 static int
40 do_test (void)
42 int result = 0;
44 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
45 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
47 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
48 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
50 if (q == (mqd_t) -1)
52 if (errno == ENOSYS)
53 FAIL_UNSUPPORTED ("mq_open not supported");
55 printf ("mq_open failed with: %m\n");
56 return 1;
59 add_temp_mq (name);
61 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
62 if (q2 != (mqd_t) -1)
64 puts ("mq_open with O_EXCL unexpectedly succeeded");
65 result = 1;
67 else if (errno != EEXIST)
69 printf ("mq_open did not fail with EEXIST: %m\n");
70 result = 1;
73 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
74 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
76 attr.mq_maxmsg = -2;
77 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
78 if (q2 != (mqd_t) -1)
80 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
81 add_temp_mq (name2);
82 result = 1;
84 else if (errno != EINVAL)
86 printf ("mq_open with invalid mq_maxmsg did not fail with "
87 "EINVAL: %m\n");
88 result = 1;
91 attr.mq_maxmsg = 2;
92 attr.mq_msgsize = -56;
93 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
94 if (q2 != (mqd_t) -1)
96 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
97 add_temp_mq (name2);
98 result = 1;
100 else if (errno != EINVAL)
102 printf ("mq_open with invalid mq_msgsize did not fail with "
103 "EINVAL: %m\n");
104 result = 1;
107 char buf[3];
108 struct timespec ts;
109 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
110 ts.tv_sec += 10;
111 else
113 ts.tv_sec = time (NULL) + 10;
114 ts.tv_nsec = 0;
117 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
119 puts ("mq_timedreceive with too small msg_len did not fail");
120 result = 1;
122 else if (errno != EMSGSIZE)
124 printf ("mq_timedreceive with too small msg_len did not fail with "
125 "EMSGSIZE: %m\n");
126 result = 1;
129 ts.tv_nsec = -1;
130 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
132 puts ("mq_timedreceive with negative tv_nsec did not fail");
133 result = 1;
135 else if (errno != EINVAL)
137 printf ("mq_timedreceive with negative tv_nsec did not fail with "
138 "EINVAL: %m\n");
139 result = 1;
142 ts.tv_nsec = 1000000000;
143 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
145 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
146 result = 1;
148 else if (errno != EINVAL)
150 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
151 "EINVAL: %m\n");
152 result = 1;
155 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
156 sigemptyset (&sa.sa_mask);
157 sigaction (SIGALRM, &sa, NULL);
159 struct itimerval it = { .it_value = { .tv_sec = 1 } };
160 setitimer (ITIMER_REAL, &it, NULL);
162 if (mq_receive (q, buf, 2, NULL) == 0)
164 puts ("mq_receive on empty queue did not block");
165 result = 1;
167 else if (errno != EINTR)
169 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
170 result = 1;
173 setitimer (ITIMER_REAL, &it, NULL);
175 ts.tv_nsec = 0;
176 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
178 puts ("mq_timedreceive on empty queue did not block");
179 result = 1;
181 else if (errno != EINTR)
183 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
184 result = 1;
187 buf[0] = '6';
188 buf[1] = '7';
189 if (mq_send (q, buf, 2, 3) != 0
190 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
192 printf ("mq_send failed: %m\n");
193 result = 1;
196 memset (buf, ' ', sizeof (buf));
198 unsigned int prio;
199 ssize_t rets = mq_receive (q, buf, 3, &prio);
200 if (rets != 1)
202 if (rets == -1)
203 printf ("mq_receive failed: %m\n");
204 else
205 printf ("mq_receive returned %zd != 1\n", rets);
206 result = 1;
208 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
210 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
211 prio, buf[0], buf[1], buf[2]);
212 result = 1;
215 rets = mq_receive (q, buf, 2, NULL);
216 if (rets != 2)
218 if (rets == -1)
219 printf ("mq_receive failed: %m\n");
220 else
221 printf ("mq_receive returned %zd != 2\n", rets);
222 result = 1;
224 else if (memcmp (buf, "67 ", 3) != 0)
226 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
227 buf[0], buf[1], buf[2]);
228 result = 1;
231 buf[0] = '2';
232 buf[1] = '1';
233 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
234 ts.tv_sec = time (NULL);
235 ts.tv_nsec = -1000000001;
236 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
237 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
238 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
239 (mq_timedsend (q, buf, 1, 4, &ts) != 0
240 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
242 printf ("mq_timedsend failed: %m\n");
243 result = 1;
246 buf[0] = '-';
247 ts.tv_nsec = 1000000001;
248 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
250 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
251 result = 1;
253 else if (errno != EINVAL)
255 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
256 "EINVAL: %m\n");
257 result = 1;
260 ts.tv_nsec = -2;
261 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
263 puts ("mq_timedsend with negative tv_nsec did not fail");
264 result = 1;
266 else if (errno != EINVAL)
268 printf ("mq_timedsend with megatove tv_nsec did not fail with "
269 "EINVAL: %m\n");
270 result = 1;
273 setitimer (ITIMER_REAL, &it, NULL);
275 if (mq_send (q, buf, 2, 8) == 0)
277 puts ("mq_send on full queue did not block");
278 result = 1;
280 else if (errno != EINTR)
282 printf ("mq_send on full queue did not fail with EINTR: %m\n");
283 result = 1;
286 setitimer (ITIMER_REAL, &it, NULL);
288 ts.tv_sec += 10;
289 ts.tv_nsec = 0;
290 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
292 puts ("mq_timedsend on full queue did not block");
293 result = 1;
295 else if (errno != EINTR)
297 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
298 result = 1;
301 memset (buf, ' ', sizeof (buf));
303 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
304 ts.tv_sec = time (NULL);
305 ts.tv_nsec = -1000000001;
306 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
307 if (rets == -1 && errno == EINVAL)
308 rets = mq_receive (q, buf, 2, &prio);
309 if (rets != 2)
311 if (rets == -1)
312 printf ("mq_timedreceive failed: %m\n");
313 else
314 printf ("mq_timedreceive returned %zd != 2\n", rets);
315 result = 1;
317 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
319 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
320 prio, buf[0], buf[1], buf[2]);
321 result = 1;
324 if (mq_receive (q, buf, 1, NULL) == 0)
326 puts ("mq_receive with too small msg_len did not fail");
327 result = 1;
329 else if (errno != EMSGSIZE)
331 printf ("mq_receive with too small msg_len did not fail with "
332 "EMSGSIZE: %m\n");
333 result = 1;
336 ts.tv_nsec = -ts.tv_nsec;
337 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
338 if (rets == -1 && errno == EINVAL)
339 rets = mq_receive (q, buf, 2, NULL);
340 if (rets != 1)
342 if (rets == -1)
343 printf ("mq_timedreceive failed: %m\n");
344 else
345 printf ("mq_timedreceive returned %zd != 1\n", rets);
346 result = 1;
348 else if (memcmp (buf, "31 ", 3) != 0)
350 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
351 buf[0], buf[1], buf[2]);
352 result = 1;
355 if (mq_send (q, "", 0, 2) != 0)
357 printf ("mq_send with msg_len 0 failed: %m\n");
358 result = 1;
361 rets = mq_receive (q, buf, 2, &prio);
362 if (rets)
364 if (rets == -1)
365 printf ("mq_receive failed: %m\n");
366 else
367 printf ("mq_receive returned %zd != 0\n", rets);
368 result = 1;
371 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
372 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
374 if (mq_send (q, buf, 1, mq_prio_max) == 0)
376 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
377 result = 1;
379 else if (errno != EINVAL)
381 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
382 "EINVAL: %m\n");
383 result = 1;
386 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
388 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
389 result = 1;
393 if (mq_unlink (name) != 0)
395 printf ("mq_unlink failed: %m\n");
396 result = 1;
399 q2 = mq_open (name, O_RDWR);
400 if (q2 != (mqd_t) -1)
402 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
403 "succeeded\n", name);
404 result = 1;
406 else if (errno != ENOENT)
408 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
409 "ENOENT: %m\n", name);
410 result = 1;
413 if (mq_close (q) != 0)
415 printf ("mq_close in parent failed: %m\n");
416 result = 1;
419 if (mq_receive (q, buf, 2, NULL) == 0)
421 puts ("mq_receive on invalid mqd_t did not fail");
422 result = 1;
424 else if (errno != EBADF)
426 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
427 result = 1;
430 if (mq_send (q, buf, 1, 2) == 0)
432 puts ("mq_send on invalid mqd_t did not fail");
433 result = 1;
435 else if (errno != EBADF)
437 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
438 result = 1;
441 if (mq_getattr (q, &attr) == 0)
443 puts ("mq_getattr on invalid mqd_t did not fail");
444 result = 1;
446 else if (errno != EBADF)
448 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
449 result = 1;
452 memset (&attr, 0, sizeof (attr));
453 if (mq_setattr (q, &attr, NULL) == 0)
455 puts ("mq_setattr on invalid mqd_t did not fail");
456 result = 1;
458 else if (errno != EBADF)
460 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
461 result = 1;
464 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
466 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
467 result = 1;
469 else if (errno != ENOENT)
471 printf ("mq_unlink of non-existant message queue did not fail with "
472 "ENOENT: %m\n");
473 result = 1;
475 return result;
478 #include "../test-skeleton.c"