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