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