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