Merge commit '1276d1e1a1b128f7093a3021d3f6bc27afa80d23' into amd64
[dragonfly.git] / sbin / mount / mount.c
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)mount.c  8.25 (Berkeley) 5/8/95
35  * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
36  * $DragonFly: src/sbin/mount/mount.c,v 1.10 2005/04/03 17:13:08 joerg Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
43
44 #include <err.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <pwd.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "extern.h"
55 #include "mntopts.h"
56 #include "pathnames.h"
57
58 /* `meta' options */
59 #define MOUNT_META_OPTION_FSTAB         "fstab" 
60 #define MOUNT_META_OPTION_CURRENT       "current"
61
62 int debug, fstab_style, verbose;
63
64 static char  *catopt(char *, const char *);
65 static struct statfs
66              *getmntpt(const char *);
67 static int    hasopt(const char *, const char *);
68 static int    ismounted(struct fstab *, struct statfs *, int);
69 static int    isremountable(const char *);
70 static void   mangle(char *, int *, const char **);
71 static char  *update_options(char *, char *, int);
72 static int    mountfs(const char *, const char *, const char *,
73                       int, const char *, const char *);
74 static void   remopt(char *, const char *);
75 static void   printdefvals(const struct statfs *);
76 static void   prmount(struct statfs *);
77 static void   putfsent(const struct statfs *);
78 static void   usage(void);
79 static char  *flags2opts(int);
80 static char  *xstrdup(const char *str);
81
82 /* Map from mount options to printable formats. */
83 static struct opt {
84         int o_opt;
85         const char *o_name;
86 } optnames[] = {
87         { MNT_ASYNC,            "asynchronous" },
88         { MNT_EXPORTED,         "NFS exported" },
89         { MNT_LOCAL,            "local" },
90         { MNT_NOATIME,          "noatime" },
91         { MNT_NODEV,            "nodev" },
92         { MNT_NOEXEC,           "noexec" },
93         { MNT_NOSUID,           "nosuid" },
94         { MNT_NOSYMFOLLOW,      "nosymfollow" },
95         { MNT_QUOTA,            "with quotas" },
96         { MNT_RDONLY,           "read-only" },
97         { MNT_SYNCHRONOUS,      "synchronous" },
98         { MNT_UNION,            "union" },
99         { MNT_NOCLUSTERR,       "noclusterr" },
100         { MNT_NOCLUSTERW,       "noclusterw" },
101         { MNT_SUIDDIR,          "suiddir" },
102         { MNT_SOFTDEP,          "soft-updates" },
103         { MNT_IGNORE,           "ignore" },
104         { 0, NULL }
105 };
106
107 /*
108  * List of VFS types that can be remounted without becoming mounted on top
109  * of each other.
110  * XXX Is this list correct?
111  */
112 static const char *
113 remountable_fs_names[] = {
114         "ufs", "ffs", "ext2fs",
115         0
116 };
117
118 int
119 main(int argc, char **argv)
120 {
121         const char *mntfromname, **vfslist, *vfstype;
122         struct fstab *fs;
123         struct statfs *mntbuf;
124         FILE *mountdfp;
125         pid_t pid;
126         int all, ch, i, init_flags, mntsize, rval, have_fstab;
127         char *options;
128
129         all = init_flags = 0;
130         options = NULL;
131         vfslist = NULL;
132         vfstype = "ufs";
133         while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
134                 switch (ch) {
135                 case 'a':
136                         all = 1;
137                         break;
138                 case 'd':
139                         debug = 1;
140                         break;
141                 case 'f':
142                         init_flags |= MNT_FORCE;
143                         break;
144                 case 'o':
145                         if (*optarg)
146                                 options = catopt(options, optarg);
147                         break;
148                 case 'p':
149                         fstab_style = 1;
150                         verbose = 1;
151                         break;
152                 case 'r':
153                         options = catopt(options, "ro");
154                         break;
155                 case 't':
156                         if (vfslist != NULL)
157                                 errx(1, "only one -t option may be specified");
158                         vfslist = makevfslist(optarg);
159                         vfstype = optarg;
160                         break;
161                 case 'u':
162                         init_flags |= MNT_UPDATE;
163                         break;
164                 case 'v':
165                         verbose = 1;
166                         break;
167                 case 'w':
168                         options = catopt(options, "noro");
169                         break;
170                 case '?':
171                 default:
172                         usage();
173                         /* NOTREACHED */
174                 }
175         argc -= optind;
176         argv += optind;
177
178 #define BADTYPE(type)                                                   \
179         (strcmp(type, FSTAB_RO) &&                                      \
180             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
181
182         rval = 0;
183         switch (argc) {
184         case 0:
185                 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
186                         err(1, "getmntinfo");
187                 if (all) {
188                         while ((fs = getfsent()) != NULL) {
189                                 if (BADTYPE(fs->fs_type))
190                                         continue;
191                                 if (checkvfsname(fs->fs_vfstype, vfslist))
192                                         continue;
193                                 if (hasopt(fs->fs_mntops, "noauto"))
194                                         continue;
195                                 if (!(init_flags & MNT_UPDATE) &&
196                                     ismounted(fs, mntbuf, mntsize))
197                                         continue;
198                                 options = update_options(options,
199                                     fs->fs_mntops, mntbuf->f_flags);
200                                 if (mountfs(fs->fs_vfstype, fs->fs_spec,
201                                     fs->fs_file, init_flags, options,
202                                     fs->fs_mntops))
203                                         rval = 1;
204                         }
205                 } else if (fstab_style) {
206                         for (i = 0; i < mntsize; i++) {
207                                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
208                                         continue;
209                                 putfsent(&mntbuf[i]);
210                         }
211                 } else {
212                         for (i = 0; i < mntsize; i++) {
213                                 if (checkvfsname(mntbuf[i].f_fstypename,
214                                     vfslist))
215                                         continue;
216                                 prmount(&mntbuf[i]);
217                         }
218                 }
219                 exit(rval);
220         case 1:
221                 if (vfslist != NULL)
222                         usage();
223
224                 rmslashes(*argv, *argv);
225
226                 if (init_flags & MNT_UPDATE) {
227                         mntfromname = NULL;
228                         have_fstab = 0;
229                         if ((mntbuf = getmntpt(*argv)) == NULL)
230                                 errx(1, "not currently mounted %s", *argv);
231                         /*
232                          * Only get the mntflags from fstab if both mntpoint
233                          * and mntspec are identical. Also handle the special
234                          * case where just '/' is mounted and 'spec' is not
235                          * identical with the one from fstab ('/dev' is missing
236                          * in the spec-string at boot-time).
237                          */
238                         if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
239                                 if (strcmp(fs->fs_spec,
240                                     mntbuf->f_mntfromname) == 0 &&
241                                     strcmp(fs->fs_file,
242                                     mntbuf->f_mntonname) == 0) {
243                                         have_fstab = 1;
244                                         mntfromname = mntbuf->f_mntfromname;
245                                 } else if (argv[0][0] == '/' &&
246                                     argv[0][1] == '\0') {
247                                         fs = getfsfile("/");
248                                         have_fstab = 1;
249                                         mntfromname = fs->fs_spec;
250                                 }
251                         }
252                         if (have_fstab) {
253                                 options = update_options(options, fs->fs_mntops,
254                                     mntbuf->f_flags);
255                         } else {
256                                 mntfromname = mntbuf->f_mntfromname;
257                                 options = update_options(options, NULL,
258                                     mntbuf->f_flags);
259                         }
260                         rval = mountfs(mntbuf->f_fstypename, mntfromname,
261                             mntbuf->f_mntonname, init_flags, options, 0);
262                         break;
263                 }
264                 if ((fs = getfsfile(*argv)) == NULL &&
265                     (fs = getfsspec(*argv)) == NULL)
266                         errx(1, "%s: unknown special file or file system",
267                             *argv);
268                 if (BADTYPE(fs->fs_type))
269                         errx(1, "%s has unknown file system type",
270                             *argv);
271                 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
272                     init_flags, options, fs->fs_mntops);
273                 break;
274         case 2:
275                 /*
276                  * If -t flag has not been specified, the path cannot be
277                  * found, spec contains either a ':' or a '@', and the
278                  * spec is not a file with those characters, then assume
279                  * that an NFS filesystem is being specified ala Sun.
280                  */
281                 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
282                     access(argv[0], 0) == -1)
283                         vfstype = "nfs";
284                 rval = mountfs(vfstype,
285                     argv[0], argv[1], init_flags, options, NULL);
286                 break;
287         default:
288                 usage();
289                 /* NOTREACHED */
290         }
291
292         /*
293          * If the mount was successfully, and done by root, tell mountd the
294          * good news.  Pid checks are probably unnecessary, but don't hurt.
295          */
296         if (rval == 0 && getuid() == 0 &&
297             (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
298                 if (fscanf(mountdfp, "%d", &pid) == 1 &&
299                      pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
300                         err(1, "signal mountd");
301                 fclose(mountdfp);
302         }
303
304         exit(rval);
305 }
306
307 static int
308 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
309 {
310         int i;
311
312         if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
313                 /* the root file system can always be remounted */
314                 return (0);
315
316         for (i = mntsize - 1; i >= 0; --i)
317                 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
318                     (!isremountable(fs->fs_vfstype) ||
319                      strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
320                         return (1);
321         return (0);
322 }
323
324 static int
325 isremountable(const char *vfsname)
326 {
327         const char **cp;
328
329         for (cp = remountable_fs_names; *cp; cp++)
330                 if (strcmp(*cp, vfsname) == 0)
331                         return (1);
332         return (0);
333 }
334
335 static int
336 hasopt(const char *mntopts, const char *option)
337 {
338         int negative, found;
339         char *opt, *optbuf;
340
341         if (option[0] == 'n' && option[1] == 'o') {
342                 negative = 1;
343                 option += 2;
344         } else
345                 negative = 0;
346         optbuf = xstrdup(mntopts);
347         found = 0;
348         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
349                 if (opt[0] == 'n' && opt[1] == 'o') {
350                         if (!strcasecmp(opt + 2, option))
351                                 found = negative;
352                 } else if (!strcasecmp(opt, option))
353                         found = !negative;
354         }
355         free(optbuf);
356         return (found);
357 }
358
359 static int
360 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
361         const char *options, const char *mntopts)
362 {
363         /* List of directories containing mount_xxx subcommands. */
364         static const char *edirs[] = {
365                 _PATH_SBIN,
366                 _PATH_USRSBIN,
367                 NULL
368         };
369         const char *argv[100], **edir;
370         struct statfs sf;
371         pid_t pid;
372         int argc, i, status;
373         char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
374
375 #if __GNUC__
376         (void)&optbuf;
377         (void)&name;
378 #endif
379
380         /* resolve the mountpoint with realpath(3) */
381         checkpath(name, mntpath);
382         name = mntpath;
383
384         if (mntopts == NULL)
385                 mntopts = "";
386         if (options == NULL) {
387                 if (*mntopts == '\0') {
388                         options = "rw";
389                 } else {
390                         options = mntopts;
391                         mntopts = "";
392                 }
393         }
394         optbuf = catopt(xstrdup(mntopts), options);
395
396         if (strcmp(name, "/") == 0)
397                 flags |= MNT_UPDATE;
398         if (flags & MNT_FORCE)
399                 optbuf = catopt(optbuf, "force");
400         if (flags & MNT_RDONLY)
401                 optbuf = catopt(optbuf, "ro");
402         /*
403          * XXX
404          * The mount_mfs (newfs) command uses -o to select the
405          * optimization mode.  We don't pass the default "-o rw"
406          * for that reason.
407          */
408         if (flags & MNT_UPDATE)
409                 optbuf = catopt(optbuf, "update");
410
411         argc = 0;
412         argv[argc++] = vfstype;
413         mangle(optbuf, &argc, argv);
414         argv[argc++] = spec;
415         argv[argc++] = name;
416         argv[argc] = NULL;
417
418         if (debug) {
419                 printf("exec: mount_%s", vfstype);
420                 for (i = 1; i < argc; i++)
421                         printf(" %s", argv[i]);
422                 printf("\n");
423                 return (0);
424         }
425
426         switch (pid = fork()) {
427         case -1:                                /* Error. */
428                 warn("fork");
429                 free(optbuf);
430                 return (1);
431         case 0:                                 /* Child. */
432                 if (strcmp(vfstype, "ufs") == 0)
433                         exit(mount_ufs(argc, argv));
434
435                 /* Go find an executable. */
436                 for (edir = edirs; *edir; edir++) {
437                         snprintf(execname,
438                             sizeof(execname), "%s/mount_%s", *edir, vfstype);
439                         execv(execname, __DECONST(char * const *, argv));
440                 }
441                 if (errno == ENOENT) {
442                         int len = 0;
443                         char *cp;
444                         for (edir = edirs; *edir; edir++)
445                                 len += strlen(*edir) + 2;       /* ", " */
446                         if ((cp = malloc(len)) == NULL)
447                                 errx(1, "malloc failed");
448                         cp[0] = '\0';
449                         for (edir = edirs; *edir; edir++) {
450                                 strcat(cp, *edir);
451                                 if (edir[1] != NULL)
452                                         strcat(cp, ", ");
453                         }
454                         warn("exec mount_%s not found in %s", vfstype, cp);
455                 }
456                 exit(1);
457                 /* NOTREACHED */
458         default:                                /* Parent. */
459                 free(optbuf);
460
461                 if (waitpid(pid, &status, 0) < 0) {
462                         warn("waitpid");
463                         return (1);
464                 }
465
466                 if (WIFEXITED(status)) {
467                         if (WEXITSTATUS(status) != 0)
468                                 return (WEXITSTATUS(status));
469                 } else if (WIFSIGNALED(status)) {
470                         warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
471                         return (1);
472                 }
473
474                 if (verbose) {
475                         if (statfs(name, &sf) < 0) {
476                                 warn("statfs %s", name);
477                                 return (1);
478                         }
479                         if (fstab_style)
480                                 putfsent(&sf);
481                         else
482                                 prmount(&sf);
483                 }
484                 break;
485         }
486
487         return (0);
488 }
489
490 static void
491 prmount(struct statfs *sfp)
492 {
493         int flags;
494         struct opt *o;
495         struct passwd *pw;
496
497         printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
498             sfp->f_fstypename);
499
500         flags = sfp->f_flags & MNT_VISFLAGMASK;
501         for (o = optnames; flags && o->o_opt; o++)
502                 if (flags & o->o_opt) {
503                         printf(", %s", o->o_name);
504                         flags &= ~o->o_opt;
505                 }
506         if (sfp->f_owner) {
507                 printf(", mounted by ");
508                 if ((pw = getpwuid(sfp->f_owner)) != NULL)
509                         printf("%s", pw->pw_name);
510                 else
511                         printf("%d", sfp->f_owner);
512         }
513         if (verbose) {
514                 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
515                         printf(", writes: sync %ld async %ld",
516                             sfp->f_syncwrites, sfp->f_asyncwrites);
517                 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
518                         printf(", reads: sync %ld async %ld",
519                             sfp->f_syncreads, sfp->f_asyncreads);
520         }
521         printf(")\n");
522 }
523
524 static struct statfs *
525 getmntpt(const char *name)
526 {
527         struct statfs *mntbuf;
528         int i, mntsize;
529
530         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
531         for (i = mntsize - 1; i >= 0; i--) {
532                 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
533                     strcmp(mntbuf[i].f_mntonname, name) == 0)
534                         return (&mntbuf[i]);
535         }
536         return (NULL);
537 }
538
539 static char *
540 catopt(char *s0, const char *s1)
541 {
542         size_t i;
543         char *cp;
544
545         if (s1 == NULL || *s1 == '\0')
546                 return s0;
547
548         if (s0 && *s0) {
549                 i = strlen(s0) + strlen(s1) + 1 + 1;
550                 if ((cp = malloc(i)) == NULL)
551                         errx(1, "malloc failed");
552                 snprintf(cp, i, "%s,%s", s0, s1);
553         } else
554                 cp = xstrdup(s1);
555
556         if (s0)
557                 free(s0);
558         return (cp);
559 }
560
561 static void
562 mangle(char *options, int *argcp, const char **argv)
563 {
564         char *p, *s;
565         int argc;
566
567         argc = *argcp;
568         for (s = options; (p = strsep(&s, ",")) != NULL;)
569                 if (*p != '\0') {
570                         if (*p == '-') {
571                                 argv[argc++] = p;
572                                 p = strchr(p, '=');
573                                 if (p) {
574                                         *p = '\0';
575                                         argv[argc++] = p+1;
576                                 }
577                         } else if (strcmp(p, "rw") != 0) {
578                                 argv[argc++] = "-o";
579                                 argv[argc++] = p;
580                         }
581                 }
582
583         *argcp = argc;
584 }
585
586
587 static char *
588 update_options(char *opts, char *fstab, int curflags)
589 {
590         char *o, *p;
591         char *cur;
592         char *expopt, *newopt, *tmpopt;
593
594         if (opts == NULL)
595                 return xstrdup("");
596
597         /* remove meta options from list */
598         remopt(fstab, MOUNT_META_OPTION_FSTAB);
599         remopt(fstab, MOUNT_META_OPTION_CURRENT);
600         cur = flags2opts(curflags);
601
602         /*
603          * Expand all meta-options passed to us first.
604          */
605         expopt = NULL;
606         for (p = opts; (o = strsep(&p, ",")) != NULL;) {
607                 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
608                         expopt = catopt(expopt, fstab);
609                 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
610                         expopt = catopt(expopt, cur);
611                 else
612                         expopt = catopt(expopt, o);
613         }
614         free(cur);
615         free(opts);
616
617         /*
618          * Remove previous contradictory arguments. Given option "foo" we
619          * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
620          * and "foo" - so we can deal with possible options like "notice".
621          */
622         newopt = NULL;
623         for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
624                 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
625                         errx(1, "malloc failed");
626         
627                 strcpy(tmpopt, "no");
628                 strcat(tmpopt, o);
629                 remopt(newopt, tmpopt);
630                 free(tmpopt);
631
632                 if (strncmp("no", o, 2) == 0)
633                         remopt(newopt, o+2);
634
635                 newopt = catopt(newopt, o);
636         }
637         free(expopt);
638
639         return newopt;
640 }
641
642 static void
643 remopt(char *string, const char *opt)
644 {
645         char *o, *p, *r;
646
647         if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
648                 return;
649
650         r = string;
651
652         for (p = string; (o = strsep(&p, ",")) != NULL;) {
653                 if (strcmp(opt, o) != 0) {
654                         if (*r == ',' && *o != '\0')
655                                 r++;
656                         while ((*r++ = *o++) != '\0')
657                             ;
658                         *--r = ',';
659                 }
660         }
661         *r = '\0';
662 }
663
664 static void
665 usage(void)
666 {
667
668         fprintf(stderr, "%s\n%s\n%s\n",
669 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
670 "       mount [-adfpruvw] [-o options] [-t ufs | external_type]",
671 "       mount [-dfpruvw] special | node");
672         exit(1);
673 }
674
675 /*
676  * Prints the default values for dump frequency and pass number of fsck.
677  * This happens when mount(8) is called with -p and there is no fstab file
678  * or there is but the values aren't specified.
679  */
680 static void
681 printdefvals(const struct statfs *ent)
682 {
683         if (strcmp(ent->f_fstypename, "ufs") == 0) {
684                 if (strcmp(ent->f_mntonname, "/") == 0)
685                         printf("\t1 1\n");
686                 else
687                         printf("\t2 2\n");
688         } else {
689                 printf("\t0 0\n");
690         }
691 }
692
693 static void
694 putfsent(const struct statfs *ent)
695 {
696         struct stat sb;
697         struct fstab *fst;
698         char *opts;
699   
700         opts = flags2opts(ent->f_flags);
701         printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
702             ent->f_fstypename, opts);
703         free(opts);
704
705         /*
706          * If fstab file is missing don't call getfsspec() or getfsfile()
707          * at all, because the same warning will be printed twice for every
708          * mounted filesystem.
709          */
710         if (stat(_PATH_FSTAB, &sb) != -1) {
711                 if ((fst = getfsspec(ent->f_mntfromname)))
712                         printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
713                 else if ((fst = getfsfile(ent->f_mntonname)))
714                         printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
715                 else
716                         printdefvals(ent);
717         } else {
718                 printdefvals(ent);
719         }
720 }
721
722
723 static char *
724 flags2opts(int flags)
725 {
726         char *res;
727
728         res = NULL;
729
730         res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
731
732         if (flags & MNT_SYNCHRONOUS)    res = catopt(res, "sync");
733         if (flags & MNT_NOEXEC)         res = catopt(res, "noexec");
734         if (flags & MNT_NOSUID)         res = catopt(res, "nosuid");
735         if (flags & MNT_NODEV)          res = catopt(res, "nodev");
736         if (flags & MNT_UNION)          res = catopt(res, "union");
737         if (flags & MNT_ASYNC)          res = catopt(res, "async");
738         if (flags & MNT_NOATIME)        res = catopt(res, "noatime");
739         if (flags & MNT_NOCLUSTERR)     res = catopt(res, "noclusterr");
740         if (flags & MNT_NOCLUSTERW)     res = catopt(res, "noclusterw");
741         if (flags & MNT_NOSYMFOLLOW)    res = catopt(res, "nosymfollow");
742         if (flags & MNT_SUIDDIR)        res = catopt(res, "suiddir");
743         if (flags & MNT_IGNORE)         res = catopt(res, "ignore");
744
745         return res;
746 }
747
748 static char*
749 xstrdup(const char *str)
750 {
751         char* ret = strdup(str);
752         if(ret == NULL) {
753                 errx(1, "strdup failed (could not allocate memory)");
754         }
755         return ret;
756 }