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.23 (Berkeley) 04/26/95"; 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 **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 (badvfsname(mntbuf[i].f_fstypename, 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 rval = mountfs(mntbuf->f_fstypename, 169 mntbuf->f_mntfromname, mntbuf->f_mntonname, 170 init_flags, options, 0); 171 break; 172 } 173 if ((fs = getfsfile(*argv)) == NULL && 174 (fs = getfsspec(*argv)) == NULL) 175 errx(1, "%s: unknown special file or file system.", 176 *argv); 177 if (BADTYPE(fs->fs_type)) 178 errx(1, "%s has unknown file system type.", 179 *argv); 180 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 181 init_flags, options, fs->fs_mntops); 182 break; 183 case 2: 184 /* 185 * If -t flag has not been specified, and spec contains either 186 * a ':' or a '@' then assume that an NFS filesystem is being 187 * specified ala Sun. 188 */ 189 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 190 vfstype = "nfs"; 191 rval = mountfs(vfstype, 192 argv[0], argv[1], init_flags, options, NULL); 193 break; 194 default: 195 usage(); 196 /* NOTREACHED */ 197 } 198 199 /* 200 * If the mount was successfully, and done by root, tell mountd the 201 * good news. Pid checks are probably unnecessary, but don't hurt. 202 */ 203 if (rval == 0 && getuid() == 0 && 204 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 205 if (fscanf(mountdfp, "%ld", &pid) == 1 && 206 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 207 err(1, "signal mountd"); 208 (void)fclose(mountdfp); 209 } 210 211 exit(rval); 212 } 213 214 int 215 hasopt(mntopts, option) 216 const char *mntopts, *option; 217 { 218 int negative, found; 219 char *opt, *optbuf; 220 221 if (option[0] == 'n' && option[1] == 'o') { 222 negative = 1; 223 option += 2; 224 } else 225 negative = 0; 226 optbuf = strdup(mntopts); 227 found = 0; 228 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 229 if (opt[0] == 'n' && opt[1] == 'o') { 230 if (!strcasecmp(opt + 2, option)) 231 found = negative; 232 } else if (!strcasecmp(opt, option)) 233 found = !negative; 234 } 235 free(optbuf); 236 return (found); 237 } 238 239 int 240 mountfs(vfstype, spec, name, flags, options, mntopts) 241 const char *vfstype, *spec, *name, *options, *mntopts; 242 int flags; 243 { 244 /* List of directories containing mount_xxx subcommands. */ 245 static const char *edirs[] = { 246 _PATH_SBIN, 247 _PATH_USRSBIN, 248 NULL 249 }; 250 const char *argv[100], **edir; 251 struct statfs sf; 252 pid_t pid; 253 int argc, i, status; 254 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 255 256 if (realpath(name, mntpath) == NULL) { 257 warn("realpath %s", mntpath); 258 return (1); 259 } 260 261 name = mntpath; 262 263 if (mntopts == NULL) 264 mntopts = ""; 265 if (options == NULL) { 266 if (*mntopts == '\0') { 267 options = "rw"; 268 } else { 269 options = mntopts; 270 mntopts = ""; 271 } 272 } 273 optbuf = catopt(strdup(mntopts), options); 274 275 if (strcmp(name, "/") == 0) 276 flags |= MNT_UPDATE; 277 if (flags & MNT_FORCE) 278 optbuf = catopt(optbuf, "force"); 279 if (flags & MNT_RDONLY) 280 optbuf = catopt(optbuf, "ro"); 281 /* 282 * XXX 283 * The mount_mfs (newfs) command uses -o to select the 284 * optimisation mode. We don't pass the default "-o rw" 285 * for that reason. 286 */ 287 if (flags & MNT_UPDATE) 288 optbuf = catopt(optbuf, "update"); 289 290 argc = 0; 291 argv[argc++] = vfstype; 292 mangle(optbuf, &argc, argv); 293 argv[argc++] = spec; 294 argv[argc++] = name; 295 argv[argc] = NULL; 296 297 if (debug) { 298 (void)printf("exec: mount_%s", vfstype); 299 for (i = 1; i < argc; i++) 300 (void)printf(" %s", argv[i]); 301 (void)printf("\n"); 302 return (0); 303 } 304 305 switch (pid = vfork()) { 306 case -1: /* Error. */ 307 warn("vfork"); 308 free(optbuf); 309 return (1); 310 case 0: /* Child. */ 311 if (strcmp(vfstype, "ufs") == 0) 312 exit(mount_ufs(argc, (char * const *) argv)); 313 314 /* Go find an executable. */ 315 edir = edirs; 316 do { 317 (void)snprintf(execname, 318 sizeof(execname), "%s/mount_%s", *edir, vfstype); 319 execv(execname, (char * const *)argv); 320 if (errno != ENOENT) 321 warn("exec %s for %s", execname, name); 322 } while (*++edir != NULL); 323 324 if (errno == ENOENT) 325 warn("exec %s for %s", execname, name); 326 exit(1); 327 /* NOTREACHED */ 328 default: /* Parent. */ 329 free(optbuf); 330 331 if (waitpid(pid, &status, 0) < 0) { 332 warn("waitpid"); 333 return (1); 334 } 335 336 if (WIFEXITED(status)) { 337 if (WEXITSTATUS(status) != 0) 338 return (WEXITSTATUS(status)); 339 } else if (WIFSIGNALED(status)) { 340 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 341 return (1); 342 } 343 344 if (verbose) { 345 if (statfs(name, &sf) < 0) { 346 warn("statfs %s", name); 347 return (1); 348 } 349 prmount(&sf); 350 } 351 break; 352 } 353 354 return (0); 355 } 356 357 void 358 prmount(sfp) 359 struct statfs *sfp; 360 { 361 int flags; 362 struct opt *o; 363 struct passwd *pw; 364 int f; 365 366 (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname); 367 368 flags = sfp->f_flags & MNT_VISFLAGMASK; 369 for (f = 0, o = optnames; flags && o->o_opt; o++) 370 if (flags & o->o_opt) { 371 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 372 flags &= ~o->o_opt; 373 } 374 if (sfp->f_owner) { 375 (void)printf("%smounted by ", !f++ ? " (" : ", "); 376 if ((pw = getpwuid(sfp->f_owner)) != NULL) 377 (void)printf("%s", pw->pw_name); 378 else 379 (void)printf("%d", sfp->f_owner); 380 } 381 (void)printf(f ? ")\n" : "\n"); 382 } 383 384 struct statfs * 385 getmntpt(name) 386 const char *name; 387 { 388 struct statfs *mntbuf; 389 int i, mntsize; 390 391 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 392 for (i = 0; i < mntsize; i++) 393 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 394 strcmp(mntbuf[i].f_mntonname, name) == 0) 395 return (&mntbuf[i]); 396 return (NULL); 397 } 398 399 int 400 badvfsname(vfsname, vfslist) 401 const char *vfsname; 402 const char **vfslist; 403 { 404 405 if (vfslist == NULL) 406 return (0); 407 while (*vfslist != NULL) { 408 if (strcmp(vfsname, *vfslist) == 0) 409 return (skipvfs); 410 ++vfslist; 411 } 412 return (!skipvfs); 413 } 414 415 const char ** 416 makevfslist(fslist) 417 char *fslist; 418 { 419 const char **av; 420 int i; 421 char *nextcp; 422 423 if (fslist == NULL) 424 return (NULL); 425 if (fslist[0] == 'n' && fslist[1] == 'o') { 426 fslist += 2; 427 skipvfs = 1; 428 } 429 for (i = 0, nextcp = fslist; *nextcp; nextcp++) 430 if (*nextcp == ',') 431 i++; 432 if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 433 warn(NULL); 434 return (NULL); 435 } 436 nextcp = fslist; 437 i = 0; 438 av[i++] = nextcp; 439 while ((nextcp = strchr(nextcp, ',')) != NULL) { 440 *nextcp++ = '\0'; 441 av[i++] = nextcp; 442 } 443 av[i++] = NULL; 444 return (av); 445 } 446 447 char * 448 catopt(s0, s1) 449 char *s0; 450 const char *s1; 451 { 452 size_t i; 453 char *cp; 454 455 if (s0 && *s0) { 456 i = strlen(s0) + strlen(s1) + 1 + 1; 457 if ((cp = malloc(i)) == NULL) 458 err(1, NULL); 459 (void)snprintf(cp, i, "%s,%s", s0, s1); 460 } else 461 cp = strdup(s1); 462 463 if (s0) 464 free(s0); 465 return (cp); 466 } 467 468 void 469 mangle(options, argcp, argv) 470 char *options; 471 int *argcp; 472 const char **argv; 473 { 474 char *p, *s; 475 int argc; 476 477 argc = *argcp; 478 for (s = options; (p = strsep(&s, ",")) != NULL;) 479 if (*p != '\0') 480 if (*p == '-') { 481 argv[argc++] = p; 482 p = strchr(p, '='); 483 if (p) { 484 *p = '\0'; 485 argv[argc++] = p+1; 486 } 487 } else if (strcmp(p, "rw") != 0) { 488 argv[argc++] = "-o"; 489 argv[argc++] = p; 490 } 491 492 *argcp = argc; 493 } 494 495 void 496 usage() 497 { 498 499 (void)fprintf(stderr, 500 "usage: mount %s %s\n mount %s\n mount %s\n", 501 "[-dfruvw] [-o options] [-t ufs | external_type]", 502 "special node", 503 "[-adfruvw] [-t ufs | external_type]", 504 "[-dfruvw] special | node"); 505 exit(1); 506 } 507