plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * chroot02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Test functionality of chroot(2) |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * ALGORITHM |
| 28 | * Change root directory and then stat a file. |
| 29 | * |
| 30 | * USAGE: <for command-line> |
| 31 | * chroot02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 32 | * where, -c n : Run n copies concurrently. |
| 33 | * -f : Turn off functionality Testing. |
| 34 | * -i n : Execute test n times. |
| 35 | * -I x : Execute test for x seconds. |
| 36 | * -P x : Pause for x seconds between iterations. |
| 37 | * -t : Turn on syscall timing. |
| 38 | * |
| 39 | * HISTORY |
| 40 | * 07/2001 Ported by Wayne Boyer |
iyermanoj | 2384ab6 | 2003-04-30 22:39:41 +0000 | [diff] [blame] | 41 | * 04/2003 Modified by Manoj Iyer - manjo@mail.utexas.edu |
| 42 | * Change testcase to chroot into a temporary directory |
| 43 | * and stat() a known file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * |
| 45 | * RESTRICTIONS |
| 46 | * NONE |
| 47 | */ |
| 48 | |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 51 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 53 | #include "test.h" |
iyermanoj | 2384ab6 | 2003-04-30 22:39:41 +0000 | [diff] [blame] | 54 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | char *TCID = "chroot02"; |
| 57 | int TST_TOTAL = 1; |
mridge | 17a8866 | 2004-08-25 14:55:18 +0000 | [diff] [blame] | 58 | int fileHandle = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 60 | #define TMP_FILENAME "chroot02_testfile" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | struct stat buf; |
| 62 | |
| 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 70 | int pid, status, retval; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 72 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 73 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | setup(); |
| 76 | |
| 77 | /* Check for looping state if -i option is given */ |
| 78 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 79 | /* reset tst_count in case we are looping */ |
| 80 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 82 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | tst_brkm(TBROK, cleanup, "Could not fork"); |
| 84 | } |
| 85 | |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 86 | if (pid == 0) { |
| 87 | retval = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
Cyril Hrubis | 9c31ad2 | 2014-05-14 17:15:39 +0200 | [diff] [blame] | 89 | if (chroot(tst_get_tmpdir()) == -1) { |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 90 | perror("chroot failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | retval = 1; |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 92 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 93 | if (stat("/" TMP_FILENAME, &buf) == -1) { |
| 94 | retval = 1; |
| 95 | perror("stat failed"); |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 96 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
plars | 3bae6a2 | 2001-09-12 18:35:49 +0000 | [diff] [blame] | 99 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* parent */ |
plars | 3bae6a2 | 2001-09-12 18:35:49 +0000 | [diff] [blame] | 103 | wait(&status); |
| 104 | /* make sure the child returned a good exit status */ |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 105 | if (WIFSIGNALED(status) || |
| 106 | (WIFEXITED(status) && WEXITSTATUS(status) != 0)) |
| 107 | tst_resm(TFAIL, "chroot functionality incorrect"); |
| 108 | else |
| 109 | tst_resm(TPASS, "chroot functionality correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 112 | cleanup(); |
| 113 | tst_exit(); |
| 114 | |
| 115 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * setup() - performs all ONE TIME setup for this test. |
| 119 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 120 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | { |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 122 | tst_require_root(NULL); |
| 123 | |
iyermanoj | 2384ab6 | 2003-04-30 22:39:41 +0000 | [diff] [blame] | 124 | tst_tmpdir(); |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 125 | if ((fileHandle = creat(TMP_FILENAME, 0777)) == -1) |
| 126 | tst_brkm(TBROK, cleanup, "failed to create temporary file " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | TMP_FILENAME); |
Garrett Cooper | f23dd41 | 2010-12-17 01:57:35 -0800 | [diff] [blame] | 128 | if (stat(TMP_FILENAME, &buf) != 0) |
| 129 | tst_brkm(TBROK, cleanup, TMP_FILENAME " does not exist"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | TEST_PAUSE; |
| 134 | } |
| 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | /* |
| 137 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 138 | * completion or premature exit. |
| 139 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 140 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | { |
| 142 | /* |
| 143 | * print timing stats if that option was specified. |
| 144 | * print errno log if that option was specified. |
| 145 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | close(fileHandle); |
mridge | 17a8866 | 2004-08-25 14:55:18 +0000 | [diff] [blame] | 147 | |
iyermanoj | 2384ab6 | 2003-04-30 22:39:41 +0000 | [diff] [blame] | 148 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 150 | } |