1 /* Test message queue passing.
2    Copyright (C) 2004-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <mqueue.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include "tst-mqueue.h"
30 
31 static int
intcmp(const void * a,const void * b)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
check_attrs(struct mq_attr * attr,int nonblock,long cnt)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 %jd, mq_msgsize %jd\n",
50 	      (intmax_t) attr->mq_maxmsg, (intmax_t) attr->mq_msgsize);
51       result = 1;
52     }
53 
54   if ((attr->mq_flags & O_NONBLOCK) != nonblock)
55     {
56       printf ("mq_flags %jx != %x\n",
57 	      (intmax_t) (attr->mq_flags & O_NONBLOCK), nonblock);
58       result = 1;
59     }
60 
61   if (attr->mq_curmsgs != cnt)
62     {
63       printf ("mq_curmsgs %jd != %ld\n", (intmax_t) attr->mq_curmsgs, cnt);
64       result = 1;
65     }
66 
67   return result;
68 }
69 
70 static int
do_one_test(mqd_t q,const char * name,int nonblock)71 do_one_test (mqd_t q, const char *name, int nonblock)
72 {
73   int result = 0;
74 
75   unsigned char v []
76     = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
77 
78   struct mq_attr attr;
79   memset (&attr, 0xaa, sizeof (attr));
80   if (mq_getattr (q, &attr) != 0)
81     {
82       printf ("mq_getattr failed: %m\n");
83       result = 1;
84     }
85   else
86     result |= check_attrs (&attr, nonblock, 0);
87 
88   if (mq_receive (q, (char *) &v[0], 1, NULL) != -1)
89     {
90       puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
91       result = 1;
92     }
93   else if (errno != EBADF)
94     {
95       printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
96       result = 1;
97     }
98 
99   struct timespec ts;
100   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
101     --ts.tv_sec;
102   else
103     {
104       ts.tv_sec = time (NULL) - 1;
105       ts.tv_nsec = 0;
106     }
107 
108   int ret;
109   for (int i = 0; i < 10; ++i)
110     {
111       if (i & 1)
112 	ret = mq_send (q, (char *) &v[i], 1, v[i] >> 4);
113       else
114 	ret = mq_timedsend (q, (char *) &v[i], 1, v[i] >> 4, &ts);
115 
116       if (ret)
117 	{
118 	  printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
119 	  result = 1;
120 	}
121     }
122 
123   ret = mq_timedsend (q, (char *) &v[10], 1, 8, &ts);
124   if (ret != -1)
125     {
126       puts ("mq_timedsend on full queue did not fail");
127       result = 1;
128     }
129   else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
130     {
131       printf ("mq_timedsend on full queue did not fail with %s: %m\n",
132 	      nonblock ? "EAGAIN" : "ETIMEDOUT");
133       result = 1;
134     }
135 
136   if (nonblock)
137     {
138       ret = mq_send (q, (char *) &v[10], 1, 8);
139       if (ret != -1)
140 	{
141 	  puts ("mq_send on full non-blocking queue did not fail");
142 	  result = 1;
143 	}
144       else if (errno != EAGAIN)
145 	{
146 	  printf ("mq_send on full non-blocking queue did not fail"
147 		  "with EAGAIN: %m\n");
148 	  result = 1;
149 	}
150     }
151 
152   memset (&attr, 0xaa, sizeof (attr));
153   if (mq_getattr (q, &attr) != 0)
154     {
155       printf ("mq_getattr failed: %m\n");
156       result = 1;
157     }
158   else
159     result |= check_attrs (&attr, nonblock, 10);
160 
161   pid_t pid = fork ();
162   if (pid == -1)
163     {
164       printf ("fork failed: %m\n");
165       result = 1;
166     }
167   else if (pid == 0)
168     {
169       result = 0;
170 
171       if (mq_close (q) != 0)
172 	{
173 	  printf ("mq_close in child failed: %m\n");
174 	  result = 1;
175 	}
176 
177       q = mq_open (name, O_RDONLY | nonblock);
178       if (q == (mqd_t) -1)
179         {
180 	  printf ("mq_open in child failed: %m\n");
181 	  exit (1);
182         }
183 
184       memset (&attr, 0xaa, sizeof (attr));
185       if (mq_getattr (q, &attr) != 0)
186 	{
187 	  printf ("mq_getattr failed: %m\n");
188 	  result = 1;
189 	}
190       else
191 	result |= check_attrs (&attr, nonblock, 10);
192 
193       unsigned char vr[11] = { };
194       unsigned int prio;
195       ssize_t rets;
196 
197       if (mq_send (q, (char *) &v[0], 1, 1) != -1)
198 	{
199 	  puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
200 	  result = 1;
201 	}
202       else if (errno != EBADF)
203 	{
204 	  printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
205 	  result = 1;
206 	}
207 
208       for (int i = 0; i < 10; ++i)
209 	{
210 	  if (i & 1)
211 	    rets = mq_receive (q, (char *) &vr[i], 1, &prio);
212 	  else
213 	    rets = mq_timedreceive (q, (char *) &vr[i], 1, &prio, &ts);
214 
215 	  if (rets != 1)
216 	    {
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;
223 	    }
224 	  else if (prio != (unsigned int) vr[i] >> 4)
225 	    {
226 	      printf ("unexpected priority %x for value %02x\n", prio,
227 		      vr[i]);
228 	      result = 1;
229 	    }
230 	}
231 
232       qsort (v, 10, 1, intcmp);
233       if (memcmp (v, vr, 10) != 0)
234 	{
235 	  puts ("messages not received in expected order");
236 	  result = 1;
237 	}
238 
239       rets = mq_timedreceive (q, (char *) &vr[10], 1, &prio, &ts);
240       if (rets != -1)
241 	{
242 	  puts ("mq_timedreceive on empty queue did not fail");
243 	  result = 1;
244 	}
245       else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
246 	{
247 	  printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
248 		  nonblock ? "EAGAIN" : "ETIMEDOUT");
249 	  result = 1;
250 	}
251 
252       if (nonblock)
253 	{
254 	  ret = mq_receive (q, (char *) &vr[10], 1, &prio);
255 	  if (ret != -1)
256 	    {
257 	      puts ("mq_receive on empty non-blocking queue did not fail");
258 	      result = 1;
259 	    }
260 	  else if (errno != EAGAIN)
261 	    {
262 	      printf ("mq_receive on empty non-blocking queue did not fail"
263 		      "with EAGAIN: %m\n");
264 	      result = 1;
265 	    }
266 	}
267 
268       memset (&attr, 0xaa, sizeof (attr));
269       if (mq_getattr (q, &attr) != 0)
270 	{
271 	  printf ("mq_getattr failed: %m\n");
272 	  result = 1;
273 	}
274       else
275 	result |= check_attrs (&attr, nonblock, 0);
276 
277       if (mq_close (q) != 0)
278 	{
279 	  printf ("mq_close in child failed: %m\n");
280 	  result = 1;
281 	}
282 
283       exit (result);
284     }
285 
286   int status;
287   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
288     {
289       printf ("waitpid failed: %m\n");
290       kill (pid, SIGKILL);
291       result = 1;
292     }
293   else if (!WIFEXITED (status) || WEXITSTATUS (status))
294     {
295       printf ("child failed: %d\n", status);
296       result = 1;
297     }
298 
299   memset (&attr, 0xaa, sizeof (attr));
300   if (mq_getattr (q, &attr) != 0)
301     {
302       printf ("mq_getattr failed: %m\n");
303       result = 1;
304     }
305   else
306     result |= check_attrs (&attr, nonblock, 0);
307 
308   return result;
309 }
310 
311 #define TEST_FUNCTION do_test ()
312 static int
do_test(void)313 do_test (void)
314 {
315   int result = 0;
316 
317   char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
318   snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
319 
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);
322 
323   if (q == (mqd_t) -1)
324     {
325       printf ("mq_open failed with: %m\n");
326       return result;
327     }
328   else
329     add_temp_mq (name);
330 
331   result |= do_one_test (q, name, 0);
332 
333   mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
334   if (q2 == (mqd_t) -1)
335     {
336       printf ("mq_open failed with: %m\n");
337       q2 = q;
338       result = 1;
339     }
340   else
341     {
342       if (mq_close (q) != 0)
343 	{
344 	  printf ("mq_close in parent failed: %m\n");
345 	  result = 1;
346 	}
347 
348       q = q2;
349       result |= do_one_test (q, name, O_NONBLOCK);
350 
351       if (mq_getattr (q, &attr) != 0)
352 	{
353 	  printf ("mq_getattr failed: %m\n");
354 	  result = 1;
355 	}
356       else
357 	{
358 	  attr.mq_flags ^= O_NONBLOCK;
359 
360 	  struct mq_attr attr2;
361 	  memset (&attr2, 0x55, sizeof (attr2));
362 	  if (mq_setattr (q, &attr, &attr2) != 0)
363 	    {
364 	      printf ("mq_setattr failed: %m\n");
365 	      result = 1;
366 	    }
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)
372 	    {
373 	      puts ("mq_setattr returned unexpected values in *omqstat");
374 	      result = 1;
375 	    }
376 	  else
377 	    {
378 	      result |= do_one_test (q, name, 0);
379 
380 	      if (mq_setattr (q, &attr2, NULL) != 0)
381 		{
382 		  printf ("mq_setattr failed: %m\n");
383 		  result = 1;
384 		}
385 	      else
386 		result |= do_one_test (q, name, O_NONBLOCK);
387 	    }
388 	}
389     }
390 
391   if (mq_unlink (name) != 0)
392     {
393       printf ("mq_unlink failed: %m\n");
394       result = 1;
395     }
396 
397   if (mq_close (q) != 0)
398     {
399       printf ("mq_close in parent failed: %m\n");
400       result = 1;
401     }
402 
403   if (mq_close (q) != -1)
404     {
405       puts ("second mq_close did not fail");
406       result = 1;
407     }
408   else if (errno != EBADF)
409     {
410       printf ("second mq_close did not fail with EBADF: %m\n");
411       result = 1;
412     }
413 
414   return result;
415 }
416 
417 #include "../test-skeleton.c"
418