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 <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdint.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 
34 #define TEST_FUNCTION do_test ()
35 static int
do_test(void)36 do_test (void)
37 {
38   int result = 0;
39 
40   char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
41   char *p;
42   p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
43   struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
44   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
45 
46   if (q == (mqd_t) -1)
47     {
48       printf ("mq_open failed with: %m\n");
49       return result;
50     }
51   else
52     add_temp_mq (name);
53 
54   *p = '.';
55   memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
56   name[NAME_MAX + 1] = '\0';
57 
58   mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
59   if (q2 == (mqd_t) -1)
60     {
61       printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
62       result = 1;
63     }
64 
65   if (mq_unlink (name) != 0)
66     {
67       printf ("mq_unlink failed: %m\n");
68       result = 1;
69     }
70 
71   if (mq_close (q2) != 0)
72     {
73       printf ("mq_close failed: %m\n");
74       result = 1;
75     }
76 
77   name[NAME_MAX + 1] = 'x';
78   name[NAME_MAX + 2] = '\0';
79   q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
80   if (q2 != (mqd_t) -1)
81     {
82       puts ("mq_open with too long name component unexpectedly succeeded");
83       mq_unlink (name);
84       mq_close (q2);
85       result = 1;
86     }
87   else if (errno != ENAMETOOLONG)
88     {
89       printf ("mq_open with too long name component did not fail with "
90 	      "ENAMETOOLONG: %m\n");
91       result = 1;
92     }
93 
94   if (mq_unlink (name) == 0)
95     {
96       puts ("mq_unlink with too long name component unexpectedly succeeded");
97       result = 1;
98     }
99   else if (errno != ENAMETOOLONG)
100     {
101       printf ("mq_unlink with too long name component did not fail with "
102 	      "ENAMETOOLONG: %m\n");
103       result = 1;
104     }
105 
106   *p = '\0';
107   attr.mq_maxmsg = 1;
108   attr.mq_msgsize = 3;
109   q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
110   if (q2 == (mqd_t) -1)
111     {
112       printf ("mq_open without O_EXCL failed with %m\n");
113       result = 1;
114     }
115 
116   char buf[3];
117   strcpy (buf, "jk");
118   if (mq_send (q, buf, 2, 4) != 0)
119     {
120       printf ("mq_send failed: %m\n");
121       result = 1;
122     }
123 
124   if (mq_send (q, buf + 1, 1, 5) != 0)
125     {
126       printf ("mq_send failed: %m\n");
127       result = 1;
128     }
129 
130   if (mq_getattr (q2, &attr) != 0)
131     {
132       printf ("mq_getattr failed: %m\n");
133       result = 1;
134     }
135 
136   if ((attr.mq_flags & O_NONBLOCK)
137       || attr.mq_maxmsg != 2
138       || attr.mq_msgsize != 2
139       || attr.mq_curmsgs != 2)
140     {
141       printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
142 	      ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
143 	      (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
144 	      (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
145       result = 1;
146     }
147 
148   struct timespec ts;
149   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
150     ++ts.tv_sec;
151   else
152     {
153       ts.tv_sec = time (NULL) + 1;
154       ts.tv_nsec = 0;
155     }
156 
157   if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
158     {
159       puts ("mq_timedsend unexpectedly succeeded");
160       result = 1;
161     }
162   else if (errno != ETIMEDOUT)
163     {
164       printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
165       result = 1;
166     }
167 
168   if (mq_close (q2) != 0)
169     {
170       printf ("mq_close failed: %m\n");
171       result = 1;
172     }
173 
174   q2 = mq_open (name, O_RDONLY, 0600);
175   if (q2 == (mqd_t) -1)
176     {
177       printf ("mq_open without O_CREAT failed with %m\n");
178       result = 1;
179     }
180 
181   mqd_t q3 = mq_open (name, O_RDONLY, 0600);
182   if (q3 == (mqd_t) -1)
183     {
184       printf ("mq_open without O_CREAT failed with %m\n");
185       result = 1;
186     }
187 
188   memset (buf, ' ', sizeof (buf));
189 
190   unsigned int prio;
191   ssize_t rets = mq_receive (q2, buf, 2, &prio);
192   if (rets != 1)
193     {
194       if (rets == -1)
195 	printf ("mq_receive failed with: %m\n");
196       else
197 	printf ("mq_receive returned %zd != 1\n", rets);
198       result = 1;
199     }
200   else if (prio != 5 || memcmp (buf, "k  ", 3) != 0)
201     {
202       printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k  \")\n",
203 	      prio, buf[0], buf[1], buf[2]);
204       result = 1;
205     }
206 
207   if (mq_getattr (q3, &attr) != 0)
208     {
209       printf ("mq_getattr failed: %m\n");
210       result = 1;
211     }
212 
213   if ((attr.mq_flags & O_NONBLOCK)
214       || attr.mq_maxmsg != 2
215       || attr.mq_msgsize != 2
216       || attr.mq_curmsgs != 1)
217     {
218       printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
219 	      ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
220 	      (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
221 	      (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
222       result = 1;
223     }
224 
225   rets = mq_receive (q3, buf, 2, NULL);
226   if (rets != 2)
227     {
228       if (rets == -1)
229 	printf ("mq_receive failed with: %m\n");
230       else
231 	printf ("mq_receive returned %zd != 2\n", rets);
232       result = 1;
233     }
234   else if (memcmp (buf, "jk ", 3) != 0)
235     {
236       printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
237 	      buf[0], buf[1], buf[2]);
238       result = 1;
239     }
240 
241   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
242     ++ts.tv_sec;
243   else
244     {
245       ts.tv_sec = time (NULL) + 1;
246       ts.tv_nsec = 0;
247     }
248 
249   if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
250     {
251       puts ("mq_timedreceive on empty queue unexpectedly succeeded");
252       result = 1;
253     }
254   else if (errno != ETIMEDOUT)
255     {
256       printf ("mq_timedreceive on empty queue did not fail with "
257 	      "ETIMEDOUT: %m\n");
258       result = 1;
259     }
260 
261   if (mq_unlink (name) != 0)
262     {
263       printf ("mq_unlink failed: %m\n");
264       result = 1;
265     }
266 
267   if (mq_close (q) != 0)
268     {
269       printf ("mq_close failed: %m\n");
270       result = 1;
271     }
272 
273   if (mq_close (q2) != 0)
274     {
275       printf ("mq_close failed: %m\n");
276       result = 1;
277     }
278 
279   if (mq_close (q3) != 0)
280     {
281       printf ("mq_close failed: %m\n");
282       result = 1;
283     }
284 
285   return result;
286 }
287 
288 #include "../test-skeleton.c"
289