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