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