1 From 9d8571e84bf2c006b5053a776358c148b641a48f Mon Sep 17 00:00:00 2001
2 From: Cameron Katri <me@cameronkatri.com>
3 Date: Thu, 20 May 2021 15:04:10 -0400
4 Subject: [PATCH] mount(8): Add libxo(3) support
7 sbin/mount/Makefile | 2 +-
8 sbin/mount/mount.8 | 14 ++++-
9 sbin/mount/mount.c | 150 ++++++++++++++++++++++++++++----------------
10 3 files changed, 111 insertions(+), 55 deletions(-)
12 diff --git a/sbin/mount/Makefile b/sbin/mount/Makefile
13 index 68c7ee9819d..34ba498a2a3 100644
14 --- a/sbin/mount/Makefile
15 +++ b/sbin/mount/Makefile
16 @@ -7,6 +7,6 @@ SRCS= mount.c mount_fs.c getmntopts.c vfslist.c
18 # We do NOT install the getmntopts.3 man page.
23 .include <bsd.prog.mk>
24 diff --git a/sbin/mount/mount.8 b/sbin/mount/mount.8
25 index 3aee1bb8615..59a0f6bb032 100644
26 --- a/sbin/mount/mount.8
27 +++ b/sbin/mount/mount.8
29 .\" @(#)mount.8 8.8 (Berkeley) 6/16/94
38 .Nd mount file systems
45 .Op Fl t Oo Cm no Oc Ns Cm Ar type Ns Op Cm , Ns Ar type ...
54 .Op Fl t Oo Cm no Oc Ns Cm Ar type Ns Op Cm , Ns Ar type ...
55 @@ -72,6 +75,13 @@ this list is printed.
57 The options are as follows:
58 .Bl -tag -width indent
62 +in a selection of different human and machine readable formats.
65 +for details on command line arguments.
67 All the file systems described in
69 @@ -552,6 +562,8 @@ support for a particular file system might be provided either on a static
74 +.Xr xo_parse_args 3 ,
78 diff --git a/sbin/mount/mount.c b/sbin/mount/mount.c
79 index fad999c97dc..de30dd39afe 100644
80 --- a/sbin/mount/mount.c
81 +++ b/sbin/mount/mount.c
82 @@ -60,11 +60,18 @@ __FBSDID("$FreeBSD$");
86 +#include <libxo/xo.h>
90 #include "pathnames.h"
93 + xo_close_container("mount"); \
99 #define MOUNT_META_OPTION_FSTAB "fstab"
100 #define MOUNT_META_OPTION_CURRENT "current"
101 @@ -146,21 +153,21 @@ exec_mountprog(const char *name, const char *execname, char *const argv[])
103 switch (pid = fork()) {
104 case -1: /* Error. */
110 /* Go find an executable. */
111 execvP(execname, _PATH_SYSPATH, argv);
112 if (errno == ENOENT) {
113 - warn("exec %s not found", execname);
114 + xo_warn("exec %s not found", execname);
115 if (execname[0] != '/') {
116 - warnx("in path: %s", _PATH_SYSPATH);
117 + xo_warnx("in path: %s", _PATH_SYSPATH);
122 default: /* Parent. */
123 if (waitpid(pid, &status, 0) < 0) {
125 + xo_warn("waitpid");
129 @@ -168,7 +175,7 @@ exec_mountprog(const char *name, const char *execname, char *const argv[])
130 if (WEXITSTATUS(status) != 0)
131 return (WEXITSTATUS(status));
132 } else if (WIFSIGNALED(status)) {
133 - warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
134 + xo_warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
138 @@ -185,7 +192,7 @@ specified_ro(const char *arg)
140 optbuf = strdup(arg);
145 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
146 if (strcmp(opt, "ro") == 0) {
147 @@ -220,13 +227,13 @@ restart_mountd(void)
148 * happened due to the bugs in pidfile(3).
150 if (mountdpid <= 0) {
151 - warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
152 + xo_warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
156 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
157 if (kill(mountdpid, SIGHUP) == -1)
158 - err(1, "signal mountd");
159 + xo_err(1, "signal mountd");
163 @@ -244,6 +251,12 @@ main(int argc, char *argv[])
168 + argc = xo_parse_args(argc, argv);
171 + xo_open_container("mount");
173 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
176 @@ -285,7 +298,7 @@ main(int argc, char *argv[])
180 - errx(1, "only one -t option may be specified");
181 + xo_errx(1, "only one -t option may be specified");
182 vfslist = makevfslist(optarg);
185 @@ -318,7 +331,7 @@ main(int argc, char *argv[])
187 if ((mntsize = getmntinfo(&mntbuf,
188 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
189 - err(1, "getmntinfo");
190 + xo_err(1, "getmntinfo");
192 while ((fs = getfsent()) != NULL) {
193 if (BADTYPE(fs->fs_type))
194 @@ -347,12 +360,17 @@ main(int argc, char *argv[])
197 } else if (fstab_style) {
198 + xo_open_list("fstab");
199 for (i = 0; i < mntsize; i++) {
200 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
202 + xo_open_instance("fstab");
203 putfsent(&mntbuf[i]);
204 + xo_close_instance("fstab");
206 + xo_close_list("fstab");
208 + xo_open_list("mounted");
209 for (i = 0; i < mntsize; i++) {
210 if (checkvfsname(mntbuf[i].f_fstypename,
212 @@ -360,10 +378,13 @@ main(int argc, char *argv[])
214 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
216 + xo_open_instance("mounted");
218 + xo_close_instance("mounted");
220 + xo_close_list("mounted");
227 @@ -373,7 +394,7 @@ main(int argc, char *argv[])
230 if ((mntbuf = getmntpt(*argv)) == NULL)
231 - errx(1, "not currently mounted %s", *argv);
232 + xo_errx(1, "not currently mounted %s", *argv);
234 * Only get the mntflags from fstab if both mntpoint
235 * and mntspec are identical. Also handle the special
236 @@ -411,10 +432,10 @@ main(int argc, char *argv[])
238 if ((fs = getfsfile(*argv)) == NULL &&
239 (fs = getfsspec(*argv)) == NULL)
240 - errx(1, "%s: unknown special file or file system",
241 + xo_errx(1, "%s: unknown special file or file system",
243 if (BADTYPE(fs->fs_type))
244 - errx(1, "%s has unknown file system type",
245 + xo_errx(1, "%s has unknown file system type",
247 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
248 init_flags, options, fs->fs_mntops);
249 @@ -460,7 +481,7 @@ main(int argc, char *argv[])
250 if (rval == 0 && getuid() == 0)
258 @@ -537,7 +558,7 @@ append_arg(struct cpa *sa, char *arg)
259 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
260 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
262 - errx(1, "realloc failed");
263 + xo_errx(1, "realloc failed");
265 sa->a[++sa->c] = arg;
267 @@ -553,7 +574,7 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
269 /* resolve the mountpoint with realpath(3) */
270 if (checkpath(name, mntpath) != 0) {
271 - warn("%s", mntpath);
272 + xo_warn("%s", mntpath);
276 @@ -596,12 +617,12 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
279 if (use_mountprog(vfstype))
280 - printf("exec: %s", execname);
281 + xo_emit("{Lwc:exec}{:execname/%s}", execname);
283 - printf("mount -t %s", vfstype);
284 + xo_emit("{:execname/mount}{P: }{l:opts/-t}{P: }{l:opts/%s}", vfstype);
285 for (i = 1; i < mnt_argv.c; i++)
286 - (void)printf(" %s", mnt_argv.a[i]);
287 - (void)printf("\n");
288 + xo_emit("{P: }{l:opts}", mnt_argv.a[i]);
293 @@ -620,13 +641,22 @@ mountfs(const char *vfstype, const char *spec, const char *name, int flags,
296 if (statfs(name, &sf) < 0) {
297 - warn("statfs %s", name);
298 + xo_warn("statfs %s", name);
303 + xo_open_list("fstab");
304 + xo_open_instance("fstab");
307 + xo_close_instance("fstab");
308 + xo_close_list("fstab");
310 + xo_open_list("mounted");
311 + xo_open_instance("mounted");
313 + xo_close_instance("mounted");
314 + xo_close_list("mounted");
319 @@ -639,14 +669,15 @@ prmount(struct statfs *sfp)
321 struct mntoptnames *o;
325 - (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
326 - sfp->f_fstypename);
327 + xo_emit("{:special}{L: on }{:node}{L: (}{:fstype}", sfp->f_mntfromname,
328 + sfp->f_mntonname, sfp->f_fstypename);
330 flags = sfp->f_flags & MNT_VISFLAGMASK;
331 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
332 if (flags & o->o_opt) {
333 - (void)printf(", %s", o->o_name);
334 + xo_emit("{D:, }{l:opts}", o->o_name);
338 @@ -654,28 +685,37 @@ prmount(struct statfs *sfp)
339 * or privileged non-root user.
341 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
342 - (void)printf(", mounted by ");
343 + xo_emit("{D:, }{L:mounted by }");
344 if ((pw = getpwuid(sfp->f_owner)) != NULL)
345 - (void)printf("%s", pw->pw_name);
346 + xo_emit("{:mounter}", pw->pw_name);
348 - (void)printf("%d", sfp->f_owner);
349 + xo_emit("{:mounter}", sfp->f_owner);
352 - if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
353 - (void)printf(", writes: sync %ju async %ju",
354 + if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) {
355 + xo_open_container("writes");
356 + xo_emit("{D:, }{Lwc:writes}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
357 (uintmax_t)sfp->f_syncwrites,
358 (uintmax_t)sfp->f_asyncwrites);
359 - if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
360 - (void)printf(", reads: sync %ju async %ju",
361 + xo_close_container("writes");
363 + if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) {
364 + xo_open_container("reads");
365 + xo_emit("{D:, }{Lwc:reads}{Lw:sync}{w:sync/%ju}{Lw:async}{:async/%ju}",
366 (uintmax_t)sfp->f_syncreads,
367 (uintmax_t)sfp->f_asyncreads);
368 + xo_close_container("reads");
370 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
371 - (void)printf(", fsid ");
372 + xo_emit("{D:, }{Lw:fsid}");
373 + fsidbuf = strdup("");
374 for (i = 0; i < sizeof(sfp->f_fsid); i++)
375 - (void)printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
376 + asprintf(&fsidbuf, "%s%02x", fsidbuf, ((u_char *)&sfp->f_fsid)[i]);
377 + xo_emit("{:fsid}", fsidbuf);
381 - (void)printf(")\n");
382 + xo_emit("{D:)}\n");
386 @@ -703,7 +743,7 @@ catopt(char *s0, const char *s1)
389 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
390 - errx(1, "asprintf failed");
391 + xo_errx(1, "asprintf failed");
395 @@ -758,7 +798,7 @@ mangle(char *options, struct cpa *a)
398 if (mountprog == NULL) {
399 - errx(1, "Need value for -o mountprog");
400 + xo_errx(1, "Need value for -o mountprog");
403 } else if (strcmp(p, "userquota") == 0) {
404 @@ -824,7 +864,7 @@ update_options(char *opts, char *fstab, int curflags)
406 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
407 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
408 - errx(1, "malloc failed");
409 + xo_errx(1, "malloc failed");
411 strcpy(tmpopt, "no");
413 @@ -867,11 +907,11 @@ void
417 - (void)fprintf(stderr, "%s\n%s\n%s\n",
418 + xo_error("%s\n%s\n%s\n",
419 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
420 " mount [-dfpruvw] special | node",
421 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
427 @@ -899,32 +939,36 @@ putfsent(struct statfs *ent)
430 l = strlen(ent->f_mntfromname);
431 - printf("%s%s%s%s", ent->f_mntfromname,
432 + xo_emit("{:device}{P:/%s}{P:/%s}{P:/%s}",
433 + ent->f_mntfromname,
436 l < 24 ? "\t" : " ");
437 l = strlen(ent->f_mntonname);
438 - printf("%s%s%s%s", ent->f_mntonname,
439 + xo_emit("{:mntpoint}{P:/%s}{P:/%s}{P:/%s}",
443 l < 24 ? "\t" : " ");
444 - printf("%s\t", ent->f_fstypename);
445 + xo_emit("{:fstype}{P:\t}", ent->f_fstypename);
447 - printf("%s%s", opts,
448 + xo_emit("{:opts}{P:/%s}", opts,
452 if ((fst = getfsspec(ent->f_mntfromname)))
453 - printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
454 + xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
455 + fst->fs_freq, fst->fs_passno);
456 else if ((fst = getfsfile(ent->f_mntonname)))
457 - printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
458 + xo_emit("{P:\t}{n:dump/%u}{P: }{n:pass/%u}\n",
459 + fst->fs_freq, fst->fs_passno);
460 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
461 if (strcmp(ent->f_mntonname, "/") == 0)
463 + xo_emit("{P:\t}{n:dump/1}{P: }{n:pass/1}\n");
466 + xo_emit("{P:\t}{n:dump/2}{P: }{n:pass/2}\n");
469 + xo_emit("{P:\t}{n:dump/0}{P: }{n:pass/0}\n");