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 *
9f08c3bdfSopenharmony_ci * Create a message queue, write a message to it and
10f08c3bdfSopenharmony_ci * read it back.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <string.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/ipc.h>
17f08c3bdfSopenharmony_ci#include <sys/msg.h>
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
21f08c3bdfSopenharmony_ci#include "libnewipc.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic int queue_id = -1;
24f08c3bdfSopenharmony_cistatic key_t msgkey;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic struct buf {
27f08c3bdfSopenharmony_ci	long type;
28f08c3bdfSopenharmony_ci	char text[MSGSIZE];
29f08c3bdfSopenharmony_ci} rcv_buf, snd_buf = {MSGTYPE, "hello, world"};
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void verify_msgget(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
34f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
35f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "msgget() failed");
36f08c3bdfSopenharmony_ci		return;
37f08c3bdfSopenharmony_ci	}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	queue_id = TST_RET;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	SAFE_MSGRCV(queue_id, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	if (strcmp(snd_buf.text, rcv_buf.text) == 0)
46f08c3bdfSopenharmony_ci		tst_res(TPASS, "message received = message sent");
47f08c3bdfSopenharmony_ci	else
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "message received != message sent");
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void cleanup(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	if (queue_id != -1)
59f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic struct tst_test test = {
63f08c3bdfSopenharmony_ci	.setup = setup,
64f08c3bdfSopenharmony_ci	.cleanup = cleanup,
65f08c3bdfSopenharmony_ci	.test_all = verify_msgget,
66f08c3bdfSopenharmony_ci	.needs_tmpdir = 1
67f08c3bdfSopenharmony_ci};
68