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