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 warnx( 594 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); 595 vfstype = "msdosfs"; 596 } 597 598 /* Construct the name of the appropriate mount command */ 599 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); 600 601 mnt_argv.c = -1; 602 append_arg(&mnt_argv, execname); 603 mangle(optbuf, &mnt_argv); 604 if (mountprog != NULL) 605 strlcpy(execname, mountprog, sizeof(execname)); 606 607 append_arg(&mnt_argv, strdup(spec)); 608 append_arg(&mnt_argv, strdup(name)); 609 append_arg(&mnt_argv, NULL); 610 611 if (debug) { 612 if (use_mountprog(vfstype)) 613 printf("exec: %s", execname); 614 else 615 printf("mount -t %s", vfstype); 616 for (i = 1; i < mnt_argv.c; i++) 617 (void)printf(" %s", mnt_argv.a[i]); 618 (void)printf("\n"); 619 free(optbuf); 620 free(mountprog); 621 mountprog = NULL; 622 return (0); 623 } 624 625 if (use_mountprog(vfstype)) { 626 ret = exec_mountprog(name, execname, mnt_argv.a); 627 } else { 628 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); 629 } 630 631 free(optbuf); 632 free(mountprog); 633 mountprog = NULL; 634 635 if (verbose) { 636 if (statfs(name, &sf) < 0) { 637 warn("statfs %s", name); 638 return (1); 639 } 640 if (fstab_style) 641 putfsent(&sf); 642 else 643 prmount(&sf); 644 } 645 646 return (ret); 647 } 648 649 void 650 prmount(struct statfs *sfp) 651 { 652 uint64_t flags; 653 unsigned int i; 654 struct opt *o; 655 struct passwd *pw; 656 657 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 658 sfp->f_fstypename); 659 660 flags = sfp->f_flags & MNT_VISFLAGMASK; 661 for (o = optnames; flags != 0 && o->o_opt != 0; o++) 662 if (flags & o->o_opt) { 663 (void)printf(", %s", o->o_name); 664 flags &= ~o->o_opt; 665 } 666 /* 667 * Inform when file system is mounted by an unprivileged user 668 * or privileged non-root user. 669 */ 670 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) { 671 (void)printf(", mounted by "); 672 if ((pw = getpwuid(sfp->f_owner)) != NULL) 673 (void)printf("%s", pw->pw_name); 674 else 675 (void)printf("%d", sfp->f_owner); 676 } 677 if (verbose) { 678 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 679 (void)printf(", writes: sync %ju async %ju", 680 (uintmax_t)sfp->f_syncwrites, 681 (uintmax_t)sfp->f_asyncwrites); 682 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0) 683 (void)printf(", reads: sync %ju async %ju", 684 (uintmax_t)sfp->f_syncreads, 685 (uintmax_t)sfp->f_asyncreads); 686 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) { 687 printf(", fsid "); 688 for (i = 0; i < sizeof(sfp->f_fsid); i++) 689 printf("%02x", ((u_char *)&sfp->f_fsid)[i]); 690 } 691 } 692 (void)printf(")\n"); 693 } 694 695 struct statfs * 696 getmntpt(const char *name) 697 { 698 struct statfs *mntbuf; 699 int i, mntsize; 700 701 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 702 for (i = mntsize - 1; i >= 0; i--) { 703 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 704 strcmp(mntbuf[i].f_mntonname, name) == 0) 705 return (&mntbuf[i]); 706 } 707 return (NULL); 708 } 709 710 char * 711 catopt(char *s0, const char *s1) 712 { 713 char *cp; 714 715 if (s1 == NULL || *s1 == '\0') 716 return (s0); 717 718 if (s0 && *s0) { 719 if (asprintf(&cp, "%s,%s", s0, s1) == -1) 720 errx(1, "asprintf failed"); 721 } else 722 cp = strdup(s1); 723 724 if (s0) 725 free(s0); 726 return (cp); 727 } 728 729 void 730 mangle(char *options, struct cpa *a) 731 { 732 char *p, *s, *val; 733 734 for (s = options; (p = strsep(&s, ",")) != NULL;) 735 if (*p != '\0') { 736 if (strcmp(p, "noauto") == 0) { 737 /* 738 * Do not pass noauto option to nmount(). 739 * or external mount program. noauto is 740 * only used to prevent mounting a filesystem 741 * when 'mount -a' is specified, and is 742 * not a real mount option. 743 */ 744 continue; 745 } else if (strcmp(p, "late") == 0) { 746 /* 747 * "late" is used to prevent certain file 748 * systems from being mounted before late 749 * in the boot cycle; for instance, 750 * loopback NFS mounts can't be mounted 751 * before mountd starts. 752 */ 753 continue; 754 } else if (strcmp(p, "failok") == 0) { 755 /* 756 * "failok" is used to prevent certain file 757 * systems from being causing the system to 758 * drop into single user mode in the boot 759 * cycle, and is not a real mount option. 760 */ 761 continue; 762 } else if (strncmp(p, "mountprog", 9) == 0) { 763 /* 764 * "mountprog" is used to force the use of 765 * userland mount programs. 766 */ 767 val = strchr(p, '='); 768 if (val != NULL) { 769 ++val; 770 if (*val != '\0') 771 mountprog = strdup(val); 772 } 773 774 if (mountprog == NULL) { 775 errx(1, "Need value for -o mountprog"); 776 } 777 continue; 778 } else if (strcmp(p, "userquota") == 0) { 779 continue; 780 } else if (strncmp(p, userquotaeq, 781 sizeof(userquotaeq) - 1) == 0) { 782 continue; 783 } else if (strcmp(p, "groupquota") == 0) { 784 continue; 785 } else if (strncmp(p, groupquotaeq, 786 sizeof(groupquotaeq) - 1) == 0) { 787 continue; 788 } else if (*p == '-') { 789 append_arg(a, p); 790 p = strchr(p, '='); 791 if (p != NULL) { 792 *p = '\0'; 793 append_arg(a, p + 1); 794 } 795 } else { 796 append_arg(a, strdup("-o")); 797 append_arg(a, p); 798 } 799 } 800 } 801 802 803 char * 804 update_options(char *opts, char *fstab, int curflags) 805 { 806 char *o, *p; 807 char *cur; 808 char *expopt, *newopt, *tmpopt; 809 810 if (opts == NULL) 811 return (strdup("")); 812 813 /* remove meta options from list */ 814 remopt(fstab, MOUNT_META_OPTION_FSTAB); 815 remopt(fstab, MOUNT_META_OPTION_CURRENT); 816 cur = flags2opts(curflags); 817 818 /* 819 * Expand all meta-options passed to us first. 820 */ 821 expopt = NULL; 822 for (p = opts; (o = strsep(&p, ",")) != NULL;) { 823 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 824 expopt = catopt(expopt, fstab); 825 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 826 expopt = catopt(expopt, cur); 827 else 828 expopt = catopt(expopt, o); 829 } 830 free(cur); 831 free(opts); 832 833 /* 834 * Remove previous contradictory arguments. Given option "foo" we 835 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 836 * and "foo" - so we can deal with possible options like "notice". 837 */ 838 newopt = NULL; 839 for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 840 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 841 errx(1, "malloc failed"); 842 843 strcpy(tmpopt, "no"); 844 strcat(tmpopt, o); 845 remopt(newopt, tmpopt); 846 free(tmpopt); 847 848 if (strncmp("no", o, 2) == 0) 849 remopt(newopt, o+2); 850 851 newopt = catopt(newopt, o); 852 } 853 free(expopt); 854 855 return (newopt); 856 } 857 858 void 859 remopt(char *string, const char *opt) 860 { 861 char *o, *p, *r; 862 863 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 864 return; 865 866 r = string; 867 868 for (p = string; (o = strsep(&p, ",")) != NULL;) { 869 if (strcmp(opt, o) != 0) { 870 if (*r == ',' && *o != '\0') 871 r++; 872 while ((*r++ = *o++) != '\0') 873 ; 874 *--r = ','; 875 } 876 } 877 *r = '\0'; 878 } 879 880 void 881 usage(void) 882 { 883 884 (void)fprintf(stderr, "%s\n%s\n%s\n", 885 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]", 886 " mount [-dfpruvw] special | node", 887 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node"); 888 exit(1); 889 } 890 891 void 892 putfsent(struct statfs *ent) 893 { 894 struct fstab *fst; 895 char *opts, *rw; 896 int l; 897 898 opts = NULL; 899 /* flags2opts() doesn't return the "rw" option. */ 900 if ((ent->f_flags & MNT_RDONLY) != 0) 901 rw = NULL; 902 else 903 rw = catopt(NULL, "rw"); 904 905 opts = flags2opts(ent->f_flags); 906 opts = catopt(rw, opts); 907 908 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 || 909 strncmp(ent->f_mntfromname, "<above>", 7) == 0) { 910 strlcpy(ent->f_mntfromname, 911 (strnstr(ent->f_mntfromname, ":", 8) +1), 912 sizeof(ent->f_mntfromname)); 913 } 914 915 l = strlen(ent->f_mntfromname); 916 printf("%s%s%s%s", ent->f_mntfromname, 917 l < 8 ? "\t" : "", 918 l < 16 ? "\t" : "", 919 l < 24 ? "\t" : " "); 920 l = strlen(ent->f_mntonname); 921 printf("%s%s%s%s", ent->f_mntonname, 922 l < 8 ? "\t" : "", 923 l < 16 ? "\t" : "", 924 l < 24 ? "\t" : " "); 925 printf("%s\t", ent->f_fstypename); 926 l = strlen(opts); 927 printf("%s%s", opts, 928 l < 8 ? "\t" : " "); 929 free(opts); 930 931 if ((fst = getfsspec(ent->f_mntfromname))) 932 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 933 else if ((fst = getfsfile(ent->f_mntonname))) 934 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 935 else if (strcmp(ent->f_fstypename, "ufs") == 0) { 936 if (strcmp(ent->f_mntonname, "/") == 0) 937 printf("\t1 1\n"); 938 else 939 printf("\t2 2\n"); 940 } else 941 printf("\t0 0\n"); 942 } 943 944 945 char * 946 flags2opts(int flags) 947 { 948 char *res; 949 950 res = NULL; 951 952 if (flags & MNT_RDONLY) res = catopt(res, "ro"); 953 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 954 if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 955 if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 956 if (flags & MNT_UNION) res = catopt(res, "union"); 957 if (flags & MNT_ASYNC) res = catopt(res, "async"); 958 if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 959 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 960 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 961 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 962 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 963 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel"); 964 if (flags & MNT_ACLS) res = catopt(res, "acls"); 965 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls"); 966 967 return (res); 968 } 969