]> granicus.if.org Git - strace/blob - statfs.c
tests: factor out common shell code to functions
[strace] / statfs.c
1 #include "defs.h"
2 #ifdef HAVE_SYS_VFS_H
3 # include <sys/vfs.h>
4 #endif
5 #include "xlat/fsmagic.h"
6
7 static const char *
8 sprintfstype(const unsigned int magic)
9 {
10         static char buf[32];
11         const char *s;
12
13         s = xlat_search(fsmagic, ARRAY_SIZE(fsmagic), magic);
14         if (s) {
15                 sprintf(buf, "\"%s\"", s);
16                 return buf;
17         }
18         sprintf(buf, "%#x", magic);
19         return buf;
20 }
21
22 static void
23 printstatfs(struct tcb *tcp, const long addr)
24 {
25         struct statfs statbuf;
26
27         if (syserror(tcp) || !verbose(tcp)) {
28                 tprintf("%#lx", addr);
29                 return;
30         }
31         if (umove(tcp, addr, &statbuf) < 0) {
32                 tprints("{...}");
33                 return;
34         }
35         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%lu, f_bfree=%lu, ",
36                 sprintfstype(statbuf.f_type),
37                 (unsigned long)statbuf.f_bsize,
38                 (unsigned long)statbuf.f_blocks,
39                 (unsigned long)statbuf.f_bfree);
40         tprintf("f_bavail=%lu, f_files=%lu, f_ffree=%lu, f_fsid={%d, %d}",
41                 (unsigned long)statbuf.f_bavail,
42                 (unsigned long)statbuf.f_files,
43                 (unsigned long)statbuf.f_ffree,
44                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
45         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
46 #ifdef _STATFS_F_FRSIZE
47         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
48 #endif
49 #ifdef _STATFS_F_FLAGS
50         tprintf(", f_flags=%lu", (unsigned long)statbuf.f_flags);
51 #endif
52         tprints("}");
53 }
54
55 int
56 sys_statfs(struct tcb *tcp)
57 {
58         if (entering(tcp)) {
59                 printpath(tcp, tcp->u_arg[0]);
60                 tprints(", ");
61         } else {
62                 printstatfs(tcp, tcp->u_arg[1]);
63         }
64         return 0;
65 }
66
67 int
68 sys_fstatfs(struct tcb *tcp)
69 {
70         if (entering(tcp)) {
71                 printfd(tcp, tcp->u_arg[0]);
72                 tprints(", ");
73         } else {
74                 printstatfs(tcp, tcp->u_arg[1]);
75         }
76         return 0;
77 }
78
79 #ifdef HAVE_STRUCT_STATFS64
80 static void
81 printstatfs64(struct tcb *tcp, long addr)
82 {
83         struct statfs64 statbuf;
84
85         if (syserror(tcp) || !verbose(tcp)) {
86                 tprintf("%#lx", addr);
87                 return;
88         }
89         if (umove(tcp, addr, &statbuf) < 0) {
90                 tprints("{...}");
91                 return;
92         }
93         tprintf("{f_type=%s, f_bsize=%llu, f_blocks=%llu, f_bfree=%llu, ",
94                 sprintfstype(statbuf.f_type),
95                 (unsigned long long)statbuf.f_bsize,
96                 (unsigned long long)statbuf.f_blocks,
97                 (unsigned long long)statbuf.f_bfree);
98         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
99                 (unsigned long long)statbuf.f_bavail,
100                 (unsigned long long)statbuf.f_files,
101                 (unsigned long long)statbuf.f_ffree,
102                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
103         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
104 #ifdef _STATFS_F_FRSIZE
105         tprintf(", f_frsize=%llu", (unsigned long long)statbuf.f_frsize);
106 #endif
107 #ifdef _STATFS_F_FLAGS
108         tprintf(", f_flags=%llu", (unsigned long long)statbuf.f_flags);
109 #endif
110         tprints("}");
111 }
112
113 struct compat_statfs64 {
114         uint32_t f_type;
115         uint32_t f_bsize;
116         uint64_t f_blocks;
117         uint64_t f_bfree;
118         uint64_t f_bavail;
119         uint64_t f_files;
120         uint64_t f_ffree;
121         fsid_t f_fsid;
122         uint32_t f_namelen;
123         uint32_t f_frsize;
124         uint32_t f_flags;
125         uint32_t f_spare[4];
126 }
127 #if defined AARCH64 || defined X86_64 || defined X32 || defined IA64
128   __attribute__ ((packed, aligned(4)))
129 #endif
130 ;
131
132 static void
133 printcompat_statfs64(struct tcb *tcp, const long addr)
134 {
135         struct compat_statfs64 statbuf;
136
137         if (syserror(tcp) || !verbose(tcp)) {
138                 tprintf("%#lx", addr);
139                 return;
140         }
141         if (umove(tcp, addr, &statbuf) < 0) {
142                 tprints("{...}");
143                 return;
144         }
145         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%llu, f_bfree=%llu, ",
146                 sprintfstype(statbuf.f_type),
147                 (unsigned long)statbuf.f_bsize,
148                 (unsigned long long)statbuf.f_blocks,
149                 (unsigned long long)statbuf.f_bfree);
150         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
151                 (unsigned long long)statbuf.f_bavail,
152                 (unsigned long long)statbuf.f_files,
153                 (unsigned long long)statbuf.f_ffree,
154                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
155         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
156         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
157         tprintf(", f_flags=%lu}", (unsigned long)statbuf.f_frsize);
158 }
159
160 int
161 sys_statfs64(struct tcb *tcp)
162 {
163         if (entering(tcp)) {
164                 printpath(tcp, tcp->u_arg[0]);
165                 tprintf(", %lu, ", tcp->u_arg[1]);
166         } else {
167                 if (tcp->u_arg[1] == sizeof(struct statfs64))
168                         printstatfs64(tcp, tcp->u_arg[2]);
169                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
170                         printcompat_statfs64(tcp, tcp->u_arg[2]);
171                 else
172                         tprints("{???}");
173         }
174         return 0;
175 }
176
177 int
178 sys_fstatfs64(struct tcb *tcp)
179 {
180         if (entering(tcp)) {
181                 printfd(tcp, tcp->u_arg[0]);
182                 tprintf(", %lu, ", tcp->u_arg[1]);
183         } else {
184                 if (tcp->u_arg[1] == sizeof(struct statfs64))
185                         printstatfs64(tcp, tcp->u_arg[2]);
186                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
187                         printcompat_statfs64(tcp, tcp->u_arg[2]);
188                 else
189                         tprints("{???}");
190         }
191         return 0;
192 }
193 #endif /* HAVE_STRUCT_STATFS64 */
194
195 #ifdef ALPHA
196 int
197 osf_statfs(struct tcb *tcp)
198 {
199         if (entering(tcp)) {
200                 printpath(tcp, tcp->u_arg[0]);
201                 tprints(", ");
202         } else {
203                 printstatfs(tcp, tcp->u_arg[1]);
204                 tprintf(", %lu", tcp->u_arg[2]);
205         }
206         return 0;
207 }
208
209 int
210 osf_fstatfs(struct tcb *tcp)
211 {
212         if (entering(tcp)) {
213                 tprintf("%lu, ", tcp->u_arg[0]);
214         } else {
215                 printstatfs(tcp, tcp->u_arg[1]);
216                 tprintf(", %lu", tcp->u_arg[2]);
217         }
218         return 0;
219 }
220 #endif /* ALPHA */