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