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