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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * chroot02.c
23 *
24 * DESCRIPTION
25 * Test functionality of chroot(2)
26 *
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
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.
44 *
45 * RESTRICTIONS
46 * NONE
47 */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 #include <errno.h>
53 #include "test.h"
54 #include <fcntl.h>
55
56 char *TCID = "chroot02";
57 int TST_TOTAL = 1;
58 int fileHandle = 0;
59
60 #define TMP_FILENAME "chroot02_testfile"
61 struct stat buf;
62
63 void setup(void);
64 void cleanup(void);
65
main(int ac,char ** av)66 int main(int ac, char **av)
67 {
68 int lc;
69 int pid, status, retval;
70
71 tst_parse_opts(ac, av, NULL, NULL);
72
73 setup();
74
75 /* Check for looping state if -i option is given */
76 for (lc = 0; TEST_LOOPING(lc); lc++) {
77 /* reset tst_count in case we are looping */
78 tst_count = 0;
79
80 if ((pid = FORK_OR_VFORK()) == -1) {
81 tst_brkm(TBROK, cleanup, "Could not fork");
82 }
83
84 if (pid == 0) {
85 retval = 0;
86
87 if (chroot(tst_get_tmpdir()) == -1) {
88 perror("chroot failed");
89 retval = 1;
90 } else {
91 if (stat("/" TMP_FILENAME, &buf) == -1) {
92 retval = 1;
93 perror("stat failed");
94 }
95 }
96
97 exit(retval);
98 }
99
100 /* parent */
101 wait(&status);
102 /* make sure the child returned a good exit status */
103 if (WIFSIGNALED(status) ||
104 (WIFEXITED(status) && WEXITSTATUS(status) != 0))
105 tst_resm(TFAIL, "chroot functionality incorrect");
106 else
107 tst_resm(TPASS, "chroot functionality correct");
108 }
109
110 cleanup();
111 tst_exit();
112
113 }
114
115 /*
116 * setup() - performs all ONE TIME setup for this test.
117 */
setup(void)118 void setup(void)
119 {
120 tst_require_root();
121
122 tst_tmpdir();
123 if ((fileHandle = creat(TMP_FILENAME, 0777)) == -1)
124 tst_brkm(TBROK, cleanup, "failed to create temporary file "
125 TMP_FILENAME);
126 if (stat(TMP_FILENAME, &buf) != 0)
127 tst_brkm(TBROK, cleanup, TMP_FILENAME " does not exist");
128
129 tst_sig(FORK, DEF_HANDLER, cleanup);
130
131 TEST_PAUSE;
132 }
133
134 /*
135 * cleanup() - performs all ONE TIME cleanup for this test at
136 * completion or premature exit.
137 */
cleanup(void)138 void cleanup(void)
139 {
140 /*
141 * print timing stats if that option was specified.
142 * print errno log if that option was specified.
143 */
144 close(fileHandle);
145
146 tst_rmdir();
147
148 }
149