1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * Copyright (c) International Business Machines  Corp., 2001
3865695bbc89088b9526ea9045410e5afb70a985cplars *
4c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * This program is free software;  you can redistribute it and/or modify
5c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * it under the terms of the GNU General Public License as published by
6c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * the Free Software Foundation; either version 2 of the License, or
7c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * (at your option) any later version.
8865695bbc89088b9526ea9045410e5afb70a985cplars *
9c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * This program is distributed in the hope that it will be useful,
10c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * the GNU General Public License for more details.
13865695bbc89088b9526ea9045410e5afb70a985cplars *
14c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * You should have received a copy of the GNU General Public License
15c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * along with this program;  if not, write to the Free Software
16c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiu * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17865695bbc89088b9526ea9045410e5afb70a985cplars */
18865695bbc89088b9526ea9045410e5afb70a985cplars
19865695bbc89088b9526ea9045410e5afb70a985cplars/*
20865695bbc89088b9526ea9045410e5afb70a985cplars * DESCRIPTION
21865695bbc89088b9526ea9045410e5afb70a985cplars *	Testcase to check fstatfs() sets errno correctly.
22865695bbc89088b9526ea9045410e5afb70a985cplars */
23865695bbc89088b9526ea9045410e5afb70a985cplars
24865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/vfs.h>
25865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h>
26865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/statfs.h>
27865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
28e8530df4da095b0ea36a9ff8118ab5ce906b3e84Garrett Cooper#include "test.h"
29fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu#include "safe_macros.h"
30865695bbc89088b9526ea9045410e5afb70a985cplars
31c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic void setup(void);
32c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic void cleanup(void);
33865695bbc89088b9526ea9045410e5afb70a985cplars
34fa31d55d3486830313bd044f7333697ce6124d22nstrazchar *TCID = "fstatfs02";
35865695bbc89088b9526ea9045410e5afb70a985cplars
36c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic struct statfs buf;
37865695bbc89088b9526ea9045410e5afb70a985cplars
38c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic struct test_case_t {
3956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int fd;
4056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	struct statfs *sbuf;
4156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int error;
42865695bbc89088b9526ea9045410e5afb70a985cplars} TC[] = {
43865695bbc89088b9526ea9045410e5afb70a985cplars	/* EBADF - fd is invalid */
4456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{
4556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	-1, &buf, EBADF},
467ec19d9c95d491394401f8e2d88ec94f13f596dfvapier#ifndef UCLINUX
4756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	    /* Skip since uClinux does not implement memory protection */
4856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	    /* EFAULT - address for buf is invalid */
4956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	{
50fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu	-1, (void *)-1, EFAULT}
517ec19d9c95d491394401f8e2d88ec94f13f596dfvapier#endif
52865695bbc89088b9526ea9045410e5afb70a985cplars};
53865695bbc89088b9526ea9045410e5afb70a985cplars
54b863a0bd366f8af57b000e2f1da5da0bfd8cebf7Cyril Hrubisint TST_TOTAL = ARRAY_SIZE(TC);
5583b1dda4c6fd8b22bdd2593fdd2d4cac34278f19vapier
565aab8a773ae811e815dcbe0ea5c4fee3f0a04f1erobbiewint main(int ac, char **av)
57865695bbc89088b9526ea9045410e5afb70a985cplars{
5889af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
59865695bbc89088b9526ea9045410e5afb70a985cplars	int i;
60865695bbc89088b9526ea9045410e5afb70a985cplars
61d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
62865695bbc89088b9526ea9045410e5afb70a985cplars
63865695bbc89088b9526ea9045410e5afb70a985cplars	setup();
64865695bbc89088b9526ea9045410e5afb70a985cplars
65865695bbc89088b9526ea9045410e5afb70a985cplars	for (lc = 0; TEST_LOOPING(lc); lc++) {
66865695bbc89088b9526ea9045410e5afb70a985cplars
67d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
68865695bbc89088b9526ea9045410e5afb70a985cplars
69865695bbc89088b9526ea9045410e5afb70a985cplars		for (i = 0; i < TST_TOTAL; i++) {
70865695bbc89088b9526ea9045410e5afb70a985cplars
71865695bbc89088b9526ea9045410e5afb70a985cplars			TEST(fstatfs(TC[i].fd, TC[i].sbuf));
72865695bbc89088b9526ea9045410e5afb70a985cplars
7356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (TEST_RETURN != -1) {
7456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL, "call succeeded unexpectedly");
7556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				continue;
7656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
7756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
7856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			if (TEST_ERRNO == TC[i].error) {
7956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TPASS, "expected failure - "
8056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "errno = %d : %s", TEST_ERRNO,
8156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 strerror(TEST_ERRNO));
8256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			} else {
8356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak				tst_resm(TFAIL, "unexpected error - %d : %s - "
8456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 "expected %d", TEST_ERRNO,
8556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak					 strerror(TEST_ERRNO), TC[i].error);
86865695bbc89088b9526ea9045410e5afb70a985cplars			}
87865695bbc89088b9526ea9045410e5afb70a985cplars		}
88865695bbc89088b9526ea9045410e5afb70a985cplars	}
89865695bbc89088b9526ea9045410e5afb70a985cplars	cleanup();
90865695bbc89088b9526ea9045410e5afb70a985cplars
917d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
92865695bbc89088b9526ea9045410e5afb70a985cplars}
93865695bbc89088b9526ea9045410e5afb70a985cplars
94c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic void setup(void)
95865695bbc89088b9526ea9045410e5afb70a985cplars{
96865695bbc89088b9526ea9045410e5afb70a985cplars	tst_sig(NOFORK, DEF_HANDLER, cleanup);
97865695bbc89088b9526ea9045410e5afb70a985cplars
98865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
99865695bbc89088b9526ea9045410e5afb70a985cplars
100865695bbc89088b9526ea9045410e5afb70a985cplars	tst_tmpdir();
101fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu#ifndef UCLINUX
102fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu	TC[1].fd = SAFE_OPEN(cleanup, "tempfile", O_RDWR | O_CREAT, 0700);
103fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu#endif
104865695bbc89088b9526ea9045410e5afb70a985cplars}
105865695bbc89088b9526ea9045410e5afb70a985cplars
106c7a4cfad9cab977a7f68479584f37e5f05351a4bShuang Qiustatic void cleanup(void)
107865695bbc89088b9526ea9045410e5afb70a985cplars{
108fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu#ifndef UCLINUX
109fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu	if (TC[1].fd > 0 && close(TC[1].fd))
110fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu		tst_resm(TWARN | TERRNO, "Failed to close fd");
111fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu#endif
112fe0461dfeb9ca6c8922e3d41c894cf74289bcab9Shuang Qiu
113865695bbc89088b9526ea9045410e5afb70a985cplars	tst_rmdir();
114ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
115