1 1.24 chs /* $NetBSD: fsdbutil.c,v 1.24 2022/11/17 06:40:38 chs Exp $ */ 2 1.2 thorpej 3 1.4 jtc /*- 4 1.4 jtc * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 1.4 jtc * All rights reserved. 6 1.4 jtc * 7 1.4 jtc * This code is derived from software contributed to The NetBSD Foundation 8 1.4 jtc * by John T. Kohl. 9 1.4 jtc * 10 1.4 jtc * Redistribution and use in source and binary forms, with or without 11 1.4 jtc * modification, are permitted provided that the following conditions 12 1.4 jtc * are met: 13 1.4 jtc * 1. Redistributions of source code must retain the above copyright 14 1.4 jtc * notice, this list of conditions and the following disclaimer. 15 1.4 jtc * 2. Redistributions in binary form must reproduce the above copyright 16 1.4 jtc * notice, this list of conditions and the following disclaimer in the 17 1.4 jtc * documentation and/or other materials provided with the distribution. 18 1.4 jtc * 19 1.4 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.4 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.4 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.6 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.6 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.4 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.4 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.4 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.4 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.4 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.7 lukem #include <sys/cdefs.h> 33 1.1 thorpej #ifndef lint 34 1.24 chs __RCSID("$NetBSD: fsdbutil.c,v 1.24 2022/11/17 06:40:38 chs Exp $"); 35 1.1 thorpej #endif /* not lint */ 36 1.1 thorpej 37 1.1 thorpej #include <sys/types.h> 38 1.1 thorpej #include <sys/stat.h> 39 1.1 thorpej #include <sys/param.h> 40 1.1 thorpej #include <sys/time.h> 41 1.1 thorpej #include <sys/mount.h> 42 1.2 thorpej #include <fcntl.h> 43 1.2 thorpej #include <grp.h> 44 1.2 thorpej #include <pwd.h> 45 1.2 thorpej #include <stdio.h> 46 1.2 thorpej #include <stdlib.h> 47 1.2 thorpej #include <string.h> 48 1.10 kleink #include <time.h> 49 1.2 thorpej #include <unistd.h> 50 1.5 christos #include <err.h> 51 1.2 thorpej 52 1.1 thorpej #include <ufs/ufs/dinode.h> 53 1.1 thorpej #include <ufs/ffs/fs.h> 54 1.1 thorpej 55 1.1 thorpej #include "fsdb.h" 56 1.1 thorpej #include "fsck.h" 57 1.1 thorpej 58 1.7 lukem char ** 59 1.15 xtraeme crack(char *line, int *argc) 60 1.1 thorpej { 61 1.7 lukem static char *argv[8]; 62 1.7 lukem int i; 63 1.7 lukem char *p, *val; 64 1.7 lukem for (p = line, i = 0; p != NULL && i < 8; i++) { 65 1.7 lukem while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0') 66 1.7 lukem /**/ ; 67 1.7 lukem if (val) 68 1.7 lukem argv[i] = val; 69 1.7 lukem else 70 1.7 lukem break; 71 1.7 lukem } 72 1.7 lukem *argc = i; 73 1.7 lukem return argv; 74 1.1 thorpej } 75 1.1 thorpej 76 1.1 thorpej int 77 1.15 xtraeme argcount(struct cmdtable *cmdp, int argc, char *argv[]) 78 1.1 thorpej { 79 1.7 lukem if (cmdp->minargc == cmdp->maxargc) 80 1.7 lukem warnx("command `%s' takes %u arguments", cmdp->cmd, 81 1.7 lukem cmdp->minargc - 1); 82 1.7 lukem else 83 1.7 lukem warnx("command `%s' takes from %u to %u arguments", 84 1.7 lukem cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1); 85 1.7 lukem 86 1.7 lukem warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt); 87 1.7 lukem return 1; 88 1.1 thorpej } 89 1.1 thorpej 90 1.1 thorpej void 91 1.15 xtraeme printstat(const char *cp, ino_t inum, union dinode *dp) 92 1.1 thorpej { 93 1.7 lukem struct group *grp; 94 1.7 lukem struct passwd *pw; 95 1.7 lukem time_t t; 96 1.7 lukem char *p; 97 1.13 fvdl uint64_t size, blocks; 98 1.24 chs uint32_t extsize; 99 1.13 fvdl uint16_t mode; 100 1.13 fvdl uint32_t rdev; 101 1.14 dbj uint32_t uid, gid; 102 1.13 fvdl 103 1.13 fvdl size = iswap64(DIP(dp, size)); 104 1.24 chs extsize = is_ufs2 ? iswap32(dp->dp2.di_extsize) : 0; 105 1.13 fvdl blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks)); 106 1.13 fvdl mode = iswap16(DIP(dp, mode)); 107 1.13 fvdl rdev = iswap32(DIP(dp, rdev)); 108 1.7 lukem 109 1.7 lukem printf("%s: ", cp); 110 1.13 fvdl switch (mode & IFMT) { 111 1.7 lukem case IFDIR: 112 1.7 lukem puts("directory"); 113 1.7 lukem break; 114 1.7 lukem case IFREG: 115 1.7 lukem puts("regular file"); 116 1.7 lukem break; 117 1.7 lukem case IFBLK: 118 1.21 mlelstv printf("block special (%llu,%llu)", 119 1.21 mlelstv (unsigned long long)major(rdev), 120 1.21 mlelstv (unsigned long long)minor(rdev)); 121 1.7 lukem break; 122 1.7 lukem case IFCHR: 123 1.20 christos printf("character special (%llu,%llu)", 124 1.20 christos (unsigned long long)major(rdev), 125 1.20 christos (unsigned long long)minor(rdev)); 126 1.7 lukem break; 127 1.7 lukem case IFLNK: 128 1.7 lukem fputs("symlink", stdout); 129 1.22 lukem if (size > 0 && size < (uint64_t)sblock->fs_maxsymlinklen && 130 1.13 fvdl DIP(dp, blocks) == 0) { 131 1.13 fvdl p = is_ufs2 ? (char *)dp->dp2.di_db : 132 1.13 fvdl (char *)dp->dp1.di_db; 133 1.13 fvdl printf(" to `%.*s'\n", (int)size, p); 134 1.13 fvdl } else 135 1.7 lukem putchar('\n'); 136 1.7 lukem break; 137 1.7 lukem case IFSOCK: 138 1.7 lukem puts("socket"); 139 1.7 lukem break; 140 1.7 lukem case IFIFO: 141 1.7 lukem puts("fifo"); 142 1.7 lukem break; 143 1.7 lukem } 144 1.24 chs printf("I=%llu MODE=%o SIZE=%llu EXTSIZE=%u", (unsigned long long)inum, 145 1.24 chs mode, (unsigned long long)size, extsize); 146 1.13 fvdl t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime); 147 1.7 lukem p = ctime(&t); 148 1.23 christos printf("\n\t MTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 149 1.13 fvdl iswap32(DIP(dp, mtimensec))); 150 1.13 fvdl t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime); 151 1.7 lukem p = ctime(&t); 152 1.23 christos printf("\n\t CTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 153 1.13 fvdl iswap32(DIP(dp, ctimensec))); 154 1.13 fvdl t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime); 155 1.7 lukem p = ctime(&t); 156 1.23 christos printf("\n\t ATIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 157 1.13 fvdl iswap32(DIP(dp,atimensec))); 158 1.23 christos if (is_ufs2) { 159 1.23 christos t = iswap64(dp->dp2.di_birthtime); 160 1.23 christos p = ctime(&t); 161 1.23 christos printf("\n\tBIRTHTIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20], 162 1.23 christos iswap32(dp->dp2.di_birthnsec)); 163 1.23 christos } else { 164 1.23 christos printf("\n"); 165 1.23 christos } 166 1.7 lukem 167 1.14 dbj if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 168 1.14 dbj uid = iswap16(dp->dp1.di_ouid); 169 1.14 dbj else 170 1.14 dbj uid = iswap32(DIP(dp, uid)); 171 1.14 dbj if ((pw = getpwuid(uid)) != NULL) 172 1.7 lukem printf("OWNER=%s ", pw->pw_name); 173 1.7 lukem else 174 1.14 dbj printf("OWNUID=%u ", uid); 175 1.14 dbj if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT) 176 1.14 dbj gid = iswap16(dp->dp1.di_ogid); 177 1.14 dbj else 178 1.14 dbj gid = iswap32(DIP(dp, gid)); 179 1.14 dbj if ((grp = getgrgid(gid)) != NULL) 180 1.7 lukem printf("GRP=%s ", grp->gr_name); 181 1.1 thorpej else 182 1.14 dbj printf("GID=%u ", gid); 183 1.1 thorpej 184 1.19 simonb printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n", 185 1.13 fvdl iswap16(DIP(dp, nlink)), 186 1.13 fvdl iswap32(DIP(dp, flags)), (unsigned long long)blocks, 187 1.13 fvdl iswap32(DIP(dp, gen))); 188 1.1 thorpej } 189 1.1 thorpej 190 1.1 thorpej int 191 1.15 xtraeme checkactive(void) 192 1.1 thorpej { 193 1.7 lukem if (!curinode) { 194 1.12 grant warnx("no current inode"); 195 1.7 lukem return 0; 196 1.7 lukem } 197 1.7 lukem return 1; 198 1.1 thorpej } 199 1.1 thorpej 200 1.1 thorpej int 201 1.15 xtraeme checkactivedir(void) 202 1.1 thorpej { 203 1.7 lukem if (!curinode) { 204 1.12 grant warnx("no current inode"); 205 1.7 lukem return 0; 206 1.7 lukem } 207 1.13 fvdl if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) { 208 1.16 christos warnx("inode %llu not a directory", 209 1.16 christos (unsigned long long)curinum); 210 1.7 lukem return 0; 211 1.7 lukem } 212 1.7 lukem return 1; 213 1.1 thorpej } 214 1.1 thorpej 215 1.1 thorpej int 216 1.15 xtraeme printactive(void) 217 1.1 thorpej { 218 1.13 fvdl uint16_t mode; 219 1.13 fvdl 220 1.7 lukem if (!checkactive()) 221 1.7 lukem return 1; 222 1.13 fvdl mode = iswap16(DIP(curinode, mode)); 223 1.13 fvdl switch (mode & IFMT) { 224 1.7 lukem case IFDIR: 225 1.7 lukem case IFREG: 226 1.7 lukem case IFBLK: 227 1.7 lukem case IFCHR: 228 1.7 lukem case IFLNK: 229 1.7 lukem case IFSOCK: 230 1.7 lukem case IFIFO: 231 1.7 lukem printstat("current inode", curinum, curinode); 232 1.7 lukem break; 233 1.7 lukem case 0: 234 1.16 christos printf("current inode %llu: unallocated inode\n", 235 1.16 christos (unsigned long long)curinum); 236 1.7 lukem break; 237 1.7 lukem default: 238 1.16 christos printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n", 239 1.16 christos (unsigned long long)curinum, mode & IFMT, mode); 240 1.7 lukem break; 241 1.7 lukem } 242 1.7 lukem return 0; 243 1.1 thorpej } 244