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