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