source: branches/libc-0.6/src/libctests/glibc/rt/tst-mqueue2.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#include <errno.h>
22#include <fcntl.h>
23#include <mqueue.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/time.h>
29#include <sys/wait.h>
30#include <time.h>
31#include <unistd.h>
32#include "tst-mqueue.h"
33
34static void
35alrm_handler (int sig)
36{
37}
38
39#define TIMEOUT 10
40#define TEST_FUNCTION do_test ()
41static int
42do_test (void)
43{
44 int result = 0;
45
46 char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
47 snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());
48
49 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
50 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
51
52 if (q == (mqd_t) -1)
53 {
54 printf ("mq_open failed with: %m\n");
55 return result;
56 }
57 else
58 add_temp_mq (name);
59
60 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
61 if (q2 != (mqd_t) -1)
62 {
63 puts ("mq_open with O_EXCL unexpectedly succeeded");
64 result = 1;
65 }
66 else if (errno != EEXIST)
67 {
68 printf ("mq_open did not fail with EEXIST: %m\n");
69 result = 1;
70 }
71
72 char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
73 snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());
74
75 attr.mq_maxmsg = -2;
76 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
77 if (q2 != (mqd_t) -1)
78 {
79 puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
80 add_temp_mq (name2);
81 result = 1;
82 }
83 else if (errno != EINVAL)
84 {
85 printf ("mq_open with invalid mq_maxmsg did not fail with "
86 "EINVAL: %m\n");
87 result = 1;
88 }
89
90 attr.mq_maxmsg = 2;
91 attr.mq_msgsize = -56;
92 q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
93 if (q2 != (mqd_t) -1)
94 {
95 puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
96 add_temp_mq (name2);
97 result = 1;
98 }
99 else if (errno != EINVAL)
100 {
101 printf ("mq_open with invalid mq_msgsize did not fail with "
102 "EINVAL: %m\n");
103 result = 1;
104 }
105
106 char buf[3];
107 struct timespec ts;
108 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
109 ts.tv_sec += 10;
110 else
111 {
112 ts.tv_sec = time (NULL) + 10;
113 ts.tv_nsec = 0;
114 }
115
116 if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
117 {
118 puts ("mq_timedreceive with too small msg_len did not fail");
119 result = 1;
120 }
121 else if (errno != EMSGSIZE)
122 {
123 printf ("mq_timedreceive with too small msg_len did not fail with "
124 "EMSGSIZE: %m\n");
125 result = 1;
126 }
127
128 ts.tv_nsec = -1;
129 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
130 {
131 puts ("mq_timedreceive with negative tv_nsec did not fail");
132 result = 1;
133 }
134 else if (errno != EINVAL)
135 {
136 printf ("mq_timedreceive with negative tv_nsec did not fail with "
137 "EINVAL: %m\n");
138 result = 1;
139 }
140
141 ts.tv_nsec = 1000000000;
142 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
143 {
144 puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
145 result = 1;
146 }
147 else if (errno != EINVAL)
148 {
149 printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
150 "EINVAL: %m\n");
151 result = 1;
152 }
153
154 struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
155 sigemptyset (&sa.sa_mask);
156 sigaction (SIGALRM, &sa, NULL);
157
158 struct itimerval it = { .it_value = { .tv_sec = 1 } };
159 setitimer (ITIMER_REAL, &it, NULL);
160
161 if (mq_receive (q, buf, 2, NULL) == 0)
162 {
163 puts ("mq_receive on empty queue did not block");
164 result = 1;
165 }
166 else if (errno != EINTR)
167 {
168 printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
169 result = 1;
170 }
171
172 setitimer (ITIMER_REAL, &it, NULL);
173
174 ts.tv_nsec = 0;
175 if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
176 {
177 puts ("mq_timedreceive on empty queue did not block");
178 result = 1;
179 }
180 else if (errno != EINTR)
181 {
182 printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
183 result = 1;
184 }
185
186 buf[0] = '6';
187 buf[1] = '7';
188 if (mq_send (q, buf, 2, 3) != 0
189 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
190 {
191 printf ("mq_send failed: %m\n");
192 result = 1;
193 }
194
195 memset (buf, ' ', sizeof (buf));
196
197 unsigned int prio;
198 ssize_t rets = mq_receive (q, buf, 3, &prio);
199 if (rets != 1)
200 {
201 if (rets == -1)
202 printf ("mq_receive failed: %m\n");
203 else
204 printf ("mq_receive returned %zd != 1\n", rets);
205 result = 1;
206 }
207 else if (prio != 4 || memcmp (buf, "8 ", 3) != 0)
208 {
209 printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n",
210 prio, buf[0], buf[1], buf[2]);
211 result = 1;
212 }
213
214 rets = mq_receive (q, buf, 2, NULL);
215 if (rets != 2)
216 {
217 if (rets == -1)
218 printf ("mq_receive failed: %m\n");
219 else
220 printf ("mq_receive returned %zd != 2\n", rets);
221 result = 1;
222 }
223 else if (memcmp (buf, "67 ", 3) != 0)
224 {
225 printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
226 buf[0], buf[1], buf[2]);
227 result = 1;
228 }
229
230 buf[0] = '2';
231 buf[1] = '1';
232 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
233 ts.tv_sec = time (NULL);
234 ts.tv_nsec = -1000000001;
235 if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
236 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
237 || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
238 (mq_timedsend (q, buf, 1, 4, &ts) != 0
239 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
240 {
241 printf ("mq_timedsend failed: %m\n");
242 result = 1;
243 }
244
245 buf[0] = '-';
246 ts.tv_nsec = 1000000001;
247 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
248 {
249 puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
250 result = 1;
251 }
252 else if (errno != EINVAL)
253 {
254 printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
255 "EINVAL: %m\n");
256 result = 1;
257 }
258
259 ts.tv_nsec = -2;
260 if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
261 {
262 puts ("mq_timedsend with negative tv_nsec did not fail");
263 result = 1;
264 }
265 else if (errno != EINVAL)
266 {
267 printf ("mq_timedsend with megatove tv_nsec did not fail with "
268 "EINVAL: %m\n");
269 result = 1;
270 }
271
272 setitimer (ITIMER_REAL, &it, NULL);
273
274 if (mq_send (q, buf, 2, 8) == 0)
275 {
276 puts ("mq_send on full queue did not block");
277 result = 1;
278 }
279 else if (errno != EINTR)
280 {
281 printf ("mq_send on full queue did not fail with EINTR: %m\n");
282 result = 1;
283 }
284
285 setitimer (ITIMER_REAL, &it, NULL);
286
287 ts.tv_sec += 10;
288 ts.tv_nsec = 0;
289 if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
290 {
291 puts ("mq_timedsend on full queue did not block");
292 result = 1;
293 }
294 else if (errno != EINTR)
295 {
296 printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
297 result = 1;
298 }
299
300 memset (buf, ' ', sizeof (buf));
301
302 if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
303 ts.tv_sec = time (NULL);
304 ts.tv_nsec = -1000000001;
305 rets = mq_timedreceive (q, buf, 2, &prio, &ts);
306 if (rets == -1 && errno == EINVAL)
307 rets = mq_receive (q, buf, 2, &prio);
308 if (rets != 2)
309 {
310 if (rets == -1)
311 printf ("mq_timedreceive failed: %m\n");
312 else
313 printf ("mq_timedreceive returned %zd != 2\n", rets);
314 result = 1;
315 }
316 else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
317 {
318 printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
319 prio, buf[0], buf[1], buf[2]);
320 result = 1;
321 }
322
323 if (mq_receive (q, buf, 1, NULL) == 0)
324 {
325 puts ("mq_receive with too small msg_len did not fail");
326 result = 1;
327 }
328 else if (errno != EMSGSIZE)
329 {
330 printf ("mq_receive with too small msg_len did not fail with "
331 "EMSGSIZE: %m\n");
332 result = 1;
333 }
334
335 ts.tv_nsec = -ts.tv_nsec;
336 rets = mq_timedreceive (q, buf, 2, NULL, &ts);
337 if (rets == -1 && errno == EINVAL)
338 rets = mq_receive (q, buf, 2, NULL);
339 if (rets != 1)
340 {
341 if (rets == -1)
342 printf ("mq_timedreceive failed: %m\n");
343 else
344 printf ("mq_timedreceive returned %zd != 1\n", rets);
345 result = 1;
346 }
347 else if (memcmp (buf, "31 ", 3) != 0)
348 {
349 printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
350 buf[0], buf[1], buf[2]);
351 result = 1;
352 }
353
354 if (mq_send (q, "", 0, 2) != 0)
355 {
356 printf ("mq_send with msg_len 0 failed: %m\n");
357 result = 1;
358 }
359
360 rets = mq_receive (q, buf, 2, &prio);
361 if (rets)
362 {
363 if (rets == -1)
364 printf ("mq_receive failed: %m\n");
365 else
366 printf ("mq_receive returned %zd != 0\n", rets);
367 result = 1;
368 }
369
370 long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
371 if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
372 {
373 if (mq_send (q, buf, 1, mq_prio_max) == 0)
374 {
375 puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
376 result = 1;
377 }
378 else if (errno != EINVAL)
379 {
380 printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
381 "EINVAL: %m\n");
382 result = 1;
383 }
384
385 if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
386 {
387 printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
388 result = 1;
389 }
390 }
391
392 if (mq_unlink (name) != 0)
393 {
394 printf ("mq_unlink failed: %m\n");
395 result = 1;
396 }
397
398 q2 = mq_open (name, O_RDWR);
399 if (q2 != (mqd_t) -1)
400 {
401 printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
402 "succeeded\n", name);
403 result = 1;
404 }
405 else if (errno != ENOENT)
406 {
407 printf ("mq_open of unlinked %s without O_CREAT did not fail with "
408 "ENOENT: %m\n", name);
409 result = 1;
410 }
411
412 if (mq_close (q) != 0)
413 {
414 printf ("mq_close in parent failed: %m\n");
415 result = 1;
416 }
417
418 if (mq_receive (q, buf, 2, NULL) == 0)
419 {
420 puts ("mq_receive on invalid mqd_t did not fail");
421 result = 1;
422 }
423 else if (errno != EBADF)
424 {
425 printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
426 result = 1;
427 }
428
429 if (mq_send (q, buf, 1, 2) == 0)
430 {
431 puts ("mq_send on invalid mqd_t did not fail");
432 result = 1;
433 }
434 else if (errno != EBADF)
435 {
436 printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
437 result = 1;
438 }
439
440 if (mq_getattr (q, &attr) == 0)
441 {
442 puts ("mq_getattr on invalid mqd_t did not fail");
443 result = 1;
444 }
445 else if (errno != EBADF)
446 {
447 printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
448 result = 1;
449 }
450
451 memset (&attr, 0, sizeof (attr));
452 if (mq_setattr (q, &attr, NULL) == 0)
453 {
454 puts ("mq_setattr on invalid mqd_t did not fail");
455 result = 1;
456 }
457 else if (errno != EBADF)
458 {
459 printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
460 result = 1;
461 }
462
463 if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
464 {
465 puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
466 result = 1;
467 }
468 else if (errno != ENOENT)
469 {
470 printf ("mq_unlink of non-existant message queue did not fail with "
471 "ENOENT: %m\n");
472 result = 1;
473 }
474 return result;
475}
476
477#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.