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