1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2014 Fujitsu Ltd.
4  * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
5  * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
6  */
7 /*
8  * msgctl12 - test for IPC_INFO MSG_INFO and MSG_STAT.
9  */
10 
11 #define _GNU_SOURCE
12 #include <errno.h>
13 
14 #include "tst_test.h"
15 #include "tst_safe_sysv_ipc.h"
16 #include "libnewipc.h"
17 
18 static int msg_q = -1;
19 static int index_q;
20 static struct msginfo msginfo_buf;
21 static struct msqid_ds msgqid_buf;
22 
23 static struct tcase {
24 	int *msg_id;
25 	int cmd;
26 	char *name;
27 	void *buf;
28 } tc[] = {
29 	{&msg_q, IPC_INFO, "IPC_INFO", &msginfo_buf},
30 	{&msg_q, MSG_INFO, "MSG_INFO", &msginfo_buf},
31 	{&index_q, MSG_STAT, "MSG_STAT", &msgqid_buf},
32 };
33 
verify_msgctl(unsigned int i)34 static void verify_msgctl(unsigned int i)
35 {
36 	TEST(msgctl(*tc[i].msg_id,  tc[i].cmd, tc[i].buf));
37 
38 	if (TST_RET == -1) {
39 		tst_res(TFAIL,
40 			 "msgctl() test %s failed with errno: "
41 			 "%d", tc[i].name, TST_ERR);
42 	}
43 
44 	tst_res(TPASS, "msgctl() test %s succeeded", tc[i].name);
45 }
46 
setup(void)47 static void setup(void)
48 {
49 	msg_q = SAFE_MSGGET(IPC_PRIVATE, MSG_RW);
50 	index_q = SAFE_MSGCTL(msg_q, IPC_INFO, (struct msqid_ds*)&msginfo_buf);
51 }
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	if (msg_q >= 0)
56 		SAFE_MSGCTL(msg_q, IPC_RMID, NULL);
57 }
58 
59 static struct tst_test test = {
60 	.setup = setup,
61 	.cleanup = cleanup,
62 	.test = verify_msgctl,
63 	.tcnt = ARRAY_SIZE(tc),
64 };
65