subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 2 | /* Copyright (c) Crackerjack Project., 2007 */ |
| 3 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 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 */ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 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 */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 17 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 18 | /******************************************************************************/ |
| 19 | /******************************************************************************/ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 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> */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 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 | |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 56 | #include "test.h" |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 57 | #include "config.h" |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 59 | char *TCID = "unshare02"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 61 | int TST_TOTAL = 2; |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 62 | |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 63 | #ifdef HAVE_UNSHARE |
| 64 | |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 65 | /* Extern Global Functions */ |
| 66 | /******************************************************************************/ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 67 | /* */ |
| 68 | /* Function: cleanup */ |
| 69 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 70 | /* Description: Performs all one time clean up for this test on successful */ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 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 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 79 | /* Return: On failure - Exits calling tst_exit(). Non '0' TEST_RETURNurn code. */ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 80 | /* On success - Exits calling tst_exit(). With '0' TEST_RETURNurn code. */ |
| 81 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 82 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 83 | void cleanup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 85 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 86 | tst_rmdir(); |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 88 | /* Exit with appropriate TEST_RETURNurn code. */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 89 | |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* Local Functions */ |
| 93 | /******************************************************************************/ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 94 | /* */ |
| 95 | /* Function: setup */ |
| 96 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 97 | /* Description: Performs all one time setup for this test. This function is */ |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 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 | /* */ |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 109 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 110 | void setup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 111 | { |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 112 | /* Capture signals if any */ |
| 113 | /* Create temporary directories */ |
| 114 | TEST_PAUSE; |
| 115 | tst_tmpdir(); |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | int main(int ac, char **av) |
| 119 | { |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 120 | pid_t pid1; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 121 | int lc; |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 122 | int rval; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 123 | const char *msg; /* message TEST_RETURNurned from parse_opts */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 125 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 127 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 128 | setup(); |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 129 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 130 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 131 | tst_count = 0; |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 132 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 133 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | TEST(pid1 = fork()); //call to fork() |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 135 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | tst_brkm(TFAIL | TTERRNO, cleanup, |
| 137 | "fork() failed."); |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 138 | } else if (TEST_RETURN == 0) { |
| 139 | TEST_RETURN = unshare(-1); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 140 | if (TEST_RETURN == 0) { |
| 141 | printf("Call unexpectedly succeeded\n"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 142 | rval = 1; |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 143 | } 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_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 153 | } |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 154 | exit(rval); |
| 155 | } else { |
| 156 | if (wait(&rval) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | tst_brkm(TBROK | TERRNO, cleanup, |
| 158 | "wait failed"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 159 | } |
| 160 | if (rval != 0 && WIFEXITED(rval)) { |
| 161 | switch (WEXITSTATUS(rval)) { |
| 162 | case 1: |
| 163 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | "unshare call unsupported " |
| 165 | "in kernel"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 166 | break; |
| 167 | case 2: |
| 168 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | "unshare call failed"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 170 | break; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 171 | } |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 172 | } |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | TEST(pid1 = fork()); //call to fork() |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 176 | if (pid1 == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | tst_brkm(TFAIL | TTERRNO, cleanup, |
| 178 | "fork() failed."); |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 179 | } else if (TEST_RETURN == 0) { |
Markos Chandras | 2a4d007 | 2012-01-03 10:44:01 +0000 | [diff] [blame] | 180 | TEST_RETURN = unshare(0); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 181 | if (TEST_RETURN == 0) { |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 182 | tst_resm(TPASS, "Call succeeded"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 183 | rval = 0; |
| 184 | } else if (TEST_RETURN == -1) { |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 185 | if (errno == ENOSYS) |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 186 | rval = 1; |
| 187 | else { |
| 188 | perror("unshare failed"); |
| 189 | rval = 2; |
| 190 | } |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 191 | } |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 192 | exit(rval); |
| 193 | } else { |
| 194 | if (wait(&rval) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | tst_brkm(TBROK | TERRNO, cleanup, |
| 196 | "wait failed"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 197 | } |
| 198 | if (rval != 0 && WIFEXITED(rval)) { |
| 199 | switch (WEXITSTATUS(rval)) { |
| 200 | case 1: |
| 201 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 202 | "unshare call unsupported " |
| 203 | "in kernel"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 204 | break; |
| 205 | case 2: |
| 206 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | "unshare call failed"); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 212 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 213 | } |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 214 | } |
| 215 | cleanup(); |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 216 | tst_exit(); |
subrata_modak | f45ba72 | 2009-05-29 12:35:09 +0000 | [diff] [blame] | 217 | } |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 218 | #else |
| 219 | int main(void) |
| 220 | { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 221 | tst_brkm(TCONF, NULL, "unshare is undefined."); |
subrata_modak | 16cdb4e | 2009-08-25 07:40:46 +0000 | [diff] [blame] | 222 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 223 | #endif |