2005-05-23 Ulrich Drepper <drepper@redhat.com>
[glibc.git] / rt / tst-mqueue1.c
blobdb4065ea93ccb0ead77a5d387428d4b7141a5efe
1 /* Test message queue passing.
2 Copyright (C) 2004 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <mqueue.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "tst-mqueue.h"
32 static int
33 intcmp (const void *a, const void *b)
35 if (*(unsigned char *)a < *(unsigned char *)b)
36 return 1;
37 if (*(unsigned char *)a > *(unsigned char *)b)
38 return -1;
39 return 0;
42 static int
43 check_attrs (struct mq_attr *attr, int nonblock, long cnt)
45 int result = 0;
47 if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
49 printf ("attributes don't match those passed to mq_open\n"
50 "mq_maxmsg %ld, mq_msgsize %ld\n",
51 attr->mq_maxmsg, attr->mq_msgsize);
52 result = 1;
55 if ((attr->mq_flags & O_NONBLOCK) != nonblock)
57 printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
58 result = 1;
61 if (attr->mq_curmsgs != cnt)
63 printf ("mq_curmsgs %ld != %ld\n", attr->mq_curmsgs, cnt);
64 result = 1;
67 return result;
70 static int
71 do_one_test (mqd_t q, const char *name, int nonblock)
73 int result = 0;
75 unsigned char v []
76 = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
78 struct mq_attr attr;
79 memset (&attr, 0xaa, sizeof (attr));
80 if (mq_getattr (q, &attr) != 0)
82 printf ("mq_getattr failed: %m\n");
83 result = 1;
85 else
86 result |= check_attrs (&attr, nonblock, 0);
88 if (mq_receive (q, &v[0], 1, NULL) != -1)
90 puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
91 result = 1;
93 else if (errno != EBADF)
95 printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
96 result = 1;
99 struct timespec ts;
100 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
101 --ts.tv_sec;
102 else
104 ts.tv_sec = time (NULL) - 1;
105 ts.tv_nsec = 0;
108 int ret;
109 for (int i = 0; i < 10; ++i)
111 if (i & 1)
112 ret = mq_send (q, &v[i], 1, v[i] >> 4);
113 else
114 ret = mq_timedsend (q, &v[i], 1, v[i] >> 4, &ts);
116 if (ret)
118 printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
119 result = 1;
123 ret = mq_timedsend (q, &v[10], 1, 8, &ts);
124 if (ret != -1)
126 puts ("mq_timedsend on full queue did not fail");
127 result = 1;
129 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
131 printf ("mq_timedsend on full queue did not fail with %s: %m\n",
132 nonblock ? "EAGAIN" : "ETIMEDOUT");
133 result = 1;
136 if (nonblock)
138 ret = mq_send (q, &v[10], 1, 8);
139 if (ret != -1)
141 puts ("mq_send on full non-blocking queue did not fail");
142 result = 1;
144 else if (errno != EAGAIN)
146 printf ("mq_send on full non-blocking queue did not fail"
147 "with EAGAIN: %m\n");
148 result = 1;
152 memset (&attr, 0xaa, sizeof (attr));
153 if (mq_getattr (q, &attr) != 0)
155 printf ("mq_getattr failed: %m\n");
156 result = 1;
158 else
159 result |= check_attrs (&attr, nonblock, 10);
161 pid_t pid = fork ();
162 if (pid == -1)
164 printf ("fork failed: %m\n");
165 result = 1;
167 else if (pid == 0)
169 result = 0;
171 if (mq_close (q) != 0)
173 printf ("mq_close in child failed: %m\n");
174 result = 1;
177 q = mq_open (name, O_RDONLY | nonblock);
178 if (q == (mqd_t) -1)
180 printf ("mq_open in child failed: %m\n");
181 exit (1);
184 memset (&attr, 0xaa, sizeof (attr));
185 if (mq_getattr (q, &attr) != 0)
187 printf ("mq_getattr failed: %m\n");
188 result = 1;
190 else
191 result |= check_attrs (&attr, nonblock, 10);
193 unsigned char vr[11] = { };
194 unsigned int prio;
195 ssize_t rets;
197 if (mq_send (q, &v[0], 1, 1) != -1)
199 puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
200 result = 1;
202 else if (errno != EBADF)
204 printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
205 result = 1;
208 for (int i = 0; i < 10; ++i)
210 if (i & 1)
211 rets = mq_receive (q, &vr[i], 1, &prio);
212 else
213 rets = mq_timedreceive (q, &vr[i], 1, &prio, &ts);
215 if (rets != 1)
217 if (rets == -1)
218 printf ("mq_%sreceive failed: %m\n", (i & 1) ? "" : "timed");
219 else
220 printf ("mq_%sreceive returned %zd != 1\n",
221 (i & 1) ? "" : "timed", rets);
222 result = 1;
224 else if (prio != (unsigned int) vr[i] >> 4)
226 printf ("unexpected priority %x for value %02x\n", prio,
227 vr[i]);
228 result = 1;
232 qsort (v, 10, 1, intcmp);
233 if (memcmp (v, vr, 10) != 0)
235 puts ("messages not received in expected order");
236 result = 1;
239 rets = mq_timedreceive (q, &vr[10], 1, &prio, &ts);
240 if (rets != -1)
242 puts ("mq_timedreceive on empty queue did not fail");
243 result = 1;
245 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
247 printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
248 nonblock ? "EAGAIN" : "ETIMEDOUT");
249 result = 1;
252 if (nonblock)
254 ret = mq_receive (q, &vr[10], 1, &prio);
255 if (ret != -1)
257 puts ("mq_receive on empty non-blocking queue did not fail");
258 result = 1;
260 else if (errno != EAGAIN)
262 printf ("mq_receive on empty non-blocking queue did not fail"
263 "with EAGAIN: %m\n");
264 result = 1;
268 memset (&attr, 0xaa, sizeof (attr));
269 if (mq_getattr (q, &attr) != 0)
271 printf ("mq_getattr failed: %m\n");
272 result = 1;
274 else
275 result |= check_attrs (&attr, nonblock, 0);
277 if (mq_close (q) != 0)
279 printf ("mq_close in child failed: %m\n");
280 result = 1;
283 exit (result);
286 int status;
287 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
289 printf ("waitpid failed: %m\n");
290 kill (pid, SIGKILL);
291 result = 1;
293 else if (!WIFEXITED (status) || WEXITSTATUS (status))
295 printf ("child failed: %d\n", status);
296 result = 1;
299 memset (&attr, 0xaa, sizeof (attr));
300 if (mq_getattr (q, &attr) != 0)
302 printf ("mq_getattr failed: %m\n");
303 result = 1;
305 else
306 result |= check_attrs (&attr, nonblock, 0);
308 return result;
311 #define TEST_FUNCTION do_test ()
312 static int
313 do_test (void)
315 int result = 0;
317 char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
318 snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
320 struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
321 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
323 if (q == (mqd_t) -1)
325 printf ("mq_open failed with: %m\n");
326 return result;
328 else
329 add_temp_mq (name);
331 result |= do_one_test (q, name, 0);
333 mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
334 if (q2 == (mqd_t) -1)
336 printf ("mq_open failed with: %m\n");
337 q2 = q;
338 result = 1;
340 else
342 if (mq_close (q) != 0)
344 printf ("mq_close in parent failed: %m\n");
345 result = 1;
348 q = q2;
349 result |= do_one_test (q, name, O_NONBLOCK);
351 if (mq_getattr (q, &attr) != 0)
353 printf ("mq_getattr failed: %m\n");
354 result = 1;
356 else
358 attr.mq_flags ^= O_NONBLOCK;
360 struct mq_attr attr2;
361 memset (&attr2, 0x55, sizeof (attr2));
362 if (mq_setattr (q, &attr, &attr2) != 0)
364 printf ("mq_setattr failed: %m\n");
365 result = 1;
367 else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
368 || attr.mq_maxmsg != attr2.mq_maxmsg
369 || attr.mq_msgsize != attr2.mq_msgsize
370 || attr.mq_curmsgs != 0
371 || attr2.mq_curmsgs != 0)
373 puts ("mq_setattr returned unexpected values in *omqstat");
374 result = 1;
376 else
378 result |= do_one_test (q, name, 0);
380 if (mq_setattr (q, &attr2, NULL) != 0)
382 printf ("mq_setattr failed: %m\n");
383 result = 1;
385 else
386 result |= do_one_test (q, name, O_NONBLOCK);
391 if (mq_unlink (name) != 0)
393 printf ("mq_unlink failed: %m\n");
394 result = 1;
397 if (mq_close (q) != 0)
399 printf ("mq_close in parent failed: %m\n");
400 result = 1;
403 if (mq_close (q) != -1)
405 puts ("second mq_close did not fail");
406 result = 1;
408 else if (errno != EBADF)
410 printf ("second mq_close did not fail with EBADF: %m\n");
411 result = 1;
414 return result;
417 #include "../test-skeleton.c"