• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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 /*
21  * NAME
22  *	msgctl05.c
23  *
24  * DESCRIPTION
25  *	msgctl05 - test for EPERM error
26  *
27  * ALGORITHM
28  *	create a message queue as root
29  *	fork a child process and change its ID to nobody
30  *	loop if that option was specified
31  *	try to remove the queue in the child process with msgctl()
32  *	check the errno value
33  *	  issue a PASS message if we get EPERM
34  *	otherwise, the tests fails
35  *	  issue a FAIL message
36  *	  break any remaining tests
37  *	  call cleanup
38  *
39  * USAGE:  <for command-line>
40  *  msgctl05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
41  *     where,  -c n : Run n copies concurrently.
42  *             -e   : Turn on errno logging.
43  *	       -i n : Execute test n times.
44  *	       -I x : Execute test for x seconds.
45  *	       -P x : Pause for x seconds between iterations.
46  *	       -t   : Turn on syscall timing.
47  *
48  * HISTORY
49  *	03/2001 - Written by Wayne Boyer
50  *
51  * RESTRICTIONS
52  *	The test must be run as root.
53  *	There must be a nobody ID installed on the system.
54  */
55 
56 #include <string.h>
57 #include <pwd.h>
58 #include <sys/wait.h>
59 
60 #include "test.h"
61 
62 #include "ipcmsg.h"
63 
64 char *TCID = "msgctl05";
65 int TST_TOTAL = 1;
66 
67 int msg_q_1 = -1;		/* The message queue id created in setup */
68 uid_t ltp_uid;			/* The user ID for a non root user */
69 char *ltp_user = "nobody";	/* A non root user */
70 
71 struct msqid_ds q_buf;
72 
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 	pid_t pid;
76 	void do_child(void);
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 
80 	setup();		/* global setup */
81 
82 	if ((pid = FORK_OR_VFORK()) == -1) {
83 		tst_brkm(TBROK, cleanup, "could not fork");
84 	}
85 
86 	if (pid == 0) {		/* child */
87 		/* set the user ID of the child to nobody */
88 		if (setuid(ltp_uid) == -1) {
89 			tst_resm(TBROK, "setuid() failed");
90 			exit(1);
91 		}
92 
93 		do_child();
94 	} else {		/* parent */
95 		if (waitpid(pid, NULL, 0) == -1) {
96 			tst_resm(TBROK | TERRNO, "waitpid() failed");
97 		}
98 
99 		/* if it exists, remove the message queue */
100 		rm_queue(msg_q_1);
101 
102 		tst_rmdir();
103 	}
104 
105 	cleanup();
106 	/**NOT REACHED**/
107 	tst_exit();
108 }
109 
110 /*
111  * do_child - make the TEST call as the child process
112  */
do_child(void)113 void do_child(void)
114 {
115 	int lc;
116 	int i;
117 
118 	/* The following loop checks looping state if -i option given */
119 
120 	for (lc = 0; TEST_LOOPING(lc); lc++) {
121 		/* reset tst_count in case we are looping */
122 		tst_count = 0;
123 
124 		/* loop through the test cases */
125 
126 		for (i = 0; i < TST_TOTAL; i++) {
127 			TEST(msgctl(msg_q_1, IPC_RMID, NULL));
128 
129 			if (TEST_RETURN != -1) {
130 				tst_resm(TFAIL, "msgget() call succeeded "
131 					 "on expected fail");
132 				continue;
133 			}
134 
135 			switch (TEST_ERRNO) {
136 			case EPERM:
137 				tst_resm(TPASS, "expected error = %d : %s",
138 					 TEST_ERRNO, strerror(TEST_ERRNO));
139 				break;
140 			default:
141 				tst_resm(TFAIL, "call failed with unexpected "
142 					 "error - %d : %s", TEST_ERRNO,
143 					 strerror(TEST_ERRNO));
144 				break;
145 			}
146 		}
147 	}
148 }
149 
150 /*
151  * setup() - performs all the ONE TIME setup for this test.
152  */
setup(void)153 void setup(void)
154 {
155 	tst_require_root();
156 
157 	tst_sig(FORK, DEF_HANDLER, cleanup);
158 
159 	TEST_PAUSE;
160 
161 	/*
162 	 * Create a temporary directory and cd into it.
163 	 * This helps to ensure that a unique msgkey is created.
164 	 * See ../lib/libipc.c for more information.
165 	 */
166 	tst_tmpdir();
167 
168 	msgkey = getipckey();
169 
170 	/* now we have a key, so let's create a message queue */
171 	if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW)) == -1) {
172 		tst_brkm(TBROK, cleanup, "Can't create message queue #1");
173 	}
174 
175 	/* get the user ID for a non root user */
176 	ltp_uid = getuserid(ltp_user);
177 }
178 
179 /*
180  * cleanup() - performs all the ONE TIME cleanup for this test at completion
181  * 	       or premature exit.
182  */
cleanup(void)183 void cleanup(void)
184 {
185 
186 }
187