Imported Upstream version 20091031
[ltp-debian.git] / testcases / kernel / syscalls / ipc / msgctl / msgctl06.c
blob029b6019717753f9fd4e72893047bb5e58ed740e
1 /*
3 * Copyright (c) International Business Machines Corp., 2002
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 */
25 * NAME
26 * msgctl06
28 * CALLS
29 * msgget(2) msgctl(2)
31 * ALGORITHM
32 * Get and manipulate a message queue.
34 * RESTRICTIONS
38 #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 */
42 #include "test.h"
43 #include "usctest.h"
44 #include "ipcmsg.h"
46 void setup();
47 void cleanup();
49 * * * These globals must be defined in the test.
50 * * */
52 char *TCID = "msgctl06"; /* Test program identifier. */
53 int TST_TOTAL = 1; /* Total number of test cases. */
54 extern int Tst_count; /* Test Case counter for tst_* routines */
56 int exp_enos[] = { 0 }; /* List must end with 0 */
59 * msgctl3_t -- union of msgctl(2)'s possible argument # 3 types.
61 typedef union msgctl3_u {
62 struct msqid_ds *msq_ds; /* pointer to msqid_ds struct */
63 struct ipc_acl *msq_acl; /* pointer ACL buff and size */
64 } msgctl3_t;
66 extern int local_flag;
68 int msqid, status;
69 struct msqid_ds buf;
71 /*--------------------------------------------------------------*/
73 int main(int argc, char *argv[])
75 key_t key;
76 setup();
78 key = getipckey();
79 TEST(msgget(key, IPC_CREAT | IPC_EXCL));
80 msqid = TEST_RETURN;
81 if (TEST_RETURN == -1) {
82 tst_resm(TFAIL|TTERRNO, "msgget() failed");
83 tst_exit();
86 TEST(msgctl(msqid, IPC_STAT, &buf));
87 status = TEST_RETURN;
88 if (TEST_RETURN == -1) {
89 tst_resm(TFAIL|TTERRNO,
90 "msgctl(msqid, IPC_STAT, &buf) failed");
91 (void)msgctl(msqid, IPC_RMID, (struct msqid_ds *)NULL);
92 tst_exit();
96 * Check contents of msqid_ds structure.
99 if (buf.msg_qnum != 0) {
100 tst_resm(TFAIL, "error: unexpected nbr of messages %d",
101 buf.msg_qnum);
102 tst_exit();
104 if (buf.msg_perm.uid != getuid()) {
105 tst_resm(TFAIL, "error: unexpected uid %d", buf.msg_perm.uid);
106 tst_exit();
108 if (buf.msg_perm.gid != getgid()) {
109 tst_resm(TFAIL, "error: unexpected gid %d", buf.msg_perm.gid);
110 tst_exit();
112 if (buf.msg_perm.cuid != getuid()) {
113 tst_resm(TFAIL, "error: unexpected cuid %d", buf.msg_perm.cuid);
114 tst_exit();
116 if (buf.msg_perm.cgid != getgid()) {
117 tst_resm(TFAIL, "error: unexpected cgid %d", buf.msg_perm.cgid);
118 tst_exit();
121 tst_resm(TPASS, "msgctl06 ran successfully!");
122 /***************************************************************
123 * cleanup and exit
124 ***************************************************************/
125 cleanup();
127 return 0;
128 } /* End main */
130 /***************************************************************
131 * * setup() - performs all ONE TIME setup for this test.
132 * ****************************************************************/
133 void setup()
135 /* You will want to enable some signal handling so you can capture
136 * unexpected signals like SIGSEGV.
138 tst_sig(NOFORK, DEF_HANDLER, cleanup);
140 /* Pause if that option was specified */
141 /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to
142 * fork the test with the -c option. You want to make sure you do this
143 * before you create your temporary directory.
145 TEST_PAUSE;
148 * Create a temporary directory and cd into it.
149 * This helps to ensure that a unique msgkey is created.
150 * See ../lib/libipc.c for more information.
152 tst_tmpdir();
155 /***************************************************************
156 * * * cleanup() - performs all ONE TIME cleanup for this test at
157 * * * completion or premature exit.
158 * * ***************************************************************/
159 void cleanup()
161 int status;
164 * print timing stats if that option was specified.
165 * print errno log if that option was specified.
167 TEST_CLEANUP;
170 * Remove the message queue from the system
172 #ifdef DEBUG
173 tst_resm(TINFO, "Remove the message queue");
174 #endif
175 fflush(stdout);
176 (void)msgctl(msqid, IPC_RMID, (struct msqid_ds *)NULL);
177 if ((status = msgctl(msqid, IPC_STAT, &buf)) != -1) {
178 (void)msgctl(msqid, IPC_RMID, (struct msqid_ds *)NULL);
179 tst_resm(TFAIL, "msgctl(msqid, IPC_RMID) failed");
180 tst_exit();
183 fflush(stdout);
185 /* Remove the temporary directory */
186 tst_rmdir();
188 /* exit with return code appropriate for results */
189 tst_exit();