1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci *    03/2001 - Written by Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Create a message queue, then issue the IPC_SET command to lower the
9f08c3bdfSopenharmony_ci * msg_qbytes value.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#include <errno.h>
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
16f08c3bdfSopenharmony_ci#include "libnewipc.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic int msg_id = -1;
19f08c3bdfSopenharmony_cistruct msqid_ds orig_buf;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void verify_msgctl(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	struct msqid_ds buf = orig_buf;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	buf.msg_qbytes -= 1;
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	TEST(msgctl(msg_id, IPC_SET, &buf));
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	if (TST_RET != 0) {
30f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "msgctl(IPC_SET) failed");
31f08c3bdfSopenharmony_ci		return;
32f08c3bdfSopenharmony_ci	}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	tst_res(TPASS, "msgctl(IPC_SET) msg_qbytes - 1");
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	memset(&buf, 0, sizeof(buf));
37f08c3bdfSopenharmony_ci	SAFE_MSGCTL(msg_id, IPC_STAT, &buf);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	if (buf.msg_qbytes == orig_buf.msg_qbytes - 1) {
40f08c3bdfSopenharmony_ci		tst_res(TPASS, "msg_qbytes = %lu",
41f08c3bdfSopenharmony_ci			(unsigned long)buf.msg_qbytes);
42f08c3bdfSopenharmony_ci	} else {
43f08c3bdfSopenharmony_ci		tst_res(TFAIL, "msg_qbytes = %lu, expected %lu",
44f08c3bdfSopenharmony_ci			(unsigned long)buf.msg_qbytes,
45f08c3bdfSopenharmony_ci			(unsigned long)orig_buf.msg_qbytes - 1);
46f08c3bdfSopenharmony_ci	}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	SAFE_MSGCTL(msg_id, IPC_SET, &orig_buf);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	key_t msgkey = GETIPCKEY();
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	msg_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW | 0660);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	SAFE_MSGCTL(msg_id, IPC_STAT, &orig_buf);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void cleanup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	if (msg_id >= 0)
63f08c3bdfSopenharmony_ci		SAFE_MSGCTL(msg_id, IPC_RMID, NULL);
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic struct tst_test test = {
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.cleanup = cleanup,
69f08c3bdfSopenharmony_ci	.test_all = verify_msgctl,
70f08c3bdfSopenharmony_ci	.needs_tmpdir = 1
71f08c3bdfSopenharmony_ci};
72