1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1989, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95"; 41 #endif 42 static const char rcsid[] = 43 "$FreeBSD$"; 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/mount.h> 48 #include <sys/socket.h> 49 #include <sys/stat.h> 50 51 #include <netdb.h> 52 #include <rpc/rpc.h> 53 #include <rpcsvc/mount.h> 54 #include <nfs/nfssvc.h> 55 56 #include <ctype.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fstab.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "mounttab.h" 66 67 typedef enum { FIND, REMOVE, CHECKUNIQUE } dowhat; 68 69 static struct addrinfo *nfshost_ai = NULL; 70 static int fflag, vflag; 71 static char *nfshost; 72 73 struct statfs *checkmntlist(char *); 74 int checkvfsname (const char *, char **); 75 struct statfs *getmntentry(const char *fromname, const char *onname, 76 fsid_t *fsid, dowhat what); 77 char **makevfslist (const char *); 78 size_t mntinfo (struct statfs **); 79 int namematch (struct addrinfo *); 80 int parsehexfsid(const char *hex, fsid_t *fsid); 81 int sacmp (void *, void *); 82 int umountall (char **); 83 int checkname (char *, char **); 84 int umountfs(struct statfs *sfs); 85 void usage (void); 86 int xdr_dir (XDR *, char *); 87 88 int 89 main(int argc, char *argv[]) 90 { 91 int all, errs, ch, mntsize, error, nfsforce, ret; 92 char **typelist = NULL; 93 struct statfs *mntbuf, *sfs; 94 struct addrinfo hints; 95 96 nfsforce = all = errs = 0; 97 while ((ch = getopt(argc, argv, "AaF:fh:Nnt: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 setfstab(optarg); 107 break; 108 case 'f': 109 fflag |= MNT_FORCE; 110 break; 111 case 'h': /* -h implies -A. */ 112 all = 2; 113 nfshost = optarg; 114 break; 115 case 'N': 116 nfsforce = 1; 117 break; 118 case 'n': 119 fflag |= MNT_NONBUSY; 120 break; 121 case 't': 122 if (typelist != NULL) 123 err(1, "only one -t option may be specified"); 124 typelist = makevfslist(optarg); 125 break; 126 case 'v': 127 vflag = 1; 128 break; 129 default: 130 usage(); 131 /* NOTREACHED */ 132 } 133 argc -= optind; 134 argv += optind; 135 136 if ((fflag & MNT_FORCE) != 0 && (fflag & MNT_NONBUSY) != 0) 137 err(1, "-f and -n are mutually exclusive"); 138 139 /* Start disks transferring immediately. */ 140 if ((fflag & (MNT_FORCE | MNT_NONBUSY)) == 0 && nfsforce == 0) 141 sync(); 142 143 if ((argc == 0 && !all) || (argc != 0 && all)) 144 usage(); 145 146 if (nfsforce != 0 && (argc == 0 || nfshost != NULL || typelist != NULL)) 147 usage(); 148 149 /* -h implies "-t nfs" if no -t flag. */ 150 if ((nfshost != NULL) && (typelist == NULL)) 151 typelist = makevfslist("nfs"); 152 153 if (nfshost != NULL) { 154 memset(&hints, 0, sizeof hints); 155 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai); 156 if (error) 157 errx(1, "%s: %s", nfshost, gai_strerror(error)); 158 } 159 160 switch (all) { 161 case 2: 162 if ((mntsize = mntinfo(&mntbuf)) <= 0) 163 break; 164 /* 165 * We unmount the nfs-mounts in the reverse order 166 * that they were mounted. 167 */ 168 for (errs = 0, mntsize--; mntsize > 0; mntsize--) { 169 sfs = &mntbuf[mntsize]; 170 if (checkvfsname(sfs->f_fstypename, typelist)) 171 continue; 172 if (strcmp(sfs->f_mntonname, "/dev") == 0) 173 continue; 174 if (umountfs(sfs) != 0) 175 errs = 1; 176 } 177 free(mntbuf); 178 break; 179 case 1: 180 if (setfsent() == 0) 181 err(1, "%s", getfstab()); 182 errs = umountall(typelist); 183 break; 184 case 0: 185 for (errs = 0; *argv != NULL; ++argv) 186 if (nfsforce != 0) { 187 /* 188 * First do the nfssvc() syscall to shut down 189 * the mount point and then do the forced 190 * dismount. 191 */ 192 ret = nfssvc(NFSSVC_FORCEDISM, *argv); 193 if (ret >= 0) 194 ret = unmount(*argv, MNT_FORCE); 195 if (ret < 0) { 196 warn("%s", *argv); 197 errs = 1; 198 } 199 } else if (checkname(*argv, typelist) != 0) 200 errs = 1; 201 break; 202 } 203 exit(errs); 204 } 205 206 int 207 umountall(char **typelist) 208 { 209 struct xvfsconf vfc; 210 struct fstab *fs; 211 int rval; 212 char *cp; 213 static int firstcall = 1; 214 215 if ((fs = getfsent()) != NULL) 216 firstcall = 0; 217 else if (firstcall) 218 errx(1, "fstab reading failure"); 219 else 220 return (0); 221 do { 222 /* Ignore the root. */ 223 if (strcmp(fs->fs_file, "/") == 0) 224 continue; 225 /* 226 * !!! 227 * Historic practice: ignore unknown FSTAB_* fields. 228 */ 229 if (strcmp(fs->fs_type, FSTAB_RW) && 230 strcmp(fs->fs_type, FSTAB_RO) && 231 strcmp(fs->fs_type, FSTAB_RQ)) 232 continue; 233 /* Ignore unknown file system types. */ 234 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) 235 continue; 236 if (checkvfsname(fs->fs_vfstype, typelist)) 237 continue; 238 239 /* 240 * We want to unmount the file systems in the reverse order 241 * that they were mounted. So, we save off the file name 242 * in some allocated memory, and then call recursively. 243 */ 244 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL) 245 err(1, "malloc failed"); 246 (void)strcpy(cp, fs->fs_file); 247 rval = umountall(typelist); 248 rval = checkname(cp, typelist) || rval; 249 free(cp); 250 return (rval); 251 } while ((fs = getfsent()) != NULL); 252 return (0); 253 } 254 255 /* 256 * Do magic checks on mountpoint/device/fsid, and then call unmount(2). 257 */ 258 int 259 checkname(char *mntname, char **typelist) 260 { 261 char buf[MAXPATHLEN]; 262 struct statfs sfsbuf; 263 struct stat sb; 264 struct statfs *sfs; 265 char *delimp; 266 dev_t dev; 267 int len; 268 269 /* 270 * 1. Check if the name exists in the mounttable. 271 */ 272 sfs = checkmntlist(mntname); 273 /* 274 * 2. Remove trailing slashes if there are any. After that 275 * we look up the name in the mounttable again. 276 */ 277 if (sfs == NULL) { 278 len = strlen(mntname); 279 while (len > 1 && mntname[len - 1] == '/') 280 mntname[--len] = '\0'; 281 sfs = checkmntlist(mntname); 282 } 283 /* 284 * 3. Check if the deprecated NFS syntax with an '@' has been used 285 * and translate it to the ':' syntax. Look up the name in the 286 * mount table again. 287 */ 288 if (sfs == NULL && (delimp = strrchr(mntname, '@')) != NULL) { 289 snprintf(buf, sizeof(buf), "%s:%.*s", delimp + 1, 290 (int)(delimp - mntname), mntname); 291 len = strlen(buf); 292 while (len > 1 && buf[len - 1] == '/') 293 buf[--len] = '\0'; 294 sfs = checkmntlist(buf); 295 } 296 /* 297 * 4. Resort to a statfs(2) call. This is the last check so that 298 * hung NFS filesystems for example can be unmounted without 299 * potentially blocking forever in statfs() as long as the 300 * filesystem is specified unambiguously. This covers all the 301 * hard cases such as symlinks and mismatches between the 302 * mount list and reality. 303 * We also do this if an ambiguous mount point was specified. 304 */ 305 if (sfs == NULL || (getmntentry(NULL, mntname, NULL, FIND) != NULL && 306 getmntentry(NULL, mntname, NULL, CHECKUNIQUE) == NULL)) { 307 if (statfs(mntname, &sfsbuf) != 0) { 308 warn("%s: statfs", mntname); 309 } else if (stat(mntname, &sb) != 0) { 310 warn("%s: stat", mntname); 311 } else if (S_ISDIR(sb.st_mode)) { 312 /* Check that `mntname' is the root directory. */ 313 dev = sb.st_dev; 314 snprintf(buf, sizeof(buf), "%s/..", mntname); 315 if (stat(buf, &sb) != 0) { 316 warn("%s: stat", buf); 317 } else if (sb.st_dev == dev) { 318 warnx("%s: not a file system root directory", 319 mntname); 320 return (1); 321 } else 322 sfs = &sfsbuf; 323 } 324 } 325 if (sfs == NULL) { 326 warnx("%s: unknown file system", mntname); 327 return (1); 328 } 329 if (checkvfsname(sfs->f_fstypename, typelist)) 330 return (1); 331 return (umountfs(sfs)); 332 } 333 334 /* 335 * NFS stuff and unmount(2) call 336 */ 337 int 338 umountfs(struct statfs *sfs) 339 { 340 char fsidbuf[64]; 341 enum clnt_stat clnt_stat; 342 struct timeval try; 343 struct addrinfo *ai, hints; 344 int do_rpc; 345 CLIENT *clp; 346 char *nfsdirname, *orignfsdirname; 347 char *hostp, *delimp; 348 char buf[1024]; 349 struct nfscl_dumpmntopts dumpmntopts; 350 const char *proto_ptr = NULL; 351 352 ai = NULL; 353 do_rpc = 0; 354 hostp = NULL; 355 nfsdirname = delimp = orignfsdirname = NULL; 356 memset(&hints, 0, sizeof hints); 357 358 if (strcmp(sfs->f_fstypename, "nfs") == 0) { 359 if ((nfsdirname = strdup(sfs->f_mntfromname)) == NULL) 360 err(1, "strdup"); 361 orignfsdirname = nfsdirname; 362 if (*nfsdirname == '[' && 363 (delimp = strchr(nfsdirname + 1, ']')) != NULL && 364 *(delimp + 1) == ':') { 365 hostp = nfsdirname + 1; 366 nfsdirname = delimp + 2; 367 } else if ((delimp = strrchr(nfsdirname, ':')) != NULL) { 368 hostp = nfsdirname; 369 nfsdirname = delimp + 1; 370 } 371 if (hostp != NULL) { 372 *delimp = '\0'; 373 getaddrinfo(hostp, NULL, &hints, &ai); 374 if (ai == NULL) { 375 warnx("can't get net id for host"); 376 } 377 } 378 379 /* 380 * Check if we have to start the rpc-call later. 381 * If there are still identical nfs-names mounted, 382 * we skip the rpc-call. Obviously this has to 383 * happen before unmount(2), but it should happen 384 * after the previous namecheck. 385 * A non-NULL return means that this is the last 386 * mount from mntfromname that is still mounted. 387 */ 388 if (getmntentry(sfs->f_mntfromname, NULL, NULL, 389 CHECKUNIQUE) != NULL) { 390 do_rpc = 1; 391 proto_ptr = "udp"; 392 /* 393 * Try and find out whether this NFS mount is NFSv4 and 394 * what protocol is being used. If this fails, the 395 * default is NFSv2,3 and use UDP for the Unmount RPC. 396 */ 397 dumpmntopts.ndmnt_fname = sfs->f_mntonname; 398 dumpmntopts.ndmnt_buf = buf; 399 dumpmntopts.ndmnt_blen = sizeof(buf); 400 if (nfssvc(NFSSVC_DUMPMNTOPTS, &dumpmntopts) >= 0) { 401 if (strstr(buf, "nfsv4,") != NULL) 402 do_rpc = 0; 403 else if (strstr(buf, ",tcp,") != NULL) 404 proto_ptr = "tcp"; 405 } 406 } 407 } 408 409 if (!namematch(ai)) { 410 free(orignfsdirname); 411 return (1); 412 } 413 /* First try to unmount using the file system ID. */ 414 snprintf(fsidbuf, sizeof(fsidbuf), "FSID:%d:%d", sfs->f_fsid.val[0], 415 sfs->f_fsid.val[1]); 416 if (unmount(fsidbuf, fflag | MNT_BYFSID) != 0) { 417 /* XXX, non-root users get a zero fsid, so don't warn. */ 418 if (errno != ENOENT || sfs->f_fsid.val[0] != 0 || 419 sfs->f_fsid.val[1] != 0) 420 warn("unmount of %s failed", sfs->f_mntonname); 421 if (errno != ENOENT) { 422 free(orignfsdirname); 423 return (1); 424 } 425 /* Compatibility for old kernels. */ 426 if (sfs->f_fsid.val[0] != 0 || sfs->f_fsid.val[1] != 0) 427 warnx("retrying using path instead of file system ID"); 428 if (unmount(sfs->f_mntonname, fflag) != 0) { 429 warn("unmount of %s failed", sfs->f_mntonname); 430 free(orignfsdirname); 431 return (1); 432 } 433 } 434 /* Mark this this file system as unmounted. */ 435 getmntentry(NULL, NULL, &sfs->f_fsid, REMOVE); 436 if (vflag) 437 (void)printf("%s: unmount from %s\n", sfs->f_mntfromname, 438 sfs->f_mntonname); 439 /* 440 * Report to mountd-server which nfsname 441 * has been unmounted. 442 */ 443 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) { 444 clp = clnt_create(hostp, MOUNTPROG, MOUNTVERS3, proto_ptr); 445 if (clp == NULL) { 446 warnx("%s: %s", hostp, 447 clnt_spcreateerror("MOUNTPROG")); 448 free(orignfsdirname); 449 return (1); 450 } 451 clp->cl_auth = authsys_create_default(); 452 try.tv_sec = 20; 453 try.tv_usec = 0; 454 clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir, 455 nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try); 456 if (clnt_stat != RPC_SUCCESS) { 457 warnx("%s: %s", hostp, 458 clnt_sperror(clp, "RPCMNT_UMOUNT")); 459 free(orignfsdirname); 460 return (1); 461 } 462 /* 463 * Remove the unmounted entry from /var/db/mounttab. 464 */ 465 if (read_mtab()) { 466 clean_mtab(hostp, nfsdirname, vflag); 467 if(!write_mtab(vflag)) 468 warnx("cannot remove mounttab entry %s:%s", 469 hostp, nfsdirname); 470 free_mtab(); 471 } 472 auth_destroy(clp->cl_auth); 473 clnt_destroy(clp); 474 } 475 free(orignfsdirname); 476 return (0); 477 } 478 479 struct statfs * 480 getmntentry(const char *fromname, const char *onname, fsid_t *fsid, dowhat what) 481 { 482 static struct statfs *mntbuf; 483 static size_t mntsize = 0; 484 static int *mntcheck = NULL; 485 struct statfs *sfs, *foundsfs; 486 int i, count; 487 488 if (mntsize <= 0) { 489 if ((mntsize = mntinfo(&mntbuf)) <= 0) 490 return (NULL); 491 } 492 if (mntcheck == NULL) { 493 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL) 494 err(1, "calloc"); 495 } 496 /* 497 * We want to get the file systems in the reverse order 498 * that they were mounted. Unmounted file systems are marked 499 * in a table called 'mntcheck'. 500 */ 501 count = 0; 502 foundsfs = NULL; 503 for (i = mntsize - 1; i >= 0; i--) { 504 if (mntcheck[i]) 505 continue; 506 sfs = &mntbuf[i]; 507 if (fromname != NULL && strcmp(sfs->f_mntfromname, 508 fromname) != 0) 509 continue; 510 if (onname != NULL && strcmp(sfs->f_mntonname, onname) != 0) 511 continue; 512 if (fsid != NULL && bcmp(&sfs->f_fsid, fsid, 513 sizeof(*fsid)) != 0) 514 continue; 515 516 switch (what) { 517 case CHECKUNIQUE: 518 foundsfs = sfs; 519 count++; 520 continue; 521 case REMOVE: 522 mntcheck[i] = 1; 523 break; 524 default: 525 break; 526 } 527 return (sfs); 528 } 529 530 if (what == CHECKUNIQUE && count == 1) 531 return (foundsfs); 532 return (NULL); 533 } 534 535 int 536 sacmp(void *sa1, void *sa2) 537 { 538 void *p1, *p2; 539 int len; 540 541 if (((struct sockaddr *)sa1)->sa_family != 542 ((struct sockaddr *)sa2)->sa_family) 543 return (1); 544 545 switch (((struct sockaddr *)sa1)->sa_family) { 546 case AF_INET: 547 p1 = &((struct sockaddr_in *)sa1)->sin_addr; 548 p2 = &((struct sockaddr_in *)sa2)->sin_addr; 549 len = 4; 550 break; 551 case AF_INET6: 552 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr; 553 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr; 554 len = 16; 555 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 556 ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 557 return (1); 558 break; 559 default: 560 return (1); 561 } 562 563 return memcmp(p1, p2, len); 564 } 565 566 int 567 namematch(struct addrinfo *ai) 568 { 569 struct addrinfo *aip; 570 571 if (nfshost == NULL || nfshost_ai == NULL) 572 return (1); 573 574 while (ai != NULL) { 575 aip = nfshost_ai; 576 while (aip != NULL) { 577 if (sacmp(ai->ai_addr, aip->ai_addr) == 0) 578 return (1); 579 aip = aip->ai_next; 580 } 581 ai = ai->ai_next; 582 } 583 584 return (0); 585 } 586 587 struct statfs * 588 checkmntlist(char *mntname) 589 { 590 struct statfs *sfs; 591 fsid_t fsid; 592 593 sfs = NULL; 594 if (parsehexfsid(mntname, &fsid) == 0) 595 sfs = getmntentry(NULL, NULL, &fsid, FIND); 596 if (sfs == NULL) 597 sfs = getmntentry(NULL, mntname, NULL, FIND); 598 if (sfs == NULL) 599 sfs = getmntentry(mntname, NULL, NULL, FIND); 600 return (sfs); 601 } 602 603 size_t 604 mntinfo(struct statfs **mntbuf) 605 { 606 static struct statfs *origbuf; 607 size_t bufsize; 608 int mntsize; 609 610 mntsize = getfsstat(NULL, 0, MNT_NOWAIT); 611 if (mntsize <= 0) 612 return (0); 613 bufsize = (mntsize + 1) * sizeof(struct statfs); 614 if ((origbuf = malloc(bufsize)) == NULL) 615 err(1, "malloc"); 616 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT); 617 *mntbuf = origbuf; 618 return (mntsize); 619 } 620 621 /* 622 * Convert a hexadecimal filesystem ID to an fsid_t. 623 * Returns 0 on success. 624 */ 625 int 626 parsehexfsid(const char *hex, fsid_t *fsid) 627 { 628 char hexbuf[3]; 629 int i; 630 631 if (strlen(hex) != sizeof(*fsid) * 2) 632 return (-1); 633 hexbuf[2] = '\0'; 634 for (i = 0; i < (int)sizeof(*fsid); i++) { 635 hexbuf[0] = hex[i * 2]; 636 hexbuf[1] = hex[i * 2 + 1]; 637 if (!isxdigit(hexbuf[0]) || !isxdigit(hexbuf[1])) 638 return (-1); 639 ((u_char *)fsid)[i] = strtol(hexbuf, NULL, 16); 640 } 641 return (0); 642 } 643 644 /* 645 * xdr routines for mount rpc's 646 */ 647 int 648 xdr_dir(XDR *xdrsp, char *dirp) 649 { 650 651 return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 652 } 653 654 void 655 usage(void) 656 { 657 658 (void)fprintf(stderr, "%s\n%s\n", 659 "usage: umount [-fNnv] special ... | node ... | fsid ...", 660 " umount -a | -A [-F fstab] [-fnv] [-h host] [-t type]"); 661 exit(1); 662 } 663