git: 3f8b52c91087 - stable/13 - mount: add libxo(3) support

From: Piotr Pawel Stefaniak <pstef_at_FreeBSD.org>
Date: Wed, 23 Mar 2022 15:51:40 UTC
The branch stable/13 has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=3f8b52c9108717129a0dfec218748f39a1b2db98

commit 3f8b52c9108717129a0dfec218748f39a1b2db98
Author:     Cameron Katri <me@cameronkatri.com>
AuthorDate: 2021-09-23 20:51:30 +0000
Commit:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
CommitDate: 2022-03-23 15:47:33 +0000

    mount: add libxo(3) support
    
    Adds --libxo to mount(8).
    
    (cherry picked from commit e725ee7eb672a8501600552283d3d08a2f7b6847)
---
 sbin/mount/Makefile |   2 +-
 sbin/mount/mount.8  |  14 ++++-
 sbin/mount/mount.c  | 153 ++++++++++++++++++++++++++++++++++------------------
 3 files changed, 114 insertions(+), 55 deletions(-)

diff --git a/sbin/mount/Makefile b/sbin/mount/Makefile
index 68c7ee9819d8..34ba498a2a3f 100644
--- a/sbin/mount/Makefile
+++ b/sbin/mount/Makefile
@@ -7,6 +7,6 @@ SRCS=	mount.c mount_fs.c getmntopts.c vfslist.c
 MAN=	mount.8
 # We do NOT install the getmntopts.3 man page.
 
-LIBADD=	util
+LIBADD=	util xo
 
 .include <bsd.prog.mk>
diff --git a/sbin/mount/mount.8 b/sbin/mount/mount.8
index 3aee1bb86151..59a0f6bb0329 100644
--- a/sbin/mount/mount.8
+++ b/sbin/mount/mount.8
@@ -28,7 +28,7 @@
 .\"     @(#)mount.8	8.8 (Berkeley) 6/16/94
 .\" $FreeBSD$
 .\"
-.Dd August 28, 2019
+.Dd May 18, 2021
 .Dt MOUNT 8
 .Os
 .Sh NAME
@@ -36,14 +36,17 @@
 .Nd mount file systems
 .Sh SYNOPSIS
 .Nm
+.Op Fl -libxo
 .Op Fl adflpruvw
 .Op Fl F Ar fstab
 .Op Fl o Ar options
 .Op Fl t Oo Cm no Oc Ns Cm Ar type Ns Op Cm , Ns Ar type ...
 .Nm
+.Op Fl -libxo
 .Op Fl dfpruvw
 .Ar special | node
 .Nm
+.Op Fl -libxo
 .Op Fl dfpruvw
 .Op Fl o Ar options
 .Op Fl t Oo Cm no Oc Ns Cm Ar type Ns Op Cm , Ns Ar type ...
@@ -72,6 +75,13 @@ this list is printed.
 .Pp
 The options are as follows:
 .Bl -tag -width indent
+.It Fl -libxo
+Generate output via
+.Xr libxo 3
+in a selection of different human and machine readable formats.
+See
+.Xr xo_parse_args 3
+for details on command line arguments.
 .It Fl a
 All the file systems described in
 .Xr fstab 5
@@ -552,6 +562,8 @@ support for a particular file system might be provided either on a static
 .Xr setfacl 1 ,
 .Xr nmount 2 ,
 .Xr acl 3 ,
+.Xr libxo 3 ,
+.Xr xo_parse_args 3 ,
 .Xr mac 4 ,
 .Xr cd9660 5 ,
 .Xr devfs 5 ,
diff --git a/sbin/mount/mount.c b/sbin/mount/mount.c
index fad999c97dc9..02242791a7a2 100644
--- a/sbin/mount/mount.c
+++ b/sbin/mount/mount.c
@@ -60,11 +60,18 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 #include <libutil.h>
+#include <libxo/xo.h>
 
 #include "extern.h"
 #include "mntopts.h"
 #include "pathnames.h"
 
+#define EXIT(a) {			\
+	xo_close_container("mount");	\
+	xo_finish();			\
+	exit(a);			\
+	}
+
 /* `meta' options */
 #define MOUNT_META_OPTION_FSTAB		"fstab"
 #define MOUNT_META_OPTION_CURRENT	"current"
