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