Benchtests: Remove simple_str(n)casecmp
[glibc.git] / rt / tst-mqueue4.c
blob841ce14ac64e06fb910c4b781c6619e24d45700a
1 /* Test message queue passing.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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 <support/check.h>
33 #include "tst-mqueue.h"
35 #define TEST_FUNCTION do_test ()
36 static int
37 do_test (void)
39 int result = 0;
41 char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
42 char *p;
43 p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
44 struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
45 mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
47 if (q == (mqd_t) -1)
49 if (errno == ENOSYS)
50 FAIL_UNSUPPORTED ("mq_open not supported");
52 printf ("mq_open failed with: %m\n");
53 return 1;
56 add_temp_mq (name);
58 *p = '.';
59 memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
60 name[NAME_MAX + 1] = '\0';
62 mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
63 if (q2 == (mqd_t) -1)
65 printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
66 result = 1;
69 if (mq_unlink (name) != 0)
71 printf ("mq_unlink failed: %m\n");
72 result = 1;
75 if (mq_close (q2) != 0)
77 printf ("mq_close failed: %m\n");
78 result = 1;
81 name[NAME_MAX + 1] = 'x';
82 name[NAME_MAX + 2] = '\0';
83 q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
84 if (q2 != (mqd_t) -1)
86 puts ("mq_open with too long name component unexpectedly succeeded");
87 mq_unlink (name);
88 mq_close (q2);
89 result = 1;
91 else if (errno != ENAMETOOLONG)
93 printf ("mq_open with too long name component did not fail with "
94 "ENAMETOOLONG: %m\n");
95 result = 1;
98 if (mq_unlink (name) == 0)
100 puts ("mq_unlink with too long name component unexpectedly succeeded");
101 result = 1;
103 else if (errno != ENAMETOOLONG)
105 printf ("mq_unlink with too long name component did not fail with "
106 "ENAMETOOLONG: %m\n");
107 result = 1;
110 *p = '\0';
111 attr.mq_maxmsg = 1;
112 attr.mq_msgsize = 3;
113 q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
114 if (q2 == (mqd_t) -1)
116 printf ("mq_open without O_EXCL failed with %m\n");
117 result = 1;
120 char buf[3];
121 strcpy (buf, "jk");
122 if (mq_send (q, buf, 2, 4) != 0)
124 printf ("mq_send failed: %m\n");
125 result = 1;
128 if (mq_send (q, buf + 1, 1, 5) != 0)
130 printf ("mq_send failed: %m\n");
131 result = 1;
134 if (mq_getattr (q2, &attr) != 0)
136 printf ("mq_getattr failed: %m\n");
137 result = 1;
140 if ((attr.mq_flags & O_NONBLOCK)
141 || attr.mq_maxmsg != 2
142 || attr.mq_msgsize != 2
143 || attr.mq_curmsgs != 2)
145 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
146 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
147 (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
148 (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
149 result = 1;
152 struct timespec ts;
153 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
154 ++ts.tv_sec;
155 else
157 ts.tv_sec = time (NULL) + 1;
158 ts.tv_nsec = 0;
161 if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
163 puts ("mq_timedsend unexpectedly succeeded");
164 result = 1;
166 else if (errno != ETIMEDOUT)
168 printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
169 result = 1;
172 if (mq_close (q2) != 0)
174 printf ("mq_close failed: %m\n");
175 result = 1;
178 q2 = mq_open (name, O_RDONLY, 0600);
179 if (q2 == (mqd_t) -1)
181 printf ("mq_open without O_CREAT failed with %m\n");
182 result = 1;
185 mqd_t q3 = mq_open (name, O_RDONLY, 0600);
186 if (q3 == (mqd_t) -1)
188 printf ("mq_open without O_CREAT failed with %m\n");
189 result = 1;
192 memset (buf, ' ', sizeof (buf));
194 unsigned int prio;
195 ssize_t rets = mq_receive (q2, buf, 2, &prio);
196 if (rets != 1)
198 if (rets == -1)
199 printf ("mq_receive failed with: %m\n");
200 else
201 printf ("mq_receive returned %zd != 1\n", rets);
202 result = 1;
204 else if (prio != 5 || memcmp (buf, "k ", 3) != 0)
206 printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n",
207 prio, buf[0], buf[1], buf[2]);
208 result = 1;
211 if (mq_getattr (q3, &attr) != 0)
213 printf ("mq_getattr failed: %m\n");
214 result = 1;
217 if ((attr.mq_flags & O_NONBLOCK)
218 || attr.mq_maxmsg != 2
219 || attr.mq_msgsize != 2
220 || attr.mq_curmsgs != 1)
222 printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
223 ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
224 (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
225 (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
226 result = 1;
229 rets = mq_receive (q3, buf, 2, NULL);
230 if (rets != 2)
232 if (rets == -1)
233 printf ("mq_receive failed with: %m\n");
234 else
235 printf ("mq_receive returned %zd != 2\n", rets);
236 result = 1;
238 else if (memcmp (buf, "jk ", 3) != 0)
240 printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
241 buf[0], buf[1], buf[2]);
242 result = 1;
245 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
246 ++ts.tv_sec;
247 else
249 ts.tv_sec = time (NULL) + 1;
250 ts.tv_nsec = 0;
253 if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
255 puts ("mq_timedreceive on empty queue unexpectedly succeeded");
256 result = 1;
258 else if (errno != ETIMEDOUT)
260 printf ("mq_timedreceive on empty queue did not fail with "
261 "ETIMEDOUT: %m\n");
262 result = 1;
265 if (mq_unlink (name) != 0)
267 printf ("mq_unlink failed: %m\n");
268 result = 1;
271 if (mq_close (q) != 0)
273 printf ("mq_close failed: %m\n");
274 result = 1;
277 if (mq_close (q2) != 0)
279 printf ("mq_close failed: %m\n");
280 result = 1;
283 if (mq_close (q3) != 0)
285 printf ("mq_close failed: %m\n");
286 result = 1;
289 return result;
292 #include "../test-skeleton.c"