1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2002
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
21/* 11/06/2002	Port to LTP	dbarrera@us.ibm.com */
22/* 12/03/2008   Fix concurrency issue     mfertre@irisa.fr */
23
24/*
25 * NAME
26 *	msgctl06
27 *
28 * CALLS
29 *	msgget(2) msgctl(2)
30 *
31 * ALGORITHM
32 *	Get and manipulate a message queue.
33 *
34 * RESTRICTIONS
35 *
36 */
37
38#include <sys/types.h>
39#include <sys/ipc.h>
40#include <sys/msg.h>
41#include <stdio.h>
42#include "test.h"
43#include "ipcmsg.h"
44
45void setup();
46void cleanup();
47
48char *TCID = "msgctl06";
49int TST_TOTAL = 1;
50
51/*
52 * msgctl3_t -- union of msgctl(2)'s possible argument # 3 types.
53 */
54typedef union msgctl3_u {
55	struct msqid_ds *msq_ds;	/* pointer to msqid_ds struct */
56	struct ipc_acl *msq_acl;	/* pointer ACL buff and size */
57} msgctl3_t;
58
59extern int local_flag;
60
61int msqid, status;
62struct msqid_ds buf;
63
64int main(int argc, char *argv[])
65{
66	key_t key;
67
68	tst_parse_opts(argc, argv, NULL, NULL);
69
70	setup();
71
72	key = getipckey();
73	TEST(msgget(key, IPC_CREAT | IPC_EXCL));
74	msqid = TEST_RETURN;
75	if (TEST_RETURN == -1) {
76		tst_brkm(TFAIL | TTERRNO, NULL, "msgget() failed");
77	}
78
79	TEST(msgctl(msqid, IPC_STAT, &buf));
80	status = TEST_RETURN;
81	if (TEST_RETURN == -1) {
82		tst_resm(TFAIL | TTERRNO,
83			 "msgctl(msqid, IPC_STAT, &buf) failed");
84		(void)msgctl(msqid, IPC_RMID, NULL);
85		tst_exit();
86	}
87
88	/*
89	 * Check contents of msqid_ds structure.
90	 */
91
92	if (buf.msg_qnum != 0) {
93		tst_brkm(TFAIL, NULL, "error: unexpected nbr of messages %ld",
94			 buf.msg_qnum);
95	}
96	if (buf.msg_perm.uid != getuid()) {
97		tst_brkm(TFAIL, NULL, "error: unexpected uid %d",
98			 buf.msg_perm.uid);
99	}
100	if (buf.msg_perm.gid != getgid()) {
101		tst_brkm(TFAIL, NULL, "error: unexpected gid %d",
102			 buf.msg_perm.gid);
103	}
104	if (buf.msg_perm.cuid != getuid()) {
105		tst_brkm(TFAIL, NULL, "error: unexpected cuid %d",
106			 buf.msg_perm.cuid);
107	}
108	if (buf.msg_perm.cgid != getgid()) {
109		tst_brkm(TFAIL, NULL, "error: unexpected cgid %d",
110			 buf.msg_perm.cgid);
111	}
112
113	tst_resm(TPASS, "msgctl06 ran successfully!");
114
115	cleanup();
116	tst_exit();
117}
118
119void setup(void)
120{
121	tst_require_root();
122
123	tst_sig(NOFORK, DEF_HANDLER, cleanup);
124
125	TEST_PAUSE;
126
127	tst_tmpdir();
128}
129
130void cleanup(void)
131{
132	int status;
133
134	(void)msgctl(msqid, IPC_RMID, NULL);
135	if ((status = msgctl(msqid, IPC_STAT, &buf)) != -1) {
136		(void)msgctl(msqid, IPC_RMID, NULL);
137		tst_resm(TFAIL, "msgctl(msqid, IPC_RMID) failed");
138
139	}
140
141	tst_rmdir();
142}
143