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
22static key_t msgkey;
23static int queue_id = -1;
24static struct buf {
25	long type;
26	char text[MSGSIZE];
27} snd_buf = {1, "hello"};
28
29static void verify_msgsnd(void)
30{
31	TST_EXP_FAIL(msgsnd(queue_id, &snd_buf, MSGSIZE, 0), EIDRM,
32		"msgsnd(%i, %p, %i, 0)", queue_id, &snd_buf, MSGSIZE);
33}
34
35static void do_test(void)
36{
37	pid_t pid;
38
39	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
40
41	while (msgsnd(queue_id, &snd_buf, MSGSIZE, IPC_NOWAIT) != -1)
42		snd_buf.type += 1;
43
44	pid = SAFE_FORK();
45	if (!pid) {
46		verify_msgsnd();
47		_exit(0);
48	}
49
50	TST_PROCESS_STATE_WAIT(pid, 'S', 0);
51
52	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
53
54	tst_reap_children();
55}
56
57static void setup(void)
58{
59	msgkey = GETIPCKEY();
60}
61
62static void cleanup(void)
63{
64	if (queue_id != -1)
65		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
66}
67
68static struct tst_test test = {
69	.needs_tmpdir = 1,
70	.needs_root = 1,
71	.forks_child = 1,
72	.setup = setup,
73	.cleanup = cleanup,
74	.test_all = do_test
75};
76