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