Imported Upstream version 20081219
[ltp-debian.git] / testcases / kernel / syscalls / ipc / msgctl / msgctl02.c
bloba455bd153ef28fd006529f316419f3e67fbf435e
1 /*
3 * Copyright (c) International Business Machines Corp., 2001
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
21 * NAME
22 * msgctl02.c
24 * DESCRIPTION
25 * msgctl02 - create a message queue, then issue the IPC_SET command
26 * to lower the msg_qbytes value.
28 * ALGORITHM
29 * create a message queue
30 * loop if that option was specified
31 * call msgctl() with the IPC_SET command with a new msg_qbytes value
32 * check the return code
33 * if failure, issue a FAIL message and break remaining tests
34 * otherwise,
35 * if doing functionality testing
36 * if the msg_qbytes value is the new value
37 * issue a PASS message
38 * otherwise
39 * issue a FAIL message
40 * else issue a PASS message
41 * call cleanup
43 * USAGE: <for command-line>
44 * msgctl02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -f : Turn off functionality Testing.
47 * -i n : Execute test n times.
48 * -I x : Execute test for x seconds.
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
55 * RESTRICTIONS
56 * none
59 #include "test.h"
60 #include "usctest.h"
62 #include "ipcmsg.h"
64 char *TCID = "msgctl02";
65 int TST_TOTAL = 1;
66 extern int Tst_count;
68 int msg_q_1 = -1; /* to hold the message queue id */
70 struct msqid_ds qs_buf;
72 unsigned long int new_bytes;
74 int main(int ac, char **av)
76 int lc; /* loop counter */
77 char *msg; /* message returned from parse_opts */
79 /* parse standard options */
80 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
81 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
84 setup(); /* global setup */
86 /* The following loop checks looping state if -i option given */
88 for (lc = 0; TEST_LOOPING(lc); lc++) {
89 /* reset Tst_count in case we are looping */
90 Tst_count = 0;
93 * Set the msqid_ds structure values for the queue
96 TEST(msgctl(msg_q_1, IPC_SET, &qs_buf));
98 if (TEST_RETURN == -1) {
99 tst_resm(TFAIL, "%s call failed - errno = %d"
100 " : %s", TCID, TEST_ERRNO,
101 strerror(TEST_ERRNO));
102 } else {
103 if (STD_FUNCTIONAL_TEST) {
105 /* do a stat to get current queue values */
106 if ((msgctl(msg_q_1, IPC_STAT, &qs_buf) == -1)){
107 tst_resm(TBROK, "stat on queue failed");
108 continue;
111 if (qs_buf.msg_qbytes == new_bytes) {
112 tst_resm(TPASS, "qs_buf.msg_qbytes is"
113 " the new value - %d",
114 qs_buf.msg_qbytes);
115 } else {
116 tst_resm(TFAIL, "qs_buf.msg_qbytes "
117 "value is not expected");
118 tst_resm(TINFO, "expected - %d, "
119 "received - %d", new_bytes,
120 qs_buf.msg_qbytes);
122 } else {
123 tst_resm(TPASS, "msgctl() call succeeded");
128 * decrement by one the msq_qbytes value
130 qs_buf.msg_qbytes -= 1;
131 new_bytes = qs_buf.msg_qbytes;
134 cleanup();
136 /*NOTREACHED*/
137 return(0);
141 * setup() - performs all the ONE TIME setup for this test.
143 void
144 setup(void)
146 /* capture signals */
147 tst_sig(NOFORK, DEF_HANDLER, cleanup);
149 /* Pause if that option was specified */
150 TEST_PAUSE;
153 * Create a temporary directory and cd into it.
154 * This helps to ensure that a unique msgkey is created.
155 * See ../lib/libipc.c for more information.
157 tst_tmpdir();
159 /* get a message key */
160 msgkey = getipckey();
162 /* make sure the initial # of bytes is 0 in our buffer */
163 qs_buf.msg_qbytes = 0x3000;
165 /* now we have a key, so let's create a message queue */
166 if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
167 tst_brkm(TBROK, cleanup, "Can't create message queue");
170 /* now stat the queue to get the default msg_qbytes value */
171 if ((msgctl(msg_q_1, IPC_STAT, &qs_buf)) == -1) {
172 tst_brkm(TBROK, cleanup, "Can't stat the message queue");
175 /* decrement msg_qbytes and copy its value */
176 qs_buf.msg_qbytes -= 1;
177 new_bytes = qs_buf.msg_qbytes;
181 * cleanup() - performs all the ONE TIME cleanup for this test at completion
182 * or premature exit.
184 void
185 cleanup(void)
187 /* if it exists, remove the message queue */
188 rm_queue(msg_q_1);
190 /* Remove the temporary directory */
191 tst_rmdir();
194 * print timing stats if that option was specified.
195 * print errno log if that option was specified.
197 TEST_CLEANUP;
199 /* exit with return code appropriate for results */
200 tst_exit();