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