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