blob: 9221f92998bfb8c72d9ca52ae65620b0629a6561 [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
Steven Jackson91d79f92017-01-16 14:55:11 +000041#define _GNU_SOURCE
42
subrata_modakf45ba722009-05-29 12:35:09 +000043#include <stdio.h>
44#include <sys/wait.h>
45#include <sys/types.h>
46#include <sched.h>
47#include <limits.h>
48#include <unistd.h>
49#include <sys/types.h>
50#include <sys/syscall.h>
51#include <errno.h>
52#include <pwd.h>
53#include <grp.h>
54#include <string.h>
55#include <sys/param.h>
56#include <stdio.h>
57
subrata_modakf45ba722009-05-29 12:35:09 +000058#include "test.h"
subrata_modak16cdb4e2009-08-25 07:40:46 +000059#include "config.h"
subrata_modakf45ba722009-05-29 12:35:09 +000060
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020061char *TCID = "unshare02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080062int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020063int TST_TOTAL = 2;
subrata_modakf45ba722009-05-29 12:35:09 +000064
subrata_modak16cdb4e2009-08-25 07:40:46 +000065#ifdef HAVE_UNSHARE
66
subrata_modakf45ba722009-05-29 12:35:09 +000067/* Extern Global Functions */
68/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -080069/* */
70/* Function: cleanup */
71/* */
subrata_modakf45ba722009-05-29 12:35:09 +000072/* Description: Performs all one time clean up for this test on successful */
Garrett Cooper43088e12010-12-13 23:30:59 -080073/* completion, premature exit or failure. Closes all temporary */
74/* files, removes all temporary directories exits the test with */
75/* appropriate TEST_RETURNurn code by calling tst_exit() function. */
76/* */
77/* Input: None. */
78/* */
79/* Output: None. */
80/* */
subrata_modakf45ba722009-05-29 12:35:09 +000081/* Return: On failure - Exits calling tst_exit(). Non '0' TEST_RETURNurn code. */
Garrett Cooper43088e12010-12-13 23:30:59 -080082/* On success - Exits calling tst_exit(). With '0' TEST_RETURNurn code. */
83/* */
subrata_modakf45ba722009-05-29 12:35:09 +000084/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040085void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080086{
Garrett Cooper2c282152010-12-16 00:55:50 -080087
Garrett Cooper43088e12010-12-13 23:30:59 -080088 tst_rmdir();
subrata_modakf45ba722009-05-29 12:35:09 +000089
Garrett Cooper43088e12010-12-13 23:30:59 -080090 /* Exit with appropriate TEST_RETURNurn code. */
Garrett Cooper2c282152010-12-16 00:55:50 -080091
subrata_modakf45ba722009-05-29 12:35:09 +000092}
93
94/* Local Functions */
95/******************************************************************************/
Garrett Cooper43088e12010-12-13 23:30:59 -080096/* */
97/* Function: setup */
98/* */
subrata_modakf45ba722009-05-29 12:35:09 +000099/* Description: Performs all one time setup for this test. This function is */
Garrett Cooper43088e12010-12-13 23:30:59 -0800100/* typically used to capture signals, create temporary dirs */
101/* and temporary files that may be used in the course of this */
102/* test. */
103/* */
104/* Input: None. */
105/* */
106/* Output: None. */
107/* */
108/* Return: On failure - Exits by calling cleanup(). */
109/* On success - TEST_RETURNurns 0. */
110/* */
subrata_modakf45ba722009-05-29 12:35:09 +0000111/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400112void setup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113{
Garrett Cooper43088e12010-12-13 23:30:59 -0800114 /* Capture signals if any */
115 /* Create temporary directories */
116 TEST_PAUSE;
117 tst_tmpdir();
subrata_modakf45ba722009-05-29 12:35:09 +0000118}
119
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120int main(int ac, char **av)
121{
Garrett Cooper43088e12010-12-13 23:30:59 -0800122 pid_t pid1;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200123 int lc;
Garrett Cooper43088e12010-12-13 23:30:59 -0800124 int rval;
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
Cyril Hrubisd6d11d02015-03-09 17:35:43 +0100126 tst_parse_opts(ac, av, NULL, NULL);
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