]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/df/df.c
Merge branch 'releng/11.3' into releng-CDN/11.3
[FreeBSD/FreeBSD.git] / bin / df / df.c
1 /*-
2  * Copyright (c) 1980, 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #if 0
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993, 1994\n\
39         The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 static char sccsid[] = "@(#)df.c        8.9 (Berkeley) 5/8/95";
44 #endif /* not lint */
45 #endif
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <sys/mount.h>
52 #include <sys/sysctl.h>
53 #ifdef MOUNT_CHAR_DEVS
54 #include <ufs/ufs/ufsmount.h>
55 #endif
56 #include <err.h>
57 #include <getopt.h>
58 #include <libutil.h>
59 #include <locale.h>
60 #ifdef MOUNT_CHAR_DEVS
61 #include <mntopts.h>
62 #endif
63 #include <stdint.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sysexits.h>
68 #include <unistd.h>
69 #include <libxo/xo.h>
70
71 #include "extern.h"
72
73 #define UNITS_SI        1
74 #define UNITS_2         2
75
76 /* Maximum widths of various fields. */
77 struct maxwidths {
78         int     mntfrom;
79         int     fstype;
80         int     total;
81         int     used;
82         int     avail;
83         int     iused;
84         int     ifree;
85 };
86
87 static void       addstat(struct statfs *, struct statfs *);
88 static char      *getmntpt(const char *);
89 static int        int64width(int64_t);
90 static char      *makenetvfslist(void);
91 static void       prthuman(const struct statfs *, int64_t);
92 static void       prthumanval(const char *, int64_t);
93 static intmax_t   fsbtoblk(int64_t, uint64_t, u_long);
94 static void       prtstat(struct statfs *, struct maxwidths *);
95 static size_t     regetmntinfo(struct statfs **, long, const char **);
96 static void       update_maxwidths(struct maxwidths *, const struct statfs *);
97 static void       usage(void);
98
99 static __inline int
100 imax(int a, int b)
101 {
102         return (a > b ? a : b);
103 }
104
105 static int      aflag = 0, cflag, hflag, iflag, kflag, lflag = 0, nflag, Tflag;
106 static int      thousands;
107 #ifdef MOUNT_CHAR_DEVS
108 static struct   ufs_args mdev;
109 #endif
110
111 static const struct option long_options[] =
112 {
113         { "si", no_argument, NULL, 'H' },
114         { NULL, no_argument, NULL, 0 },
115 };
116
117 int
118 main(int argc, char *argv[])
119 {
120         struct stat stbuf;
121         struct statfs statfsbuf, totalbuf;
122         struct maxwidths maxwidths;
123         struct statfs *mntbuf;
124 #ifdef MOUNT_CHAR_DEVS
125         struct iovec *iov = NULL;
126 #endif
127         const char *fstype;
128 #ifdef MOUNT_CHAR_DEVS
129         char *mntpath;
130         char errmsg[255] = {0};
131 #endif
132         char *mntpt;
133         const char **vfslist;
134         int i, mntsize;
135         int ch, rv;
136 #ifdef MOUNT_CHAR_DEVS
137         int iovlen = 0;
138 #endif
139
140         fstype = "ufs";
141         (void)setlocale(LC_ALL, "");
142         memset(&maxwidths, 0, sizeof(maxwidths));
143         memset(&totalbuf, 0, sizeof(totalbuf));
144         totalbuf.f_bsize = DEV_BSIZE;
145         strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
146         vfslist = NULL;
147
148         argc = xo_parse_args(argc, argv);
149         if (argc < 0)
150                 exit(1);
151
152         while ((ch = getopt_long(argc, argv, "+abcgHhiklmnPt:T,", long_options,
153             NULL)) != -1)
154                 switch (ch) {
155                 case 'a':
156                         aflag = 1;
157                         break;
158                 case 'b':
159                                 /* FALLTHROUGH */
160                 case 'P':
161                         /*
162                          * POSIX specifically discusses the behavior of
163                          * both -k and -P. It states that the blocksize should
164                          * be set to 1024. Thus, if this occurs, simply break
165                          * rather than clobbering the old blocksize.
166                          */
167                         if (kflag)
168                                 break;
169                         setenv("BLOCKSIZE", "512", 1);
170                         hflag = 0;
171                         break;
172                 case 'c':
173                         cflag = 1;
174                         break;
175                 case 'g':
176                         setenv("BLOCKSIZE", "1g", 1);
177                         hflag = 0;
178                         break;
179                 case 'H':
180                         hflag = UNITS_SI;
181                         break;
182                 case 'h':
183                         hflag = UNITS_2;
184                         break;
185                 case 'i':
186                         iflag = 1;
187                         break;
188                 case 'k':
189                         kflag++;
190                         setenv("BLOCKSIZE", "1024", 1);
191                         hflag = 0;
192                         break;
193                 case 'l':
194                         /* Ignore duplicate -l */
195                         if (lflag)
196                                 break;
197                         if (vfslist != NULL)
198                                 xo_errx(1, "-l and -t are mutually exclusive.");
199                         vfslist = makevfslist(makenetvfslist());
200                         lflag = 1;
201                         break;
202                 case 'm':
203                         setenv("BLOCKSIZE", "1m", 1);
204                         hflag = 0;
205                         break;
206                 case 'n':
207                         nflag = 1;
208                         break;
209                 case 't':
210                         if (lflag)
211                                 xo_errx(1, "-l and -t are mutually exclusive.");
212                         if (vfslist != NULL)
213                                 xo_errx(1, "only one -t option may be specified");
214                         fstype = optarg;
215                         vfslist = makevfslist(optarg);
216                         break;
217                 case 'T':
218                         Tflag = 1;
219                         break;
220                 case ',':
221                         thousands = 1;
222                         break;
223                 case '?':
224                 default:
225                         usage();
226                 }
227         argc -= optind;
228         argv += optind;
229
230         rv = 0;
231         if (!*argv) {
232                 /* everything (modulo -t) */
233                 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
234                 mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
235         } else {
236                 /* just the filesystems specified on the command line */
237                 mntbuf = malloc(argc * sizeof(*mntbuf));
238                 if (mntbuf == NULL)
239                         xo_err(1, "malloc()");
240                 mntsize = 0;
241                 /* continued in for loop below */
242         }
243
244         xo_open_container("storage-system-information");
245         xo_open_list("filesystem");
246
247         /* iterate through specified filesystems */
248         for (; *argv; argv++) {
249                 if (stat(*argv, &stbuf) < 0) {
250                         if ((mntpt = getmntpt(*argv)) == NULL) {
251                                 xo_warn("%s", *argv);
252                                 rv = 1;
253                                 continue;
254                         }
255                 } else if (S_ISCHR(stbuf.st_mode)) {
256                         if ((mntpt = getmntpt(*argv)) == NULL) {
257 #ifdef MOUNT_CHAR_DEVS
258                                 xo_warnx(
259                                     "df on unmounted devices is deprecated");
260                                 mdev.fspec = *argv;
261                                 mntpath = strdup("/tmp/df.XXXXXX");
262                                 if (mntpath == NULL) {
263                                         xo_warn("strdup failed");
264                                         rv = 1;
265                                         continue;
266                                 }
267                                 mntpt = mkdtemp(mntpath);
268                                 if (mntpt == NULL) {
269                                         xo_warn("mkdtemp(\"%s\") failed", mntpath);
270                                         rv = 1;
271                                         free(mntpath);
272                                         continue;
273                                 }
274                                 if (iov != NULL)
275                                         free_iovec(&iov, &iovlen);
276                                 build_iovec_argf(&iov, &iovlen, "fstype", "%s",
277                                     fstype);
278                                 build_iovec_argf(&iov, &iovlen, "fspath", "%s",
279                                     mntpath);
280                                 build_iovec_argf(&iov, &iovlen, "from", "%s",
281                                     *argv);
282                                 build_iovec(&iov, &iovlen, "errmsg", errmsg,
283                                     sizeof(errmsg));
284                                 if (nmount(iov, iovlen,
285                                     MNT_RDONLY|MNT_NOEXEC) < 0) {
286                                         if (errmsg[0])
287                                                 xo_warn("%s: %s", *argv,
288                                                     errmsg);
289                                         else
290                                                 xo_warn("%s", *argv);
291                                         rv = 1;
292                                         (void)rmdir(mntpt);
293                                         free(mntpath);
294                                         continue;
295                                 } else if (statfs(mntpt, &statfsbuf) == 0) {
296                                         statfsbuf.f_mntonname[0] = '\0';
297                                         prtstat(&statfsbuf, &maxwidths);
298                                         if (cflag)
299                                                 addstat(&totalbuf, &statfsbuf);
300                                 } else {
301                                         xo_warn("%s", *argv);
302                                         rv = 1;
303                                 }
304                                 (void)unmount(mntpt, 0);
305                                 (void)rmdir(mntpt);
306                                 free(mntpath);
307                                 continue;
308 #else
309                                 xo_warnx("%s: not mounted", *argv);
310                                 rv = 1;
311                                 continue;
312 #endif
313                         }
314                 } else
315                         mntpt = *argv;
316
317                 /*
318                  * Statfs does not take a `wait' flag, so we cannot
319                  * implement nflag here.
320                  */
321                 if (statfs(mntpt, &statfsbuf) < 0) {
322                         xo_warn("%s", mntpt);
323                         rv = 1;
324                         continue;
325                 }
326
327                 /*
328                  * Check to make sure the arguments we've been given are
329                  * satisfied.  Return an error if we have been asked to
330                  * list a mount point that does not match the other args
331                  * we've been given (-l, -t, etc.).
332                  */
333                 if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
334                         rv = 1;
335                         continue;
336                 }
337
338                 /* the user asked for it, so ignore the ignore flag */
339                 statfsbuf.f_flags &= ~MNT_IGNORE;
340
341                 /* add to list */
342                 mntbuf[mntsize++] = statfsbuf;
343         }
344
345         memset(&maxwidths, 0, sizeof(maxwidths));
346         for (i = 0; i < mntsize; i++) {
347                 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
348                         update_maxwidths(&maxwidths, &mntbuf[i]);
349                         if (cflag)
350                                 addstat(&totalbuf, &mntbuf[i]);
351                 }
352         }
353         for (i = 0; i < mntsize; i++)
354                 if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
355                         prtstat(&mntbuf[i], &maxwidths);
356
357         xo_close_list("filesystem");
358
359         if (cflag)
360                 prtstat(&totalbuf, &maxwidths);
361
362         xo_close_container("storage-system-information");
363         xo_finish();
364         exit(rv);
365 }
366
367 static char *
368 getmntpt(const char *name)
369 {
370         size_t mntsize, i;
371         struct statfs *mntbuf;
372
373         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
374         for (i = 0; i < mntsize; i++) {
375                 if (!strcmp(mntbuf[i].f_mntfromname, name))
376                         return (mntbuf[i].f_mntonname);
377         }
378         return (NULL);
379 }
380
381 /*
382  * Make a pass over the file system info in ``mntbuf'' filtering out
383  * file system types not in vfslist and possibly re-stating to get
384  * current (not cached) info.  Returns the new count of valid statfs bufs.
385  */
386 static size_t
387 regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
388 {
389         int error, i, j;
390         struct statfs *mntbuf;
391
392         if (vfslist == NULL)
393                 return (nflag ? mntsize : getmntinfo(mntbufp, MNT_WAIT));
394
395         mntbuf = *mntbufp;
396         for (j = 0, i = 0; i < mntsize; i++) {
397                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
398                         continue;
399                 /*
400                  * XXX statfs(2) can fail for various reasons. It may be
401                  * possible that the user does not have access to the
402                  * pathname, if this happens, we will fall back on
403                  * "stale" filesystem statistics.
404                  */
405                 error = statfs(mntbuf[i].f_mntonname, &mntbuf[j]);
406                 if (nflag || error < 0)
407                         if (i != j) {
408                                 if (error < 0)
409                                         xo_warnx("%s stats possibly stale",
410                                             mntbuf[i].f_mntonname);
411                                 mntbuf[j] = mntbuf[i];
412                         }
413                 j++;
414         }
415         return (j);
416 }
417
418 static void
419 prthuman(const struct statfs *sfsp, int64_t used)
420 {
421
422         prthumanval("  {:blocks/%6s}", sfsp->f_blocks * sfsp->f_bsize);
423         prthumanval("  {:used/%6s}", used * sfsp->f_bsize);
424         prthumanval("  {:available/%6s}", sfsp->f_bavail * sfsp->f_bsize);
425 }
426
427 static void
428 prthumanval(const char *fmt, int64_t bytes)
429 {
430         char buf[6];
431         int flags;
432
433         flags = HN_B | HN_NOSPACE | HN_DECIMAL;
434         if (hflag == UNITS_SI)
435                 flags |= HN_DIVISOR_1000;
436
437         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
438             bytes, "", HN_AUTOSCALE, flags);
439
440         xo_attr("value", "%lld", (long long) bytes);
441         xo_emit(fmt, buf);
442 }
443
444 /*
445  * Print an inode count in "human-readable" format.
446  */
447 static void
448 prthumanvalinode(const char *fmt, int64_t bytes)
449 {
450         char buf[6];
451         int flags;
452
453         flags = HN_NOSPACE | HN_DECIMAL | HN_DIVISOR_1000;
454
455         humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
456             bytes, "", HN_AUTOSCALE, flags);
457
458         xo_attr("value", "%lld", (long long) bytes);
459         xo_emit(fmt, buf);
460 }
461
462 /*
463  * Convert statfs returned file system size into BLOCKSIZE units.
464  */
465 static intmax_t
466 fsbtoblk(int64_t num, uint64_t fsbs, u_long bs)
467 {
468         return (num * (intmax_t) fsbs / (int64_t) bs);
469 }
470
471 /*
472  * Print out status about a file system.
473  */
474 static void
475 prtstat(struct statfs *sfsp, struct maxwidths *mwp)
476 {
477         static long blocksize;
478         static int headerlen, timesthrough = 0;
479         static const char *header;
480         int64_t used, availblks, inodes;
481         const char *format;
482
483         if (++timesthrough == 1) {
484                 mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
485                 mwp->fstype = imax(mwp->fstype, (int)strlen("Type"));
486                 if (thousands) {                /* make space for commas */
487                     mwp->total += (mwp->total - 1) / 3;
488                     mwp->used  += (mwp->used - 1) / 3;
489                     mwp->avail += (mwp->avail - 1) / 3;
490                     mwp->iused += (mwp->iused - 1) / 3;
491                     mwp->ifree += (mwp->ifree - 1) / 3;
492                 }
493                 if (hflag) {
494                         header = "   Size";
495                         mwp->total = mwp->used = mwp->avail =
496                             (int)strlen(header);
497                 } else {
498                         header = getbsize(&headerlen, &blocksize);
499                         mwp->total = imax(mwp->total, headerlen);
500                 }
501                 mwp->used = imax(mwp->used, (int)strlen("Used"));
502                 mwp->avail = imax(mwp->avail, (int)strlen("Avail"));
503
504                 xo_emit("{T:/%-*s}", mwp->mntfrom, "Filesystem");
505                 if (Tflag)
506                         xo_emit("  {T:/%-*s}", mwp->fstype, "Type");
507                 xo_emit(" {T:/%*s} {T:/%*s} {T:/%*s} Capacity",
508                         mwp->total, header,
509                         mwp->used, "Used", mwp->avail, "Avail");
510                 if (iflag) {
511                         mwp->iused = imax(hflag ? 0 : mwp->iused,
512                             (int)strlen("  iused"));
513                         mwp->ifree = imax(hflag ? 0 : mwp->ifree,
514                             (int)strlen("ifree"));
515                         xo_emit(" {T:/%*s} {T:/%*s} {T:\%iused}",
516                             mwp->iused - 2, "iused", mwp->ifree, "ifree");
517                 }
518                 xo_emit("  {T:Mounted on}\n");
519         }
520
521         xo_open_instance("filesystem");
522         /* Check for 0 block size.  Can this happen? */
523         if (sfsp->f_bsize == 0) {
524                 xo_warnx ("File system %s does not have a block size, assuming 512.",
525                     sfsp->f_mntonname);
526                 sfsp->f_bsize = 512;
527         }
528         xo_emit("{tk:name/%-*s}", mwp->mntfrom, sfsp->f_mntfromname);
529         if (Tflag)
530                 xo_emit("  {:type/%-*s}", mwp->fstype, sfsp->f_fstypename);
531         used = sfsp->f_blocks - sfsp->f_bfree;
532         availblks = sfsp->f_bavail + used;
533         if (hflag) {
534                 prthuman(sfsp, used);
535         } else {
536                 if (thousands)
537                     format = " {t:total-blocks/%*j'd} {t:used-blocks/%*j'd} "
538                         "{t:available-blocks/%*j'd}";
539                 else
540                     format = " {t:total-blocks/%*jd} {t:used-blocks/%*jd} "
541                         "{t:available-blocks/%*jd}";
542                 xo_emit(format,
543                     mwp->total, fsbtoblk(sfsp->f_blocks,
544                     sfsp->f_bsize, blocksize),
545                     mwp->used, fsbtoblk(used, sfsp->f_bsize, blocksize),
546                     mwp->avail, fsbtoblk(sfsp->f_bavail,
547                     sfsp->f_bsize, blocksize));
548         }
549         xo_emit(" {:used-percent/%5.0f}{U:%%}",
550             availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
551         if (iflag) {
552                 inodes = sfsp->f_files;
553                 used = inodes - sfsp->f_ffree;
554                 if (hflag) {
555                         xo_emit("  ");
556                         prthumanvalinode(" {:inodes-used/%5s}", used);
557                         prthumanvalinode(" {:inodes-free/%5s}", sfsp->f_ffree);
558                 } else {
559                         if (thousands)
560                             format = " {:inodes-used/%*j'd} {:inodes-free/%*j'd}";
561                         else
562                             format = " {:inodes-used/%*jd} {:inodes-free/%*jd}";
563                         xo_emit(format, mwp->iused, (intmax_t)used,
564                             mwp->ifree, (intmax_t)sfsp->f_ffree);
565                 }
566                 xo_emit(" {:inodes-used-percent/%4.0f}{U:%%} ",
567                         inodes == 0 ? 100.0 :
568                         (double)used / (double)inodes * 100.0);
569         } else
570                 xo_emit("  ");
571         if (strncmp(sfsp->f_mntfromname, "total", MNAMELEN) != 0)
572                 xo_emit("  {:mounted-on}", sfsp->f_mntonname);
573         xo_emit("\n");
574         xo_close_instance("filesystem");
575 }
576
577 static void
578 addstat(struct statfs *totalfsp, struct statfs *statfsp)
579 {
580         uint64_t bsize;
581
582         bsize = statfsp->f_bsize / totalfsp->f_bsize;
583         totalfsp->f_blocks += statfsp->f_blocks * bsize;
584         totalfsp->f_bfree += statfsp->f_bfree * bsize;
585         totalfsp->f_bavail += statfsp->f_bavail * bsize;
586         totalfsp->f_files += statfsp->f_files;
587         totalfsp->f_ffree += statfsp->f_ffree;
588 }
589
590 /*
591  * Update the maximum field-width information in `mwp' based on
592  * the file system specified by `sfsp'.
593  */
594 static void
595 update_maxwidths(struct maxwidths *mwp, const struct statfs *sfsp)
596 {
597         static long blocksize = 0;
598         int dummy;
599
600         if (blocksize == 0)
601                 getbsize(&dummy, &blocksize);
602
603         mwp->mntfrom = imax(mwp->mntfrom, (int)strlen(sfsp->f_mntfromname));
604         mwp->fstype = imax(mwp->fstype, (int)strlen(sfsp->f_fstypename));
605         mwp->total = imax(mwp->total, int64width(
606             fsbtoblk((int64_t)sfsp->f_blocks, sfsp->f_bsize, blocksize)));
607         mwp->used = imax(mwp->used,
608             int64width(fsbtoblk((int64_t)sfsp->f_blocks -
609             (int64_t)sfsp->f_bfree, sfsp->f_bsize, blocksize)));
610         mwp->avail = imax(mwp->avail, int64width(fsbtoblk(sfsp->f_bavail,
611             sfsp->f_bsize, blocksize)));
612         mwp->iused = imax(mwp->iused, int64width((int64_t)sfsp->f_files -
613             sfsp->f_ffree));
614         mwp->ifree = imax(mwp->ifree, int64width(sfsp->f_ffree));
615 }
616
617 /* Return the width in characters of the specified value. */
618 static int
619 int64width(int64_t val)
620 {
621         int len;
622
623         len = 0;
624         /* Negative or zero values require one extra digit. */
625         if (val <= 0) {
626                 val = -val;
627                 len++;
628         }
629         while (val > 0) {
630                 len++;
631                 val /= 10;
632         }
633
634         return (len);
635 }
636
637 static void
638 usage(void)
639 {
640
641         xo_error(
642 "usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [-,]\n"
643 "          [file | filesystem ...]\n");
644         exit(EX_USAGE);
645 }
646
647 static char *
648 makenetvfslist(void)
649 {
650         char *str, *strptr, **listptr;
651         struct xvfsconf *xvfsp, *keep_xvfsp;
652         size_t buflen;
653         int cnt, i, maxvfsconf;
654
655         if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
656                 xo_warn("sysctl(vfs.conflist)");
657                 return (NULL);
658         }
659         xvfsp = malloc(buflen);
660         if (xvfsp == NULL) {
661                 xo_warnx("malloc failed");
662                 return (NULL);
663         }
664         keep_xvfsp = xvfsp;
665         if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
666                 xo_warn("sysctl(vfs.conflist)");
667                 free(keep_xvfsp);
668                 return (NULL);
669         }
670         maxvfsconf = buflen / sizeof(struct xvfsconf);
671
672         if ((listptr = malloc(sizeof(char*) * maxvfsconf)) == NULL) {
673                 xo_warnx("malloc failed");
674                 free(keep_xvfsp);
675                 return (NULL);
676         }
677
678         for (cnt = 0, i = 0; i < maxvfsconf; i++) {
679                 if (xvfsp->vfc_flags & VFCF_NETWORK) {
680                         listptr[cnt++] = strdup(xvfsp->vfc_name);
681                         if (listptr[cnt-1] == NULL) {
682                                 xo_warnx("malloc failed");
683                                 free(listptr);
684                                 free(keep_xvfsp);
685                                 return (NULL);
686                         }
687                 }
688                 xvfsp++;
689         }
690
691         if (cnt == 0 ||
692             (str = malloc(sizeof(char) * (32 * cnt + cnt + 2))) == NULL) {
693                 if (cnt > 0)
694                         xo_warnx("malloc failed");
695                 free(listptr);
696                 free(keep_xvfsp);
697                 return (NULL);
698         }
699
700         *str = 'n'; *(str + 1) = 'o';
701         for (i = 0, strptr = str + 2; i < cnt; i++, strptr++) {
702                 strlcpy(strptr, listptr[i], 32);
703                 strptr += strlen(listptr[i]);
704                 *strptr = ',';
705                 free(listptr[i]);
706         }
707         *(--strptr) = '\0';
708
709         free(keep_xvfsp);
710         free(listptr);
711         return (str);
712 }