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