1 /*
2 *
3 * Copyright (C) Bull S.A. 2001
4 * Copyright (c) International Business Machines Corp., 2001
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * NAME
23 * statfs03.c
24 *
25 * DESCRIPTION
26 * Testcase to check that statfs(2) sets errno to EACCES when
27 * search permission is denied for a component of the path prefix of path.
28 *
29 * ALGORITHM
30 * Use a component of the pathname, where search permission
31 * is denied for a component of the path prefix of path.
32 *
33 * USAGE: <for command-line>
34 * statfs03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 05/2002 Ported by Jacky Malcles
44 *
45 * RESTRICTIONS
46 * NONE
47 *
48 */
49 #include <sys/types.h>
50 #include <sys/statfs.h>
51 #include <sys/stat.h>
52 #include <sys/vfs.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include "test.h"
57 #include <pwd.h>
58
59 char *TCID = "statfs03";
60 int TST_TOTAL = 1;
61 int fileHandle = 0;
62
63 char nobody_uid[] = "nobody";
64 struct passwd *ltpuser;
65
66 char fname[30] = "testfile";
67 char path[50];
68 struct statfs buf;
69
70 void setup(void);
71 void cleanup(void);
72
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 int lc;
76
77 tst_parse_opts(ac, av, NULL, NULL);
78
79 setup();
80
81 for (lc = 0; TEST_LOOPING(lc); lc++) {
82
83 tst_count = 0;
84
85 TEST(statfs(path, &buf));
86
87 if (TEST_RETURN != -1) {
88 tst_resm(TFAIL, "call succeeded unexpectedly");
89
90 } else {
91
92 if (TEST_ERRNO == EACCES) {
93 tst_resm(TPASS, "expected failure - "
94 "errno = %d : %s", TEST_ERRNO,
95 strerror(TEST_ERRNO));
96 } else {
97 tst_resm(TFAIL, "unexpected error - %d : %s - "
98 "expected %d", TEST_ERRNO,
99 strerror(TEST_ERRNO), EACCES);
100 }
101 }
102 }
103
104 cleanup();
105 tst_exit();
106 }
107
108 /*
109 * setup() - performs all ONE TIME setup for this test.
110 */
setup(void)111 void setup(void)
112 {
113
114 tst_require_root();
115
116 tst_sig(NOFORK, DEF_HANDLER, cleanup);
117
118 TEST_PAUSE;
119
120 /* make a temporary directory and cd to it */
121 tst_tmpdir();
122 if (chmod(tst_get_tmpdir(), S_IRWXU) == -1)
123 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed",
124 tst_get_tmpdir());
125
126 /* create a test file */
127 sprintf(fname, "%s.%d", fname, getpid());
128 if (mkdir(fname, 0444) == -1) {
129 tst_resm(TFAIL, "creat(2) FAILED to creat temp file");
130 } else {
131 sprintf(path, "%s/%s", fname, fname);
132 if ((fileHandle = creat(path, 0444)) == -1)
133 tst_brkm(TFAIL | TERRNO, cleanup, "creat failed");
134 }
135
136 ltpuser = getpwnam(nobody_uid);
137 if (ltpuser == NULL)
138 tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed");
139 if (seteuid(ltpuser->pw_uid) == -1) {
140 tst_resm(TINFO | TERRNO, "seteuid failed to "
141 "to set the effective uid to %d", ltpuser->pw_uid);
142 }
143
144 }
145
146 /*
147 * cleanup() - performs all ONE TIME cleanup for this test at
148 * completion or premature exit.
149 */
cleanup(void)150 void cleanup(void)
151 {
152 /* reset the process ID to the saved ID (root) */
153 if (setuid(0) == -1) {
154 tst_resm(TINFO, "setuid(0) failed");
155 }
156
157 /*
158 * print timing stats if that option was specified.
159 * print errno log if that option was specified.
160 */
161 close(fileHandle);
162
163 /* delete the test directory created in setup() */
164 tst_rmdir();
165
166 }
167