1 /******************************************************************************/
2 /* Copyright (c) Crackerjack Project., 2007				   */
3 /*									    */
4 /* This program is free software;  you can redistribute it and/or modify      */
5 /* it under the terms of the GNU General Public License as published by       */
6 /* the Free Software Foundation; either version 2 of the License, or	  */
7 /* (at your option) any later version.					*/
8 /*									    */
9 /* This program is distributed in the hope that it will be useful,	    */
10 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of	    */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See		  */
12 /* the GNU General Public License for more details.			   */
13 /*									    */
14 /* You should have received a copy of the GNU General Public License	  */
15 /* along with this program;  if not, write to the Free Software	       */
16 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
17 /*									    */
18 /******************************************************************************/
19 /******************************************************************************/
20 /*									    */
21 /* File:	unshare02.c						   */
22 /*									    */
23 /* Description: This tests the unshare error() syscall			*/
24 /*									    */
25 /* Usage:  <for command-line>						 */
26 /* unshare02 [-c n] [-e][-i n] [-I x] [-p x] [-t]			     */
27 /*      where,  -c n : Run n copies concurrently.			     */
28 /*	      -e   : Turn on errno logging.				 */
29 /*	      -i n : Execute test n times.				  */
30 /*	      -I x : Execute test for x seconds.			    */
31 /*	      -P x : Pause for x seconds between iterations.		*/
32 /*	      -t   : Turn on syscall timing.				*/
33 /*									    */
34 /* Total Tests: 2							     */
35 /*									    */
36 /* Test Name:   unshare02						     */
37 /* History:     Porting from Crackerjack to LTP is done by		    */
38 /*	      Manas Kumar Nayak maknayak@in.ibm.com>			*/
39 /******************************************************************************/
40 
41 #include <stdio.h>
42 #include <sys/wait.h>
43 #include <sys/types.h>
44 #include <sched.h>
45 #include <limits.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <sys/syscall.h>
49 #include <errno.h>
50 #include <pwd.h>
51 #include <grp.h>
52 #include <string.h>
53 #include <sys/param.h>
54 #include <stdio.h>
55 
56 #include "test.h"
57 #include "config.h"
58 
59 char *TCID = "unshare02";
60 int testno;
61 int TST_TOTAL = 2;
62 
63 #ifdef HAVE_UNSHARE
64 
65 /* Extern Global Functions */
66 /******************************************************************************/
67 /*									    */
68 /* Function:    cleanup						       */
69 /*									    */
70 /* Description: Performs all one time clean up for this test on successful    */
71 /*	      completion,  premature exit or  failure. Closes all temporary */
72 /*	      files, removes all temporary directories exits the test with  */
73 /*	      appropriate TEST_RETURNurn code by calling tst_exit() function.       */
74 /*									    */
75 /* Input:       None.							 */
76 /*									    */
77 /* Output:      None.							 */
78 /*									    */
79 /* Return:      On failure - Exits calling tst_exit(). Non '0' TEST_RETURNurn code.   */
80 /*	      On success - Exits calling tst_exit(). With '0' TEST_RETURNurn code.  */
81 /*									    */
82 /******************************************************************************/
cleanup(void)83 void cleanup(void)
84 {
85 
86 	tst_rmdir();
87 
88 	/* Exit with appropriate TEST_RETURNurn code. */
89 
90 }
91 
92 /* Local  Functions */
93 /******************************************************************************/
94 /*									    */
95 /* Function:    setup							 */
96 /*									    */
97 /* Description: Performs all one time setup for this test. This function is   */
98 /*	      typically used to capture signals, create temporary dirs      */
99 /*	      and temporary files that may be used in the course of this    */
100 /*	      test.							 */
101 /*									    */
102 /* Input:       None.							 */
103 /*									    */
104 /* Output:      None.							 */
105 /*									    */
106 /* Return:      On failure - Exits by calling cleanup().		      */
107 /*	      On success - TEST_RETURNurns 0.				       */
108 /*									    */
109 /******************************************************************************/
setup(void)110 void setup(void)
111 {
112 	/* Capture signals if any */
113 	/* Create temporary directories */
114 	TEST_PAUSE;
115 	tst_tmpdir();
116 }
117 
main(int ac,char ** av)118 int main(int ac, char **av)
119 {
120 	pid_t pid1;
121 	int lc;
122 	int rval;
123 
124 	tst_parse_opts(ac, av, NULL, NULL);
125 
126 	setup();
127 
128 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
129 		tst_count = 0;
130 		for (testno = 0; testno < TST_TOTAL; ++testno) {
131 
132 			TEST(pid1 = fork());	//call to fork()
133 			if (TEST_RETURN == -1) {
134 				tst_brkm(TFAIL | TTERRNO, cleanup,
135 					 "fork() failed.");
136 			} else if (TEST_RETURN == 0) {
137 				TEST_RETURN = unshare(-1);
138 				if (TEST_RETURN == 0) {
139 					printf("Call unexpectedly succeeded\n");
140 					rval = 1;
141 				} else if (TEST_RETURN == -1) {
142 					if (errno == EINVAL) {
143 						printf("Got EINVAL\n");
144 						rval = 0;
145 					} else if (errno == ENOSYS) {
146 						rval = 1;
147 					} else {
148 						perror("unshare failed");
149 						rval = 2;
150 					}
151 				}
152 				exit(rval);
153 			} else {
154 				if (wait(&rval) == -1) {
155 					tst_brkm(TBROK | TERRNO, cleanup,
156 						 "wait failed");
157 				}
158 				if (rval != 0 && WIFEXITED(rval)) {
159 					switch (WEXITSTATUS(rval)) {
160 					case 1:
161 						tst_brkm(TBROK, cleanup,
162 							 "unshare call unsupported "
163 							 "in kernel");
164 						break;
165 					case 2:
166 						tst_brkm(TFAIL, cleanup,
167 							 "unshare call failed");
168 						break;
169 					}
170 				}
171 			}
172 
173 			TEST(pid1 = fork());	//call to fork()
174 			if (pid1 == -1) {
175 				tst_brkm(TFAIL | TTERRNO, cleanup,
176 					 "fork() failed.");
177 			} else if (TEST_RETURN == 0) {
178 				TEST_RETURN = unshare(0);
179 				if (TEST_RETURN == 0) {
180 					tst_resm(TPASS, "Call succeeded");
181 					rval = 0;
182 				} else if (TEST_RETURN == -1) {
183 					if (errno == ENOSYS)
184 						rval = 1;
185 					else {
186 						perror("unshare failed");
187 						rval = 2;
188 					}
189 				}
190 				exit(rval);
191 			} else {
192 				if (wait(&rval) == -1) {
193 					tst_brkm(TBROK | TERRNO, cleanup,
194 						 "wait failed");
195 				}
196 				if (rval != 0 && WIFEXITED(rval)) {
197 					switch (WEXITSTATUS(rval)) {
198 					case 1:
199 						tst_brkm(TBROK, cleanup,
200 							 "unshare call unsupported "
201 							 "in kernel");
202 						break;
203 					case 2:
204 						tst_brkm(TFAIL, cleanup,
205 							 "unshare call failed");
206 						break;
207 					}
208 				}
209 			}
210 
211 		}
212 	}
213 	cleanup();
214 	tst_exit();
215 }
216 #else
main(void)217 int main(void)
218 {
219 	tst_brkm(TCONF, NULL, "unshare is undefined.");
220 }
221 #endif
222