• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Testcase to check if fstatfs() sets errno correctly.
10  */
11 
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/statfs.h>
16 #include <sys/wait.h>
17 #include "tst_test.h"
18 #include "tst_safe_macros.h"
19 
20 static struct statfs buf;
21 
22 static int fd;
23 static int bad_fd = -1;
24 
25 static struct test_case_t {
26 	int *fd;
27 	struct statfs *sbuf;
28 	int error;
29 } tests[] = {
30 	{&bad_fd, &buf, EBADF},
31 	{&fd, (void *)-1, EFAULT},
32 };
33 
fstatfs_verify(unsigned int n)34 static void fstatfs_verify(unsigned int n)
35 {
36 	int pid, status;
37 
38 	pid = SAFE_FORK();
39 	if (!pid) {
40 		TST_EXP_FAIL(fstatfs(*tests[n].fd, tests[n].sbuf), tests[n].error, "fstatfs()");
41 		exit(0);
42 	}
43 
44 	SAFE_WAITPID(pid, &status, 0);
45 
46 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
47 		return;
48 
49 	if (tests[n].error == EFAULT &&
50 	    WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
51 		tst_res(TPASS, "Got SIGSEGV instead of EFAULT");
52 		return;
53 	}
54 
55 	tst_res(TFAIL, "Child %s", tst_strstatus(status));
56 }
57 
setup(void)58 static void setup(void)
59 {
60 	fd = SAFE_OPEN("tempfile", O_RDWR | O_CREAT, 0700);
61 }
62 
cleanup(void)63 static void cleanup(void)
64 {
65 	if (fd > 0)
66 		SAFE_CLOSE(fd);
67 }
68 
69 static struct tst_test test = {
70 	.test = fstatfs_verify,
71 	.tcnt = ARRAY_SIZE(tests),
72 	.setup = setup,
73 	.cleanup = cleanup,
74 	.needs_tmpdir = 1,
75 	.forks_child = 1,
76 };
77