2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95";
44 static const char rcsid[] =
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
54 #include <nfs/rpcv2.h>
65 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
66 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
68 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
69 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
71 struct mtablist *mtabhead;
72 struct addrinfo *nfshost_ai = NULL;
76 void checkmntlist (char *, char **, char **, char **);
77 int checkvfsname (const char *, char **);
78 char *getmntname (const char *, const char *,
79 mntwhat, char **, dowhat);
80 char *getrealname(char *, char *resolved_path);
81 char **makevfslist (const char *);
82 size_t mntinfo (struct statfs **);
83 int namematch (struct addrinfo *);
84 int sacmp (struct sockaddr *, struct sockaddr *);
85 int umountall (char **);
86 int checkname (char *, char **);
87 int umountfs (char *, char *, char *);
89 int xdr_dir (XDR *, char *);
92 main(int argc, char *argv[])
94 int all, errs, ch, mntsize, error;
95 char **typelist = NULL, *mntonname, *mntfromname;
96 char *type, *mntfromnamerev, *mntonnamerev;
97 struct statfs *mntbuf;
98 struct addrinfo hints;
100 /* Start disks transferring immediately. */
104 while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1)
115 case 'h': /* -h implies -A. */
120 if (typelist != NULL)
121 err(1, "only one -t option may be specified");
122 typelist = makevfslist(optarg);
134 if ((argc == 0 && !all) || (argc != 0 && all))
137 /* -h implies "-t nfs" if no -t flag. */
138 if ((nfshost != NULL) && (typelist == NULL))
139 typelist = makevfslist("nfs");
141 if (nfshost != NULL) {
142 memset(&hints, 0, sizeof hints);
143 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
145 fprintf(stderr, "ndp: %s: %s\n", nfshost,
146 gai_strerror(error));
152 if ((mntsize = mntinfo(&mntbuf)) <= 0)
155 * We unmount the nfs-mounts in the reverse order
156 * that they were mounted.
158 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
159 if (checkvfsname(mntbuf[mntsize].f_fstypename,
163 * Check if a mountpoint is laid over by another mount.
164 * A warning will be printed to stderr if this is
165 * the case. The laid over mount remains unmounted.
167 mntonname = mntbuf[mntsize].f_mntonname;
168 mntfromname = mntbuf[mntsize].f_mntfromname;
169 mntonnamerev = getmntname(getmntname(mntonname,
170 NULL, MNTFROM, &type, NAME), NULL,
173 mntfromnamerev = getmntname(mntonnamerev,
174 NULL, MNTFROM, &type, NAME);
176 if (strcmp(mntonnamerev, mntonname) == 0 &&
177 strcmp(mntfromnamerev, mntfromname ) != 0)
178 warnx("cannot umount %s, %s\n "
179 "is mounted there, umount it first",
180 mntonname, mntfromnamerev);
182 if (checkname(mntbuf[mntsize].f_mntonname,
190 err(1, "%s", _PATH_FSTAB);
191 errs = umountall(typelist);
194 for (errs = 0; *argv != NULL; ++argv)
195 if (checkname(*argv, typelist) != 0)
199 (void)getmntname(NULL, NULL, NOTHING, NULL, FREE);
204 umountall(char **typelist)
210 static int firstcall = 1;
212 if ((fs = getfsent()) != NULL)
215 errx(1, "fstab reading failure");
219 /* Ignore the root. */
220 if (strcmp(fs->fs_file, "/") == 0)
224 * Historic practice: ignore unknown FSTAB_* fields.
226 if (strcmp(fs->fs_type, FSTAB_RW) &&
227 strcmp(fs->fs_type, FSTAB_RO) &&
228 strcmp(fs->fs_type, FSTAB_RQ))
230 /* Ignore unknown file system types. */
231 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
233 if (checkvfsname(fs->fs_vfstype, typelist))
237 * We want to unmount the file systems in the reverse order
238 * that they were mounted. So, we save off the file name
239 * in some allocated memory, and then call recursively.
241 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
242 err(1, "malloc failed");
243 (void)strcpy(cp, fs->fs_file);
244 rval = umountall(typelist);
245 rval = checkname(cp, typelist) || rval;
248 } while ((fs = getfsent()) != NULL);
253 * Do magic checks on mountpoint and device or hand over
254 * it to unmount(2) if everything fails.
257 checkname(char *name, char **typelist)
261 char *mntonname, *mntfromname;
262 char *mntfromnamerev;
263 char *resolved, realname[MAXPATHLEN];
264 char *type, *hostp, *delimp, *origname;
267 mntfromname = mntonname = delimp = hostp = NULL;
270 * 1. Check if the name exists in the mounttable.
272 (void)checkmntlist(name, &mntfromname, &mntonname, &type);
274 * 2. Remove trailing slashes if there are any. After that
275 * we look up the name in the mounttable again.
277 if (mntfromname == NULL && mntonname == NULL) {
278 speclen = strlen(name);
279 for (speclen = strlen(name);
280 speclen > 1 && name[speclen - 1] == '/';
282 name[speclen - 1] = '\0';
283 (void)checkmntlist(name, &mntfromname, &mntonname, &type);
285 /* Save off original name in origname */
286 if ((origname = strdup(name)) == NULL)
289 * 3. Check if the deprecated nfs-syntax with an '@'
290 * has been used and translate it to the ':' syntax.
291 * Look up the name in the mounttable again.
293 if (mntfromname == NULL && mntonname == NULL) {
294 if ((delimp = strrchr(name, '@')) != NULL) {
296 if (*hostp != '\0') {
298 * Make both '@' and ':'
301 char *host = strdup(hostp);
305 memmove(name + len + 1, name,
306 (size_t)(delimp - name));
308 memmove(name, host, len);
311 for (speclen = strlen(name);
312 speclen > 1 && name[speclen - 1] == '/';
314 name[speclen - 1] = '\0';
315 name[len + speclen + 1] = '\0';
316 (void)checkmntlist(name, &mntfromname,
321 * 4. Check if a relative mountpoint has been
322 * specified. This should happen as last check,
323 * the order is important. To prevent possible
324 * nfs-hangs, we just call realpath(3) on the
325 * basedir of mountpoint and add the dirname again.
326 * Check the name in mounttable one last time.
328 if (mntfromname == NULL && mntonname == NULL) {
329 (void)strcpy(name, origname);
330 if ((getrealname(name, realname)) != NULL) {
331 (void)checkmntlist(realname,
332 &mntfromname, &mntonname, &type);
336 * 5. All tests failed, just hand over the
337 * mountpoint to the kernel, maybe the statfs
338 * structure has been truncated or is not
339 * useful anymore because of a chroot(2).
340 * Please note that nfs will not be able to
341 * notify the nfs-server about unmounting.
342 * These things can change in future when the
343 * fstat structure get's more reliable,
344 * but at the moment we cannot thrust it.
346 if (mntfromname == NULL && mntonname == NULL) {
347 (void)strcpy(name, origname);
348 if (umountfs(NULL, origname,
350 warnx("%s not found in "
352 "unmounted it anyway",
366 if (checkvfsname(type, typelist))
370 * Check if the reverse entrys of the mounttable are really the
371 * same as the normal ones.
373 if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
374 NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
377 * Mark the uppermost mount as unmounted.
379 (void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
381 * If several equal mounts are in the mounttable, check the order
382 * and warn the user if necessary.
384 if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
385 strcmp(resolved, mntonname) != 0) {
386 warnx("cannot umount %s, %s\n "
387 "is mounted there, umount it first",
388 mntonname, mntfromnamerev);
390 /* call getmntname again to set mntcheck[i] to 0 */
391 (void)getmntname(mntfromname, mntonname,
392 NOTHING, &type, UNMARK);
395 free(mntfromnamerev);
396 umountfs(mntfromname, mntonname, type);
400 * NFS stuff and unmount(2) call
403 umountfs(char *mntfromname, char *mntonname, char *type)
405 enum clnt_stat clnt_stat;
407 struct mtablist *mtab;
408 struct addrinfo *ai, hints;
411 char *nfsdirname, *orignfsdirname;
412 char *hostp, *delimp;
416 nfsdirname = delimp = orignfsdirname = NULL;
417 memset(&hints, 0, sizeof hints);
419 if (!strcmp(type, "nfs")) {
420 if ((nfsdirname = strdup(mntfromname)) == NULL)
422 orignfsdirname = nfsdirname;
423 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
426 getaddrinfo(hostp, NULL, &hints, &ai);
428 warnx("can't get net id for host");
430 nfsdirname = delimp + 1;
434 * Check if we have to start the rpc-call later.
435 * If there are still identical nfs-names mounted,
436 * we skip the rpc-call. Obviously this has to
437 * happen before unmount(2), but it should happen
438 * after the previous namecheck.
440 if (strcmp(type, "nfs") == 0 && getmntname(mntfromname, NULL, NOTHING,
441 &type, COUNT) != NULL)
448 if (unmount(mntonname, fflag) != 0 ) {
449 warn("unmount of %s failed", mntonname);
453 (void)printf("%s: unmount from %s\n", mntfromname, mntonname);
455 * Report to mountd-server which nfsname
456 * has been unmounted.
458 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
459 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
461 clnt_pcreateerror("Cannot MNT PRC");
464 clp->cl_auth = authsys_create_default();
467 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
468 nfsdirname, xdr_void, (caddr_t)0, try);
469 if (clnt_stat != RPC_SUCCESS) {
470 clnt_perror(clp, "Bad MNT RPC");
474 * Remove the unmounted entry from /var/db/mounttab.
476 if (read_mtab(mtab)) {
478 clean_mtab(hostp, nfsdirname);
480 warnx("cannot remove entry %s:%s",
484 free(orignfsdirname);
485 auth_destroy(clp->cl_auth);
492 getmntname(const char *fromname, const char *onname,
493 mntwhat what, char **type, dowhat mark)
495 static struct statfs *mntbuf;
496 static size_t mntsize = 0;
497 static char *mntcheck = NULL;
498 static char *mntcount = NULL;
502 if ((mntsize = mntinfo(&mntbuf)) <= 0)
505 if (mntcheck == NULL) {
506 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
507 (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
511 * We want to get the file systems in the reverse order
512 * that they were mounted. Mounted and unmounted filesystems
513 * are marked or unmarked in a table called 'mntcheck'.
514 * Unmount(const char *dir, int flags) does only take the
515 * mountpoint as argument, not the destination. If we don't pay
516 * attention to the order, it can happen that a overlaying
517 * filesystem get's unmounted instead of the one the user
522 /* Return only the specific name */
523 for (i = mntsize - 1; i >= 0; i--) {
524 if (fromname != NULL && what == MNTON &&
525 !strcmp(mntbuf[i].f_mntfromname, fromname) &&
528 *type = mntbuf[i].f_fstypename;
529 return (mntbuf[i].f_mntonname);
531 if (fromname != NULL && what == MNTFROM &&
532 !strcmp(mntbuf[i].f_mntonname, fromname) &&
535 *type = mntbuf[i].f_fstypename;
536 return (mntbuf[i].f_mntfromname);
541 /* Mark current mount with '1' and return name */
542 for (i = mntsize - 1; i >= 0; i--) {
543 if (mntcheck[i] == 0 &&
544 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
545 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
547 return (mntbuf[i].f_mntonname);
552 /* Unmark current mount with '0' and return name */
553 for (i = 0; i < mntsize; i++) {
554 if (mntcheck[i] == 1 &&
555 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
556 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
558 return (mntbuf[i].f_mntonname);
563 /* Count the equal mntfromnames */
565 for (i = mntsize - 1; i >= 0; i--) {
566 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
569 /* Mark the already unmounted mounts and return
570 * mntfromname if count <= 1. Else return NULL.
572 for (i = mntsize - 1; i >= 0; i--) {
573 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
574 if (mntcount[i] == 1)
583 return (mntbuf[i].f_mntonname);
597 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
602 if (sa1->sa_family != sa2->sa_family)
605 switch (sa1->sa_family) {
607 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
608 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
612 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
613 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
615 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
616 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
623 return memcmp(p1, p2, len);
627 namematch(struct addrinfo *ai)
629 struct addrinfo *aip;
631 if (nfshost == NULL || nfshost_ai == NULL)
636 while (aip != NULL) {
637 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
648 checkmntlist(char *name, char **fromname, char **onname, char **type)
651 *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
652 if (*fromname == NULL) {
653 *onname = getmntname(name, NULL, MNTON, type, NAME);
661 mntinfo(struct statfs **mntbuf)
663 static struct statfs *origbuf;
667 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
670 bufsize = (mntsize + 1) * sizeof(struct statfs);
671 if ((origbuf = malloc(bufsize)) == NULL)
673 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
679 getrealname(char *name, char *realname)
689 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
690 strcpy(realname, "/");
692 if ((dirname = strrchr(name + 1, '/')) == NULL)
693 snprintf(realname, MAXPATHLEN, "%s", name);
698 if (ISDOT(name) || ISDOTDOT(name))
699 (void)realpath(name, realname);
701 if ((dirname = strrchr(name, '/')) == NULL) {
702 if ((realpath(name, realname)) == NULL)
710 if (ISDOT(dirname)) {
712 if ((realpath(name, realname)) == NULL)
714 } else if (ISDOTDOT(dirname)) {
716 if ((realpath(name, realname)) == NULL)
719 if ((realpath(name, realname)) == NULL)
721 baselen = strlen(realname);
722 dirlen = strlen(dirname);
723 if (baselen + dirlen + 1 > MAXPATHLEN)
725 if (realname[1] == '\0') {
726 memmove(realname + 1, dirname, dirlen);
727 realname[dirlen + 1] = '\0';
729 realname[baselen] = '/';
730 memmove(realname + baselen + 1,
732 realname[baselen + dirlen + 1] = '\0';
740 * xdr routines for mount rpc's
743 xdr_dir(XDR *xdrsp, char *dirp)
746 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
753 (void)fprintf(stderr, "%s\n%s\n",
754 "usage: umount [-fv] special | node",
755 " umount -a | -A [-fv] [-h host] [-t type]");