Consolidate sin/cos table lookup code
[glibc.git] / rt / tst-mqueue2.c
blobc5f7ed8bb8dfb25a8e5c6f19b7783511553cb320
1 /* Test message queue passing.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <mqueue.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "tst-mqueue.h"
33 static void
34 alrm_handler (int sig)
38 #define TIMEOUT 10
39 #define TEST_FUNCTION do_test ()
40 static int
41 do_test (void)
43 int result = 0;
45 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
46 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
48 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
49 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
51 if (q == (mqd_t) -1)
53 printf ("mq_open failed with: %m\n");
54 return result;
56 else
57 add_temp_mq (name);
59 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
60 if (q2 != (mqd_t) -1)
62 puts ("mq_open with O_EXCL unexpectedly succeeded");
63 result = 1;
65 else if (errno != EEXIST)
67 printf ("mq_open did not fail with EEXIST: %m\n");
68 result = 1;
71 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
72 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
74 attr.mq_maxmsg = -2;
75 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
76 if (q2 != (mqd_t) -1)
78 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
79 add_temp_mq (name2);
80 result = 1;
82 else if (errno != EINVAL)
84 printf ("mq_open with invalid mq_maxmsg did not fail with "
85 "EINVAL: %m\n");
86 result = 1;
89 attr.mq_maxmsg = 2;
90 attr.mq_msgsize = -56;
91 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
92 if (q2 != (mqd_t) -1)
94 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
95 add_temp_mq (name2);
96 result = 1;
98 else if (errno != EINVAL)
100 printf ("mq_open with invalid mq_msgsize did not fail with "
101 "EINVAL: %m\n");
102 result = 1;
105 char buf[3];
106 struct timespec ts;
107 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
108 ts.tv_sec += 10;
109 else
111 ts.tv_sec = time (NULL) + 10;
112 ts.tv_nsec = 0;
115 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
117 puts ("mq_timedreceive with too small msg_len did not fail");
118 result = 1;
120 else if (errno != EMSGSIZE)
122 printf ("mq_timedreceive with too small msg_len did not fail with "
123 "EMSGSIZE: %m\n");
124 result = 1;
127 ts.tv_nsec = -1;
128 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
130 puts ("mq_timedreceive with negative tv_nsec did not fail");
131 result = 1;
133 else if (errno != EINVAL)
135 printf ("mq_timedreceive with negative tv_nsec did not fail with "
136 "EINVAL: %m\n");
137 result = 1;
140 ts.tv_nsec = 1000000000;
141 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
143 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
144 result = 1;
146 else if (errno != EINVAL)
148 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
149 "EINVAL: %m\n");
150 result = 1;
153 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
154 sigemptyset (&sa.sa_mask);
155 sigaction (SIGALRM, &sa, NULL);
157 struct itimerval it = { .it_value = { .tv_sec = 1 } };
158 setitimer (ITIMER_REAL, &it, NULL);
160 if (mq_receive (q, buf, 2, NULL) == 0)
162 puts ("mq_receive on empty queue did not block");
163 result = 1;
165 else if (errno != EINTR)
167 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
168 result = 1;
171 setitimer (ITIMER_REAL, &it, NULL);
173 ts.tv_nsec = 0;
174 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
176 puts ("mq_timedreceive on empty queue did not block");
177 result = 1;
179 else if (errno != EINTR)
181 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
182 result = 1;
185 buf[0] = '6';
186 buf[1] = '7';
187 if (mq_send (q, buf, 2, 3) != 0
188 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
190 printf ("mq_send failed: %m\n");
191 result = 1;
194 memset (buf, ' ', sizeof (buf));
196 unsigned int prio;
197 ssize_t rets = mq_receive (q, buf, 3, &prio);
198 if (rets != 1)
200 if (rets == -1)
201 printf ("mq_receive failed: %m\n");
202 else
203 printf ("mq_receive returned %zd != 1\n", rets);
204 result = 1;
206 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
208 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
209 prio, buf[0], buf[1], buf[2]);
210 result = 1;
213 rets = mq_receive (q, buf, 2, NULL);
214 if (rets != 2)
216 if (rets == -1)
217 printf ("mq_receive failed: %m\n");
218 else
219 printf ("mq_receive returned %zd != 2\n", rets);
220 result = 1;
222 else if (memcmp (buf, "67 ", 3) != 0)
224 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
225 buf[0], buf[1], buf[2]);
226 result = 1;
229 buf[0] = '2';
230 buf[1] = '1';
231 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
232 ts.tv_sec = time (NULL);
233 ts.tv_nsec = -1000000001;
234 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
235 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
236 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
237 (mq_timedsend (q, buf, 1, 4, &ts) != 0
238 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
240 printf ("mq_timedsend failed: %m\n");
241 result = 1;
244 buf[0] = '-';
245 ts.tv_nsec = 1000000001;
246 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
248 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
249 result = 1;
251 else if (errno != EINVAL)
253 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
254 "EINVAL: %m\n");
255 result = 1;
258 ts.tv_nsec = -2;
259 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
261 puts ("mq_timedsend with negative tv_nsec did not fail");
262 result = 1;
264 else if (errno != EINVAL)
266 printf ("mq_timedsend with megatove tv_nsec did not fail with "
267 "EINVAL: %m\n");
268 result = 1;
271 setitimer (ITIMER_REAL, &it, NULL);
273 if (mq_send (q, buf, 2, 8) == 0)
275 puts ("mq_send on full queue did not block");
276 result = 1;
278 else if (errno != EINTR)
280 printf ("mq_send on full queue did not fail with EINTR: %m\n");
281 result = 1;
284 setitimer (ITIMER_REAL, &it, NULL);
286 ts.tv_sec += 10;
287 ts.tv_nsec = 0;
288 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
290 puts ("mq_timedsend on full queue did not block");
291 result = 1;
293 else if (errno != EINTR)
295 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
296 result = 1;
299 memset (buf, ' ', sizeof (buf));
301 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
302 ts.tv_sec = time (NULL);
303 ts.tv_nsec = -1000000001;
304 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
305 if (rets == -1 && errno == EINVAL)
306 rets = mq_receive (q, buf, 2, &prio);
307 if (rets != 2)
309 if (rets == -1)
310 printf ("mq_timedreceive failed: %m\n");
311 else
312 printf ("mq_timedreceive returned %zd != 2\n", rets);
313 result = 1;
315 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
317 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
318 prio, buf[0], buf[1], buf[2]);
319 result = 1;
322 if (mq_receive (q, buf, 1, NULL) == 0)
324 puts ("mq_receive with too small msg_len did not fail");
325 result = 1;
327 else if (errno != EMSGSIZE)
329 printf ("mq_receive with too small msg_len did not fail with "
330 "EMSGSIZE: %m\n");
331 result = 1;
334 ts.tv_nsec = -ts.tv_nsec;
335 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
336 if (rets == -1 && errno == EINVAL)
337 rets = mq_receive (q, buf, 2, NULL);
338 if (rets != 1)
340 if (rets == -1)
341 printf ("mq_timedreceive failed: %m\n");
342 else
343 printf ("mq_timedreceive returned %zd != 1\n", rets);
344 result = 1;
346 else if (memcmp (buf, "31 ", 3) != 0)
348 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
349 buf[0], buf[1], buf[2]);
350 result = 1;
353 if (mq_send (q, "", 0, 2) != 0)
355 printf ("mq_send with msg_len 0 failed: %m\n");
356 result = 1;
359 rets = mq_receive (q, buf, 2, &prio);
360 if (rets)
362 if (rets == -1)
363 printf ("mq_receive failed: %m\n");
364 else
365 printf ("mq_receive returned %zd != 0\n", rets);
366 result = 1;
369 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
370 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
372 if (mq_send (q, buf, 1, mq_prio_max) == 0)
374 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
375 result = 1;
377 else if (errno != EINVAL)
379 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
380 "EINVAL: %m\n");
381 result = 1;
384 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
386 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
387 result = 1;
391 if (mq_unlink (name) != 0)
393 printf ("mq_unlink failed: %m\n");
394 result = 1;
397 q2 = mq_open (name, O_RDWR);
398 if (q2 != (mqd_t) -1)
400 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
401 "succeeded\n", name);
402 result = 1;
404 else if (errno != ENOENT)
406 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
407 "ENOENT: %m\n", name);
408 result = 1;
411 if (mq_close (q) != 0)
413 printf ("mq_close in parent failed: %m\n");
414 result = 1;
417 if (mq_receive (q, buf, 2, NULL) == 0)
419 puts ("mq_receive on invalid mqd_t did not fail");
420 result = 1;
422 else if (errno != EBADF)
424 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
425 result = 1;
428 if (mq_send (q, buf, 1, 2) == 0)
430 puts ("mq_send on invalid mqd_t did not fail");
431 result = 1;
433 else if (errno != EBADF)
435 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
436 result = 1;
439 if (mq_getattr (q, &attr) == 0)
441 puts ("mq_getattr on invalid mqd_t did not fail");
442 result = 1;
444 else if (errno != EBADF)
446 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
447 result = 1;
450 memset (&attr, 0, sizeof (attr));
451 if (mq_setattr (q, &attr, NULL) == 0)
453 puts ("mq_setattr on invalid mqd_t did not fail");
454 result = 1;
456 else if (errno != EBADF)
458 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
459 result = 1;
462 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
464 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
465 result = 1;
467 else if (errno != ENOENT)
469 printf ("mq_unlink of non-existant message queue did not fail with "
470 "ENOENT: %m\n");
471 result = 1;
473 return result;
476 #include "../test-skeleton.c"