blob: 26c01a0341f4dbcd623028eb2791ad6f63d1e628 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * chroot02.c
23 *
24 * DESCRIPTION
25 * Test functionality of chroot(2)
subrata_modakbdbaec52009-02-26 12:14:51 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * 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
iyermanoj2384ab62003-04-30 22:39:41 +000041 * 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.
plars865695b2001-08-27 22:15:12 +000044 *
45 * RESTRICTIONS
46 * NONE
47 */
48
49#include <sys/types.h>
50#include <sys/stat.h>
robbiew2c945242002-11-11 19:01:25 +000051#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000052#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080053#include "test.h"
iyermanoj2384ab62003-04-30 22:39:41 +000054#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000055
56char *TCID = "chroot02";
57int TST_TOTAL = 1;
mridge17a88662004-08-25 14:55:18 +000058int fileHandle = 0;
plars865695b2001-08-27 22:15:12 +000059
Garrett Cooperf23dd412010-12-17 01:57:35 -080060#define TMP_FILENAME "chroot02_testfile"
plars865695b2001-08-27 22:15:12 +000061struct stat buf;
62
63void setup(void);
64void cleanup(void);
65
subrata_modak56207ce2009-03-23 13:35:39 +000066int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000067{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
Garrett Cooperf23dd412010-12-17 01:57:35 -080070 int pid, status, retval;
plars865695b2001-08-27 22:15:12 +000071
Garrett Cooperf23dd412010-12-17 01:57:35 -080072 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080073 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000074
75 setup();
76
77 /* Check for looping state if -i option is given */
78 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080079 /* reset tst_count in case we are looping */
80 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000081
robbiewd34d5812005-07-11 22:28:09 +000082 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +000083 tst_brkm(TBROK, cleanup, "Could not fork");
84 }
85
Garrett Cooperf23dd412010-12-17 01:57:35 -080086 if (pid == 0) {
87 retval = 0;
plars865695b2001-08-27 22:15:12 +000088
Cyril Hrubis9c31ad22014-05-14 17:15:39 +020089 if (chroot(tst_get_tmpdir()) == -1) {
Garrett Cooperf23dd412010-12-17 01:57:35 -080090 perror("chroot failed");
subrata_modak56207ce2009-03-23 13:35:39 +000091 retval = 1;
Garrett Cooperf23dd412010-12-17 01:57:35 -080092 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020093 if (stat("/" TMP_FILENAME, &buf) == -1) {
94 retval = 1;
95 perror("stat failed");
Garrett Cooperf23dd412010-12-17 01:57:35 -080096 }
plars865695b2001-08-27 22:15:12 +000097 }
98
plars3bae6a22001-09-12 18:35:49 +000099 exit(retval);
plars865695b2001-08-27 22:15:12 +0000100 }
101
102 /* parent */
plars3bae6a22001-09-12 18:35:49 +0000103 wait(&status);
104 /* make sure the child returned a good exit status */
Garrett Cooperf23dd412010-12-17 01:57:35 -0800105 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");
plars865695b2001-08-27 22:15:12 +0000110 }
plars865695b2001-08-27 22:15:12 +0000111
Garrett Cooperf23dd412010-12-17 01:57:35 -0800112 cleanup();
113 tst_exit();
114
115}
plars865695b2001-08-27 22:15:12 +0000116
117/*
118 * setup() - performs all ONE TIME setup for this test.
119 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400120void setup(void)
plars865695b2001-08-27 22:15:12 +0000121{
Garrett Cooperf23dd412010-12-17 01:57:35 -0800122 tst_require_root(NULL);
123
iyermanoj2384ab62003-04-30 22:39:41 +0000124 tst_tmpdir();
Garrett Cooperf23dd412010-12-17 01:57:35 -0800125 if ((fileHandle = creat(TMP_FILENAME, 0777)) == -1)
126 tst_brkm(TBROK, cleanup, "failed to create temporary file "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 TMP_FILENAME);
Garrett Cooperf23dd412010-12-17 01:57:35 -0800128 if (stat(TMP_FILENAME, &buf) != 0)
129 tst_brkm(TBROK, cleanup, TMP_FILENAME " does not exist");
plars865695b2001-08-27 22:15:12 +0000130
plars865695b2001-08-27 22:15:12 +0000131 tst_sig(FORK, DEF_HANDLER, cleanup);
132
plars865695b2001-08-27 22:15:12 +0000133 TEST_PAUSE;
134}
135
plars865695b2001-08-27 22:15:12 +0000136/*
137 * cleanup() - performs all ONE TIME cleanup for this test at
138 * completion or premature exit.
139 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400140void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000141{
142 /*
143 * print timing stats if that option was specified.
144 * print errno log if that option was specified.
145 */
subrata_modak56207ce2009-03-23 13:35:39 +0000146 close(fileHandle);
mridge17a88662004-08-25 14:55:18 +0000147
iyermanoj2384ab62003-04-30 22:39:41 +0000148 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000149
Chris Dearmanec6edca2012-10-17 19:54:01 -0700150}