@@ -146,21 +153,21 @@ exec_mountprog(const char *name, const char *execname, char *const argv[])
 
 	switch (pid = fork()) {
 	case -1:				/* Error. */
-		warn("fork");
-		exit (1);
+		xo_warn("fork");
+		EXIT(1);
 	case 0:					/* Child. */
 		/* Go find an executable. */
 		execvP(execname, _PATH_SYSPATH, argv);
 		if (errno == ENOENT) {
-			warn("exec %s not found", execname);
+			xo_warn("exec %s not found", execname);
 			if (execname[0] != '/') {
-				warnx("in path: %s", _PATH_SYSPATH);
+				xo_warnx("in path: %s", _PATH_SYSPATH);
 			}
 		}
-		exit(1);
+		EXIT(1);
 	default:				/* Parent. */
 		if (waitpid(pid, &status, 0) < 0) {
-			warn("waitpid");
+			xo_warn("waitpid");
 			return (1);
 		}
 
@@ -168,7 +175,7 @@ exec_mountprog(const char *name, const char *execname, char *const argv[])
 			if (WEXITSTATUS(status) != 0)
 				return (WEXITSTATUS(status));
 		} else if (WIFSIGNALED(status)) {
-			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
+			xo_warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
 			return (1);
 		}
 		break;
@@ -185,7 +192,7 @@ specified_ro(const char *arg)
 
 	optbuf = strdup(arg);
 	if (optbuf == NULL)
-		 err(1, NULL);
+		 xo_err(1, NULL);
 
 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
 		if (strcmp(opt, "ro") == 0) {
@@ -220,13 +227,13 @@ restart_mountd(void)
 	 * happened due to the bugs in pidfile(3).
 	 */
 	if (mountdpid <= 0) {
-		warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
+		xo_warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
 		return;
 	}
 
 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
 	if (kill(mountdpid, SIGHUP) == -1)
-		err(1, "signal mountd");
+		xo_err(1, "signal mountd");
 }
 
 int
@@ -244,6 +251,12 @@ main(int argc, char *argv[])
 	options = NULL;
 	vfslist = NULL;
 	vfstype = "ufs";
