lsvfs.c revision 251802
155682Smarkm/*
2233294Sstas * lsvfs - list loaded VFSes
3233294Sstas * Garrett A. Wollman, September 1994
4233294Sstas * This file is in the public domain.
555682Smarkm *
6233294Sstas */
7233294Sstas
8233294Sstas#include <sys/cdefs.h>
955682Smarkm__FBSDID("$FreeBSD: stable/9/usr.bin/lsvfs/lsvfs.c 251802 2013-06-16 07:18:07Z hrs $");
10233294Sstas
11233294Sstas#include <sys/param.h>
1255682Smarkm#include <sys/mount.h>
13233294Sstas#include <sys/sysctl.h>
14233294Sstas
15233294Sstas#include <err.h>
1655682Smarkm#include <stdio.h>
1755682Smarkm#include <stdlib.h>
1855682Smarkm#include <string.h>
1955682Smarkm
2055682Smarkm#define FMT	"%-32.32s 0x%08x %5d  %s\n"
2155682Smarkm#define HDRFMT	"%-32.32s %10s %5.5s  %s\n"
2255682Smarkm#define DASHES	"-------------------------------- "	\
2355682Smarkm		"---------- -----  ---------------\n"
2455682Smarkm
2555682Smarkmstatic struct flaglist {
2655682Smarkm	int		flag;
2755682Smarkm	const char	str[32]; /* must be longer than the longest one. */
2855682Smarkm} fl[] = {
2955682Smarkm	{ .flag = VFCF_STATIC, .str = "static", },
3055682Smarkm	{ .flag = VFCF_NETWORK, .str = "network", },
3155682Smarkm	{ .flag = VFCF_READONLY, .str = "read-only", },
3255682Smarkm	{ .flag = VFCF_SYNTHETIC, .str = "synthetic", },
3355682Smarkm	{ .flag = VFCF_LOOPBACK, .str = "loopback", },
3455682Smarkm	{ .flag = VFCF_UNICODE, .str = "unicode", },
35233294Sstas	{ .flag = VFCF_JAIL, .str = "jail", },
3655682Smarkm	{ .flag = VFCF_DELEGADMIN, .str = "delegated-administration", },
37233294Sstas};
3855682Smarkm
3955682Smarkmstatic const char *fmt_flags(int);
4055682Smarkm
4155682Smarkmint
42main(int argc, char **argv)
43{
44  int cnt, rv = 0, i;
45  struct xvfsconf vfc, *xvfsp;
46  size_t buflen;
47  argc--, argv++;
48
49  printf(HDRFMT, "Filesystem", "Num", "Refs", "Flags");
50  fputs(DASHES, stdout);
51
52  if(argc) {
53    for(; argc; argc--, argv++) {
54      if (getvfsbyname(*argv, &vfc) == 0) {
55        printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
56	    fmt_flags(vfc.vfc_flags));
57      } else {
58	warnx("VFS %s unknown or not loaded", *argv);
59        rv++;
60      }
61    }
62  } else {
63    if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
64      err(1, "sysctl(vfs.conflist)");
65    xvfsp = malloc(buflen);
66    if (xvfsp == NULL)
67      errx(1, "malloc failed");
68    if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0)
69      err(1, "sysctl(vfs.conflist)");
70    cnt = buflen / sizeof(struct xvfsconf);
71
72    for (i = 0; i < cnt; i++) {
73      printf(FMT, xvfsp[i].vfc_name, xvfsp[i].vfc_typenum,
74	    xvfsp[i].vfc_refcount, fmt_flags(xvfsp[i].vfc_flags));
75    }
76    free(xvfsp);
77  }
78
79  return rv;
80}
81
82static const char *
83fmt_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) {
91			strlcat(buf, fl[i].str, sizeof(buf));
92			strlcat(buf, ", ", sizeof(buf));
93		}
94	if (buf[0] != '\0')
95		buf[strlen(buf) - 2] = '\0';
96	return (buf);
97}
98