1 /*
2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
3 * Copyright (c) 2016-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #include <linux/types.h>
14 #include <asm/statfs.h>
15
16 #include "xlat.h"
17 #include "xlat/fsmagic.h"
18 #include "xlat/statfs_flags.h"
19
20 #define PRINT_NUM(arg) \
21 do { \
22 if (sizeof(b->arg) == sizeof(int)) \
23 printf(", %s=%u", #arg, \
24 (unsigned int) b->arg); \
25 else if (sizeof(b->arg) == sizeof(long)) \
26 printf(", %s=%lu", #arg, \
27 (unsigned long) b->arg); \
28 else \
29 printf(", %s=%llu", #arg, \
30 (unsigned long long) b->arg); \
31 } while (0)
32
33 static void
34 print_statfs_type(const char *const prefix, const unsigned int magic)
35 {
36 fputs(prefix, stdout);
37 for (unsigned int i = 0; i < fsmagic->size; ++i)
38 if (magic == fsmagic->data[i].val) {
39 fputs(fsmagic->data[i].str, stdout);
40 return;
41 }
42 printf("%#x", magic);
43 }
44
45 static void
46 print_statfs(const char *const sample, const char *magic_str)
47 {
48 int fd = open(sample, O_RDONLY);
49 if (fd < 0)
50 perror_msg_and_skip("open: %s", sample);
51
52 TAIL_ALLOC_OBJECT_CONST_PTR(STRUCT_STATFS, b);
53 long rc = SYSCALL_INVOKE(sample, fd, b, sizeof(*b));
54 if (rc)
55 perror_msg_and_skip(SYSCALL_NAME);
56
57 PRINT_SYSCALL_HEADER(sample, fd, sizeof(*b));
58 if (magic_str)
59 printf("{f_type=%s", magic_str);
60 else
61 print_statfs_type("{f_type=", b->f_type);
62 PRINT_NUM(f_bsize);
63 PRINT_NUM(f_blocks);
64 PRINT_NUM(f_bfree);
65 PRINT_NUM(f_bavail);
66 PRINT_NUM(f_files);
67 PRINT_NUM(f_ffree);
68 #ifdef PRINT_F_FSID
69 printf(", f_fsid={val=[%#llx, %#llx]}",
70 zero_extend_signed_to_ull(b->PRINT_F_FSID[0]),
71 zero_extend_signed_to_ull(b->PRINT_F_FSID[1]));
72 #endif
73 PRINT_NUM(f_namelen);
74 #ifdef PRINT_F_FRSIZE
75 PRINT_NUM(f_frsize);
76 #endif
77 #ifdef PRINT_F_FLAGS
78 if (b->f_flags & ST_VALID) {
79 printf(", f_flags=");
80 printflags(statfs_flags, b->f_flags, "ST_???");
81 }
82 #endif
83 printf("}) = 0\n");
84 }
85
86 int
87 main(void)
88 {
89 print_statfs("/proc/self/status", "PROC_SUPER_MAGIC");
90
91 print_statfs(".", NULL);
92
93 long rc = SYSCALL_INVOKE("", -1, 0, sizeof(STRUCT_STATFS));
94 const char *errstr = sprintrc(rc);
95 PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS));
96 printf("NULL) = %s\n", errstr);
97
98 #ifdef CHECK_ODD_SIZE
99 const unsigned long addr = (unsigned long) 0xfacefeeddeadbeefULL;
100 rc = SYSCALL_INVOKE("", -1, addr, sizeof(STRUCT_STATFS) + 1);
101 errstr = sprintrc(rc);
102 PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS) + 1);
103 printf("%#lx) = %s\n", addr, errstr);
104 #endif
105
106 puts("+++ exited with 0 +++");
107 return 0;
108 }