1/* 2 * Copyright (c) 1999-2009 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23/*- 24 * Copyright (c) 1980, 1989, 1993, 1994 25 * The Regents of the University of California. All rights reserved. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 3. All advertising materials mentioning features or use of this software 36 * must display the following acknowledgement: 37 * This product includes software developed by the University of 38 * California, Berkeley and its contributors. 39 * 4. Neither the name of the University nor the names of its contributors 40 * may be used to endorse or promote products derived from this software 41 * without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 */ 55 56 57#include <sys/param.h> 58#include <sys/mount.h> 59#include <sys/wait.h> 60 61#include <err.h> 62#include <errno.h> 63#include <fstab.h> 64#include <pwd.h> 65#include <signal.h> 66#include <stdio.h> 67#include <stdlib.h> 68#include <string.h> 69#include <unistd.h> 70 71#include "pathnames.h" 72 73int debug, verbose; 74 75int checkvfsname __P((const char *, const char **)); 76char *catopt __P((char *, const char *)); 77struct statfs *getmntpt __P((const char *)); 78int hasopt __P((const char *, const char *)); 79int ismounted __P((const char *, const char *)); 80const char 81 **makevfslist __P((char *)); 82void mangle __P((char *, int *, const char **)); 83int mountfs __P((const char *, const char *, const char *, 84 int, const char *, const char *)); 85void prmount __P((struct statfs *)); 86void usage __P((void)); 87 88/* Map from mount otions to printable formats. */ 89static struct opt { 90 int o_opt; 91 const char *o_name; 92} optnames[] = { 93 { MNT_ASYNC, "asynchronous" }, 94 { MNT_EXPORTED, "NFS exported" }, 95 { MNT_LOCAL, "local" }, 96 { MNT_NODEV, "nodev" }, 97 { MNT_NOEXEC, "noexec" }, 98 { MNT_NOSUID, "nosuid" }, 99 { MNT_QUOTA, "with quotas" }, 100 { MNT_RDONLY, "read-only" }, 101 { MNT_SYNCHRONOUS, "synchronous" }, 102 { MNT_UNION, "union" }, 103 { MNT_AUTOMOUNTED, "automounted" }, 104 { MNT_JOURNALED, "journaled" }, 105 { MNT_DEFWRITE, "defwrite" }, 106 { MNT_IGNORE_OWNERSHIP, "noowners" }, 107 { MNT_NOATIME, "noatime" }, 108 { MNT_QUARANTINE, "quarantine" }, 109 { MNT_DONTBROWSE, "nobrowse"}, 110 { MNT_CPROTECT, "protect"}, 111 { 0, NULL } 112}; 113 114 115int 116main(argc, argv) 117 int argc; 118 char * const argv[]; 119{ 120 const char *mntfromname, **vfslist, *vfstype; 121 struct fstab *fs; 122 struct statfs *mntbuf; 123 int all, ch, i, init_flags, mntsize, rval; 124 char *options; 125 126 all = init_flags = 0; 127 options = NULL; 128 vfslist = NULL; 129 vfstype = NULL; 130 while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF) 131 switch (ch) { 132 case 'a': 133 all = 1; 134 break; 135 case 'd': 136 debug = 1; 137 break; 138 case 'f': 139 init_flags |= MNT_FORCE; 140 break; 141 case 'o': 142 if (*optarg) { 143 options = catopt(options, optarg); 144 if (strstr(optarg, "union")) 145 init_flags |= MNT_UNION; 146 } 147 break; 148 case 'r': 149 init_flags |= MNT_RDONLY; 150 break; 151 case 't': 152 if (vfslist != NULL) 153 errx(1, "only one -t option may be specified."); 154 vfslist = makevfslist(optarg); 155 vfstype = optarg; 156 break; 157 case 'u': 158 init_flags |= MNT_UPDATE; 159 break; 160 case 'v': 161 verbose = 1; 162 break; 163 case 'w': 164 init_flags &= ~MNT_RDONLY; 165 break; 166 case '?': 167 default: 168 usage(); 169 /* NOTREACHED */ 170 } 171 argc -= optind; 172 argv += optind; 173 174#define BADTYPE(type) \ 175 (strcmp(type, FSTAB_RO) && \ 176 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 177 178 rval = 0; 179 switch (argc) { 180 case 0: 181 if (all) { 182 setfsent(); 183 while ((fs = getfsent()) != NULL) { 184 if (BADTYPE(fs->fs_type)) 185 continue; 186 if (checkvfsname(fs->fs_vfstype, vfslist)) 187 continue; 188 if (hasopt(fs->fs_mntops, "noauto")) 189 continue; 190 if (!strcmp(fs->fs_vfstype, "nfs")) { 191 if (hasopt(fs->fs_mntops, "net")) 192 continue; 193 /* check if already mounted */ 194 if (fs->fs_spec == NULL || 195 fs->fs_file == NULL || 196 ismounted(fs->fs_spec, fs->fs_file)) 197 continue; 198 } 199 if (mountfs(fs->fs_vfstype, fs->fs_spec, 200 fs->fs_file, init_flags, options, 201 fs->fs_mntops)) 202 rval = 1; 203 } 204 endfsent(); 205 } else { 206 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 207 err(1, "getmntinfo"); 208 for (i = 0; i < mntsize; i++) { 209 if (checkvfsname(mntbuf[i].f_fstypename, vfslist)) 210 continue; 211 prmount(&mntbuf[i]); 212 } 213 } 214 exit(rval); 215 case 1: 216 if (vfslist != NULL) 217 usage(); 218 219 if (init_flags & MNT_UPDATE) { 220 if ((mntbuf = getmntpt(*argv)) == NULL) 221 errx(1, 222 "unknown special file or file system %s.", 223 *argv); 224 /* 225 * Handle the special case of upgrading the root file 226 * system from read-only to read/write. The root file 227 * system was originally mounted with a "mount from" name 228 * of "root_device". The getfsfile("/") returns non- 229 * NULL at this point, with fs_spec set to the true 230 * path to the root device (regardless of what /etc/fstab 231 * contains). 232 */ 233 mntfromname = mntbuf->f_mntfromname; 234 if (strchr(mntfromname, '/') == NULL) { 235 fs = getfsfile(mntbuf->f_mntonname); 236 if (fs != NULL) 237 mntfromname = fs->fs_spec; 238 } 239 240 /* Do the update mount */ 241 rval = mountfs(mntbuf->f_fstypename, mntfromname, 242 mntbuf->f_mntonname, init_flags, options, 0); 243 break; 244 } 245 if ((fs = getfsfile(*argv)) == NULL && 246 (fs = getfsspec(*argv)) == NULL) 247 errx(1, "%s: unknown special file or file system.", 248 *argv); 249 if (BADTYPE(fs->fs_type)) 250 errx(1, "%s has unknown file system type.", 251 *argv); 252 if (!strcmp(fs->fs_vfstype, "nfs")) { 253 if (hasopt(fs->fs_mntops, "net")) 254 errx(1, "%s is owned by the automounter.", *argv); 255 if (ismounted(fs->fs_spec, fs->fs_file)) 256 errx(1, "%s is already mounted at %s.", 257 fs->fs_spec, fs->fs_file); 258 } 259 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 260 init_flags, options, fs->fs_mntops); 261 break; 262 case 2: 263 /* 264 * If -t flag has not been specified, and spec contains a ':' 265 * then assume that an NFS filesystem is being specified. 266 */ 267 if (vfslist == NULL && strchr(argv[0], ':') != NULL) { 268 vfstype = "nfs"; 269 /* check if already mounted */ 270 if (ismounted(argv[0], argv[1])) 271 errx(1, "%s is already mounted at %s.", 272 argv[0], argv[1]); 273 } 274 275 /* If we have both a devnode and a pathname, and an update mount was requested, 276 * then figure out where the devnode is mounted. We will need to run 277 * an update mount on its path. It wouldn't make sense to do an 278 * update mount on a path other than the one it's already using. 279 * 280 * XXX: Should we implement the same workaround for updating the 281 * root file system at boot time? 282 */ 283 if (init_flags & MNT_UPDATE) { 284 if ((mntbuf = getmntpt(*argv)) == NULL) 285 errx(1,"unknown special file or file system %s.", *argv); 286 rval = mountfs(mntbuf->f_fstypename, mntbuf->f_mntfromname, 287 mntbuf->f_mntonname, init_flags, options, 0); 288 } 289 else { 290 /* 291 * If update mount not requested, then go with the vfstype and arguments 292 * specified. If no vfstype specified, then error out. 293 */ 294 if (vfstype == NULL) { 295 errx (1, "You must specify a filesystem type with -t."); 296 } 297 rval = mountfs(vfstype, 298 argv[0], argv[1], init_flags, options, NULL); 299 } 300 break; 301 default: 302 usage(); 303 /* NOTREACHED */ 304 } 305 306 exit(rval); 307} 308 309int 310hasopt(mntopts, option) 311 const char *mntopts, *option; 312{ 313 int negative, found; 314 char *opt, *optbuf; 315 316 if (option[0] == 'n' && option[1] == 'o') { 317 negative = 1; 318 option += 2; 319 } else 320 negative = 0; 321 optbuf = strdup(mntopts); 322 found = 0; 323 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 324 if (opt[0] == 'n' && opt[1] == 'o') { 325 if (!strcasecmp(opt + 2, option)) 326 found = negative; 327 } else if (!strcasecmp(opt, option)) 328 found = !negative; 329 } 330 free(optbuf); 331 return (found); 332} 333 334int 335ismounted(fs_spec, fs_file) 336 const char *fs_spec, *fs_file; 337{ 338 int i, mntsize; 339 struct statfs *mntbuf; 340 341 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 342 err(1, "getmntinfo"); 343 for (i = 0; i < mntsize; i++) { 344 if (strcmp(mntbuf[i].f_mntfromname, fs_spec)) 345 continue; 346 if (strcmp(mntbuf[i].f_mntonname, fs_file)) 347 continue; 348 return 1; 349 } 350 return 0; 351} 352 353int 354mountfs(vfstype, spec, name, flags, options, mntopts) 355 const char *vfstype, *spec, *name, *options, *mntopts; 356 int flags; 357{ 358 /* List of directories containing mount_xxx subcommands. */ 359 static const char *edirs[] = { 360 _PATH_SBIN, 361 _PATH_USRSBIN, 362 _PATH_FSBNDL, /* always last */ 363 NULL 364 }; 365 const char *argv[100], **edir; 366 struct statfs sf; 367 pid_t pid; 368 int argc, i, status; 369 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 370 371 if (realpath(name, mntpath) == NULL) { 372 warn("realpath %s", mntpath); 373 return (1); 374 } 375 376 name = mntpath; 377 378 if (mntopts == NULL) 379 mntopts = ""; 380 if (options == NULL) { 381 if (*mntopts == '\0') { 382 options = ""; 383 } else { 384 options = mntopts; 385 mntopts = ""; 386 } 387 } 388 optbuf = catopt(strdup(mntopts), options); 389 390 if ((strcmp(name, "/") == 0) && !(flags & MNT_UNION)) 391 flags |= MNT_UPDATE; 392 if (flags & MNT_FORCE) 393 optbuf = catopt(optbuf, "force"); 394 if (flags & MNT_RDONLY) 395 optbuf = catopt(optbuf, "ro"); 396 if (flags & MNT_UPDATE) 397 optbuf = catopt(optbuf, "update"); 398 399 argc = 0; 400 argv[argc++] = vfstype; 401 mangle(optbuf, &argc, argv); 402 argv[argc++] = spec; 403 argv[argc++] = name; 404 argv[argc] = NULL; 405 406 if (debug) { 407 (void)printf("exec: mount_%s", vfstype); 408 for (i = 1; i < argc; i++) 409 (void)printf(" %s", argv[i]); 410 (void)printf("\n"); 411 return (0); 412 } 413 414 switch (pid = fork()) { 415 case -1: /* Error. */ 416 warn("fork"); 417 free(optbuf); 418 return (1); 419 case 0: /* Child. */ 420 /* Go find an executable. */ 421 edir = edirs; 422 do { 423 /* Special case file system bundle executable path */ 424 if (*(edir+1) == NULL) 425 (void)snprintf(execname, sizeof(execname), 426 "%s/%s.fs/%s/mount_%s", *edir, 427 vfstype, _PATH_FSBNDLBIN, vfstype); 428 else 429 (void)snprintf(execname, sizeof(execname), 430 "%s/mount_%s", *edir, vfstype); 431 432 argv[0] = execname; 433 execv(execname, (char * const *)argv); 434 if (errno != ENOENT) 435 warn("exec %s for %s", execname, name); 436 } while (*++edir != NULL); 437 438 if (errno == ENOENT) 439 warn("exec %s for %s", execname, name); 440 exit(1); 441 /* NOTREACHED */ 442 default: /* Parent. */ 443 free(optbuf); 444 445 if (waitpid(pid, &status, 0) < 0) { 446 warn("waitpid"); 447 return (1); 448 } 449 450 if (WIFEXITED(status)) { 451 if (WEXITSTATUS(status) != 0) 452 return (WEXITSTATUS(status)); 453 } else if (WIFSIGNALED(status)) { 454 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 455 return (1); 456 } 457 458 if (verbose) { 459 if (statfs(name, &sf) < 0) { 460 warn("statfs %s", name); 461 return (1); 462 } 463 prmount(&sf); 464 } 465 break; 466 } 467 468 return (0); 469} 470 471void 472prmount(sfp) 473 struct statfs *sfp; 474{ 475 int flags; 476 struct opt *o; 477 struct passwd *pw; 478 479 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname, 480 sfp->f_fstypename); 481 482 flags = sfp->f_flags & MNT_VISFLAGMASK; 483 for (o = optnames; flags && o->o_opt; o++) 484 if (flags & o->o_opt) { 485 (void)printf(", %s", o->o_name); 486 flags &= ~o->o_opt; 487 } 488 if (sfp->f_owner) { 489 (void)printf(", mounted by "); 490 if ((pw = getpwuid(sfp->f_owner)) != NULL) 491 (void)printf("%s", pw->pw_name); 492 else 493 (void)printf("%d", sfp->f_owner); 494 } 495 (void)printf(")\n"); 496} 497 498struct statfs * 499getmntpt(name) 500 const char *name; 501{ 502 struct statfs *mntbuf; 503 int i, mntsize; 504 505 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 506 for (i = 0; i < mntsize; i++) { 507 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 508 strcmp(mntbuf[i].f_mntonname, name) == 0) 509 return (&mntbuf[i]); 510 } 511 return (NULL); 512} 513 514char * 515catopt(s0, s1) 516 char *s0; 517 const char *s1; 518{ 519 size_t i; 520 char *cp; 521 522 if (s0 && *s0) { 523 i = strlen(s0) + strlen(s1) + 1 + 1; 524 if ((cp = malloc(i)) == NULL) 525 err(1, NULL); 526 (void)snprintf(cp, i, "%s,%s", s0, s1); 527 } else 528 cp = strdup(s1); 529 530 if (s0) 531 free(s0); 532 return (cp); 533} 534 535void 536mangle(options, argcp, argv) 537 char *options; 538 int *argcp; 539 const char **argv; 540{ 541 char *p, *s; 542 int argc; 543 544 argc = *argcp; 545 for (s = options; (p = strsep(&s, ",")) != NULL;) 546 if (*p != '\0') { 547 if (*p == '-') { 548 argv[argc++] = p; 549 p = strchr(p, '='); 550 if (p) { 551 *p = '\0'; 552 argv[argc++] = p+1; 553 } 554 } else { 555 argv[argc++] = "-o"; 556 argv[argc++] = p; 557 } 558 } 559 560 *argcp = argc; 561} 562 563void 564usage() 565{ 566 567 (void)fprintf(stderr, 568 "usage: mount %s %s\n mount %s\n mount %s\n", 569 "[-dfruvw] [-o options] [-t external_type]", 570 "special node", 571 "[-adfruvw] [-t external_type]", 572 "[-dfruvw] special | node"); 573 exit(1); 574} 575