1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #if 0 37 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/mount.h> 46 #include <sys/stat.h> 47 #include <sys/wait.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fstab.h> 53 #include <paths.h> 54 #include <pwd.h> 55 #include <signal.h> 56 #include <stdint.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <libutil.h> 62 63 #include "extern.h" 64 #include "mntopts.h" 65 #include "pathnames.h" 66 67 /* `meta' options */ 68 #define MOUNT_META_OPTION_FSTAB "fstab" 69 #define MOUNT_META_OPTION_CURRENT "current" 70 71 static int debug, fstab_style, verbose; 72 73 struct cpa { 74 char **a; 75 ssize_t sz; 76 int c; 77 }; 78 79 char *catopt(char *, const char *); 80 struct statfs *getmntpt(const char *); 81 int hasopt(const char *, const char *); 82 int ismounted(struct fstab *, struct statfs *, int); 83 int isremountable(const char *); 84 void mangle(char *, struct cpa *); 85 char *update_options(char *, char *, int); 86 int mountfs(const char *, const char *, const char *, 87 int, const char *, const char *); 88 void remopt(char *, const char *); 89 void prmount(struct statfs *); 90 void putfsent(struct statfs *); 91 void usage(void); 92 char *flags2opts(int); 93 94 /* Map from mount options to printable formats. */ 95 static struct opt { 96 uint64_t o_opt; 97 const char *o_name; 98 } optnames[] = { 99 { MNT_ASYNC, "asynchronous" }, 100 { MNT_EXPORTED, "NFS exported" }, 101 { MNT_LOCAL, "local" }, 102 { MNT_NOATIME, "noatime" }, 103 { MNT_NOEXEC, "noexec" }, 104 { MNT_NOSUID, "nosuid" }, 105 { MNT_NOSYMFOLLOW, "nosymfollow" }, 106 { MNT_QUOTA, "with quotas" }, 107 { MNT_RDONLY, "read-only" }, 108 { MNT_SYNCHRONOUS, "synchronous" }, 109 { MNT_UNION, "union" }, 110 { MNT_NOCLUSTERR, "noclusterr" }, 111 { MNT_NOCLUSTERW, "noclusterw" }, 112 { MNT_SUIDDIR, "suiddir" }, 113 { MNT_SOFTDEP, "soft-updates" }, 114 { MNT_SUJ, "journaled soft-updates" }, 115 { MNT_MULTILABEL, "multilabel" }, 116 { MNT_ACLS, "acls" }, 117 { MNT_NFS4ACLS, "nfsv4acls" }, 118 { MNT_GJOURNAL, "gjournal" }, 119 { MNT_AUTOMOUNTED, "automounted" }, 120 { MNT_VERIFIED, "verified" }, 121 { 0, NULL } 122 }; 123 124 /* 125 * List of VFS types that can be remounted without becoming mounted on top 126 * of each other. 127 * XXX Is this list correct? 128 */ 129 static const char * 130 remountable_fs_names[] = { 131 "ufs", "ffs", "ext2fs", 132 0 133 }; 134 135 static const char userquotaeq[] = "userquota="; 136 static const char groupquotaeq[] = "groupquota="; 137 138 static char *mountprog = NULL; 139 140 static int 141 use_mountprog(const char *vfstype) 142 { 143 /* XXX: We need to get away from implementing external mount 144 * programs for every filesystem, and move towards having 145 * each filesystem properly implement the nmount() system call. 146 */ 147 unsigned int i; 148 const char *fs[] = { 149 "cd9660", "mfs", "msdosfs", "nfs", 150 "nullfs", "smbfs", "udf", "unionfs", 151 NULL 152 }; 153 154 if (mountprog != NULL) 155 return (1); 156 157 for (i = 0; fs[i] != NULL; ++i) { 158 if (strcmp(vfstype, fs[i]) == 0) 159 return (1); 160 } 161 162 return (0); 163 } 164 165 static int 166 exec_mountprog(const char *name, const char *execname, char *const argv[]) 167 { 168 pid_t pid; 169 int status; 170 171 switch (pid = fork()) { 172 case -1: /* Error. */ 173 warn("fork"); 174 exit (1); 175 case 0: /* Child. */ 176 /* Go find an executable. */ 177 execvP(execname, _PATH_SYSPATH, argv); 178 if (errno == ENOENT) { 179 warn("exec %s not found", execname); 180 if (execname[0] != '/') { 181 warnx("in path: %s", _PATH_SYSPATH); 182 } 183 } 184 exit(1); 185 default: /* Parent. */ 186 if (waitpid(pid, &status, 0) < 0) { 187 warn("waitpid"); 188 return (1); 189 } 190 191 if (WIFEXITED(status)) { 192 if (WEXITSTATUS(status) != 0) 193 return (WEXITSTATUS(status)); 194 } else if (WIFSIGNALED(status)) { 195 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 196 return (1); 197 } 198 break; 199 } 200 201 return (0); 202 } 203 204 static int 205 specified_ro(const char *arg) 206 { 207 char *optbuf, *opt; 208 int ret = 0; 209 210 optbuf = strdup(arg); 211 if (optbuf == NULL) 212 err(1, NULL); 213 214 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 215 if (strcmp(opt, "ro") == 0) { 216 ret = 1; 217 break; 218 } 219 } 220 free(optbuf); 221 return (ret); 222 } 223 224 static void 225 restart_mountd(void) 226 { 227 struct pidfh *pfh; 228 pid_t mountdpid; 229 230 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid); 231 if (pfh != NULL) { 232 /* Mountd is not running. */ 233 pidfile_remove(pfh); 234 return; 235 } 236 if (errno != EEXIST) { 237 /* Cannot open pidfile for some reason. */ 238 return; 239 } 240 /* We have mountd(8) PID in mountdpid varible, let's signal it. */ 241 if (kill(mountdpid, SIGHUP) == -1) 242 err(1, "signal mountd"); 243 } 244 245 int 246 main(int argc, char *argv[]) 247 { 248 const char *mntfromname, **vfslist, *vfstype; 249 struct fstab *fs; 250 struct statfs *mntbuf; 251 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro; 252 int onlylate; 253 char *cp, *ep, *options; 254 255 all = init_flags = late = onlylate = 0; 256 ro = 0; 257 options = NULL; 258 vfslist = NULL; 259 vfstype = "ufs"; 260 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1) 261 switch (ch) { 262 case 'a': 263 all = 1; 264 break; 265 case 'd': 266 debug = 1; 267 break; 268 case 'F': 269 setfstab(optarg); 270 break; 271 case 'f': 272 init_flags |= MNT_FORCE; 273 break; 274 case 'L': 275 onlylate = 1; 276 late = 1; 277 break; 278 case 'l': 279 late = 1; 280 break; 281 case 'n': 282 /* For compatibility with the Linux version of mount. */ 283 break; 284 case 'o': 285 if (*optarg) { 286 options = catopt(options, optarg); 287 if (specified_ro(optarg)) 288 ro = 1; 289 } 290 break; 291 case 'p': 292 fstab_style = 1; 293 verbose = 1; 294 break; 295 case 'r': 296 options = catopt(options, "ro"); 297 ro = 1; 298 break; 299 case 't': 300 if (vfslist != NULL) 301 errx(1, "only one -t option may be specified"); 302 vfslist = makevfslist(optarg); 303 vfstype = optarg; 304 break; 305 case 'u': 306 init_flags |= MNT_UPDATE; 307 break; 308 case 'v': 309 verbose = 1; 310 break; 311 case 'w': 312 options = catopt(options, "noro"); 313 break; 314 case '?': 315 default: 316 usage(); 317 /* NOTREACHED */ 318 } 319 argc -= optind; 320 argv += optind; 321 322 #define BADTYPE(type) \ 323 (strcmp(type, FSTAB_RO) && \ 324 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 325 326 if ((init_flags & MNT_UPDATE) && (ro == 0)) 327 options = catopt(options, "noro"); 328 329 rval = 0; 330 switch (argc) { 331 case 0: 332 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 333 err(1, "getmntinfo"); 334 if (all) { 335 while ((fs = getfsent()) != NULL) { 336 if (BADTYPE(fs->fs_type)) 337 continue; 338 if (checkvfsname(fs->fs_vfstype, vfslist)) 339 continue; 340 if (hasopt(fs->fs_mntops, "noauto")) 341 continue; 342 if (!hasopt(fs->fs_mntops, "late") && onlylate) 343 continue; 344 if (hasopt(fs->fs_mntops, "late") && !late) 345 continue; 346 if (hasopt(fs->fs_mntops, "failok")) 347 failok = 1; 348 else 349 failok = 0; 350 if (!(init_flags & MNT_UPDATE) && 351 ismounted(fs, mntbuf, mntsize)) 352 continue; 353 options = update_options(options, fs->fs_mntops, 354 mntbuf->f_flags); 355 if (mountfs(fs->fs_vfstype, fs->fs_spec, 356 fs->fs_file, init_flags, options, 357 fs->fs_mntops) && !failok) 358 rval = 1; 359 } 360 } else if (fstab_style) { 361 for (i = 0; i < mntsize; i++) { 362 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 363 continue; 364 putfsent(&mntbuf[i]); 365 } 366 } else { 367 for (i = 0; i < mntsize; i++) { 368 if (checkvfsname(mntbuf[i].f_fstypename, 369 vfslist)) 370 continue; 371 if (!verbose && 372 (mntbuf[i].f_flags & MNT_IGNORE) != 0) 373 continue; 374 prmount(&mntbuf[i]); 375 } 376 } 377 exit(rval); 378 case 1: 379 if (vfslist != NULL) 380 usage(); 381 382 rmslashes(*argv, *argv); 383 if (init_flags & MNT_UPDATE) { 384 mntfromname = NULL; 385 have_fstab = 0; 386 if ((mntbuf = getmntpt(*argv)) == NULL) 387 errx(1, "not currently mounted %s", *argv); 388 /* 389 * Only get the mntflags from fstab if both mntpoint 390 * and mntspec are identical. Also handle the special 391 * case where just '/' is mounted and 'spec' is not 392 * identical with the one from fstab ('/dev' is missing 393 * in the spec-string at boot-time). 394 */ 395 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 396 if (strcmp(fs->fs_spec, 397 mntbuf->f_mntfromname) == 0 && 398 strcmp(fs->fs_file, 399 mntbuf->f_mntonname) == 0) { 400 have_fstab = 1; 401 mntfromname = mntbuf->f_mntfromname; 402 } else if (argv[0][0] == '/' && 403 argv[0][1] == '\0' && 404 strcmp(fs->fs_vfstype, 405 mntbuf->f_fstypename) == 0) { 406 fs = getfsfile("/"); 407 have_fstab = 1; 408 mntfromname = fs->fs_spec; 409 } 410 } 411 if (have_fstab) { 412 options = update_options(options, fs->fs_mntops, 413 mntbuf->f_flags); 414 } else { 415 mntfromname = mntbuf->f_mntfromname; 416 options = update_options(options, NULL, 417 mntbuf->f_flags); 418 } 419 rval = mountfs(mntbuf->f_fstypename, mntfromname, 420 mntbuf->f_mntonname, init_flags, options, 0); 421 break; 422 } 423 if ((fs = getfsfile(*argv)) == NULL && 424 (fs = getfsspec(*argv)) == NULL) 425 errx(1, "%s: unknown special file or file system", 426 *argv); 427 if (BADTYPE(fs->fs_type)) 428 errx(1, "%s has unknown file system type", 429 *argv); 430 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 431 init_flags, options, fs->fs_mntops); 432 break; 433 case 2: 434 /* 435 * If -t flag has not been specified, the path cannot be 436 * found, spec contains either a ':' or a '@', then assume 437 * that an NFS file system is being specified ala Sun. 438 * Check if the hostname contains only allowed characters 439 * to reduce false positives. IPv6 addresses containing 440 * ':' will be correctly parsed only if the separator is '@'. 441 * The definition of a valid hostname is taken from RFC 1034. 442 */ 443 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL || 444 (ep = strchr(argv[0], ':')) != NULL)) { 445 if (*ep == '@') { 446 cp = ep + 1; 447 ep = cp + strlen(cp); 448 } else 449 cp = argv[0]; 450 while (cp != ep) { 451 if (!isdigit(*cp) && !isalpha(*cp) && 452 *cp != '.' && *cp != '-' && *cp != ':') 453 break; 454 cp++; 455 } 456 if (cp == ep) 457 vfstype = "nfs"; 458 } 459 rval = mountfs(vfstype, 460 argv[0], argv[1], init_flags, options, NULL); 461 break; 462 default: 463 usage(); 464 /* NOTREACHED */ 465 } 466 467 /* 468 * If the mount was successfully, and done by root, tell mountd the 469 * good news. 470 */ 471 if (rval == 0 && getuid() == 0) 472 restart_mountd(); 473 474 exit(rval); 475 } 476 477 int 478 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize) 479 { 480 char realfsfile[PATH_MAX]; 481 int i; 482 483 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 484 /* the root file system can always be remounted */ 485 return (0); 486 487 /* The user may have specified a symlink in fstab, resolve the path */ 488 if (realpath(fs->fs_file, realfsfile) == NULL) { 489 /* Cannot resolve the path, use original one */ 490 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile)); 491 } 492 493 /* 494 * Consider the filesystem to be mounted if: 495 * It has the same mountpoint as a mounted filesytem, and 496 * It has the same type as that same mounted filesystem, and 497 * It has the same device name as that same mounted filesystem, OR 498 * It is a nonremountable filesystem 499 */ 500 for (i = mntsize - 1; i >= 0; --i) 501 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 && 502 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 && 503 (!isremountable(fs->fs_vfstype) || 504 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))) 505 return (1); 506 return (0); 507 } 508 509 int 510 isremountable(const char *vfsname) 511 { 512 const char **cp; 513 514 for (cp = remountable_fs_names; *cp; cp++) 515 if (strcmp(*cp, vfsname) == 0) 516 return (1); 517 return (0); 518 } 519 520 int 521 hasopt(const char *mntopts, const char *option) 522 { 523 int negative, found; 524 char *opt, *optbuf; 525 526 if (option[0] == 'n' && option[1] == 'o') { 527 negative = 1; 528 option += 2; 529 } else 530 negative = 0; 531 optbuf = strdup(mntopts); 532 found = 0; 533 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 534 if (opt[0] == 'n' && opt[1] == 'o') { 535 if (!strcasecmp(opt + 2, option)) 536 found = negative; 537 } else if (!strcasecmp(opt, option)) 538 found = !negative; 539 } 540 free(optbuf); 541 return (found); 542 } 543 544 static void 545 append_arg(struct cpa *sa, char *arg) 546 { 547 if (sa->c + 1 == sa->sz) { 548 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2; 549 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz); 550 if (sa->a == NULL) 551 errx(1, "realloc failed"); 552 } 553 sa->a[++sa->c] = arg; 554 } 555 556 int 557 mountfs(const char *vfstype, const char *spec, const char *name, int flags, 558 const char *options, const char *mntopts) 559 { 560 struct statfs sf; 561 int i, ret; 562 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; 563 static struct cpa mnt_argv; 564 565 /* resolve the mountpoint with realpath(3) */ 566 if (checkpath(name, mntpath) != 0) { 567 warn("%s", mntpath); 568 return (1); 569 } 570 name = mntpath; 571 572 if (mntopts == NULL) 573 mntopts = ""; 574 optbuf = catopt(strdup(mntopts), options); 575 576 if (strcmp(name, "/") == 0) 577 flags |= MNT_UPDATE; 578 if (flags & MNT_FORCE) 579 optbuf = catopt(optbuf, "force"); 580 if (flags & MNT_RDONLY) 581 optbuf = catopt(optbuf, "ro"); 582 /* 583 * XXX 584 * The mount_mfs (newfs) command uses -o to select the 585 * optimization mode. We don't pass the default "-o rw" 586 * for that reason. 587 */ 588 if (flags & MNT_UPDATE) 589 optbuf = catopt(optbuf, "update"); 590 591 /* Compatibility glue. */ 592 if (strcmp(vfstype, "msdos") == 0) 593 vfstype = "msdosfs"; 594 595 /* Construct the name of the appropriate mount command */ 596 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 597 598 mnt_argv.c = -1; 599 append_arg(&mnt_argv, execname); 600 mangle(optbuf, &mnt_argv); 601 if (mountprog != NULL) 602 strlcpy(execname, mountprog, sizeof(execname)); 603 604 append_arg(&mnt_argv, strdup(spec)); 605 append_arg(&mnt_argv, strdup(name)); 606 append_arg(&mnt_argv, NULL); 607 608 if (debug) { 609 if (use_mountprog(vfstype)) 610 printf("exec: %s", execname); 611 else 612 printf("mount -t %s", vfstype); 613 for (i = 1; i < mnt_argv.c; i++) 614 (void)printf(" %s", mnt_argv.a[i]); 615 (void)printf("\n"); 616 free(optbuf); 617 free(mountprog); 618 mountprog = NULL; 619 return (0); 620 } 621 622 if (use_mountprog(vfstype)) { 623 ret = exec_mountprog(name, execname, mnt_argv.a); 624 } else { 625 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 626 } 627 628 free(optbuf); 629 free(mountprog); 630 mountprog = NULL; 631 632 if (verbose) { 633 if (statfs(name, &sf) < 0) { 634 warn("statfs %s", name); 635 return (1); 636 } 637 if (fstab_style) 638 putfsent(&sf); 639 else 640 prmount(&sf); 641 } 642 643 return (ret); 644 } 645 646 void 647 prmount(struct statfs *sfp) 648 { 649 uint64_t flags; 650 unsigned int i; 651 struct opt *o; 652 struct passwd *pw; 653 654 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 655 sfp->f_fstypename); 656 657 flags = sfp->f_flags & MNT_VISFLAGMASK; 658 for (o = optnames; flags != 0 && o->o_opt != 0; o++) 659 if (flags & o->o_opt) { 660 (void)printf(", %s", o->o_name); 661 flags &= ~o->o_opt; 662 } 663 /* 664 * Inform when file system is mounted by an unprivileged user 665 * or privileged non-root user. 666 */ 667 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 668 (void)printf(", mounted by "); 669 if ((pw = getpwuid(sfp->f_owner)) != NULL) 670 (void)printf("%s", pw->pw_name); 671 else 672 (void)printf("%d", sfp->f_owner); 673 } 674 if (verbose) { 675 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 676 (void)printf(", writes: sync %ju async %ju", 677 (uintmax_t)sfp->f_syncwrites, 678 (uintmax_t)sfp->f_asyncwrites); 679 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 680 (void)printf(", reads: sync %ju async %ju", 681 (uintmax_t)sfp->f_syncreads, 682 (uintmax_t)sfp->f_asyncreads); 683 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 684 printf(", fsid "); 685 for (i = 0; i < sizeof(sfp->f_fsid); i++) 686 printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 687 } 688 } 689 (void)printf(")\n"); 690 } 691 692 struct statfs * 693 getmntpt(const char *name) 694 { 695 struct statfs *mntbuf; 696 int i, mntsize; 697 698 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 699 for (i = mntsize - 1; i >= 0; i--) { 700 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 701 strcmp(mntbuf[i].f_mntonname, name) == 0) 702 return (&mntbuf[i]); 703 } 704 return (NULL); 705 } 706 707 char * 708 catopt(char *s0, const char *s1) 709 { 710 char *cp; 711 712 if (s1 == NULL || *s1 == '\0') 713 return (s0); 714 715 if (s0 && *s0) { 716 if (asprintf(&cp, "%s,%s", s0, s1) == -1) 717 errx(1, "asprintf failed"); 718 } else 719 cp = strdup(s1); 720 721 if (s0) 722 free(s0); 723 return (cp); 724 } 725 726 void 727 mangle(char *options, struct cpa *a) 728 { 729 char *p, *s, *val; 730 731 for (s = options; (p = strsep(&s, ",")) != NULL;) 732 if (*p != '\0') { 733 if (strcmp(p, "noauto") == 0) { 734 /* 735 * Do not pass noauto option to nmount(). 736 * or external mount program. noauto is 737 * only used to prevent mounting a filesystem 738 * when 'mount -a' is specified, and is 739 * not a real mount option. 740 */ 741 continue; 742 } else if (strcmp(p, "late") == 0) { 743 /* 744 * "late" is used to prevent certain file 745 * systems from being mounted before late 746 * in the boot cycle; for instance, 747 * loopback NFS mounts can't be mounted 748 * before mountd starts. 749 */ 750 continue; 751 } else if (strcmp(p, "failok") == 0) { 752 /* 753 * "failok" is used to prevent certain file 754 * systems from being causing the system to 755 * drop into single user mode in the boot 756 * cycle, and is not a real mount option. 757 */ 758 continue; 759 } else if (strncmp(p, "mountprog", 9) == 0) { 760 /* 761 * "mountprog" is used to force the use of 762 * userland mount programs. 763 */ 764 val = strchr(p, '='); 765 if (val != NULL) { 766 ++val; 767 if (*val != '\0') 768 mountprog = strdup(val); 769 } 770 771 if (mountprog == NULL) { 772 errx(1, "Need value for -o mountprog"); 773 } 774 continue; 775 } else if (strcmp(p, "userquota") == 0) { 776 continue; 777 } else if (strncmp(p, userquotaeq, 778 sizeof(userquotaeq) - 1) == 0) { 779 continue; 780 } else if (strcmp(p, "groupquota") == 0) { 781 continue; 782 } else if (strncmp(p, groupquotaeq, 783 sizeof(groupquotaeq) - 1) == 0) { 784 continue; 785 } else if (*p == '-') { 786 append_arg(a, p); 787 p = strchr(p, '='); 788 if (p != NULL) { 789 *p = '\0'; 790 append_arg(a, p + 1); 791 } 792 } else { 793 append_arg(a, strdup("-o")); 794 append_arg(a, p); 795 } 796 } 797 } 798 799 800 char * 801 update_options(char *opts, char *fstab, int curflags) 802 { 803 char *o, *p; 804 char *cur; 805 char *expopt, *newopt, *tmpopt; 806 807 if (opts == NULL) 808 return (strdup("")); 809 810 /* remove meta options from list */ 811 remopt(fstab, MOUNT_META_OPTION_FSTAB); 812 remopt(fstab, MOUNT_META_OPTION_CURRENT); 813 cur = flags2opts(curflags); 814 815 /* 816 * Expand all meta-options passed to us first. 817 */ 818 expopt = NULL; 819 for (p = opts; (o = strsep(&p, ",")) != NULL;) { 820 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 821 expopt = catopt(expopt, fstab); 822 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 823 expopt = catopt(expopt, cur); 824 else 825 expopt = catopt(expopt, o); 826 } 827 free(cur); 828 free(opts); 829 830 /* 831 * Remove previous contradictory arguments. Given option "foo" we 832 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 833 * and "foo" - so we can deal with possible options like "notice". 834 */ 835 newopt = NULL; 836 for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 837 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 838 errx(1, "malloc failed"); 839 840 strcpy(tmpopt, "no"); 841 strcat(tmpopt, o); 842 remopt(newopt, tmpopt); 843 free(tmpopt); 844 845 if (strncmp("no", o, 2) == 0) 846 remopt(newopt, o+2); 847 848 newopt = catopt(newopt, o); 849 } 850 free(expopt); 851 852 return (newopt); 853 } 854 855 void 856 remopt(char *string, const char *opt) 857 { 858 char *o, *p, *r; 859 860 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 861 return; 862 863 r = string; 864 865 for (p = string; (o = strsep(&p, ",")) != NULL;) { 866 if (strcmp(opt, o) != 0) { 867 if (*r == ',' && *o != '\0') 868 r++; 869 while ((*r++ = *o++) != '\0') 870 ; 871 *--r = ','; 872 } 873 } 874 *r = '\0'; 875 } 876 877 void 878 usage(void) 879 { 880 881 (void)fprintf(stderr, "%s\n%s\n%s\n", 882 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 883 " mount [-dfpruvw] special | node", 884 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 885 exit(1); 886 } 887 888 void 889 putfsent(struct statfs *ent) 890 { 891 struct fstab *fst; 892 char *opts, *rw; 893 int l; 894 895 opts = NULL; 896 /* flags2opts() doesn't return the "rw" option. */ 897 if ((ent->f_flags & MNT_RDONLY) != 0) 898 rw = NULL; 899 else 900 rw = catopt(NULL, "rw"); 901 902 opts = flags2opts(ent->f_flags); 903 opts = catopt(rw, opts); 904 905 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 906 strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 907 strlcpy(ent->f_mntfromname, 908 (strnstr(ent->f_mntfromname, ":", 8) +1), 909 sizeof(ent->f_mntfromname)); 910 } 911 912 l = strlen(ent->f_mntfromname); 913 printf("%s%s%s%s", ent->f_mntfromname, 914 l < 8 ? "\t" : "", 915 l < 16 ? "\t" : "", 916 l < 24 ? "\t" : " "); 917 l = strlen(ent->f_mntonname); 918 printf("%s%s%s%s", ent->f_mntonname, 919 l < 8 ? "\t" : "", 920 l < 16 ? "\t" : "", 921 l < 24 ? "\t" : " "); 922 printf("%s\t", ent->f_fstypename); 923 l = strlen(opts); 924 printf("%s%s", opts, 925 l < 8 ? "\t" : " "); 926 free(opts); 927 928 if ((fst = getfsspec(ent->f_mntfromname))) 929 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 930 else if ((fst = getfsfile(ent->f_mntonname))) 931 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 932 else if (strcmp(ent->f_fstypename, "ufs") == 0) { 933 if (strcmp(ent->f_mntonname, "/") == 0) 934 printf("\t1 1\n"); 935 else 936 printf("\t2 2\n"); 937 } else 938 printf("\t0 0\n"); 939 } 940 941 942 char * 943 flags2opts(int flags) 944 { 945 char *res; 946 947 res = NULL; 948 949 if (flags & MNT_RDONLY) res = catopt(res, "ro"); 950 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 951 if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 952 if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 953 if (flags & MNT_UNION) res = catopt(res, "union"); 954 if (flags & MNT_ASYNC) res = catopt(res, "async"); 955 if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 956 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 957 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 958 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 959 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 960 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 961 if (flags & MNT_ACLS) res = catopt(res, "acls"); 962 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 963 964 return (res); 965 } 966