mount.c revision 35105
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#else 44static const char rcsid[] = 45 "$Id: mount.c,v 1.24 1998/03/27 10:52:13 peter Exp $"; 46#endif 47#endif /* not lint */ 48 49#include <sys/param.h> 50#include <sys/mount.h> 51#include <sys/stat.h> 52#include <sys/wait.h> 53 54#include <err.h> 55#include <errno.h> 56#include <fstab.h> 57#include <pwd.h> 58#include <signal.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <unistd.h> 63 64#include "extern.h" 65#include "pathnames.h" 66 67int debug, fstab_style, verbose; 68 69char *catopt __P((char *, const char *)); 70struct statfs 71 *getmntpt __P((const char *)); 72int hasopt __P((const char *, const char *)); 73int ismounted __P((struct fstab *, struct statfs *, int)); 74int isremountable __P((const char *)); 75void mangle __P((char *, int *, const char **)); 76int mountfs __P((const char *, const char *, const char *, 77 int, const char *, const char *)); 78void prmount __P((struct statfs *)); 79void putfsent __P((const struct statfs *)); 80void usage __P((void)); 81 82/* Map from mount otions to printable formats. */ 83static struct opt { 84 int o_opt; 85 const char *o_name; 86} optnames[] = { 87 { MNT_ASYNC, "asynchronous" }, 88 { MNT_EXPORTED, "NFS exported" }, 89 { MNT_LOCAL, "local" }, 90 { MNT_NOATIME, "noatime" }, 91 { MNT_NODEV, "nodev" }, 92 { MNT_NOEXEC, "noexec" }, 93 { MNT_NOSUID, "nosuid" }, 94 { MNT_NOSYMFOLLOW, "nosymfollow" }, 95 { MNT_QUOTA, "with quotas" }, 96 { MNT_RDONLY, "read-only" }, 97 { MNT_SYNCHRONOUS, "synchronous" }, 98 { MNT_UNION, "union" }, 99 { MNT_NOCLUSTERR, "noclusterr" }, 100 { MNT_NOCLUSTERW, "noclusterw" }, 101 { MNT_SUIDDIR, "suiddir" }, 102 { MNT_SOFTDEP, "soft-updates" }, 103 { NULL } 104}; 105 106/* 107 * List of VFS types that can be remounted without becoming mounted on top 108 * of each other. 109 * XXX Is this list correct? 110 */ 111static const char * 112remountable_fs_names[] = { 113 "ufs", "ffs", "lfs", "ext2fs", 114 0 115}; 116 117int 118main(argc, argv) 119 int argc; 120 char * const argv[]; 121{ 122 const char *mntfromname, **vfslist, *vfstype; 123 struct fstab *fs; 124 struct statfs *mntbuf; 125 FILE *mountdfp; 126 pid_t pid; 127 int all, ch, i, init_flags, mntsize, rval; 128 char *options; 129 130 all = init_flags = 0; 131 options = NULL; 132 vfslist = NULL; 133 vfstype = "ufs"; 134 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1) 135 switch (ch) { 136 case 'a': 137 all = 1; 138 break; 139 case 'd': 140 debug = 1; 141 break; 142 case 'f': 143 init_flags |= MNT_FORCE; 144 break; 145 case 'o': 146 if (*optarg) 147 options = catopt(options, optarg); 148 break; 149 case 'p': 150 fstab_style = 1; 151 verbose = 1; 152 break; 153 case 'r': 154 init_flags |= MNT_RDONLY; 155 break; 156 case 't': 157 if (vfslist != NULL) 158 errx(1, "only one -t option may be specified."); 159 vfslist = makevfslist(optarg); 160 vfstype = optarg; 161 break; 162 case 'u': 163 init_flags |= MNT_UPDATE; 164 break; 165 case 'v': 166 verbose = 1; 167 break; 168 case 'w': 169 init_flags &= ~MNT_RDONLY; 170 break; 171 case '?': 172 default: 173 usage(); 174 /* NOTREACHED */ 175 } 176 argc -= optind; 177 argv += optind; 178 179#define BADTYPE(type) \ 180 (strcmp(type, FSTAB_RO) && \ 181 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 182 183 rval = 0; 184 switch (argc) { 185 case 0: 186 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 187 err(1, "getmntinfo"); 188 if (all) { 189 while ((fs = getfsent()) != NULL) { 190 if (BADTYPE(fs->fs_type)) 191 continue; 192 if (checkvfsname(fs->fs_vfstype, vfslist)) 193 continue; 194 if (hasopt(fs->fs_mntops, "noauto")) 195 continue; 196 if (ismounted(fs, mntbuf, mntsize)) 197 continue; 198 if (mountfs(fs->fs_vfstype, fs->fs_spec, 199 fs->fs_file, init_flags, options, 200 fs->fs_mntops)) 201 rval = 1; 202 } 203 } else if (fstab_style) { 204 for (i = 0; i < mntsize; i++) { 205 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 206 continue; 207 putfsent(&mntbuf[i]); 208 } 209 } else { 210 for (i = 0; i < mntsize; i++) { 211 if (checkvfsname(mntbuf[i].f_fstypename, 212 vfslist)) 213 continue; 214 prmount(&mntbuf[i]); 215 } 216 } 217 exit(rval); 218 case 1: 219 if (vfslist != NULL) 220 usage(); 221 222 if (init_flags & MNT_UPDATE) { 223 if ((mntbuf = getmntpt(*argv)) == NULL) 224 errx(1, 225 "unknown special file or file system %s.", 226 *argv); 227 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) 228 mntfromname = fs->fs_spec; 229 else 230 mntfromname = mntbuf->f_mntfromname; 231 rval = mountfs(mntbuf->f_fstypename, mntfromname, 232 mntbuf->f_mntonname, init_flags, options, 0); 233 break; 234 } 235 if ((fs = getfsfile(*argv)) == NULL && 236 (fs = getfsspec(*argv)) == NULL) 237 errx(1, "%s: unknown special file or file system.", 238 *argv); 239 if (BADTYPE(fs->fs_type)) 240 errx(1, "%s has unknown file system type.", 241 *argv); 242 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 243 init_flags, options, fs->fs_mntops); 244 break; 245 case 2: 246 /* 247 * If -t flag has not been specified, and spec contains either 248 * a ':' or a '@' then assume that an NFS filesystem is being 249 * specified ala Sun. 250 */ 251 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 252 vfstype = "nfs"; 253 rval = mountfs(vfstype, 254 argv[0], argv[1], init_flags, options, NULL); 255 break; 256 default: 257 usage(); 258 /* NOTREACHED */ 259 } 260 261 /* 262 * If the mount was successfully, and done by root, tell mountd the 263 * good news. Pid checks are probably unnecessary, but don't hurt. 264 */ 265 if (rval == 0 && getuid() == 0 && 266 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 267 if (fscanf(mountdfp, "%d", &pid) == 1 && 268 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 269 err(1, "signal mountd"); 270 (void)fclose(mountdfp); 271 } 272 273 exit(rval); 274} 275 276int 277ismounted(fs, mntbuf, mntsize) 278 struct fstab *fs; 279 struct statfs *mntbuf; 280 int mntsize; 281{ 282 int i; 283 284 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0') 285 /* the root file system can always be remounted */ 286 return (0); 287 288 for (i = mntsize - 1; i >= 0; --i) 289 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 && 290 (!isremountable(fs->fs_vfstype) || 291 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)) 292 return (1); 293 return (0); 294} 295 296int 297isremountable(vfsname) 298 const char *vfsname; 299{ 300 const char **cp; 301 302 for (cp = remountable_fs_names; *cp; cp++) 303 if (strcmp(*cp, vfsname) == 0) 304 return (1); 305 return (0); 306} 307 308int 309hasopt(mntopts, option) 310 const char *mntopts, *option; 311{ 312 int negative, found; 313 char *opt, *optbuf; 314 315 if (option[0] == 'n' && option[1] == 'o') { 316 negative = 1; 317 option += 2; 318 } else 319 negative = 0; 320 optbuf = strdup(mntopts); 321 found = 0; 322 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 323 if (opt[0] == 'n' && opt[1] == 'o') { 324 if (!strcasecmp(opt + 2, option)) 325 found = negative; 326 } else if (!strcasecmp(opt, option)) 327 found = !negative; 328 } 329 free(optbuf); 330 return (found); 331} 332 333int 334mountfs(vfstype, spec, name, flags, options, mntopts) 335 const char *vfstype, *spec, *name, *options, *mntopts; 336 int flags; 337{ 338 /* List of directories containing mount_xxx subcommands. */ 339 static const char *edirs[] = { 340 _PATH_SBIN, 341 _PATH_USRSBIN, 342 NULL 343 }; 344 const char *argv[100], **edir; 345 struct stat sb; 346 struct statfs sf; 347 pid_t pid; 348 int argc, i, status; 349 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 350 351#if __GNUC__ 352 (void)&optbuf; 353 (void)&name; 354#endif 355 356 if (realpath(name, mntpath) != NULL && stat(mntpath, &sb) == 0) { 357 if (!S_ISDIR(sb.st_mode)) { 358 warnx("%s: Not a directory", mntpath); 359 return (1); 360 } 361 } else { 362 warn("%s", mntpath); 363 return (1); 364 } 365 366 name = mntpath; 367 368 if (mntopts == NULL) 369 mntopts = ""; 370 if (options == NULL) { 371 if (*mntopts == '\0') { 372 options = "rw"; 373 } else { 374 options = mntopts; 375 mntopts = ""; 376 } 377 } 378 optbuf = catopt(strdup(mntopts), options); 379 380 if (strcmp(name, "/") == 0) 381 flags |= MNT_UPDATE; 382 if (flags & MNT_FORCE) 383 optbuf = catopt(optbuf, "force"); 384 if (flags & MNT_RDONLY) 385 optbuf = catopt(optbuf, "ro"); 386 /* 387 * XXX 388 * The mount_mfs (newfs) command uses -o to select the 389 * optimisation mode. We don't pass the default "-o rw" 390 * for that reason. 391 */ 392 if (flags & MNT_UPDATE) 393 optbuf = catopt(optbuf, "update"); 394 395 argc = 0; 396 argv[argc++] = vfstype; 397 mangle(optbuf, &argc, argv); 398 argv[argc++] = spec; 399 argv[argc++] = name; 400 argv[argc] = NULL; 401 402 if (debug) { 403 (void)printf("exec: mount_%s", vfstype); 404 for (i = 1; i < argc; i++) 405 (void)printf(" %s", argv[i]); 406 (void)printf("\n"); 407 return (0); 408 } 409 410 switch (pid = fork()) { 411 case -1: /* Error. */ 412 warn("fork"); 413 free(optbuf); 414 return (1); 415 case 0: /* Child. */ 416 if (strcmp(vfstype, "ufs") == 0) 417 exit(mount_ufs(argc, (char * const *) argv)); 418 419 /* Go find an executable. */ 420 for (edir = edirs; *edir; edir++) { 421 (void)snprintf(execname, 422 sizeof(execname), "%s/mount_%s", *edir, vfstype); 423 execv(execname, (char * const *)argv); 424 } 425 if (errno == ENOENT) { 426 int len = 0; 427 char *cp; 428 for (edir = edirs; *edir; edir++) 429 len += strlen(*edir) + 2; /* ", " */ 430 if ((cp = malloc(len)) == NULL) { 431 warn(NULL); 432 exit(1); 433 } 434 cp[0] = '\0'; 435 for (edir = edirs; *edir; edir++) { 436 strcat(cp, *edir); 437 if (edir[1] != NULL) 438 strcat(cp, ", "); 439 } 440 warn("exec mount_%s not found in %s", vfstype, cp); 441 } 442 exit(1); 443 /* NOTREACHED */ 444 default: /* Parent. */ 445 free(optbuf); 446 447 if (waitpid(pid, &status, 0) < 0) { 448 warn("waitpid"); 449 return (1); 450 } 451 452 if (WIFEXITED(status)) { 453 if (WEXITSTATUS(status) != 0) 454 return (WEXITSTATUS(status)); 455 } else if (WIFSIGNALED(status)) { 456 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 457 return (1); 458 } 459 460 if (verbose) { 461 if (statfs(name, &sf) < 0) { 462 warn("statfs %s", name); 463 return (1); 464 } 465 if (fstab_style) 466 putfsent(&sf); 467 else 468 prmount(&sf); 469 } 470 break; 471 } 472 473 return (0); 474} 475 476void 477prmount(sfp) 478 struct statfs *sfp; 479{ 480 int flags; 481 struct opt *o; 482 struct passwd *pw; 483 int f; 484 485 (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname); 486 487 flags = sfp->f_flags & MNT_VISFLAGMASK; 488 for (f = 0, o = optnames; flags && o->o_opt; o++) 489 if (flags & o->o_opt) { 490 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 491 flags &= ~o->o_opt; 492 } 493 if (sfp->f_owner) { 494 (void)printf("%smounted by ", !f++ ? " (" : ", "); 495 if ((pw = getpwuid(sfp->f_owner)) != NULL) 496 (void)printf("%s", pw->pw_name); 497 else 498 (void)printf("%d", sfp->f_owner); 499 } 500 if ((sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0) && 501 (sfp->f_flags & MNT_RDONLY) == 0) 502 (void)printf("%swrites: sync %d async %d)", 503 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites); 504 (void)printf("%s\n", f ? ")" : ""); 505} 506 507struct statfs * 508getmntpt(name) 509 const char *name; 510{ 511 struct statfs *mntbuf; 512 int i, mntsize; 513 514 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 515 for (i = 0; i < mntsize; i++) 516 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 517 strcmp(mntbuf[i].f_mntonname, name) == 0) 518 return (&mntbuf[i]); 519 return (NULL); 520} 521 522char * 523catopt(s0, s1) 524 char *s0; 525 const char *s1; 526{ 527 size_t i; 528 char *cp; 529 530 if (s0 && *s0) { 531 i = strlen(s0) + strlen(s1) + 1 + 1; 532 if ((cp = malloc(i)) == NULL) 533 err(1, NULL); 534 (void)snprintf(cp, i, "%s,%s", s0, s1); 535 } else 536 cp = strdup(s1); 537 538 if (s0) 539 free(s0); 540 return (cp); 541} 542 543void 544mangle(options, argcp, argv) 545 char *options; 546 int *argcp; 547 const char **argv; 548{ 549 char *p, *s; 550 int argc; 551 552 argc = *argcp; 553 for (s = options; (p = strsep(&s, ",")) != NULL;) 554 if (*p != '\0') 555 if (*p == '-') { 556 argv[argc++] = p; 557 p = strchr(p, '='); 558 if (p) { 559 *p = '\0'; 560 argv[argc++] = p+1; 561 } 562 } else if (strcmp(p, "rw") != 0) { 563 argv[argc++] = "-o"; 564 argv[argc++] = p; 565 } 566 567 *argcp = argc; 568} 569 570void 571usage() 572{ 573 574 (void)fprintf(stderr, 575 "usage: mount %s %s\n mount %s\n mount %s\n", 576 "[-dfpruvw] [-o options] [-t ufs | external_type]", 577 "special node", 578 "[-adfpruvw] [-t ufs | external_type]", 579 "[-dfpruvw] special | node"); 580 exit(1); 581} 582 583void 584putfsent(ent) 585 const struct statfs *ent; 586{ 587 struct fstab *fst; 588 589 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname, 590 ent->f_fstypename, (ent->f_flags & MNT_RDONLY) ? "ro" : "rw"); 591 592 /* XXX should use optnames[] - put shorter names in it. */ 593 if (ent->f_flags & MNT_SYNCHRONOUS) 594 printf(",sync"); 595 if (ent->f_flags & MNT_NOEXEC) 596 printf(",noexec"); 597 if (ent->f_flags & MNT_NOSUID) 598 printf(",nosuid"); 599 if (ent->f_flags & MNT_NODEV) 600 printf(",nodev"); 601 if (ent->f_flags & MNT_UNION) 602 printf(",union"); 603 if (ent->f_flags & MNT_ASYNC) 604 printf(",async"); 605 if (ent->f_flags & MNT_NOATIME) 606 printf(",noatime"); 607 if (ent->f_flags & MNT_NOCLUSTERR) 608 printf(",noclusterr"); 609 if (ent->f_flags & MNT_NOCLUSTERW) 610 printf(",noclusterw"); 611 if (ent->f_flags & MNT_NOSYMFOLLOW) 612 printf (",nosymfollow"); 613 if (ent->f_flags & MNT_SUIDDIR) 614 printf(",suiddir"); 615 616 if ((fst = getfsspec(ent->f_mntfromname))) 617 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 618 else if ((fst = getfsfile(ent->f_mntonname))) 619 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno); 620 else if (strcmp(ent->f_fstypename, "ufs") == 0) 621 printf("\t1 1\n"); 622 else 623 printf("\t0 0\n"); 624} 625