1 /*- 2 * Copyright (c) 1980, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1989, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/mount.h> 50 51 #include <netdb.h> 52 #include <rpc/rpc.h> 53 #include <nfs/rpcv2.h> 54 55 #include <err.h> 56 #include <fstab.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0') 63 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0') 64 65 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat; 66 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat; 67 68 int fflag, vflag; 69 char *nfshost; 70 71 void checkmntlist (char *, char **, char **, char **); 72 int checkvfsname (const char *, char **); 73 char *getmntname (const char *, const char *, 74 mntwhat, char **, dowhat); 75 char *getrealname(char *, char *resolved_path); 76 char **makevfslist (const char *); 77 size_t mntinfo (struct statfs **); 78 int namematch (struct hostent *); 79 int selected (int); 80 int umountall (char **); 81 int umountfs (char *, char **); 82 void usage (void); 83 int xdr_dir (XDR *, char *); 84 85 int 86 main(int argc, char *argv[]) 87 { 88 int all, errs, ch, mntsize; 89 char **typelist = NULL, *mntonname, *mntfromname; 90 char *type, *mntfromnamerev, *mntonnamerev; 91 struct statfs *mntbuf; 92 93 /* Start disks transferring immediately. */ 94 sync(); 95 96 all = errs = 0; 97 while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1) 98 switch (ch) { 99 case 'A': 100 all = 2; 101 break; 102 case 'a': 103 all = 1; 104 break; 105 case 'f': 106 fflag = MNT_FORCE; 107 break; 108 case 'h': /* -h implies -A. */ 109 all = 2; 110 nfshost = optarg; 111 break; 112 case 't': 113 if (typelist != NULL) 114 err(1, "only one -t option may be specified"); 115 typelist = makevfslist(optarg); 116 break; 117 case 'v': 118 vflag = 1; 119 break; 120 default: 121 usage(); 122 /* NOTREACHED */ 123 } 124 argc -= optind; 125 argv += optind; 126 127 if ((argc == 0 && !all) || (argc != 0 && all)) 128 usage(); 129 130 /* -h implies "-t nfs" if no -t flag. */ 131 if ((nfshost != NULL) && (typelist == NULL)) 132 typelist = makevfslist("nfs"); 133 134 switch (all) { 135 case 2: 136 if ((mntsize = mntinfo(&mntbuf)) <= 0) 137 break; 138 /* 139 * We unmount the nfs-mounts in the reverse order 140 * that they were mounted. 141 */ 142 for (errs = 0, mntsize--; mntsize > 0; mntsize--) { 143 if (checkvfsname(mntbuf[mntsize].f_fstypename, 144 typelist)) 145 continue; 146 /* 147 * Check if a mountpoint is laid over by another mount. 148 * A warning will be printed to stderr if this is 149 * the case. The laid over mount remains unmounted. 150 */ 151 mntonname = mntbuf[mntsize].f_mntonname; 152 mntfromname = mntbuf[mntsize].f_mntfromname; 153 mntonnamerev = getmntname(getmntname(mntonname, 154 NULL, MNTFROM, &type, NAME), NULL, 155 MNTON, &type, NAME); 156 157 mntfromnamerev = getmntname(mntonnamerev, 158 NULL, MNTFROM, &type, NAME); 159 160 if (strcmp(mntonnamerev, mntonname) == 0 && 161 strcmp(mntfromnamerev, mntfromname ) != 0) 162 warnx("cannot umount %s, %s\n " 163 "is mounted there, umount it first", 164 mntonname, mntfromnamerev); 165 166 if (umountfs(mntbuf[mntsize].f_mntonname, 167 typelist) != 0) 168 errs = 1; 169 } 170 free(mntbuf); 171 break; 172 case 1: 173 if (setfsent() == 0) 174 err(1, "%s", _PATH_FSTAB); 175 errs = umountall(typelist); 176 break; 177 case 0: 178 for (errs = 0; *argv != NULL; ++argv) 179 if (umountfs(*argv, typelist) != 0) 180 errs = 1; 181 break; 182 } 183 (void)getmntname(NULL, NULL, NOTHING, NULL, FREE); 184 exit(errs); 185 } 186 187 int 188 umountall(char **typelist) 189 { 190 struct vfsconf vfc; 191 struct fstab *fs; 192 int rval; 193 char *cp; 194 static int firstcall = 1; 195 196 if ((fs = getfsent()) != NULL) 197 firstcall = 0; 198 else if (firstcall) 199 errx(1, "fstab reading failure"); 200 else 201 return (0); 202 do { 203 /* Ignore the root. */ 204 if (strcmp(fs->fs_file, "/") == 0) 205 continue; 206 /* 207 * !!! 208 * Historic practice: ignore unknown FSTAB_* fields. 209 */ 210 if (strcmp(fs->fs_type, FSTAB_RW) && 211 strcmp(fs->fs_type, FSTAB_RO) && 212 strcmp(fs->fs_type, FSTAB_RQ)) 213 continue; 214 /* If an unknown file system type, complain. */ 215 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) { 216 warnx("%s: unknown mount type", fs->fs_vfstype); 217 continue; 218 } 219 if (checkvfsname(fs->fs_vfstype, typelist)) 220 continue; 221 222 /* 223 * We want to unmount the file systems in the reverse order 224 * that they were mounted. So, we save off the file name 225 * in some allocated memory, and then call recursively. 226 */ 227 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL) 228 err(1, "malloc failed"); 229 (void)strcpy(cp, fs->fs_file); 230 rval = umountall(typelist); 231 rval = umountfs(cp, typelist) || rval; 232 free(cp); 233 return (rval); 234 } while ((fs = getfsent()) != NULL); 235 return (0); 236 } 237 238 int 239 umountfs(char *name, char **typelist) 240 { 241 enum clnt_stat clnt_stat; 242 struct hostent *hp; 243 struct sockaddr_in saddr; 244 struct timeval pertry, try; 245 CLIENT *clp; 246 size_t len; 247 int so, speclen, do_rpc; 248 char *mntonname, *mntfromname; 249 char *mntfromnamerev; 250 char *nfsdirname, *orignfsdirname; 251 char *resolved, realname[MAXPATHLEN]; 252 char *type, *delimp, *hostp, *origname; 253 254 len = 0; 255 mntfromname = mntonname = delimp = hostp = orignfsdirname = NULL; 256 257 /* 258 * 1. Check if the name exists in the mounttable. 259 */ 260 (void)checkmntlist(name, &mntfromname, &mntonname, &type); 261 /* 262 * 2. Remove trailing slashes if there are any. After that 263 * we look up the name in the mounttable again. 264 */ 265 if (mntfromname == NULL && mntonname == NULL) { 266 speclen = strlen(name); 267 for (speclen = strlen(name); 268 speclen > 1 && name[speclen - 1] == '/'; 269 speclen--) 270 name[speclen - 1] = '\0'; 271 (void)checkmntlist(name, &mntfromname, &mntonname, &type); 272 resolved = name; 273 /* Save off original name in origname */ 274 if ((origname = strdup(name)) == NULL) 275 err(1, "strdup"); 276 /* 277 * 3. Check if the deprecated nfs-syntax with an '@' 278 * has been used and translate it to the ':' syntax. 279 * Look up the name in the mounttable again. 280 */ 281 if (mntfromname == NULL && mntonname == NULL) { 282 if ((delimp = strrchr(name, '@')) != NULL) { 283 hostp = delimp + 1; 284 if (*hostp != '\0') { 285 /* 286 * Make both '@' and ':' 287 * notations equal 288 */ 289 char *host = strdup(hostp); 290 len = strlen(hostp); 291 if (host == NULL) 292 err(1, "strdup"); 293 memmove(name + len + 1, name, 294 (size_t)(delimp - name)); 295 name[len] = ':'; 296 memmove(name, host, len); 297 free(host); 298 } 299 for (speclen = strlen(name); 300 speclen > 1 && name[speclen - 1] == '/'; 301 speclen--) 302 name[speclen - 1] = '\0'; 303 name[len + speclen + 1] = '\0'; 304 (void)checkmntlist(name, &mntfromname, 305 &mntonname, &type); 306 resolved = name; 307 } 308 /* 309 * 4. Check if a relative mountpoint has been 310 * specified. This should happen as last check, 311 * the order is important. To prevent possible 312 * nfs-hangs, we just call realpath(3) on the 313 * basedir of mountpoint and add the dirname again. 314 * Check the name in mounttable one last time. 315 */ 316 if (mntfromname == NULL && mntonname == NULL) { 317 (void)strcpy(name, origname); 318 if ((getrealname(name, realname)) != NULL) { 319 (void)checkmntlist(realname, 320 &mntfromname, &mntonname, &type); 321 resolved = realname; 322 } 323 /* 324 * All tests failed, return to main() 325 */ 326 if (mntfromname == NULL && mntonname == NULL) { 327 (void)strcpy(name, origname); 328 warnx("%s: not currently mounted", 329 origname); 330 free(origname); 331 return (1); 332 } 333 } 334 } 335 free(origname); 336 } else 337 resolved = name; 338 339 if (checkvfsname(type, typelist)) 340 return (1); 341 342 hp = NULL; 343 nfsdirname = NULL; 344 if (!strcmp(type, "nfs")) { 345 if ((nfsdirname = strdup(mntfromname)) == NULL) 346 err(1, "strdup"); 347 orignfsdirname = nfsdirname; 348 if ((delimp = strchr(nfsdirname, ':')) != NULL) { 349 *delimp = '\0'; 350 hostp = nfsdirname; 351 if ((hp = gethostbyname(hostp)) == NULL) { 352 warnx("can't get net id for host"); 353 } 354 nfsdirname = delimp + 1; 355 } 356 } 357 /* 358 * Check if the reverse entrys of the mounttable are really the 359 * same as the normal ones. 360 */ 361 if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname, 362 NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL) 363 err(1, "strdup"); 364 /* 365 * Mark the uppermost mount as unmounted. 366 */ 367 (void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK); 368 /* 369 * If several equal mounts are in the mounttable, check the order 370 * and warn the user if necessary. 371 */ 372 if (strcmp(mntfromnamerev, mntfromname ) != 0 && 373 strcmp(resolved, mntonname) != 0) { 374 warnx("cannot umount %s, %s\n " 375 "is mounted there, umount it first", 376 mntonname, mntfromnamerev); 377 378 /* call getmntname again to set mntcheck[i] to 0 */ 379 (void)getmntname(mntfromname, mntonname, 380 NOTHING, &type, UNMARK); 381 return (1); 382 } 383 free(mntfromnamerev); 384 /* 385 * Check if we have to start the rpc-call later. 386 * If there are still identical nfs-names mounted, 387 * we skip the rpc-call. Obviously this has to 388 * happen before unmount(2), but it should happen 389 * after the previous namecheck. 390 */ 391 if (strcmp(type, "nfs") == 0 && getmntname(mntfromname, NULL, NOTHING, 392 &type, COUNT) != NULL) 393 do_rpc = 1; 394 else 395 do_rpc = 0; 396 if (!namematch(hp)) 397 return (1); 398 if (unmount(mntonname, fflag) != 0 ) { 399 warn("unmount of %s failed", mntonname); 400 return (1); 401 } 402 if (vflag) 403 (void)printf("%s: unmount from %s\n", mntfromname, mntonname); 404 /* 405 * Report to mountd-server which nfsname 406 * has been unmounted. 407 */ 408 if (hp != NULL && !(fflag & MNT_FORCE) && do_rpc) { 409 memset(&saddr, 0, sizeof(saddr)); 410 saddr.sin_family = AF_INET; 411 saddr.sin_port = 0; 412 memmove(&saddr.sin_addr, hp->h_addr, 413 MIN(hp->h_length, sizeof(saddr.sin_addr))); 414 pertry.tv_sec = 3; 415 pertry.tv_usec = 0; 416 so = RPC_ANYSOCK; 417 if ((clp = clntudp_create(&saddr, 418 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) { 419 clnt_pcreateerror("Cannot MNT PRC"); 420 return (1); 421 } 422 clp->cl_auth = authunix_create_default(); 423 try.tv_sec = 20; 424 try.tv_usec = 0; 425 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, 426 nfsdirname, xdr_void, (caddr_t)0, try); 427 if (clnt_stat != RPC_SUCCESS) { 428 clnt_perror(clp, "Bad MNT RPC"); 429 return (1); 430 } 431 free(orignfsdirname); 432 auth_destroy(clp->cl_auth); 433 clnt_destroy(clp); 434 } 435 return (0); 436 } 437 438 char * 439 getmntname(const char *fromname, const char *onname, 440 mntwhat what, char **type, dowhat mark) 441 { 442 static struct statfs *mntbuf; 443 static size_t mntsize = 0; 444 static char *mntcheck = NULL; 445 static char *mntcount = NULL; 446 int i, count; 447 448 if (mntsize <= 0) { 449 if ((mntsize = mntinfo(&mntbuf)) <= 0) 450 return (NULL); 451 } 452 if (mntcheck == NULL) { 453 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL || 454 (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL) 455 err(1, "calloc"); 456 } 457 /* 458 * We want to get the file systems in the reverse order 459 * that they were mounted. Mounted and unmounted filesystems 460 * are marked or unmarked in a table called 'mntcheck'. 461 * Unmount(const char *dir, int flags) does only take the 462 * mountpoint as argument, not the destination. If we don't pay 463 * attention to the order, it can happen that a overlaying 464 * filesystem get's unmounted instead of the one the user 465 * has choosen. 466 */ 467 switch (mark) { 468 case NAME: 469 /* Return only the specific name */ 470 for (i = mntsize - 1; i >= 0; i--) { 471 if (fromname != NULL && what == MNTON && 472 !strcmp(mntbuf[i].f_mntfromname, fromname) && 473 mntcheck[i] != 1) { 474 if (type) 475 *type = mntbuf[i].f_fstypename; 476 return (mntbuf[i].f_mntonname); 477 } 478 if (fromname != NULL && what == MNTFROM && 479 !strcmp(mntbuf[i].f_mntonname, fromname) && 480 mntcheck[i] != 1) { 481 if (type) 482 *type = mntbuf[i].f_fstypename; 483 return (mntbuf[i].f_mntfromname); 484 } 485 } 486 return (NULL); 487 case MARK: 488 /* Mark current mount with '1' and return name */ 489 for (i = mntsize - 1; i >= 0; i--) { 490 if (mntcheck[i] == 0 && 491 (strcmp(mntbuf[i].f_mntonname, onname) == 0) && 492 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) { 493 mntcheck[i] = 1; 494 return (mntbuf[i].f_mntonname); 495 } 496 } 497 return (NULL); 498 case UNMARK: 499 /* Unmark current mount with '0' and return name */ 500 for (i = 0; i < mntsize; i++) { 501 if (mntcheck[i] == 1 && 502 (strcmp(mntbuf[i].f_mntonname, onname) == 0) && 503 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) { 504 mntcheck[i] = 0; 505 return (mntbuf[i].f_mntonname); 506 } 507 } 508 return (NULL); 509 case COUNT: 510 /* Count the equal mntfromnames */ 511 count = 0; 512 for (i = mntsize - 1; i >= 0; i--) { 513 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) 514 count++; 515 } 516 /* Mark the already unmounted mounts and return 517 * mntfromname if count <= 1. Else return NULL. 518 */ 519 for (i = mntsize - 1; i >= 0; i--) { 520 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) { 521 if (mntcount[i] == 1) 522 count--; 523 else { 524 mntcount[i] = 1; 525 break; 526 } 527 } 528 } 529 if (count <= 1) 530 return (mntbuf[i].f_mntonname); 531 else 532 return (NULL); 533 case FREE: 534 free(mntbuf); 535 free(mntcheck); 536 return (NULL); 537 default: 538 return (NULL); 539 } 540 } 541 542 int 543 namematch(struct hostent *hp) 544 { 545 char *cp, **np; 546 547 if ((hp == NULL) || (nfshost == NULL)) 548 return (1); 549 550 if (strcasecmp(nfshost, hp->h_name) == 0) 551 return (1); 552 553 if ((cp = strchr(hp->h_name, '.')) != NULL) { 554 *cp = '\0'; 555 if (strcasecmp(nfshost, hp->h_name) == 0) 556 return (1); 557 } 558 for (np = hp->h_aliases; *np; np++) { 559 if (strcasecmp(nfshost, *np) == 0) 560 return (1); 561 if ((cp = strchr(*np, '.')) != NULL) { 562 *cp = '\0'; 563 if (strcasecmp(nfshost, *np) == 0) 564 return (1); 565 } 566 } 567 return (0); 568 } 569 570 void 571 checkmntlist(char *name, char **fromname, char **onname, char **type) 572 { 573 574 *fromname = getmntname(name, NULL, MNTFROM, type, NAME); 575 if (*fromname == NULL) { 576 *onname = getmntname(name, NULL, MNTON, type, NAME); 577 if (*onname != NULL) 578 *fromname = name; 579 } else 580 *onname = name; 581 } 582 583 size_t 584 mntinfo(struct statfs **mntbuf) 585 { 586 static struct statfs *origbuf; 587 size_t bufsize; 588 int mntsize; 589 590 mntsize = getfsstat(NULL, 0l, MNT_NOWAIT); 591 if (mntsize <= 0) 592 return (0); 593 bufsize = (mntsize + 1) * sizeof(struct statfs); 594 if ((origbuf = malloc(bufsize)) == NULL) 595 err(1, "malloc"); 596 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT); 597 *mntbuf = origbuf; 598 return (mntsize); 599 } 600 601 char * 602 getrealname(char *name, char *realname) 603 { 604 char *dirname; 605 int havedir; 606 size_t baselen; 607 size_t dirlen; 608 609 dirname = '\0'; 610 havedir = 0; 611 if (*name == '/') { 612 if (ISDOT(name + 1) || ISDOTDOT(name + 1)) 613 strcpy(realname, "/"); 614 else { 615 if ((dirname = strrchr(name + 1, '/')) == NULL) 616 snprintf(realname, MAXPATHLEN, "%s", name); 617 else 618 havedir = 1; 619 } 620 } else { 621 if (ISDOT(name) || ISDOTDOT(name)) 622 (void)realpath(name, realname); 623 else { 624 if ((dirname = strrchr(name, '/')) == NULL) { 625 if ((realpath(name, realname)) == NULL) 626 return (NULL); 627 } else 628 havedir = 1; 629 } 630 } 631 if (havedir) { 632 *dirname++ = '\0'; 633 if (ISDOT(dirname)) { 634 *dirname = '\0'; 635 if ((realpath(name, realname)) == NULL) 636 return (NULL); 637 } else if (ISDOTDOT(dirname)) { 638 *--dirname = '/'; 639 if ((realpath(name, realname)) == NULL) 640 return (NULL); 641 } else { 642 if ((realpath(name, realname)) == NULL) 643 return (NULL); 644 baselen = strlen(realname); 645 dirlen = strlen(dirname); 646 if (baselen + dirlen + 1 > MAXPATHLEN) 647 return (NULL); 648 if (realname[1] == '\0') { 649 memmove(realname + 1, dirname, dirlen); 650 realname[dirlen + 1] = '\0'; 651 } else { 652 realname[baselen] = '/'; 653 memmove(realname + baselen + 1, 654 dirname, dirlen); 655 realname[baselen + dirlen + 1] = '\0'; 656 } 657 } 658 } 659 return (realname); 660 } 661 662 /* 663 * xdr routines for mount rpc's 664 */ 665 int 666 xdr_dir(XDR *xdrsp, char *dirp) 667 { 668 669 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 670 } 671 672 void 673 usage() 674 { 675 676 (void)fprintf(stderr, "%s\n%s\n", 677 "usage: umount [-fv] special | node", 678 " umount -a | -A [-fv] [-h host] [-t type]"); 679 exit(1); 680 } 681