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