1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  * Tests if EIDRM is returned when message queue was removed while
9  * msgsnd() was trying to send a message.
10  */
11 
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/ipc.h>
16 #include <sys/msg.h>
17 
18 #include "tst_test.h"
19 #include "tst_safe_sysv_ipc.h"
20 #include "libnewipc.h"
21 
22 static key_t msgkey;
23 static int queue_id = -1;
24 static struct buf {
25 	long type;
26 	char text[MSGSIZE];
27 } snd_buf = {1, "hello"};
28 
verify_msgsnd(void)29 static void verify_msgsnd(void)
30 {
31 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
32 	if (TST_RET != -1) {
33 		tst_res(TFAIL, "msgsnd() succeeded unexpectedly");
34 		return;
35 	}
36 
37 	if (TST_ERR == EIDRM) {
38 		tst_res(TPASS | TTERRNO, "msgsnd() failed as expected");
39 	} else {
40 		tst_res(TFAIL | TTERRNO,
41 			"msgsnd() failed unexpectedly, expected EIDRM");
42 	}
43 }
44 
do_test(void)45 static void do_test(void)
46 {
47 	pid_t pid;
48 
49 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
50 
51 	while (msgsnd(queue_id, &snd_buf, MSGSIZE, IPC_NOWAIT) != -1)
52 		snd_buf.type += 1;
53 
54 	pid = SAFE_FORK();
55 	if (!pid) {
56 		verify_msgsnd();
57 		_exit(0);
58 	}
59 
60 	TST_PROCESS_STATE_WAIT(pid, 'S');
61 
62 	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
63 
64 	tst_reap_children();
65 }
66 
setup(void)67 static void setup(void)
68 {
69 	msgkey = GETIPCKEY();
70 }
71 
cleanup(void)72 static void cleanup(void)
73 {
74 	if (queue_id != -1)
75 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
76 }
77 
78 static struct tst_test test = {
79 	.needs_tmpdir = 1,
80 	.needs_root = 1,
81 	.forks_child = 1,
82 	.setup = setup,
83 	.cleanup = cleanup,
84 	.test_all = do_test
85 };
86