Update copyright notices with scripts/update-copyrights
[glibc.git] / rt / tst-mqueue1.c
blobc242c37733c442a543d8ef77e8bc58a40551272d
1 /* Test message queue passing.
2 Copyright (C) 2004-2014 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/wait.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "tst-mqueue.h"
31 static int
32 intcmp (const void *a, const void *b)
34 if (*(unsigned char *)a < *(unsigned char *)b)
35 return 1;
36 if (*(unsigned char *)a > *(unsigned char *)b)
37 return -1;
38 return 0;
41 static int
42 check_attrs (struct mq_attr *attr, int nonblock, long cnt)
44 int result = 0;
46 if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
48 printf ("attributes don't match those passed to mq_open\n"
49 "mq_maxmsg %ld, mq_msgsize %ld\n",
50 attr->mq_maxmsg, attr->mq_msgsize);
51 result = 1;
54 if ((attr->mq_flags & O_NONBLOCK) != nonblock)
56 printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
57 result = 1;
60 if (attr->mq_curmsgs != cnt)
62 printf ("mq_curmsgs %ld != %ld\n", attr->mq_curmsgs, cnt);
63 result = 1;
66 return result;
69 static int
70 do_one_test (mqd_t q, const char *name, int nonblock)
72 int result = 0;
74 unsigned char v []
75 = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
77 struct mq_attr attr;
78 memset (&attr, 0xaa, sizeof (attr));
79 if (mq_getattr (q, &attr) != 0)
81 printf ("mq_getattr failed: %m\n");
82 result = 1;
84 else
85 result |= check_attrs (&attr, nonblock, 0);
87 if (mq_receive (q, (char *) &v[0], 1, NULL) != -1)
89 puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
90 result = 1;
92 else if (errno != EBADF)
94 printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
95 result = 1;
98 struct timespec ts;
99 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
100 --ts.tv_sec;
101 else
103 ts.tv_sec = time (NULL) - 1;
104 ts.tv_nsec = 0;
107 int ret;
108 for (int i = 0; i < 10; ++i)
110 if (i & 1)
111 ret = mq_send (q, (char *) &v[i], 1, v[i] >> 4);
112 else
113 ret = mq_timedsend (q, (char *) &v[i], 1, v[i] >> 4, &ts);
115 if (ret)
117 printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
118 result = 1;
122 ret = mq_timedsend (q, (char *) &v[10], 1, 8, &ts);
123 if (ret != -1)
125 puts ("mq_timedsend on full queue did not fail");
126 result = 1;
128 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
130 printf ("mq_timedsend on full queue did not fail with %s: %m\n",
131 nonblock ? "EAGAIN" : "ETIMEDOUT");
132 result = 1;
135 if (nonblock)
137 ret = mq_send (q, (char *) &v[10], 1, 8);
138 if (ret != -1)
140 puts ("mq_send on full non-blocking queue did not fail");
141 result = 1;
143 else if (errno != EAGAIN)
145 printf ("mq_send on full non-blocking queue did not fail"
146 "with EAGAIN: %m\n");
147 result = 1;
151 memset (&attr, 0xaa, sizeof (attr));
152 if (mq_getattr (q, &attr) != 0)
154 printf ("mq_getattr failed: %m\n");
155 result = 1;
157 else
158 result |= check_attrs (&attr, nonblock, 10);
160 pid_t pid = fork ();
161 if (pid == -1)
163 printf ("fork failed: %m\n");
164 result = 1;
166 else if (pid == 0)
168 result = 0;
170 if (mq_close (q) != 0)
172 printf ("mq_close in child failed: %m\n");
173 result = 1;
176 q = mq_open (name, O_RDONLY | nonblock);
177 if (q == (mqd_t) -1)
179 printf ("mq_open in child failed: %m\n");
180 exit (1);
183 memset (&attr, 0xaa, sizeof (attr));
184 if (mq_getattr (q, &attr) != 0)
186 printf ("mq_getattr failed: %m\n");
187 result = 1;
189 else
190 result |= check_attrs (&attr, nonblock, 10);
192 unsigned char vr[11] = { };
193 unsigned int prio;
194 ssize_t rets;
196 if (mq_send (q, (char *) &v[0], 1, 1) != -1)
198 puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
199 result = 1;
201 else if (errno != EBADF)
203 printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
204 result = 1;
207 for (int i = 0; i < 10; ++i)
209 if (i & 1)
210 rets = mq_receive (q, (char *) &vr[i], 1, &prio);
211 else
212 rets = mq_timedreceive (q, (char *) &vr[i], 1, &prio, &ts);
214 if (rets != 1)
216 if (rets == -1)
217 printf ("mq_%sreceive failed: %m\n", (i & 1) ? "" : "timed");
218 else
219 printf ("mq_%sreceive returned %zd != 1\n",
220 (i & 1) ? "" : "timed", rets);
221 result = 1;
223 else if (prio != (unsigned int) vr[i] >> 4)
225 printf ("unexpected priority %x for value %02x\n", prio,
226 vr[i]);
227 result = 1;
231 qsort (v, 10, 1, intcmp);
232 if (memcmp (v, vr, 10) != 0)
234 puts ("messages not received in expected order");
235 result = 1;
238 rets = mq_timedreceive (q, (char *) &vr[10], 1, &prio, &ts);
239 if (rets != -1)
241 puts ("mq_timedreceive on empty queue did not fail");
242 result = 1;
244 else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
246 printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
247 nonblock ? "EAGAIN" : "ETIMEDOUT");
248 result = 1;
251 if (nonblock)
253 ret = mq_receive (q, (char *) &vr[10], 1, &prio);
254 if (ret != -1)
256 puts ("mq_receive on empty non-blocking queue did not fail");
257 result = 1;
259 else if (errno != EAGAIN)
261 printf ("mq_receive on empty non-blocking queue did not fail"
262 "with EAGAIN: %m\n");
263 result = 1;
267 memset (&attr, 0xaa, sizeof (attr));
268 if (mq_getattr (q, &attr) != 0)
270 printf ("mq_getattr failed: %m\n");
271 result = 1;
273 else
274 result |= check_attrs (&attr, nonblock, 0);
276 if (mq_close (q) != 0)
278 printf ("mq_close in child failed: %m\n");
279 result = 1;
282 exit (result);
285 int status;
286 if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
288 printf ("waitpid failed: %m\n");
289 kill (pid, SIGKILL);
290 result = 1;
292 else if (!WIFEXITED (status) || WEXITSTATUS (status))
294 printf ("child failed: %d\n", status);
295 result = 1;
298 memset (&attr, 0xaa, sizeof (attr));
299 if (mq_getattr (q, &attr) != 0)
301 printf ("mq_getattr failed: %m\n");
302 result = 1;
304 else
305 result |= check_attrs (&attr, nonblock, 0);
307 return result;
310 #define TEST_FUNCTION do_test ()
311 static int
312 do_test (void)
314 int result = 0;
316 char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
317 snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
319 struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
320 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
322 if (q == (mqd_t) -1)
324 printf ("mq_open failed with: %m\n");
325 return result;
327 else
328 add_temp_mq (name);
330 result |= do_one_test (q, name, 0);
332 mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
333 if (q2 == (mqd_t) -1)
335 printf ("mq_open failed with: %m\n");
336 q2 = q;
337 result = 1;
339 else
341 if (mq_close (q) != 0)
343 printf ("mq_close in parent failed: %m\n");
344 result = 1;
347 q = q2;
348 result |= do_one_test (q, name, O_NONBLOCK);
350 if (mq_getattr (q, &attr) != 0)
352 printf ("mq_getattr failed: %m\n");
353 result = 1;
355 else
357 attr.mq_flags ^= O_NONBLOCK;
359 struct mq_attr attr2;
360 memset (&attr2, 0x55, sizeof (attr2));
361 if (mq_setattr (q, &attr, &attr2) != 0)
363 printf ("mq_setattr failed: %m\n");
364 result = 1;
366 else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
367 || attr.mq_maxmsg != attr2.mq_maxmsg
368 || attr.mq_msgsize != attr2.mq_msgsize
369 || attr.mq_curmsgs != 0
370 || attr2.mq_curmsgs != 0)
372 puts ("mq_setattr returned unexpected values in *omqstat");
373 result = 1;
375 else
377 result |= do_one_test (q, name, 0);
379 if (mq_setattr (q, &attr2, NULL) != 0)
381 printf ("mq_setattr failed: %m\n");
382 result = 1;
384 else
385 result |= do_one_test (q, name, O_NONBLOCK);
390 if (mq_unlink (name) != 0)
392 printf ("mq_unlink failed: %m\n");
393 result = 1;
396 if (mq_close (q) != 0)
398 printf ("mq_close in parent failed: %m\n");
399 result = 1;
402 if (mq_close (q) != -1)
404 puts ("second mq_close did not fail");
405 result = 1;
407 else if (errno != EBADF)
409 printf ("second mq_close did not fail with EBADF: %m\n");
410 result = 1;
413 return result;
416 #include "../test-skeleton.c"