+
+	argc = xo_parse_args(argc, argv);
+	if (argc < 0)
+		exit(1);
+	xo_open_container("mount");
+
 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
 		switch (ch) {
 		case 'a':
@@ -285,7 +298,7 @@ main(int argc, char *argv[])
 			break;
 		case 't':
 			if (vfslist != NULL)
-				errx(1, "only one -t option may be specified");
+				xo_errx(1, "only one -t option may be specified");
 			vfslist = makevfslist(optarg);
 			vfstype = optarg;
 			break;
@@ -318,7 +331,7 @@ main(int argc, char *argv[])
 	case 0:
 		if ((mntsize = getmntinfo(&mntbuf,
 		     verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
-			err(1, "getmntinfo");
+			xo_err(1, "getmntinfo");
 		if (all) {
 			while ((fs = getfsent()) != NULL) {
 				if (BADTYPE(fs->fs_type))
@@ -347,12 +360,17 @@ main(int argc, char *argv[])
 					rval = 1;
 			}
 		} else if (fstab_style) {
+			xo_open_list("fstab");
 			for (i = 0; i < mntsize; i++) {
 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
 					continue;
+				xo_open_instance("fstab");
 				putfsent(&mntbuf[i]);
+				xo_close_instance("fstab");
 			}
+			xo_close_list("fstab");
 		} else {
+			xo_open_list("mounted");
 			for (i = 0; i < mntsize; i++) {
 				if (checkvfsname(mntbuf[i].f_fstypename,
 				    vfslist))
@@ -360,10 +378,13 @@ main(int argc, char *argv[])
 				if (!verbose &&
 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
 					continue;
+				xo_open_instance("mounted");
 				prmount(&mntbuf[i]);
+				xo_close_instance("mounted");
 			}
+			xo_close_list("mounted");
 		}
-		exit(rval);
+		EXIT(rval);
 	case 1:
 		if (vfslist != NULL)
 			usage();
@@ -373,7 +394,7 @@ main(int argc, char *argv[])
 			mntfromname = NULL;
 			have_fstab = 0;
 			if ((mntbuf = getmntpt(*argv)) == NULL)
-				errx(1, "not currently mounted %s", *argv);
+				xo_errx(1, "not currently mounted %s", *argv);
 			/*
 			 * Only get the mntflags from fstab if both mntpoint
 			 * and mntspec are identical. Also handle the special
@@ -411,10 +432,10 @@ main(int argc, char *argv[])
 		}
 		if ((fs = getfsfile(*argv)) == NULL &&
 		    (fs = getfsspec(*argv)) == NULL)
-			errx(1, "%s: unknown special file or file system",
+			xo_errx(1, "%s: unknown special file or file system",
 			    *argv);
 		if (BADTYPE(fs->fs_type))
-			errx(1, "%s has unknown file system type",
+			xo_errx(1, "%s has unknown file system type",
 			    *argv);
 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
 		    init_flags, options, fs->fs_mntops);
@@ -460,7 +481,7 @@ main(int argc, char *argv[])
 	if (rval == 0 && getuid() == 0)
 		restart_mountd();
 
-	exit(rval);
+	EXIT(rval);
 }
 
 int
@@ -537,7 +558,7 @@ append_arg(struct cpa *sa, char *arg)
 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
 		if (sa->a == NULL)
-			errx(1, "realloc failed");
+			xo_errx(1, "realloc failed");
 	}
 	sa->a[++sa->c] = arg;
 }
@@ -553,7 +574,7 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
 
 	/* resolve the mountpoint with realpath(3) */
 	if (checkpath(name, mntpath) != 0) {
-		warn("%s", mntpath);
+		xo_warn("%s", mntpath);
 		return (1);
 	}
 	name = mntpath;
@@ -596,12 +617,12 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
 
 	if (debug) {
 		if (use_mountprog(vfstype))
-			printf("exec: %s", execname);
+			xo_emit("{Lwc:exec}{:execname/%s}", execname);
 		else
-			printf("mount -t %s", vfstype);
+			xo_emit("{:execname/mount}{P: }{l:opts/-t}{P: }{l:opts/%s}", vfstype);
 		for (i = 1; i < mnt_argv.c; i++)
-			(void)printf(" %s", mnt_argv.a[i]);
-		(void)printf("\n");
+			xo_emit("{P: }{l:opts}", mnt_argv.a[i]);
+		xo_emit("\n");
 		free(optbuf);
 		free(mountprog);
 		mountprog = NULL;
@@ -620,13 +641,22 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
 
 	if (verbose) {
 		if (statfs(name, &sf) < 0) {
-			warn("statfs %s", name);
+			xo_warn("statfs %s", name);
 			return (1);
 		}
-		if (fstab_style)
+		if (fstab_style) {
+			xo_open_list("fstab");
+			xo_open_instance("fstab");
 			putfsent(&sf);
-		else
+			xo_close_instance("fstab");
+			xo_close_list("fstab");
+		} else {
+			xo_open_list("mounted");
+			xo_open_instance("mounted");
 			prmount(&sf);
+			xo_close_instance("mounted");
+			xo_close_list("mounted");
+		}
 	}
 
 	return (ret);
@@ -639,14 +669,15 @@ prmount(struct statfs *sfp)
 	unsigned int i;
 	struct mntoptnames *o;
 	struct passwd *pw;
+	char *fsidbuf;
 
-	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
-	    sfp->f_fstypename);
+	xo_emit("{:special}{L: on }{:node}{L: (}{:fstype}", sfp->f_mntfromname,
+	    sfp->f_mntonname, sfp->f_fstypename);
 
 	flags = sfp->f_flags & MNT_VISFLAGMASK;
 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
 		if (flags & o->o_opt) {
-			(void)printf(", %s", o->o_name);
+			xo_emit("{D:, }{l:opts}", o->o_name);
 			flags &= ~o->o_opt;
 		}
 	/*
@@ -654,28 +685,40 @@ prmount(struct statfs *sfp)
 	 * or privileged non-root user.
 	 */
 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
-		(void)printf(", mounted by ");
+		xo_emit("{D:, }{L:mounted by }");
 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
-			(void)printf("%s", pw->pw_name);
+			xo_emit("{:mounter}", pw->pw_name);
 		else
-			(void)printf("%d", sfp->f_owner);
+			xo_emit("{:mounter}", sfp->f_owner);
 	}
 	if (verbose) {
-		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
-			(void)printf(", writes: sync %ju async %ju",
+		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) {
+			xo_open_container("writes");
+			xo_emit("{D:, }{Lwc:writes}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
 			    (uintmax_t)sfp->f_syncwrites,
 			    (uintmax_t)sfp->f_asyncwrites);
-		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
-			(void)printf(", reads: sync %ju async %ju",
+			xo_close_container("writes");
+		}
+		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) {
+			xo_open_container("reads");
+			xo_emit("{D:, }{Lwc:reads}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
 			    (uintmax_t)sfp->f_syncreads,
 			    (uintmax_t)sfp->f_asyncreads);
+			xo_close_container("reads");
+		}
 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
-			(void)printf(", fsid ");
+			fsidbuf = malloc(sizeof(sfp->f_fsid) * 2 + 1);
+			if (fsidbuf == NULL)
+				xo_errx(1, "malloc failed");
 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
-				(void)printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
+				sprintf(&fsidbuf[i * 2], "%02x",
+				    ((u_char *)&sfp->f_fsid)[i]);
+			fsidbuf[i * 2] = '\0';
+			xo_emit("{D:, }{Lw:fsid}{:fsid}", fsidbuf);
+			free(fsidbuf);
 		}
 	}
-	(void)printf(")\n");
+	xo_emit("{D:)}\n");
 }
 
 struct statfs *
@@ -703,7 +746,7 @@ catopt(char *s0, const char *s1)
 
 	if (s0 && *s0) {
 		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
-			errx(1, "asprintf failed");
+			xo_errx(1, "asprintf failed");
 	} else
 		cp = strdup(s1);
 
@@ -758,7 +801,7 @@ mangle(char *options, struct cpa *a)
 				}
 
 				if (mountprog == NULL) {
-					errx(1, "Need value for -o mountprog");
+					xo_errx(1, "Need value for -o mountprog");
 				}
 				continue;
 			} else if (strcmp(p, "userquota") == 0) {
@@ -824,7 +867,7 @@ update_options(char *opts, char *fstab, int curflags)
 	newopt = NULL;
 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
-			errx(1, "malloc failed");
+			xo_errx(1, "malloc failed");
 
 		strcpy(tmpopt, "no");
 		strcat(tmpopt, o);
@@ -867,11 +910,11 @@ void
 usage(void)
 {
 
-	(void)fprintf(stderr, "%s\n%s\n%s\n",
+	xo_error("%s\n%s\n%s\n",
 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
 "       mount [-dfpruvw] special | node",
 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
-	exit(1);
+	EXIT(1);
 }
 
 void
@@ -899,32 +942,36 @@ putfsent(struct statfs *ent)
 	}
 
 	l = strlen(ent->f_mntfromname);
-	printf("%s%s%s%s", ent->f_mntfromname,
+	xo_emit("{:device}{P:/%s}{P:/%s}{P:/%s}",
+	    ent->f_mntfromname,
 	    l < 8 ? "\t" : "",
 	    l < 16 ? "\t" : "",
 	    l < 24 ? "\t" : " ");
 	l = strlen(ent->f_mntonname);
-	printf("%s%s%s%s", ent->f_mntonname,
+	xo_emit("{:mntpoint}{P:/%s}{P:/%s}{P:/%s}",
+	    ent->f_mntonname,
 	    l < 8 ? "\t" : "",
 	    l < 16 ? "\t" : "",
 	    l < 24 ? "\t" : " ");
-	printf("%s\t", ent->f_fstypename);
+	xo_emit("{:fstype}{P:\t}", ent->f_fstypename);
 	l = strlen(opts);
-	printf("%s%s", opts,
+	xo_emit("{:opts}{P:/%s}", opts,
 	    l < 8 ? "\t" : " ");
 	free(opts);
 
 	if ((fst = getfsspec(ent->f_mntfromname)))
-		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
+		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
+		    fst->fs_freq, fst->fs_passno);
 	else if ((fst = getfsfile(ent->f_mntonname)))
-		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
+		xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
+		    fst->fs_freq, fst->fs_passno);
 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
 		if (strcmp(ent->f_mntonname, "/") == 0)
-			printf("\t1 1\n");
+			xo_emit("{P:\t}{n:dump/1}{P: }{n:pass/1}\n");
 		else
-			printf("\t2 2\n");
+			xo_emit("{P:\t}{n:dump/2}{P: }{n:pass/2}\n");
 	} else
-		printf("\t0 0\n");
+		xo_emit("{P:\t}{n:dump/0}{P: }{n:pass/0}\n");
 }