xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fstatfs/fstatfs01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Verify that fstatfs() syscall executes successfully for all
11*49cdfc7eSAndroid Build Coastguard Worker  * available filesystems.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
19*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE MNT_POINT"/test_file"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static int file_fd;
22*49cdfc7eSAndroid Build Coastguard Worker static int pipe_fd;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
25*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
26*49cdfc7eSAndroid Build Coastguard Worker 	const char *msg;
27*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
28*49cdfc7eSAndroid Build Coastguard Worker 	{&file_fd, "fstatfs() on a file"},
29*49cdfc7eSAndroid Build Coastguard Worker 	{&pipe_fd, "fstatfs() on a pipe"},
30*49cdfc7eSAndroid Build Coastguard Worker };
31*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)32*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
35*49cdfc7eSAndroid Build Coastguard Worker 	struct statfs buf;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(fstatfs(*tc->fd, &buf), "%s", tc->msg);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)40*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	int pipe[2];
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	file_fd = SAFE_OPEN(TEMP_FILE, O_RDWR | O_CREAT, 0700);
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipe);
46*49cdfc7eSAndroid Build Coastguard Worker 	pipe_fd = pipe[0];
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipe[1]);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	if (file_fd > 0)
53*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(file_fd);
54*49cdfc7eSAndroid Build Coastguard Worker 	if (pipe_fd > 0)
55*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(pipe_fd);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
60*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
61*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
62*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
63*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
65*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
66*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1
67*49cdfc7eSAndroid Build Coastguard Worker };
68