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