xref: /freebsd/usr.bin/lsvfs/lsvfs.c (revision cb2887746f8b9dd4ad6b1e757cdc053a08b25a2e)
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 cnt, buflen;
43 	int 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(EXIT_FAILURE, "sysctl(vfs.conflist)");
63 		if ((xvfsp = malloc(buflen)) == NULL)
64 			errx(EXIT_FAILURE, "malloc failed");
65 		if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0)
66 			err(EXIT_FAILURE, "sysctl(vfs.conflist)");
67 		cnt = buflen / sizeof(struct xvfsconf);
68 
69 		for (size_t i = 0; i < cnt; i++) {
70 			printf(FMT, xvfsp[i].vfc_name, xvfsp[i].vfc_typenum,
71 			    xvfsp[i].vfc_refcount,
72 			    fmt_flags(xvfsp[i].vfc_flags));
73 		}
74 		free(xvfsp);
75 	}
76 
77 	return (rv);
78 }
79 
80 static const char *
81 fmt_flags(int flags)
82 {
83 	static char buf[sizeof(struct flaglist) * sizeof(fl)];
84 
85 	buf[0] = '\0';
86 	for (size_t i = 0; i < (int)nitems(fl); i++) {
87 		if ((flags & fl[i].flag) != 0) {
88 			strlcat(buf, fl[i].str, sizeof(buf));
89 			strlcat(buf, ", ", sizeof(buf));
90 		}
91 	}
92 
93 	/* Zap the trailing comma + space. */
94 	if (buf[0] != '\0')
95 		buf[strlen(buf) - 2] = '\0';
96 	return (buf);
97 }
98