]> granicus.if.org Git - strace/blob - tests/xstatfsx.c
cb524ff3eddaee52b03c787887388f52a2f8219f
[strace] / tests / xstatfsx.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include <linux/types.h>
34 #include <asm/statfs.h>
35
36 #include "xlat.h"
37 #include "xlat/fsmagic.h"
38 #include "xlat/statfs_flags.h"
39
40 #define PRINT_NUM(arg)                                                  \
41         if (sizeof(b->arg) == sizeof(int))                              \
42                 printf(", %s=%u", #arg, (unsigned int) b->arg);         \
43         else if (sizeof(b->arg) == sizeof(long))                                \
44                 printf(", %s=%lu", #arg, (unsigned long) b->arg);       \
45         else                                                            \
46                 printf(", %s=%llu", #arg, (unsigned long long) b->arg)
47
48 static void
49 print_statfs_type(const char *const prefix, const unsigned int magic)
50 {
51         fputs(prefix, stdout);
52         unsigned int i;
53         for (i = 0; i < ARRAY_SIZE(fsmagic); ++i)
54                 if (magic == fsmagic[i].val) {
55                         fputs(fsmagic[i].str, stdout);
56                         return;
57                 }
58         printf("%#x", magic);
59 }
60
61 static void
62 print_statfs(const char *const sample, const char *magic_str)
63 {
64         int fd = open(sample, O_RDONLY);
65         if (fd < 0)
66                 perror_msg_and_skip("open: %s", sample);
67
68         TAIL_ALLOC_OBJECT_CONST_PTR(STRUCT_STATFS, b);
69         long rc = SYSCALL_INVOKE(sample, fd, b, sizeof(*b));
70         if (rc)
71                 perror_msg_and_skip(SYSCALL_NAME);
72
73         PRINT_SYSCALL_HEADER(sample, fd, sizeof(*b));
74         if (magic_str)
75                 printf("{f_type=%s", magic_str);
76         else
77                 print_statfs_type("{f_type=", b->f_type);
78         PRINT_NUM(f_bsize);
79         PRINT_NUM(f_blocks);
80         PRINT_NUM(f_bfree);
81         PRINT_NUM(f_bavail);
82         PRINT_NUM(f_files);
83         PRINT_NUM(f_ffree);
84 #ifdef PRINT_F_FSID
85         printf(", f_fsid={val=[%u, %u]}",
86                (unsigned) b->PRINT_F_FSID[0], (unsigned) b->PRINT_F_FSID[1]);
87 #endif
88         PRINT_NUM(f_namelen);
89 #ifdef PRINT_F_FRSIZE
90         PRINT_NUM(f_frsize);
91 #endif
92 #ifdef PRINT_F_FLAGS
93         if (b->f_flags & ST_VALID) {
94                 printf(", f_flags=");
95                 printflags(statfs_flags, b->f_flags, "ST_???");
96         }
97 #endif
98         printf("}) = 0\n");
99 }
100
101 int
102 main(void)
103 {
104         print_statfs("/proc/self/status", "PROC_SUPER_MAGIC");
105
106         print_statfs(".", NULL);
107
108         long rc = SYSCALL_INVOKE("", -1, 0, sizeof(STRUCT_STATFS));
109         const char *errstr = sprintrc(rc);
110         PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS));
111         printf("NULL) = %s\n", errstr);
112
113 #ifdef CHECK_ODD_SIZE
114         const unsigned long addr = (unsigned long) 0xfacefeeddeadbeefULL;
115         rc = SYSCALL_INVOKE("", -1, addr, sizeof(STRUCT_STATFS) + 1);
116         errstr = sprintrc(rc);
117         PRINT_SYSCALL_HEADER("", -1, sizeof(STRUCT_STATFS) + 1);
118         printf("%#lx) = %s\n", addr, errstr);
119 #endif
120
121         puts("+++ exited with 0 +++");
122         return 0;
123 }