]> granicus.if.org Git - strace/blob - tests/xstatfsx.c
Update copyright headers
[strace] / tests / xstatfsx.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2018 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         unsigned int i;
38         for (i = 0; i < ARRAY_SIZE(fsmagic); ++i)
39                 if (magic == fsmagic[i].val) {
40                         fputs(fsmagic[i].str, stdout);
41                         return;
42                 }
43         printf("%#x", magic);
44 }
45
46 static void
47 print_statfs(const char *const sample, const char *magic_str)
48 {
49         int fd = open(sample, O_RDONLY);
50         if (fd < 0)
51                 perror_msg_and_skip("open: %s", sample);
52
53         TAIL_ALLOC_OBJECT_CONST_PTR(STRUCT_STATFS, b);
54         long rc = SYSCALL_INVOKE(sample, fd, b, sizeof(*b));
55         if (rc)
56                 perror_msg_and_skip(SYSCALL_NAME);
57
58         PRINT_SYSCALL_HEADER(sample, fd, sizeof(*b));
59         if (magic_str)
60                 printf("{f_type=%s", magic_str);
61         else
62                 print_statfs_type("{f_type=", b->f_type);
63         PRINT_NUM(f_bsize);
64         PRINT_NUM(f_blocks);
65         PRINT_NUM(f_bfree);
66         PRINT_NUM(f_bavail);
67         PRINT_NUM(f_files);
68         PRINT_NUM(f_ffree);
69 #ifdef PRINT_F_FSID
70         printf(", f_fsid={val=[%u, %u]}",
71                (unsigned) b->PRINT_F_FSID[0], (unsigned) 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 }