blob: 9e9b59d97de8dde3188b2f6297a1fd9782d3f7cc [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Zeng Linggang327e6192014-05-09 17:06:54 +08002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Zeng Linggang327e6192014-05-09 17:06:54 +08005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Zeng Linggang327e6192014-05-09 17:06:54 +080010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Zeng Linggang327e6192014-05-09 17:06:54 +080015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * DESCRIPTION
plars865695b2001-08-27 22:15:12 +000022 * 1. Use a component of the pathname, which is not a directory
23 * in the "path" parameter to statfs(). Expect ENOTDIR
24 * 2. Pass a filename which doesn't exist, and expect ENOENT.
25 * 3. Pass a pathname which is more than MAXNAMLEN, and expect
26 * ENAMETOOLONG.
27 * 4. Pass a pointer to the pathname outside the address space of
28 * the process, and expect EFAULT.
29 * 5. Pass a pointer to the buf paramter outside the address space
30 * of the process, and expect EFAULT.
Zeng Linggang661ece32014-05-09 19:39:48 +080031 * 6. Pass a filename which has too many symbolic links, and expect
32 * ELOOP.
plars865695b2001-08-27 22:15:12 +000033 */
Zeng Linggang327e6192014-05-09 17:06:54 +080034
plars865695b2001-08-27 22:15:12 +000035#include <sys/types.h>
36#include <sys/statfs.h>
37#include <sys/stat.h>
plars74948ad2002-11-14 16:16:14 +000038#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000039#include <sys/vfs.h>
plars1ad84512002-07-23 13:11:18 +000040#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000041#include <errno.h>
42#include "test.h"
Zeng Linggang327e6192014-05-09 17:06:54 +080043#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000044
nstrazfa31d552002-05-14 16:50:06 +000045char *TCID = "statfs02";
plars865695b2001-08-27 22:15:12 +000046
Zeng Linggang327e6192014-05-09 17:06:54 +080047static int fd;
plars1ad84512002-07-23 13:11:18 +000048
Zeng Linggang327e6192014-05-09 17:06:54 +080049#define TEST_FILE "statfs_file"
50#define TEST_FILE1 TEST_FILE"/statfs_file1"
51#define TEST_NOEXIST "statfs_noexist"
Zeng Linggang661ece32014-05-09 19:39:48 +080052#define TEST_SYMLINK "statfs_symlink"
subrata_modakbdbaec52009-02-26 12:14:51 +000053
Zeng Linggang327e6192014-05-09 17:06:54 +080054static char test_toolong[PATH_MAX+2];
55static struct statfs buf;
56
57static struct test_case_t {
plars865695b2001-08-27 22:15:12 +000058 char *path;
59 struct statfs *buf;
Zeng Linggang327e6192014-05-09 17:06:54 +080060 int exp_error;
plars865695b2001-08-27 22:15:12 +000061} TC[] = {
Zeng Linggang327e6192014-05-09 17:06:54 +080062 {TEST_FILE1, &buf, ENOTDIR},
63 {TEST_NOEXIST, &buf, ENOENT},
64 {test_toolong, &buf, ENAMETOOLONG},
vapier7ec19d92006-02-27 04:38:56 +000065#ifndef UCLINUX
Zeng Linggang327e6192014-05-09 17:06:54 +080066 {(char *)-1, &buf, EFAULT},
67 {TEST_FILE, (struct statfs *)-1, EFAULT},
vapier7ec19d92006-02-27 04:38:56 +000068#endif
Zeng Linggang661ece32014-05-09 19:39:48 +080069 {TEST_SYMLINK, &buf, ELOOP},
plars865695b2001-08-27 22:15:12 +000070};
71
Zeng Linggang327e6192014-05-09 17:06:54 +080072int TST_TOTAL = ARRAY_SIZE(TC);
73static void setup(void);
74static void cleanup(void);
75static void statfs_verify(const struct test_case_t *);
plars865695b2001-08-27 22:15:12 +000076
plars74948ad2002-11-14 16:16:14 +000077int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000078{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020079 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020080 const char *msg;
plars865695b2001-08-27 22:15:12 +000081 int i;
82
Zeng Linggang327e6192014-05-09 17:06:54 +080083 msg = parse_opts(ac, av, NULL, NULL);
84 if (msg != NULL)
Garrett Cooper45e285d2010-11-22 12:19:25 -080085 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000086
87 setup();
88
plars865695b2001-08-27 22:15:12 +000089 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080090 tst_count = 0;
Zeng Linggang327e6192014-05-09 17:06:54 +080091 for (i = 0; i < TST_TOTAL; i++)
92 statfs_verify(&TC[i]);
plars865695b2001-08-27 22:15:12 +000093 }
Garrett Cooper45e285d2010-11-22 12:19:25 -080094
plars865695b2001-08-27 22:15:12 +000095 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080096 tst_exit();
plars865695b2001-08-27 22:15:12 +000097}
98
Zeng Linggang327e6192014-05-09 17:06:54 +080099static void setup(void)
plars865695b2001-08-27 22:15:12 +0000100{
plars865695b2001-08-27 22:15:12 +0000101 tst_sig(NOFORK, DEF_HANDLER, cleanup);
102
plars865695b2001-08-27 22:15:12 +0000103 TEST_PAUSE;
104
plars865695b2001-08-27 22:15:12 +0000105 tst_tmpdir();
106
Zeng Linggang327e6192014-05-09 17:06:54 +0800107 fd = SAFE_CREAT(cleanup, TEST_FILE, 0444);
plars865695b2001-08-27 22:15:12 +0000108
Zeng Linggang327e6192014-05-09 17:06:54 +0800109 memset(test_toolong, 'a', PATH_MAX+1);
plars1ad84512002-07-23 13:11:18 +0000110
vapier62b16cf2007-02-09 20:48:23 +0000111#if !defined(UCLINUX)
Zeng Linggang327e6192014-05-09 17:06:54 +0800112 TC[3].path = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
113 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
vapierb5ed1f62006-08-24 04:16:32 +0000114#endif
Zeng Linggang661ece32014-05-09 19:39:48 +0800115
116 SAFE_SYMLINK(cleanup, TEST_SYMLINK, "statfs_symlink_2");
117 SAFE_SYMLINK(cleanup, "statfs_symlink_2", TEST_SYMLINK);
plars865695b2001-08-27 22:15:12 +0000118}
119
Zeng Linggang327e6192014-05-09 17:06:54 +0800120static void statfs_verify(const struct test_case_t *test)
plars865695b2001-08-27 22:15:12 +0000121{
Zeng Linggang327e6192014-05-09 17:06:54 +0800122 TEST(statfs(test->path, test->buf));
123
124 if (TEST_RETURN != -1) {
125 tst_resm(TFAIL, "call succeeded unexpectedly");
126 return;
127 }
128
Zeng Linggang327e6192014-05-09 17:06:54 +0800129 if (TEST_ERRNO == test->exp_error) {
130 tst_resm(TPASS | TTERRNO, "expected failure");
131 } else {
132 tst_resm(TFAIL | TTERRNO, "unexpected error, expected %d",
133 TEST_ERRNO);
134 }
135}
136
137static void cleanup(void)
138{
139 if (fd > 0)
140 close(fd);
mridge3db3f032004-08-25 16:26:11 +0000141
plars865695b2001-08-27 22:15:12 +0000142 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700143}