xref: /freebsd/usr.bin/lsvfs/lsvfs.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1 /*
2  * lsvfs - list loaded VFSes
3  * Garrett A. Wollman, September 1994
4  * This file is in the public domain.
5  *
6  */
7 
8 #include <sys/cdefs.h>
9 __FBSDID("$FreeBSD$");
10 
11 #include <sys/param.h>
12 #include <sys/mount.h>
13 #include <sys/sysctl.h>
14 
15 #include <err.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #define FMT	"%-32.32s 0x%08x %5d  %s\n"
21 #define HDRFMT	"%-32.32s %10s %5.5s  %s\n"
22 #define DASHES	"-------------------------------- "	\
23 		"---------- -----  ---------------\n"
24 
25 static struct flaglist {
26 	int		flag;
27 	const char	str[32]; /* must be longer than the longest one. */
28 } fl[] = {
29 	{ .flag = VFCF_STATIC, .str = "static", },
30 	{ .flag = VFCF_NETWORK, .str = "network", },
31 	{ .flag = VFCF_READONLY, .str = "read-only", },
32 	{ .flag = VFCF_SYNTHETIC, .str = "synthetic", },
33 	{ .flag = VFCF_LOOPBACK, .str = "loopback", },
34 	{ .flag = VFCF_UNICODE, .str = "unicode", },
35 	{ .flag = VFCF_JAIL, .str = "jail", },
36 	{ .flag = VFCF_DELEGADMIN, .str = "delegated-administration", },
37 };
38 
39 static const char *fmt_flags(int);
40 
41 int
42 main(int argc, char **argv)
43 {
44 	struct xvfsconf vfc, *xvfsp;
45 	size_t buflen;
46 	int cnt, i, rv = 0;
47 
48 	argc--, argv++;
49 
50 	printf(HDRFMT, "Filesystem", "Num", "Refs", "Flags");
51 	fputs(DASHES, stdout);
52 
53 	if (argc > 0) {
54 		for (; argc > 0; argc--, argv++) {
55 			if (getvfsbyname(*argv, &vfc) == 0) {
56 				printf(FMT, vfc.vfc_name, vfc.vfc_typenum,
57 				    vfc.vfc_refcount, fmt_flags(vfc.vfc_flags));
58 			} else {
59 				warnx("VFS %s unknown or not loaded", *argv);
60 				rv++;
61 			}
62 		}
63 	} else {
64 		if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
65 			err(1, "sysctl(vfs.conflist)");
66 		xvfsp = malloc(buflen);
67 		if (xvfsp == NULL)
68 			errx(1, "malloc failed");
69 		if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0)
70 			err(1, "sysctl(vfs.conflist)");
71 		cnt = buflen / sizeof(struct xvfsconf);
72 
73 		for (i = 0; i < cnt; i++) {
74 			printf(FMT, xvfsp[i].vfc_name, xvfsp[i].vfc_typenum,
75 			    xvfsp[i].vfc_refcount,
76 			    fmt_flags(xvfsp[i].vfc_flags));
77 		}
78 		free(xvfsp);
79 	}
80 
81 	return (rv);
82 }
83 
84 static const char *
85 fmt_flags(int flags)
86 {
87 	static char buf[sizeof(struct flaglist) * sizeof(fl)];
88 	int i;
89 
90 	buf[0] = '\0';
91 	for (i = 0; i < (int)nitems(fl); i++) {
92 		if ((flags & fl[i].flag) != 0) {
93 			strlcat(buf, fl[i].str, sizeof(buf));
94 			strlcat(buf, ", ", sizeof(buf));
95 		}
96 	}
97 
98 	/* Zap the trailing comma + space. */
99 	if (buf[0] != '\0')
100 		buf[strlen(buf) - 2] = '\0';
101 	return (buf);
102 }
103