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