blob: a0b1d35780a61ce65ff9a4c2ed2bee2ff60826c1 [file] [log] [blame]
robbiew577ff1a2002-12-05 20:12:28 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew577ff1a2002-12-05 20:12:28 +000018 */
19
20/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
21/* 11/06/2002 Port to LTP dbarrera@us.ibm.com */
subrata_modak824b75f2008-04-06 11:43:02 +000022/* 12/03/2008 Fix concurrency issue mfertre@irisa.fr */
robbiew577ff1a2002-12-05 20:12:28 +000023
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
subrata_modak56207ce2009-03-23 13:35:39 +000038#include <sys/types.h> /* needed for test */
39#include <sys/ipc.h> /* needed for test */
40#include <sys/msg.h> /* needed for test */
41#include <stdio.h> /* needed by testhead.h */
robbiew577ff1a2002-12-05 20:12:28 +000042#include "test.h"
subrata_modak824b75f2008-04-06 11:43:02 +000043#include "ipcmsg.h"
robbiew577ff1a2002-12-05 20:12:28 +000044
45void setup();
46void cleanup();
robbiew577ff1a2002-12-05 20:12:28 +000047
Cyril Hrubis605fa332015-02-04 13:11:20 +010048char *TCID = "msgctl06";
49int TST_TOTAL = 1;
robbiew577ff1a2002-12-05 20:12:28 +000050
51/*
52 * msgctl3_t -- union of msgctl(2)'s possible argument # 3 types.
53 */
54typedef union msgctl3_u {
subrata_modak56207ce2009-03-23 13:35:39 +000055 struct msqid_ds *msq_ds; /* pointer to msqid_ds struct */
56 struct ipc_acl *msq_acl; /* pointer ACL buff and size */
robbiew577ff1a2002-12-05 20:12:28 +000057} msgctl3_t;
58
59extern int local_flag;
60
subrata_modak56207ce2009-03-23 13:35:39 +000061int msqid, status;
62struct msqid_ds buf;
robbiew577ff1a2002-12-05 20:12:28 +000063
robbiew577ff1a2002-12-05 20:12:28 +000064/*--------------------------------------------------------------*/
65
subrata_modak56207ce2009-03-23 13:35:39 +000066int main(int argc, char *argv[])
robbiew577ff1a2002-12-05 20:12:28 +000067{
subrata_modak56207ce2009-03-23 13:35:39 +000068 key_t key;
robbiew577ff1a2002-12-05 20:12:28 +000069 setup();
70
subrata_modak824b75f2008-04-06 11:43:02 +000071 key = getipckey();
72 TEST(msgget(key, IPC_CREAT | IPC_EXCL));
robbiew577ff1a2002-12-05 20:12:28 +000073 msqid = TEST_RETURN;
subrata_modak56207ce2009-03-23 13:35:39 +000074 if (TEST_RETURN == -1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010075 tst_brkm(TFAIL | TTERRNO, NULL, "msgget() failed");
robbiew577ff1a2002-12-05 20:12:28 +000076 }
subrata_modak824b75f2008-04-06 11:43:02 +000077
robbiew577ff1a2002-12-05 20:12:28 +000078 TEST(msgctl(msqid, IPC_STAT, &buf));
79 status = TEST_RETURN;
subrata_modak56207ce2009-03-23 13:35:39 +000080 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 tst_resm(TFAIL | TTERRNO,
subrata_modak98be5892009-10-26 14:41:46 +000082 "msgctl(msqid, IPC_STAT, &buf) failed");
Garrett Cooperdf3eb162010-11-28 22:44:32 -080083 (void)msgctl(msqid, IPC_RMID, NULL);
subrata_modak56207ce2009-03-23 13:35:39 +000084 tst_exit();
robbiew577ff1a2002-12-05 20:12:28 +000085 }
86
87 /*
88 * Check contents of msqid_ds structure.
89 */
90
subrata_modak56207ce2009-03-23 13:35:39 +000091 if (buf.msg_qnum != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010092 tst_brkm(TFAIL, NULL, "error: unexpected nbr of messages %ld",
subrata_modak56207ce2009-03-23 13:35:39 +000093 buf.msg_qnum);
robbiew577ff1a2002-12-05 20:12:28 +000094 }
subrata_modak56207ce2009-03-23 13:35:39 +000095 if (buf.msg_perm.uid != getuid()) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010096 tst_brkm(TFAIL, NULL, "error: unexpected uid %d",
97 buf.msg_perm.uid);
robbiew577ff1a2002-12-05 20:12:28 +000098 }
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (buf.msg_perm.gid != getgid()) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100100 tst_brkm(TFAIL, NULL, "error: unexpected gid %d",
101 buf.msg_perm.gid);
robbiew577ff1a2002-12-05 20:12:28 +0000102 }
subrata_modak56207ce2009-03-23 13:35:39 +0000103 if (buf.msg_perm.cuid != getuid()) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100104 tst_brkm(TFAIL, NULL, "error: unexpected cuid %d",
105 buf.msg_perm.cuid);
robbiew577ff1a2002-12-05 20:12:28 +0000106 }
subrata_modak56207ce2009-03-23 13:35:39 +0000107 if (buf.msg_perm.cgid != getgid()) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100108 tst_brkm(TFAIL, NULL, "error: unexpected cgid %d",
109 buf.msg_perm.cgid);
robbiew577ff1a2002-12-05 20:12:28 +0000110 }
111
subrata_modak56207ce2009-03-23 13:35:39 +0000112 tst_resm(TPASS, "msgctl06 ran successfully!");
robbiew577ff1a2002-12-05 20:12:28 +0000113 /***************************************************************
114 * cleanup and exit
115 ***************************************************************/
116 cleanup();
117
Garrett Cooper2c282152010-12-16 00:55:50 -0800118 tst_exit();
119}
robbiew577ff1a2002-12-05 20:12:28 +0000120
121/***************************************************************
122 * * setup() - performs all ONE TIME setup for this test.
123 * ****************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400124void setup(void)
robbiew577ff1a2002-12-05 20:12:28 +0000125{
subrata_modak56207ce2009-03-23 13:35:39 +0000126 /* You will want to enable some signal handling so you can capture
127 * unexpected signals like SIGSEGV.
128 */
129 tst_sig(NOFORK, DEF_HANDLER, cleanup);
robbiew577ff1a2002-12-05 20:12:28 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131 /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to
robbiew577ff1a2002-12-05 20:12:28 +0000132 * fork the test with the -c option. You want to make sure you do this
133 * before you create your temporary directory.
134 */
135 TEST_PAUSE;
subrata_modak824b75f2008-04-06 11:43:02 +0000136
137 /*
138 * Create a temporary directory and cd into it.
139 * This helps to ensure that a unique msgkey is created.
140 * See ../lib/libipc.c for more information.
141 */
142 tst_tmpdir();
robbiew577ff1a2002-12-05 20:12:28 +0000143}
144
robbiew577ff1a2002-12-05 20:12:28 +0000145/***************************************************************
146 * * * cleanup() - performs all ONE TIME cleanup for this test at
147 * * * completion or premature exit.
148 * * ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400149void cleanup(void)
robbiew577ff1a2002-12-05 20:12:28 +0000150{
151 int status;
subrata_modak824b75f2008-04-06 11:43:02 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000154 * Remove the message queue from the system
155 */
robbiew577ff1a2002-12-05 20:12:28 +0000156#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000157 tst_resm(TINFO, "Remove the message queue");
robbiew577ff1a2002-12-05 20:12:28 +0000158#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000159 fflush(stdout);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800160 (void)msgctl(msqid, IPC_RMID, NULL);
subrata_modak56207ce2009-03-23 13:35:39 +0000161 if ((status = msgctl(msqid, IPC_STAT, &buf)) != -1) {
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800162 (void)msgctl(msqid, IPC_RMID, NULL);
subrata_modak56207ce2009-03-23 13:35:39 +0000163 tst_resm(TFAIL, "msgctl(msqid, IPC_RMID) failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800164
robbiew577ff1a2002-12-05 20:12:28 +0000165 }
166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 fflush(stdout);
subrata_modak824b75f2008-04-06 11:43:02 +0000168
subrata_modakcced8452008-05-22 12:29:29 +0000169 tst_rmdir();
170
Garrett Cooperf6b06a12010-12-19 10:23:41 -0800171}