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 * msgrcv error test for EIDRM.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci#include <errno.h>
9f08c3bdfSopenharmony_ci#include <unistd.h>
10f08c3bdfSopenharmony_ci#include <sys/types.h>
11f08c3bdfSopenharmony_ci#include <sys/ipc.h>
12f08c3bdfSopenharmony_ci#include <sys/msg.h>
13f08c3bdfSopenharmony_ci#include <stdlib.h>
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
16f08c3bdfSopenharmony_ci#include "libnewipc.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic key_t msgkey;
19f08c3bdfSopenharmony_cistatic int queue_id = -1;
20f08c3bdfSopenharmony_cistatic struct buf {
21f08c3bdfSopenharmony_ci	long type;
22f08c3bdfSopenharmony_ci	char text[MSGSIZE];
23f08c3bdfSopenharmony_ci} rcv_buf = {1, "hello"};
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void verify_msgrcv(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0), EIDRM,
28f08c3bdfSopenharmony_ci		"msgrcv(%i, %p, %d, 1, 0)", queue_id, &rcv_buf, MSGSIZE);
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void do_test(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	int pid;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
36f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
37f08c3bdfSopenharmony_ci	if (pid == 0) {
38f08c3bdfSopenharmony_ci		verify_msgrcv();
39f08c3bdfSopenharmony_ci		exit(0);
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci	TST_PROCESS_STATE_WAIT(pid, 'S', 0);
42f08c3bdfSopenharmony_ci	SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
43f08c3bdfSopenharmony_ci	tst_reap_children();
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void setup(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void cleanup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	if (queue_id != -1)
54f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic struct tst_test test = {
58f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
59f08c3bdfSopenharmony_ci	.forks_child = 1,
60f08c3bdfSopenharmony_ci	.setup = setup,
61f08c3bdfSopenharmony_ci	.cleanup = cleanup,
62f08c3bdfSopenharmony_ci	.test_all = do_test,
63f08c3bdfSopenharmony_ci};
64