mount.c revision 52055
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/sbin/mount/mount.c 52055 1999-10-09 11:54:14Z phk $"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/mount.h> 50#include <sys/stat.h> 51#include <sys/wait.h> 52 53#include <err.h> 54#include <errno.h> 55#include <fstab.h> 56#include <pwd.h> 57#include <signal.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61#include <unistd.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 73char *catopt __P((char *, const char *)); 74struct statfs 75 *getmntpt __P((const char *)); 76int hasopt __P((const char *, const char *)); 77int ismounted __P((struct fstab *, struct statfs *, int)); 78int isremountable __P((const char *)); 79void mangle __P((char *, int *, const char **)); 80char *update_options __P((char *, char *, int)); 81int mountfs __P((const char *, const char *, const char *, 82 int, const char *, const char *)); 83void remopt __P((char *, const char *)); 84void prmount __P((struct statfs *)); 85void putfsent __P((const struct statfs *)); 86void usage __P((void)); 87char *flags2opts __P((int)); 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", "lfs", "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; 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 init_flags |= MNT_RDONLY; 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 init_flags &= ~MNT_RDONLY; 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 if ((mntbuf = getmntpt(*argv)) == NULL) 232 errx(1, 233 "unknown special file or file system %s", 234 *argv); 235 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) { 236 mntfromname = fs->fs_spec; 237 options = update_options(options, fs->fs_mntops, 238 mntbuf->f_flags); 239 } else { 240 mntfromname = mntbuf->f_mntfromname; 241 options = update_options(options, NULL, 242 mntbuf->f_flags); 243 } 244 rval = mountfs(mntbuf->f_fstypename, mntfromname, 245 mntbuf->f_mntonname, init_flags, options, 0); 246 break; 247 } 248 if ((fs = getfsfile(*argv)) == NULL && 249 (fs = getfsspec(*argv)) == NULL) 250 errx(1, "%s: unknown special file or file system", 251 *argv); 252 if (BADTYPE(fs->fs_type)) 253 errx(1, "%s has unknown file system type", 254 *argv); 255 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 256 init_flags, options, fs->fs_mntops); 257 break; 258 case 2: 259 /* 260 * If -t flag has not been specified, and spec contains either 261 * a ':' or a '@' then assume that an NFS filesystem is being 262 * specified ala Sun. 263 */ 264 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 265 vfstype = "nfs"; 266 rval = mountfs(vfstype, 267 argv[0], argv[1], init_flags, options, NULL); 268 break; 269 default: 270 usage(); 271 /* NOTREACHED */ 272 } 273 274 /* 275 * If the mount was successfully, and done by root, tell mountd the 276 * good news. Pid checks are probably unnecessary, but don't hurt. 277 */ 278 if (rval == 0 && getuid() == 0 && 279 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 280 if (fscanf(mountdfp, "%d", &pid) == 1 && 281 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 282 err(1, "signal mountd"); 283 (void)fclose(mountdfp); 284 } 285 286 exit(rval); 287} 288 289int 290ismounted(fs, mntbuf, mntsize) 291 struct fstab *fs; 292 struct statfs *mntbuf; 293 int mntsize; 294{ 295 int i; 296 297 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 298 /* the root file system can always be remounted */ 299 return (0); 300 301 for (i = mntsize - 1; i >= 0; --i) 302 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 && 303 (!isremountable(fs->fs_vfstype) || 304 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 305 return (1); 306 return (0); 307} 308 309int 310isremountable(vfsname) 311 const char *vfsname; 312{ 313 const char **cp; 314 315 for (cp = remountable_fs_names; *cp; cp++) 316 if (strcmp(*cp, vfsname) == 0) 317 return (1); 318 return (0); 319} 320 321int 322hasopt(mntopts, option) 323 const char *mntopts, *option; 324{ 325 int negative, found; 326 char *opt, *optbuf; 327 328 if (option[0] == 'n' && option[1] == 'o') { 329 negative = 1; 330 option += 2; 331 } else 332 negative = 0; 333 optbuf = strdup(mntopts); 334 found = 0; 335 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 336 if (opt[0] == 'n' && opt[1] == 'o') { 337 if (!strcasecmp(opt + 2, option)) 338 found = negative; 339 } else if (!strcasecmp(opt, option)) 340 found = !negative; 341 } 342 free(optbuf); 343 return (found); 344} 345 346int 347mountfs(vfstype, spec, name, flags, options, mntopts) 348 const char *vfstype, *spec, *name, *options, *mntopts; 349 int flags; 350{ 351 /* List of directories containing mount_xxx subcommands. */ 352 static const char *edirs[] = { 353 _PATH_SBIN, 354 _PATH_USRSBIN, 355 NULL 356 }; 357 const char *argv[100], **edir; 358 struct statfs sf; 359 pid_t pid; 360 int argc, i, status; 361 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 362 363#if __GNUC__ 364 (void)&optbuf; 365 (void)&name; 366#endif 367 368 /* resolve the mountpoint with realpath(3) */ 369 (void)checkpath(name, mntpath); 370 name = mntpath; 371 372 if (mntopts == NULL) 373 mntopts = ""; 374 if (options == NULL) { 375 if (*mntopts == '\0') { 376 options = "rw"; 377 } else { 378 options = mntopts; 379 mntopts = ""; 380 } 381 } 382 optbuf = catopt(strdup(mntopts), options); 383 384 if (strcmp(name, "/") == 0) 385 flags |= MNT_UPDATE; 386 if (flags & MNT_FORCE) 387 optbuf = catopt(optbuf, "force"); 388 if (flags & MNT_RDONLY) 389 optbuf = catopt(optbuf, "ro"); 390 /* 391 * XXX 392 * The mount_mfs (newfs) command uses -o to select the 393 * optimization mode. We don't pass the default "-o rw" 394 * for that reason. 395 */ 396 if (flags & MNT_UPDATE) 397 optbuf = catopt(optbuf, "update"); 398 399 argc = 0; 400 argv[argc++] = vfstype; 401 mangle(optbuf, &argc, argv); 402 argv[argc++] = spec; 403 argv[argc++] = name; 404 argv[argc] = NULL; 405 406 if (debug) { 407 (void)printf("exec: mount_%s", vfstype); 408 for (i = 1; i < argc; i++) 409 (void)printf(" %s", argv[i]); 410 (void)printf("\n"); 411 return (0); 412 } 413 414 switch (pid = fork()) { 415 case -1: /* Error. */ 416 warn("fork"); 417 free(optbuf); 418 return (1); 419 case 0: /* Child. */ 420 if (strcmp(vfstype, "ufs") == 0) 421 exit(mount_ufs(argc, (char * const *) argv)); 422 423 /* Go find an executable. */ 424 for (edir = edirs; *edir; edir++) { 425 (void)snprintf(execname, 426 sizeof(execname), "%s/mount_%s", *edir, vfstype); 427 execv(execname, (char * const *)argv); 428 } 429 if (errno == ENOENT) { 430 int len = 0; 431 char *cp; 432 for (edir = edirs; *edir; edir++) 433 len += strlen(*edir) + 2; /* ", " */ 434 if ((cp = malloc(len)) == NULL) 435 errx(1, "malloc failed"); 436 cp[0] = '\0'; 437 for (edir = edirs; *edir; edir++) { 438 strcat(cp, *edir); 439 if (edir[1] != NULL) 440 strcat(cp, ", "); 441 } 442 warn("exec mount_%s not found in %s", vfstype, cp); 443 } 444 exit(1); 445 /* NOTREACHED */ 446 default: /* Parent. */ 447 free(optbuf); 448 449 if (waitpid(pid, &status, 0) < 0) { 450 warn("waitpid"); 451 return (1); 452 } 453 454 if (WIFEXITED(status)) { 455 if (WEXITSTATUS(status) != 0) 456 return (WEXITSTATUS(status)); 457 } else if (WIFSIGNALED(status)) { 458 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 459 return (1); 460 } 461 462 if (verbose) { 463 if (statfs(name, &sf) < 0) { 464 warn("statfs %s", name); 465 return (1); 466 } 467 if (fstab_style) 468 putfsent(&sf); 469 else 470 prmount(&sf); 471 } 472 break; 473 } 474 475 return (0); 476} 477 478void 479prmount(sfp) 480 struct statfs *sfp; 481{ 482 int flags; 483 struct opt *o; 484 struct passwd *pw; 485 486 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 487 sfp->f_fstypename); 488 489 flags = sfp->f_flags & MNT_VISFLAGMASK; 490 for (o = optnames; flags && o->o_opt; o++) 491 if (flags & o->o_opt) { 492 (void)printf(", %s", o->o_name); 493 flags &= ~o->o_opt; 494 } 495 if (sfp->f_owner) { 496 (void)printf(", mounted by "); 497 if ((pw = getpwuid(sfp->f_owner)) != NULL) 498 (void)printf("%s", pw->pw_name); 499 else 500 (void)printf("%d", sfp->f_owner); 501 } 502 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) 503 (void)printf(", writes: sync %ld async %ld", 504 sfp->f_syncwrites, sfp->f_asyncwrites); 505 (void)printf(")\n"); 506} 507 508struct statfs * 509getmntpt(name) 510 const char *name; 511{ 512 struct statfs *mntbuf; 513 int i, mntsize; 514 515 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 516 for (i = 0; i < mntsize; i++) 517 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 518 strcmp(mntbuf[i].f_mntonname, name) == 0) 519 return (&mntbuf[i]); 520 return (NULL); 521} 522 523char * 524catopt(s0, s1) 525 char *s0; 526 const char *s1; 527{ 528 size_t i; 529 char *cp; 530 531 if (s1 == NULL || *s1 == '\0') 532 return s0; 533 534 if (s0 && *s0) { 535 i = strlen(s0) + strlen(s1) + 1 + 1; 536 if ((cp = malloc(i)) == NULL) 537 errx(1, "malloc failed"); 538 (void)snprintf(cp, i, "%s,%s", s0, s1); 539 } else 540 cp = strdup(s1); 541 542 if (s0) 543 free(s0); 544 return (cp); 545} 546 547void 548mangle(options, argcp, argv) 549 char *options; 550 int *argcp; 551 const char **argv; 552{ 553 char *p, *s; 554 int argc; 555 556 argc = *argcp; 557 for (s = options; (p = strsep(&s, ",")) != NULL;) 558 if (*p != '\0') { 559 if (*p == '-') { 560 argv[argc++] = p; 561 p = strchr(p, '='); 562 if (p) { 563 *p = '\0'; 564 argv[argc++] = p+1; 565 } 566 } else if (strcmp(p, "rw") != 0) { 567 argv[argc++] = "-o"; 568 argv[argc++] = p; 569 } 570 } 571 572 *argcp = argc; 573} 574 575 576char * 577update_options(opts, fstab, curflags) 578 char *opts; 579 char *fstab; 580 int curflags; 581{ 582 char *o, *p; 583 char *cur; 584 char *expopt, *newopt, *tmpopt; 585 586 if (opts == NULL) 587 return strdup(""); 588 589 if (fstab == NULL) 590 fstab = strdup(""); 591 592 /* remove meta options from list */ 593 remopt(fstab, MOUNT_META_OPTION_FSTAB); 594 remopt(fstab, MOUNT_META_OPTION_CURRENT); 595 cur = flags2opts(curflags); 596 597 /* 598 * Expand all meta-options passed to us first. 599 */ 600 expopt = NULL; 601 for (p = opts; (o = strsep(&p, ",")) != NULL;) { 602 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0) 603 expopt = catopt(expopt, fstab); 604 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0) 605 expopt = catopt(expopt, cur); 606 else 607 expopt = catopt(expopt, o); 608 } 609 if (fstab == "") 610 free(fstab); 611 free(cur); 612 free(opts); 613 614 /* 615 * Remove previous contradictory arguments. Given option "foo" we 616 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo" 617 * and "foo" - so we can deal with possible options like "notice". 618 */ 619 newopt = NULL; 620 for (p = expopt; (o = strsep(&p, ",")) != NULL;) { 621 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL) 622 errx(1, "malloc failed"); 623 624 strcpy(tmpopt, "no"); 625 strcat(tmpopt, o); 626 remopt(newopt, tmpopt); 627 free(tmpopt); 628 629 if (strncmp("no", o, 2) == 0) 630 remopt(newopt, o+2); 631 632 newopt = catopt(newopt, o); 633 } 634 free(expopt); 635 636 return newopt; 637} 638 639void 640remopt(string, opt) 641 char *string; 642 const char *opt; 643{ 644 char *o, *p, *r; 645 646 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0') 647 return; 648 649 r = string; 650 651 for (p = string; (o = strsep(&p, ",")) != NULL;) { 652 if (strcmp(opt, o) != 0) { 653 if (*r == ',' && *o != '\0') 654 r++; 655 while ((*r++ = *o++) != '\0') 656 ; 657 *--r = ','; 658 } 659 } 660 *r = '\0'; 661} 662 663void 664usage() 665{ 666 667 (void)fprintf(stderr, "%s\n%s\n%s\n", 668"usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node", 669" mount [-adfpruvw] [-t ufs | external_type]", 670" mount [-dfpruvw] special | node"); 671 exit(1); 672} 673 674void 675putfsent(ent) 676 const struct statfs *ent; 677{ 678 struct fstab *fst; 679 char *opts; 680 681 opts = flags2opts(ent->f_flags); 682 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname, 683 ent->f_fstypename, opts); 684 free(opts); 685 686 if ((fst = getfsspec(ent->f_mntfromname))) 687 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 688 else if ((fst = getfsfile(ent->f_mntonname))) 689 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 690 else if (strcmp(ent->f_fstypename, "ufs") == 0) 691 printf("\t1 1\n"); 692 else 693 printf("\t0 0\n"); 694} 695 696 697char * 698flags2opts(flags) 699 int flags; 700{ 701 char *res; 702 703 res = NULL; 704 705 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw"); 706 707 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync"); 708 if (flags & MNT_NOEXEC) res = catopt(res, "noexec"); 709 if (flags & MNT_NOSUID) res = catopt(res, "nosuid"); 710 if (flags & MNT_NODEV) res = catopt(res, "nodev"); 711 if (flags & MNT_UNION) res = catopt(res, "union"); 712 if (flags & MNT_ASYNC) res = catopt(res, "async"); 713 if (flags & MNT_NOATIME) res = catopt(res, "noatime"); 714 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr"); 715 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw"); 716 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow"); 717 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir"); 718 719 return res; 720} 721