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 * msgrcv01 - test that msgrcv() receives the expected message
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci#include <string.h>
9f08c3bdfSopenharmony_ci#include <sys/wait.h>
10f08c3bdfSopenharmony_ci#include "tst_test.h"
11f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
12f08c3bdfSopenharmony_ci#include "tst_clocks.h"
13f08c3bdfSopenharmony_ci#include "libnewipc.h"
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_cistatic key_t msgkey;
16f08c3bdfSopenharmony_cistatic int queue_id = -1, pid;
17f08c3bdfSopenharmony_cistatic struct buf {
18f08c3bdfSopenharmony_ci	long type;
19f08c3bdfSopenharmony_ci	char mtext[MSGSIZE];
20f08c3bdfSopenharmony_ci} rcv_buf, snd_buf = {MSGTYPE, "hello"};
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic void verify_msgrcv(void)
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	struct msqid_ds qs_buf;
25f08c3bdfSopenharmony_ci	time_t before_rcv, after_rcv;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	before_rcv = tst_get_fs_timestamp();
30f08c3bdfSopenharmony_ci	TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0));
31f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
32f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "msgrcv failed");
33f08c3bdfSopenharmony_ci		return;
34f08c3bdfSopenharmony_ci	}
35f08c3bdfSopenharmony_ci	after_rcv = tst_get_fs_timestamp();
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
38f08c3bdfSopenharmony_ci		tst_res(TPASS, "message received(%s) = message sent(%s)",
39f08c3bdfSopenharmony_ci			rcv_buf.mtext, snd_buf.mtext);
40f08c3bdfSopenharmony_ci	else
41f08c3bdfSopenharmony_ci		tst_res(TFAIL, "message received(%s) != message sent(%s)",
42f08c3bdfSopenharmony_ci			rcv_buf.mtext, snd_buf.mtext);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
45f08c3bdfSopenharmony_ci	if (qs_buf.msg_cbytes == 0 && qs_buf.msg_qnum == 0)
46f08c3bdfSopenharmony_ci		tst_res(TPASS, "queue bytes and number of queues matched");
47f08c3bdfSopenharmony_ci	else
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "queue bytes or number of queues mismatched");
49f08c3bdfSopenharmony_ci	if (qs_buf.msg_lrpid == pid)
50f08c3bdfSopenharmony_ci		tst_res(TPASS, "PID of last msgrcv(2) matched");
51f08c3bdfSopenharmony_ci	else
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "PID of last msgrcv(2) mismatched");
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	if (qs_buf.msg_rtime >= before_rcv && qs_buf.msg_rtime <= after_rcv) {
55f08c3bdfSopenharmony_ci		tst_res(TPASS, "msg_rtime = %lu in [%lu, %lu]",
56f08c3bdfSopenharmony_ci			(unsigned long)qs_buf.msg_rtime,
57f08c3bdfSopenharmony_ci			(unsigned long)before_rcv, (unsigned long)after_rcv);
58f08c3bdfSopenharmony_ci	} else {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL, "msg_rtime = %lu out of [%lu, %lu]",
60f08c3bdfSopenharmony_ci			(unsigned long)qs_buf.msg_rtime,
61f08c3bdfSopenharmony_ci			(unsigned long)before_rcv, (unsigned long)after_rcv);
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic void setup(void)
66f08c3bdfSopenharmony_ci{
67f08c3bdfSopenharmony_ci	msgkey = GETIPCKEY();
68f08c3bdfSopenharmony_ci	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
69f08c3bdfSopenharmony_ci	pid = getpid();
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic void cleanup(void)
73f08c3bdfSopenharmony_ci{
74f08c3bdfSopenharmony_ci	if (queue_id != -1)
75f08c3bdfSopenharmony_ci		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
76f08c3bdfSopenharmony_ci}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_cistatic struct tst_test test = {
79f08c3bdfSopenharmony_ci	.setup = setup,
80f08c3bdfSopenharmony_ci	.cleanup = cleanup,
81f08c3bdfSopenharmony_ci	.test_all = verify_msgrcv,
82f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
83f08c3bdfSopenharmony_ci};
84