1 /* 2 * Copyright (c) 1980, 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)mount.c 5.51 (Berkeley) 11/15/92"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/file.h> 20 #include <sys/time.h> 21 #include <sys/wait.h> 22 #include <sys/errno.h> 23 #include <sys/signal.h> 24 #include <sys/ucred.h> 25 #include <sys/mount.h> 26 #include <fstab.h> 27 #include <string.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include "pathnames.h" 31 32 #define DEFAULT_ROOTUID -2 33 34 #define BADTYPE(type) \ 35 (strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \ 36 strcmp(type, FSTAB_RQ)) 37 #define SETTYPE(type) \ 38 (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ)) 39 40 int debug, force, verbose, updateflg, mnttype; 41 char *mntname, **envp; 42 char **vfslist, **makevfslist(); 43 static void prmount(); 44 45 main(argc, argv, arge) 46 int argc; 47 char **argv; 48 char **arge; 49 { 50 extern char *optarg; 51 extern int optind; 52 register struct fstab *fs; 53 int all, ch, rval, flags, ret, pid, i; 54 long mntsize; 55 struct statfs *mntbuf, *getmntpt(); 56 char *type, *options = NULL; 57 FILE *pidfile; 58 59 envp = arge; 60 all = 0; 61 type = NULL; 62 mnttype = MOUNT_UFS; 63 mntname = "ufs"; 64 while ((ch = getopt(argc, argv, "adfrwuvt:o:")) != EOF) 65 switch((char)ch) { 66 case 'a': 67 all = 1; 68 break; 69 case 'd': 70 debug = 1; 71 break; 72 case 'f': 73 force = 1; 74 break; 75 case 'r': 76 type = FSTAB_RO; 77 break; 78 case 'u': 79 updateflg = MNT_UPDATE; 80 break; 81 case 'v': 82 verbose = 1; 83 break; 84 case 'w': 85 type = FSTAB_RW; 86 break; 87 case 'o': 88 options = optarg; 89 break; 90 case 't': 91 vfslist = makevfslist(optarg); 92 mnttype = getmnttype(optarg); 93 break; 94 case '?': 95 default: 96 usage(); 97 /* NOTREACHED */ 98 } 99 argc -= optind; 100 argv += optind; 101 102 /* NOSTRICT */ 103 104 if (all) { 105 rval = 0; 106 while (fs = getfsent()) { 107 if (BADTYPE(fs->fs_type)) 108 continue; 109 if (badvfsname(fs->fs_vfstype, vfslist)) 110 continue; 111 /* `/' is special, it's always mounted */ 112 if (!strcmp(fs->fs_file, "/")) 113 flags = MNT_UPDATE; 114 else 115 flags = updateflg; 116 mnttype = getmnttype(fs->fs_vfstype); 117 rval |= mountfs(fs->fs_spec, fs->fs_file, flags, 118 type, options, fs->fs_mntops); 119 } 120 exit(rval); 121 } 122 123 if (argc == 0) { 124 if (verbose || debug || type) 125 usage(); 126 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) { 127 (void) fprintf(stderr, 128 "mount: cannot get mount information\n"); 129 exit(1); 130 } 131 for (i = 0; i < mntsize; i++) { 132 if (badvfstype(mntbuf[i].f_type, vfslist)) 133 continue; 134 prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname, 135 mntbuf[i].f_flags); 136 } 137 exit(0); 138 } 139 140 if (argc == 1 && updateflg) { 141 if ((mntbuf = getmntpt(*argv)) == NULL) { 142 (void) fprintf(stderr, 143 "mount: unknown special file or file system %s.\n", 144 *argv); 145 exit(1); 146 } 147 mnttype = mntbuf->f_type; 148 if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) { 149 (void) fprintf(stderr, 150 "mount: can't find fstab entry for %s.\n", *argv); 151 exit(1); 152 } 153 mntname = fs->fs_vfstype; 154 155 /* 156 * Default type to fstab version if none specified on the 157 * command line. 158 */ 159 if (type == NULL) 160 type = fs->fs_type; 161 162 /* 163 * Default options to fstab version if none specified on the 164 * command line. 165 */ 166 if (options == NULL) 167 options = fs->fs_mntops; 168 else { 169 register char *cp; 170 171 /* 172 * Concat the two strings with the command line 173 * options last so that they will override the 174 * fstab options. 175 */ 176 i = strlen(fs->fs_mntops) + strlen(options) + 2; 177 if ((cp = malloc((size_t)i)) == NULL) { 178 (void) fprintf(stderr, 179 "mount: -u malloc failed\n"); 180 exit(1); 181 } 182 sprintf(cp, "%s,%s", fs->fs_mntops, options); 183 options = cp; 184 } 185 ret = mountfs(fs->fs_spec, mntbuf->f_mntonname, 186 updateflg, type, options, (char *)NULL); 187 } else if (argc == 1) { 188 if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 189 (void) fprintf(stderr, 190 "mount: unknown special file or file system %s.\n", 191 *argv); 192 exit(1); 193 } 194 if (BADTYPE(fs->fs_type)) { 195 (void) fprintf(stderr, 196 "mount: %s has unknown file system type.\n", *argv); 197 exit(1); 198 } 199 mnttype = getmnttype(fs->fs_vfstype); 200 ret = mountfs(fs->fs_spec, fs->fs_file, updateflg, 201 type, options, fs->fs_mntops); 202 } else if (argc != 2) { 203 usage(); 204 ret = 1; 205 } else { 206 /* 207 * If -t flag has not been specified, and spec 208 * contains either a ':' or a '@' then assume that 209 * an NFS filesystem is being specified ala Sun. 210 */ 211 if (vfslist == (char **)0 && 212 (index(argv[0], ':') || index(argv[0], '@'))) { 213 mnttype = MOUNT_NFS; 214 mntname = "nfs"; 215 } 216 ret = mountfs(argv[0], argv[1], updateflg, type, options, 217 (char *)NULL); 218 } 219 if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 220 pid = 0; 221 fscanf(pidfile, "%d", &pid); 222 fclose(pidfile); 223 if (pid > 0) 224 kill(pid, SIGHUP); 225 } 226 exit (ret); 227 } 228 229 mountfs(spec, name, flags, type, options, mntopts) 230 char *spec, *name, *type, *options, *mntopts; 231 int flags; 232 { 233 union wait status; 234 pid_t pid; 235 int argc, i; 236 struct ufs_args args; 237 char *argp, *argv[50]; 238 char execname[MAXPATHLEN + 1], flagval[12]; 239 240 if (mntopts) 241 getstdopts(mntopts, &flags); 242 if (options) 243 getstdopts(options, &flags); 244 if (type) 245 getstdopts(type, &flags); 246 if (force) 247 flags |= MNT_FORCE; 248 switch (mnttype) { 249 case MOUNT_LFS: 250 case MOUNT_UFS: 251 if (mntopts) 252 getufsopts(mntopts, &flags); 253 if (options) 254 getufsopts(options, &flags); 255 args.fspec = spec; 256 args.exroot = DEFAULT_ROOTUID; 257 if (flags & MNT_RDONLY) 258 args.exflags = MNT_EXRDONLY; 259 else 260 args.exflags = 0; 261 argp = (caddr_t)&args; 262 break; 263 264 case MOUNT_MFS: 265 case MOUNT_NFS: 266 default: 267 argv[0] = mntname; 268 argc = 1; 269 if (flags) { 270 argv[argc++] = "-F"; 271 sprintf(flagval, "%d", flags); 272 argv[argc++] = flagval; 273 } 274 if (mntopts) 275 argc += getexecopts(mntopts, &argv[argc]); 276 if (options) 277 argc += getexecopts(options, &argv[argc]); 278 argv[argc++] = spec; 279 argv[argc++] = name; 280 argv[argc++] = NULL; 281 sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname); 282 if (verbose) { 283 (void)printf("exec: %s", execname); 284 for (i = 1; i < argc - 1; i++) 285 (void)printf(" %s", argv[i]); 286 (void)printf("\n"); 287 } 288 if (debug) 289 break; 290 if (pid = vfork()) { 291 if (pid == -1) { 292 perror("mount: vfork starting file system"); 293 return (1); 294 } 295 if (waitpid(pid, (int *)&status, 0) != -1 && 296 WIFEXITED(status) && 297 WEXITSTATUS(status) != 0) 298 return (WEXITSTATUS(status)); 299 spec = mntname; 300 goto out; 301 } 302 execve(execname, argv, envp); 303 (void) fprintf(stderr, "mount: cannot exec %s for %s: ", 304 execname, name); 305 perror((char *)NULL); 306 exit (1); 307 /* NOTREACHED */ 308 309 } 310 if (!debug && mount(mnttype, name, flags, argp)) { 311 (void) fprintf(stderr, "%s on %s: ", spec, name); 312 switch (errno) { 313 case EMFILE: 314 (void) fprintf(stderr, "Mount table full\n"); 315 break; 316 case EINVAL: 317 if (flags & MNT_UPDATE) 318 (void) fprintf(stderr, "Specified device %s\n", 319 "does not match mounted device"); 320 else if (mnttype == MOUNT_UFS) 321 (void) fprintf(stderr, "Bogus super block\n"); 322 else 323 perror((char *)NULL); 324 break; 325 default: 326 perror((char *)NULL); 327 break; 328 } 329 return(1); 330 } 331 332 out: 333 if (verbose) 334 prmount(spec, name, flags); 335 336 return(0); 337 } 338 339 static void 340 prmount(spec, name, flags) 341 char *spec, *name; 342 register short flags; 343 { 344 register int first; 345 346 (void)printf("%s on %s", spec, name); 347 if (!(flags & MNT_VISFLAGMASK)) { 348 (void)printf("\n"); 349 return; 350 } 351 first = 0; 352 #define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg) 353 if (flags & MNT_RDONLY) 354 PR("read-only"); 355 if (flags & MNT_NOEXEC) 356 PR("noexec"); 357 if (flags & MNT_NOSUID) 358 PR("nosuid"); 359 if (flags & MNT_NODEV) 360 PR("nodev"); 361 if (flags & MNT_SYNCHRONOUS) 362 PR("synchronous"); 363 if (flags & MNT_QUOTA) 364 PR("with quotas"); 365 if (flags & MNT_LOCAL) 366 PR("local"); 367 if (flags & MNT_UNION) 368 PR("union"); 369 if (flags & MNT_EXPORTED) 370 PR("NFS exported"); 371 (void)printf(")\n"); 372 } 373 374 getmnttype(fstype) 375 char *fstype; 376 { 377 378 mntname = fstype; 379 if (!strcmp(fstype, "ufs")) 380 return (MOUNT_UFS); 381 if (!strcmp(fstype, "nfs")) 382 return (MOUNT_NFS); 383 if (!strcmp(fstype, "mfs")) 384 return (MOUNT_MFS); 385 if (!strcmp(fstype, "lfs")) 386 return (MOUNT_LFS); 387 return (0); 388 } 389 390 usage() 391 { 392 393 (void) fprintf(stderr, 394 "usage:\n mount %s %s\n mount %s\n mount %s\n", 395 "[ -frwu ] [ -t ufs | external_type ]", 396 "[ -o options ] special node", 397 "[ -afrwu ] [ -t ufs | external_type ]", 398 "[ -frwu ] special | node"); 399 exit(1); 400 } 401 402 getstdopts(options, flagp) 403 char *options; 404 int *flagp; 405 { 406 register char *opt; 407 int negative; 408 char optbuf[BUFSIZ]; 409 410 (void)strcpy(optbuf, options); 411 for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) { 412 if (opt[0] == 'n' && opt[1] == 'o') { 413 negative++; 414 opt += 2; 415 } else { 416 negative = 0; 417 } 418 if (!negative && !strcasecmp(opt, FSTAB_RO)) { 419 *flagp |= MNT_RDONLY; 420 continue; 421 } 422 if (!negative && !strcasecmp(opt, FSTAB_RW)) { 423 *flagp &= ~MNT_RDONLY; 424 continue; 425 } 426 if (!strcasecmp(opt, "exec")) { 427 if (negative) 428 *flagp |= MNT_NOEXEC; 429 else 430 *flagp &= ~MNT_NOEXEC; 431 continue; 432 } 433 if (!strcasecmp(opt, "suid")) { 434 if (negative) 435 *flagp |= MNT_NOSUID; 436 else 437 *flagp &= ~MNT_NOSUID; 438 continue; 439 } 440 if (!strcasecmp(opt, "dev")) { 441 if (negative) 442 *flagp |= MNT_NODEV; 443 else 444 *flagp &= ~MNT_NODEV; 445 continue; 446 } 447 if (!strcasecmp(opt, "synchronous")) { 448 if (!negative) 449 *flagp |= MNT_SYNCHRONOUS; 450 else 451 *flagp &= ~MNT_SYNCHRONOUS; 452 continue; 453 } 454 if (!strcasecmp(opt, "union")) { 455 if (!negative) 456 *flagp |= MNT_UNION; 457 else 458 *flagp &= ~MNT_UNION; 459 continue; 460 } 461 } 462 } 463 464 /* ARGSUSED */ 465 getufsopts(options, flagp) 466 char *options; 467 int *flagp; 468 { 469 return; 470 } 471 472 getexecopts(options, argv) 473 char *options; 474 char **argv; 475 { 476 register int argc = 0; 477 register char *opt; 478 479 for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) { 480 if (opt[0] != '-') 481 continue; 482 argv[argc++] = opt; 483 if (opt[2] == '\0' || opt[2] != '=') 484 continue; 485 opt[2] = '\0'; 486 argv[argc++] = &opt[3]; 487 } 488 return (argc); 489 } 490 491 struct statfs * 492 getmntpt(name) 493 char *name; 494 { 495 long mntsize; 496 register long i; 497 struct statfs *mntbuf; 498 499 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 500 for (i = 0; i < mntsize; i++) { 501 if (!strcmp(mntbuf[i].f_mntfromname, name) || 502 !strcmp(mntbuf[i].f_mntonname, name)) 503 return (&mntbuf[i]); 504 } 505 return ((struct statfs *)0); 506 } 507 508 static int skipvfs; 509 510 badvfstype(vfstype, vfslist) 511 short vfstype; 512 char **vfslist; 513 { 514 515 if (vfslist == 0) 516 return(0); 517 while (*vfslist) { 518 if (vfstype == getmnttype(*vfslist)) 519 return(skipvfs); 520 vfslist++; 521 } 522 return (!skipvfs); 523 } 524 525 badvfsname(vfsname, vfslist) 526 char *vfsname; 527 char **vfslist; 528 { 529 530 if (vfslist == 0) 531 return(0); 532 while (*vfslist) { 533 if (strcmp(vfsname, *vfslist) == 0) 534 return(skipvfs); 535 vfslist++; 536 } 537 return (!skipvfs); 538 } 539 540 char ** 541 makevfslist(fslist) 542 char *fslist; 543 { 544 register char **av, *nextcp; 545 register int i; 546 547 if (fslist == NULL) 548 return (NULL); 549 if (fslist[0] == 'n' && fslist[1] == 'o') { 550 fslist += 2; 551 skipvfs = 1; 552 } 553 for (i = 0, nextcp = fslist; *nextcp; nextcp++) 554 if (*nextcp == ',') 555 i++; 556 av = (char **)malloc((size_t)(i+2) * sizeof(char *)); 557 if (av == NULL) 558 return (NULL); 559 nextcp = fslist; 560 i = 0; 561 av[i++] = nextcp; 562 while (nextcp = index(nextcp, ',')) { 563 *nextcp++ = '\0'; 564 av[i++] = nextcp; 565 } 566 av[i++] = 0; 567 return (av); 568 } 569