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