blob: 2a9d80bfd762bbf98c20ee10e26e0fe3d710e298 [file] [log] [blame]
subrata_modakf45ba722009-05-29 12:35:09 +00001/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -08002/* Copyright (c) Crackerjack Project., 2007 */
3/* */
subrata_modakf45ba722009-05-29 12:35:09 +00004/* 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 */
Garrett Cooper43088e12010-12-13 23:30:59 -08006/* 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 */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
Garrett Cooper43088e12010-12-13 23:30:59 -080017/* */
subrata_modakf45ba722009-05-29 12:35:09 +000018/******************************************************************************/
19/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -080020/* */
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> */
subrata_modakf45ba722009-05-29 12:35:09 +000039/******************************************************************************/
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
subrata_modakf45ba722009-05-29 12:35:09 +000056#include "test.h"
subrata_modak16cdb4e2009-08-25 07:40:46 +000057#include "config.h"
subrata_modakf45ba722009-05-29 12:35:09 +000058
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020059char *TCID = "unshare02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080060int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020061int TST_TOTAL = 2;
subrata_modakf45ba722009-05-29 12:35:09 +000062
subrata_modak16cdb4e2009-08-25 07:40:46 +000063#ifdef HAVE_UNSHARE
64
subrata_modakf45ba722009-05-29 12:35:09 +000065/* Extern Global Functions */
66/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -080067/* */
68/* Function: cleanup */
69/* */
subrata_modakf45ba722009-05-29 12:35:09 +000070/* Description: Performs all one time clean up for this test on successful */
Garrett Cooper43088e12010-12-13 23:30:59 -080071/* 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/* */
subrata_modakf45ba722009-05-29 12:35:09 +000079/* Return: On failure - Exits calling tst_exit(). Non '0' TEST_RETURNurn code. */
Garrett Cooper43088e12010-12-13 23:30:59 -080080/* On success - Exits calling tst_exit(). With '0' TEST_RETURNurn code. */
81/* */
subrata_modakf45ba722009-05-29 12:35:09 +000082/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040083void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080084{
Garrett Cooper2c282152010-12-16 00:55:50 -080085
Garrett Cooper43088e12010-12-13 23:30:59 -080086 tst_rmdir();
subrata_modakf45ba722009-05-29 12:35:09 +000087
Garrett Cooper43088e12010-12-13 23:30:59 -080088 /* Exit with appropriate TEST_RETURNurn code. */
Garrett Cooper2c282152010-12-16 00:55:50 -080089
subrata_modakf45ba722009-05-29 12:35:09 +000090}
91
92/* Local Functions */
93/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -080094/* */
95/* Function: setup */
96/* */
subrata_modakf45ba722009-05-29 12:35:09 +000097/* Description: Performs all one time setup for this test. This function is */
Garrett Cooper43088e12010-12-13 23:30:59 -080098/* 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/* */
subrata_modakf45ba722009-05-29 12:35:09 +0000109/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400110void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111{
Garrett Cooper43088e12010-12-13 23:30:59 -0800112 /* Capture signals if any */
113 /* Create temporary directories */
114 TEST_PAUSE;
115 tst_tmpdir();
subrata_modakf45ba722009-05-29 12:35:09 +0000116}
117
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118int main(int ac, char **av)
119{
Garrett Cooper43088e12010-12-13 23:30:59 -0800120 pid_t pid1;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200121 int lc;
Garrett Cooper43088e12010-12-13 23:30:59 -0800122 int rval;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200123 const char *msg; /* message TEST_RETURNurned from parse_opts */
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
Garrett Cooper43088e12010-12-13 23:30:59 -0800125 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakf45ba722009-05-29 12:35:09 +0000127
Garrett Cooper43088e12010-12-13 23:30:59 -0800128 setup();
subrata_modakf45ba722009-05-29 12:35:09 +0000129
Garrett Cooper43088e12010-12-13 23:30:59 -0800130 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800131 tst_count = 0;
Garrett Cooper43088e12010-12-13 23:30:59 -0800132 for (testno = 0; testno < TST_TOTAL; ++testno) {
subrata_modakf45ba722009-05-29 12:35:09 +0000133
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 TEST(pid1 = fork()); //call to fork()
subrata_modak16cdb4e2009-08-25 07:40:46 +0000135 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 tst_brkm(TFAIL | TTERRNO, cleanup,
137 "fork() failed.");
subrata_modak16cdb4e2009-08-25 07:40:46 +0000138 } else if (TEST_RETURN == 0) {
139 TEST_RETURN = unshare(-1);
Garrett Cooper43088e12010-12-13 23:30:59 -0800140 if (TEST_RETURN == 0) {
141 printf("Call unexpectedly succeeded\n");
Garrett Cooper53740502010-12-16 00:04:01 -0800142 rval = 1;
Garrett Cooper43088e12010-12-13 23:30:59 -0800143 } else if (TEST_RETURN == -1) {
144 if (errno == EINVAL) {
145 printf("Got EINVAL\n");
146 rval = 0;
147 } else if (errno == ENOSYS) {
148 rval = 1;
149 } else {
150 perror("unshare failed");
151 rval = 2;
152 }
subrata_modakf45ba722009-05-29 12:35:09 +0000153 }
Garrett Cooper43088e12010-12-13 23:30:59 -0800154 exit(rval);
155 } else {
156 if (wait(&rval) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 tst_brkm(TBROK | TERRNO, cleanup,
158 "wait failed");
Garrett Cooper43088e12010-12-13 23:30:59 -0800159 }
160 if (rval != 0 && WIFEXITED(rval)) {
161 switch (WEXITSTATUS(rval)) {
162 case 1:
163 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 "unshare call unsupported "
165 "in kernel");
Garrett Cooper43088e12010-12-13 23:30:59 -0800166 break;
167 case 2:
168 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 "unshare call failed");
Garrett Cooper43088e12010-12-13 23:30:59 -0800170 break;
Garrett Cooper2c282152010-12-16 00:55:50 -0800171 }
Garrett Cooper43088e12010-12-13 23:30:59 -0800172 }
subrata_modakf45ba722009-05-29 12:35:09 +0000173 }
174
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 TEST(pid1 = fork()); //call to fork()
subrata_modak16cdb4e2009-08-25 07:40:46 +0000176 if (pid1 == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 tst_brkm(TFAIL | TTERRNO, cleanup,
178 "fork() failed.");
subrata_modak16cdb4e2009-08-25 07:40:46 +0000179 } else if (TEST_RETURN == 0) {
Markos Chandras2a4d0072012-01-03 10:44:01 +0000180 TEST_RETURN = unshare(0);
Garrett Cooper43088e12010-12-13 23:30:59 -0800181 if (TEST_RETURN == 0) {
subrata_modak16cdb4e2009-08-25 07:40:46 +0000182 tst_resm(TPASS, "Call succeeded");
Garrett Cooper43088e12010-12-13 23:30:59 -0800183 rval = 0;
184 } else if (TEST_RETURN == -1) {
subrata_modak16cdb4e2009-08-25 07:40:46 +0000185 if (errno == ENOSYS)
Garrett Cooper43088e12010-12-13 23:30:59 -0800186 rval = 1;
187 else {
188 perror("unshare failed");
189 rval = 2;
190 }
subrata_modak16cdb4e2009-08-25 07:40:46 +0000191 }
Garrett Cooper43088e12010-12-13 23:30:59 -0800192 exit(rval);
193 } else {
194 if (wait(&rval) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800195 tst_brkm(TBROK | TERRNO, cleanup,
196 "wait failed");
Garrett Cooper43088e12010-12-13 23:30:59 -0800197 }
198 if (rval != 0 && WIFEXITED(rval)) {
199 switch (WEXITSTATUS(rval)) {
200 case 1:
201 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 "unshare call unsupported "
203 "in kernel");
Garrett Cooper43088e12010-12-13 23:30:59 -0800204 break;
205 case 2:
206 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 "unshare call failed");
Garrett Cooper43088e12010-12-13 23:30:59 -0800208 break;
209 }
210 }
211 }
subrata_modakf45ba722009-05-29 12:35:09 +0000212
Garrett Cooper43088e12010-12-13 23:30:59 -0800213 }
subrata_modakf45ba722009-05-29 12:35:09 +0000214 }
215 cleanup();
Garrett Cooper43088e12010-12-13 23:30:59 -0800216 tst_exit();
subrata_modakf45ba722009-05-29 12:35:09 +0000217}
subrata_modak16cdb4e2009-08-25 07:40:46 +0000218#else
219int main(void)
220{
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100221 tst_brkm(TCONF, NULL, "unshare is undefined.");
subrata_modak16cdb4e2009-08-25 07:40:46 +0000222}
Chris Dearmanec6edca2012-10-17 19:54:01 -0700223#endif