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