]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/nptl/tst-mqueue1.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / nptl / tst-mqueue1.c
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.
5
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.
10
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.
15
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/>.  */
19
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"
30
31 static int
32 intcmp (const void *a, const void *b)
33 {
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;
39 }
40
41 static int
42 check_attrs (struct mq_attr *attr, int nonblock, long cnt)
43 {
44   int result = 0;
45
46   if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
47     {
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;
52     }
53
54   if ((attr->mq_flags & O_NONBLOCK) != nonblock)
55     {
56       printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
57       result = 1;
58     }
59
60   if (attr->mq_curmsgs != cnt)
61     {
62       printf ("mq_curmsgs %ld != %ld\n", attr->mq_curmsgs, cnt);
63       result = 1;
64     }
65
66   return result;
67 }
68
69 static int
70 do_one_test (mqd_t q, const char *name, int nonblock)
71 {
72   int result = 0;
73
74   char v []
75     = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
76
77   struct mq_attr attr;
78   memset (&attr, 0xaa, sizeof (attr));
79   if (mq_getattr (q, &attr) != 0)
80     {
81       printf ("mq_getattr failed: %m\n");
82       result = 1;
83     }
84   else
85     result |= check_attrs (&attr, nonblock, 0);
86
87   if (mq_receive (q, &v[0], 1, NULL) != -1)
88     {
89       puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
90       result = 1;
91     }
92   else if (errno != EBADF)
93     {
94       printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
95       result = 1;
96     }
97
98   struct timespec ts;
99   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
100     --ts.tv_sec;
101   else
102     {
103       ts.tv_sec = time (NULL) - 1;
104       ts.tv_nsec = 0;
105     }
106
107   int ret;
108   for (int i = 0; i < 10; ++i)
109     {
110       if (i & 1)
111         ret = mq_send (q, &v[i], 1, v[i] >> 4);
112       else
113         ret = mq_timedsend (q, &v[i], 1, v[i] >> 4, &ts);
114
115       if (ret)
116         {
117           printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
118           result = 1;
119         }
120     }
121
122   ret = mq_timedsend (q, &v[10], 1, 8, &ts);
123   if (ret != -1)
124     {
125       puts ("mq_timedsend on full queue did not fail");
126       result = 1;
127     }
128   else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
129     {
130       printf ("mq_timedsend on full queue did not fail with %s: %m\n",
131               nonblock ? "EAGAIN" : "ETIMEDOUT");
132       result = 1;
133     }
134
135   if (nonblock)
136     {
137       ret = mq_send (q, &v[10], 1, 8);
138       if (ret != -1)
139         {
140           puts ("mq_send on full non-blocking queue did not fail");
141           result = 1;
142         }
143       else if (errno != EAGAIN)
144         {
145           printf ("mq_send on full non-blocking queue did not fail"
146                   "with EAGAIN: %m\n");
147           result = 1;
148         }
149     }
150
151   memset (&attr, 0xaa, sizeof (attr));
152   if (mq_getattr (q, &attr) != 0)
153     {
154       printf ("mq_getattr failed: %m\n");
155       result = 1;
156     }
157   else
158     result |= check_attrs (&attr, nonblock, 10);
159
160   pid_t pid = fork ();
161   if (pid == -1)
162     {
163       printf ("fork failed: %m\n");
164       result = 1;
165     }
166   else if (pid == 0)
167     {
168       result = 0;
169
170       if (mq_close (q) != 0)
171         {
172           printf ("mq_close in child failed: %m\n");
173           result = 1;
174         }
175
176       q = mq_open (name, O_RDONLY | nonblock);
177       if (q == (mqd_t) -1)
178         {
179           printf ("mq_open in child failed: %m\n");
180           exit (1);
181         }
182
183       memset (&attr, 0xaa, sizeof (attr));
184       if (mq_getattr (q, &attr) != 0)
185         {
186           printf ("mq_getattr failed: %m\n");
187           result = 1;
188         }
189       else
190         result |= check_attrs (&attr, nonblock, 10);
191
192       char vr[11] = { };
193       unsigned int prio;
194       ssize_t rets;
195
196       if (mq_send (q, &v[0], 1, 1) != -1)
197         {
198           puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
199           result = 1;
200         }
201       else if (errno != EBADF)
202         {
203           printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
204           result = 1;
205         }
206
207       for (int i = 0; i < 10; ++i)
208         {
209           if (i & 1)
210             rets = mq_receive (q, &vr[i], 1, &prio);
211           else
212             rets = mq_timedreceive (q, &vr[i], 1, &prio, &ts);
213
214           if (rets != 1)
215             {
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;
222             }
223           else if (prio != (unsigned int) vr[i] >> 4)
224             {
225               printf ("unexpected priority %x for value %02x\n", prio,
226                       vr[i]);
227               result = 1;
228             }
229         }
230
231       qsort (v, 10, 1, intcmp);
232       if (memcmp (v, vr, 10) != 0)
233         {
234           puts ("messages not received in expected order");
235           result = 1;
236         }
237
238       rets = mq_timedreceive (q, &vr[10], 1, &prio, &ts);
239       if (rets != -1)
240         {
241           puts ("mq_timedreceive on empty queue did not fail");
242           result = 1;
243         }
244       else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
245         {
246           printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
247                   nonblock ? "EAGAIN" : "ETIMEDOUT");
248           result = 1;
249         }
250
251       if (nonblock)
252         {
253           ret = mq_receive (q, &vr[10], 1, &prio);
254           if (ret != -1)
255             {
256               puts ("mq_receive on empty non-blocking queue did not fail");
257               result = 1;
258             }
259           else if (errno != EAGAIN)
260             {
261               printf ("mq_receive on empty non-blocking queue did not fail"
262                       "with EAGAIN: %m\n");
263               result = 1;
264             }
265         }
266
267       memset (&attr, 0xaa, sizeof (attr));
268       if (mq_getattr (q, &attr) != 0)
269         {
270           printf ("mq_getattr failed: %m\n");
271           result = 1;
272         }
273       else
274         result |= check_attrs (&attr, nonblock, 0);
275
276       if (mq_close (q) != 0)
277         {
278           printf ("mq_close in child failed: %m\n");
279           result = 1;
280         }
281
282       exit (result);
283     }
284
285   int status;
286   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
287     {
288       printf ("waitpid failed: %m\n");
289       kill (pid, SIGKILL);
290       result = 1;
291     }
292   else if (!WIFEXITED (status) || WEXITSTATUS (status))
293     {
294       printf ("child failed: %d\n", status);
295       result = 1;
296     }
297
298   memset (&attr, 0xaa, sizeof (attr));
299   if (mq_getattr (q, &attr) != 0)
300     {
301       printf ("mq_getattr failed: %m\n");
302       result = 1;
303     }
304   else
305     result |= check_attrs (&attr, nonblock, 0);
306
307   return result;
308 }
309
310 #define TEST_FUNCTION do_test ()
311 static int
312 do_test (void)
313 {
314   int result = 0;
315
316   char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
317   snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
318
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);
321
322   if (q == (mqd_t) -1)
323     {
324       printf ("mq_open failed with: %m\n");
325       return result;
326     }
327   else
328     add_temp_mq (name);
329
330   result |= do_one_test (q, name, 0);
331
332   mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
333   if (q2 == (mqd_t) -1)
334     {
335       printf ("mq_open failed with: %m\n");
336       q2 = q;
337       result = 1;
338     }
339   else
340     {
341       if (mq_close (q) != 0)
342         {
343           printf ("mq_close in parent failed: %m\n");
344           result = 1;
345         }
346
347       q = q2;
348       result |= do_one_test (q, name, O_NONBLOCK);
349
350       if (mq_getattr (q, &attr) != 0)
351         {
352           printf ("mq_getattr failed: %m\n");
353           result = 1;
354         }
355       else
356         {
357           attr.mq_flags ^= O_NONBLOCK;
358
359           struct mq_attr attr2;
360           memset (&attr2, 0x55, sizeof (attr2));
361           if (mq_setattr (q, &attr, &attr2) != 0)
362             {
363               printf ("mq_setattr failed: %m\n");
364               result = 1;
365             }
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)
371             {
372               puts ("mq_setattr returned unexpected values in *omqstat");
373               result = 1;
374             }
375           else
376             {
377               result |= do_one_test (q, name, 0);
378
379               if (mq_setattr (q, &attr2, NULL) != 0)
380                 {
381                   printf ("mq_setattr failed: %m\n");
382                   result = 1;
383                 }
384               else
385                 result |= do_one_test (q, name, O_NONBLOCK);
386             }
387         }
388     }
389
390   if (mq_unlink (name) != 0)
391     {
392       printf ("mq_unlink failed: %m\n");
393       result = 1;
394     }
395
396   if (mq_close (q) != 0)
397     {
398       printf ("mq_close in parent failed: %m\n");
399       result = 1;
400     }
401
402   if (mq_close (q) != -1)
403     {
404       puts ("second mq_close did not fail");
405       result = 1;
406     }
407   else if (errno != EBADF)
408     {
409       printf ("second mq_close did not fail with EBADF: %m\n");
410       result = 1;
411     }
412
413   return result;
414 }
415
416 #include "../test-skeleton.c"