-
Notifications
You must be signed in to change notification settings - Fork 1
/
t_msgrcv.c
388 lines (298 loc) · 8.41 KB
/
t_msgrcv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* $OpenBSD: t_msgrcv.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */
/* $NetBSD: t_msgrcv.c,v 1.5 2017/10/08 08:31:05 kre Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jukka Ruohonen.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "macros.h"
#include <sys/cdefs.h>
__RCSID("$NetBSD: t_msgrcv.c,v 1.5 2017/10/08 08:31:05 kre Exp $");
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/wait.h>
#include "atf-c.h"
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#define MSG_KEY 1234
#define MSG_MTYPE_1 0x41
#define MSG_MTYPE_2 0x42
#define MSG_MTYPE_3 0x43
#define MSG_LEN 3
struct msg {
long mtype;
char buf[MSG_LEN];
};
static void clean(void);
static void
clean(void)
{
int id;
if ((id = msgget(MSG_KEY, 0)) != -1)
(void)msgctl(id, IPC_RMID, 0);
}
ATF_TC_WITH_CLEANUP(msgrcv_basic);
ATF_TC_HEAD(msgrcv_basic, tc)
{
atf_tc_set_md_var(tc, "descr", "A basic test of msgrcv(2)");
}
ATF_TC_BODY(msgrcv_basic, tc)
{
struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
struct msg msg2 = { MSG_MTYPE_1, { 'x', 'y', 'z' } };
int id;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
(void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
(void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT);
ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_basic, tc)
{
clean();
}
ATF_TC_WITH_CLEANUP(msgrcv_block);
ATF_TC_HEAD(msgrcv_block, tc)
{
atf_tc_set_md_var(tc, "descr", "Test that msgrcv(2) blocks");
}
ATF_TC_BODY(msgrcv_block, tc)
{
struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
int id, sta;
pid_t pid;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
pid = fork();
ATF_REQUIRE(pid >= 0);
if (pid == 0) {
if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1, 0) < 0)
_exit(EXIT_FAILURE);
_exit(EXIT_SUCCESS);
}
/*
* Below msgsnd(2) should unblock the child,
* and hence kill(2) should fail with ESRCH.
*/
(void)sleep(1);
(void)msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT);
(void)sleep(1);
(void)kill(pid, SIGKILL);
(void)wait(&sta);
if (WIFEXITED(sta) == 0 || WIFSIGNALED(sta) != 0)
atf_tc_fail("msgrcv(2) did not block");
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_block, tc)
{
clean();
}
ATF_TC_WITH_CLEANUP(msgrcv_err);
ATF_TC_HEAD(msgrcv_err, tc)
{
atf_tc_set_md_var(tc, "descr", "Test errors from msgrcv(2)");
}
ATF_TC_BODY(msgrcv_err, tc)
{
struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
int id, r = 0;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
errno = 0;
ATF_REQUIRE_ERRNO(ENOMSG, msgrcv(id, &msg,
MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
errno = 0;
ATF_REQUIRE_ERRNO(EFAULT, msgrcv(id, (void *)-1,
MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
errno = 0;
ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT) == -1);
errno = 0;
ATF_REQUIRE_ERRNO(EINVAL, msgrcv(-1, &msg,
SSIZE_MAX, MSG_MTYPE_1, IPC_NOWAIT) == -1);
ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
errno = 0;
ATF_REQUIRE_ERRNO(E2BIG, msgrcv(id, &r,
MSG_LEN - 1, MSG_MTYPE_1, IPC_NOWAIT) == -1);
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_err, tc)
{
clean();
}
ATF_TC_WITH_CLEANUP(msgrcv_mtype);
ATF_TC_HEAD(msgrcv_mtype, tc)
{
atf_tc_set_md_var(tc, "descr", "Test message types with msgrcv(2)");
}
ATF_TC_BODY(msgrcv_mtype, tc)
{
struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
struct msg msg2 = { MSG_MTYPE_3, { 'x', 'y', 'z' } };
int id;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
(void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
(void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_2, IPC_NOWAIT);
ATF_CHECK(msg1.buf[0] != msg2.buf[0]); /* Different mtype. */
ATF_CHECK(msg1.buf[1] != msg2.buf[1]);
ATF_CHECK(msg1.buf[2] != msg2.buf[2]);
(void)msgrcv(id, &msg2, MSG_LEN, MSG_MTYPE_1, IPC_NOWAIT);
ATF_CHECK(msg1.buf[0] == msg2.buf[0]); /* Same mtype. */
ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
ATF_CHECK(msg1.buf[2] == msg2.buf[2]);
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_mtype, tc)
{
clean();
}
ATF_TC_WITH_CLEANUP(msgrcv_nonblock);
ATF_TC_HEAD(msgrcv_nonblock, tc)
{
atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with IPC_NOWAIT");
atf_tc_set_md_var(tc, "timeout", "10");
}
ATF_TC_BODY(msgrcv_nonblock, tc)
{
struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
const ssize_t n = 10;
int id, sta;
ssize_t i;
pid_t pid;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
for (i = 0; i < n; i++) {
ATF_REQUIRE(msgsnd(id, &msg, MSG_LEN, IPC_NOWAIT) == 0);
}
pid = fork();
ATF_REQUIRE(pid >= 0);
if (pid == 0) {
while (i != 0) {
if (msgrcv(id, &msg, MSG_LEN, MSG_MTYPE_1,
IPC_NOWAIT) == -1)
_exit(EXIT_FAILURE);
i--;
}
_exit(EXIT_SUCCESS);
}
(void)sleep(2);
(void)kill(pid, SIGKILL);
(void)wait(&sta);
if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT");
if (WIFEXITED(sta) == 0 && WEXITSTATUS(sta) != EXIT_SUCCESS)
atf_tc_fail("msgrcv(2) failed");
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_nonblock, tc)
{
clean();
}
ATF_TC_WITH_CLEANUP(msgrcv_truncate);
ATF_TC_HEAD(msgrcv_truncate, tc)
{
atf_tc_set_md_var(tc, "descr", "Test msgrcv(2) with MSG_NOERROR");
}
ATF_TC_BODY(msgrcv_truncate, tc)
{
#define MSG_SMALLLEN 2
struct msgsmall {
long mtype;
char buf[MSG_SMALLLEN];
};
struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } };
struct msgsmall msg2 = { MSG_MTYPE_1, { 'x', 'y' } };
int id;
id = msgget(MSG_KEY, IPC_CREAT | 0600);
ATF_REQUIRE(id != -1);
(void)msgsnd(id, &msg1, MSG_LEN, IPC_NOWAIT);
(void)msgrcv(id, &msg2, MSG_SMALLLEN,
MSG_MTYPE_1, IPC_NOWAIT | MSG_NOERROR);
ATF_CHECK(msg1.buf[0] == msg2.buf[0]);
ATF_CHECK(msg1.buf[1] == msg2.buf[1]);
ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0);
}
ATF_TC_CLEANUP(msgrcv_truncate, tc)
{
clean();
}
static volatile int sig_caught;
static void
sigsys_handler(int signum)
{
sig_caught = signum;
}
static int
no_kernel_sysvmsg(void)
{
int id;
void (*osig)(int);
sig_caught = 0;
osig = signal(SIGSYS, sigsys_handler);
id = msgget(MSG_KEY, IPC_CREAT | 0600);
if (sig_caught || id == -1)
return 1;
(void)msgctl(id, IPC_RMID, 0);
(void)signal(SIGSYS, osig);
return 0;
}
ATF_TC(msgrcv_query);
ATF_TC_HEAD(msgrcv_query, tc)
{
atf_tc_set_md_var(tc, "descr", "Skip msgrcv_* tests - no SYSVMSG");
}
ATF_TC_BODY(msgrcv_query, tc)
{
atf_tc_skip("No SYSVMSG in kernel");
}
ATF_TP_ADD_TCS(tp)
{
if (no_kernel_sysvmsg()) {
ATF_TP_ADD_TC(tp, msgrcv_query);
} else {
ATF_TP_ADD_TC(tp, msgrcv_basic);
ATF_TP_ADD_TC(tp, msgrcv_block);
ATF_TP_ADD_TC(tp, msgrcv_err);
ATF_TP_ADD_TC(tp, msgrcv_mtype);
ATF_TP_ADD_TC(tp, msgrcv_nonblock);
ATF_TP_ADD_TC(tp, msgrcv_truncate);
}
return atf_no_error();
}