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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1989, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)umount.c 8.8 (Berkeley) 5/8/95
31 * $FreeBSD: src/sbin/umount/umount.c,v 1.28 2001/10/13 02:04:54 iedowse Exp $
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/socket.h>
40 #include <nfs/rpcv2.h>
51 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
52 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
54 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
55 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
57 struct addrinfo *nfshost_ai = NULL;
61 void checkmntlist (char *, char **, char **, char **);
62 int checkvfsname (const char *, char **);
63 char *getmntname (const char *, const char *,
64 mntwhat, char **, dowhat);
65 char *getrealname(char *, char *resolved_path);
66 char **makevfslist (const char *);
67 int mntinfo (struct statfs **);
68 int namematch (struct addrinfo *);
69 int sacmp (struct sockaddr *, struct sockaddr *);
70 int umountall (char **);
71 int checkname (char *, char **);
72 int umountfs (char *, char *, char *);
74 int xdr_dir (XDR *, char *);
77 main(int argc, char *argv[])
79 int all, errs, ch, mntsize, error;
80 char **typelist = NULL, *mntonname, *mntfromname;
81 char *type, *mntfromnamerev, *mntonnamerev;
82 struct statfs *mntbuf;
83 struct addrinfo hints;
86 while ((ch = getopt(argc, argv, "AaF:fh:t:v")) != -1) {
100 case 'h': /* -h implies -A. */
105 if (typelist != NULL)
106 err(1, "only one -t option may be specified");
107 typelist = makevfslist(optarg);
120 if ((argc == 0 && !all) || (argc != 0 && all))
123 /* -h implies "-t nfs" if no -t flag. */
124 if ((nfshost != NULL) && (typelist == NULL))
125 typelist = makevfslist("nfs");
127 if (nfshost != NULL) {
128 memset(&hints, 0, sizeof hints);
129 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
131 errx(1, "%s: %s", nfshost, gai_strerror(error));
136 if ((mntsize = mntinfo(&mntbuf)) <= 0)
139 * We unmount the nfs-mounts in the reverse order
140 * that they were mounted.
142 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
143 if (checkvfsname(mntbuf[mntsize].f_fstypename,
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.
151 mntonname = mntbuf[mntsize].f_mntonname;
152 mntfromname = mntbuf[mntsize].f_mntfromname;
153 mntonnamerev = getmntname(getmntname(mntonname,
154 NULL, MNTFROM, &type, NAME), NULL,
157 mntfromnamerev = getmntname(mntonnamerev,
158 NULL, MNTFROM, &type, NAME);
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);
166 if (checkname(mntbuf[mntsize].f_mntonname,
174 err(1, "%s", getfstab());
175 errs = umountall(typelist);
178 for (errs = 0; *argv != NULL; ++argv)
179 if (checkname(*argv, typelist) != 0)
183 getmntname(NULL, NULL, NOTHING, NULL, FREE);
188 umountall(char **typelist)
194 static int firstcall = 1;
196 if ((fs = getfsent()) != NULL)
199 errx(1, "fstab reading failure");
203 /* Ignore the root. */
204 if (strcmp(fs->fs_file, "/") == 0)
208 * Historic practice: ignore unknown FSTAB_* fields.
210 if (strcmp(fs->fs_type, FSTAB_RW) &&
211 strcmp(fs->fs_type, FSTAB_RO) &&
212 strcmp(fs->fs_type, FSTAB_RQ))
214 /* Ignore unknown file system types. */
215 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
217 if (checkvfsname(fs->fs_vfstype, typelist))
221 * We want to unmount the file systems in the reverse order
222 * that they were mounted. So, we save off the file name
223 * in some allocated memory, and then call recursively.
225 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
226 err(1, "malloc failed");
227 strcpy(cp, fs->fs_file);
228 rval = umountall(typelist);
229 rval = checkname(cp, typelist) || rval;
232 } while ((fs = getfsent()) != NULL);
237 * Do magic checks on mountpoint and device or hand over
238 * it to unmount(2) if everything fails.
241 checkname(char *name, char **typelist)
245 char *mntonname, *mntfromname;
246 char *mntfromnamerev;
247 char *resolved, realname[MAXPATHLEN];
248 char *type, *hostp, *delimp, *origname;
249 char none[] = "none";
252 mntfromname = mntonname = delimp = hostp = NULL;
255 * 1. Check if the name exists in the mounttable.
257 checkmntlist(name, &mntfromname, &mntonname, &type);
258 if (mntfromname == NULL && mntonname == NULL) {
259 checkmntlist(getdevpath(name, 0), &mntfromname,
264 * 2. Remove trailing slashes if there are any. After that
265 * we look up the name in the mounttable again.
267 if (mntfromname == NULL && mntonname == NULL) {
268 speclen = strlen(name);
269 for (speclen = strlen(name);
270 speclen > 1 && name[speclen - 1] == '/';
272 name[speclen - 1] = '\0';
273 checkmntlist(name, &mntfromname, &mntonname, &type);
275 /* Save off original name in origname */
276 if ((origname = strdup(name)) == NULL)
279 * 3. Check if the deprecated nfs-syntax with an '@'
280 * has been used and translate it to the ':' syntax.
281 * Look up the name in the mounttable again.
283 if (mntfromname == NULL && mntonname == NULL) {
284 if ((delimp = strrchr(name, '@')) != NULL) {
286 if (*hostp != '\0') {
288 * Make both '@' and ':'
291 char *host = strdup(hostp);
295 memmove(name + len + 1, name,
296 (size_t)(delimp - name));
298 memmove(name, host, len);
301 for (speclen = strlen(name);
302 speclen > 1 && name[speclen - 1] == '/';
304 name[speclen - 1] = '\0';
305 name[len + speclen + 1] = '\0';
306 checkmntlist(name, &mntfromname,
311 * 4. Check if a relative mountpoint has been
312 * specified. This should happen as last check,
313 * the order is important. To prevent possible
314 * nfs-hangs, we just call realpath(3) on the
315 * basedir of mountpoint and add the dirname again.
316 * Check the name in mounttable one last time.
318 if (mntfromname == NULL && mntonname == NULL) {
319 strcpy(name, origname);
320 if ((getrealname(name, realname)) != NULL) {
321 checkmntlist(realname,
322 &mntfromname, &mntonname, &type);
326 * 5. All tests failed, just hand over the
327 * mountpoint to the kernel, maybe the statfs
328 * structure has been truncated or is not
329 * useful anymore because of a chroot(2).
330 * Please note that nfs will not be able to
331 * notify the nfs-server about unmounting.
332 * These things can change in future when the
333 * fstat structure get's more reliable,
334 * but at the moment we cannot thrust it.
336 if (mntfromname == NULL && mntonname == NULL) {
337 strcpy(name, origname);
338 if (umountfs(NULL, origname,
340 warnx("%s not found in "
342 "unmounted it anyway",
356 if (checkvfsname(type, typelist))
360 * Check if the reverse entrys of the mounttable are really the
361 * same as the normal ones.
363 if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
364 NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
367 * Mark the uppermost mount as unmounted.
369 getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
371 * If several equal mounts are in the mounttable, check the order
372 * and warn the user if necessary.
374 if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
375 strcmp(resolved, mntonname) != 0) {
376 warnx("cannot umount %s, %s\n "
377 "is mounted there, umount it first",
378 mntonname, mntfromnamerev);
380 /* call getmntname again to set mntcheck[i] to 0 */
381 getmntname(mntfromname, mntonname,
382 NOTHING, &type, UNMARK);
385 free(mntfromnamerev);
386 return (umountfs(mntfromname, mntonname, type));
390 * NFS stuff and unmount(2) call
393 umountfs(char *mntfromname, char *mntonname, char *type)
395 enum clnt_stat clnt_stat;
397 struct addrinfo *ai, hints;
400 char *nfsdirname, *orignfsdirname;
401 char *hostp, *delimp;
406 nfsdirname = delimp = orignfsdirname = NULL;
407 memset(&hints, 0, sizeof hints);
409 if (strcmp(type, "nfs") == 0) {
410 if ((nfsdirname = strdup(mntfromname)) == NULL)
412 orignfsdirname = nfsdirname;
413 if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
416 getaddrinfo(hostp, NULL, &hints, &ai);
418 warnx("can't get net id for host");
420 nfsdirname = delimp + 1;
424 * Check if we have to start the rpc-call later.
425 * If there are still identical nfs-names mounted,
426 * we skip the rpc-call. Obviously this has to
427 * happen before unmount(2), but it should happen
428 * after the previous namecheck.
429 * A non-NULL return means that this is the last
430 * mount from mntfromname that is still mounted.
432 if (getmntname(mntfromname, NULL, NOTHING, &type, COUNT)
439 if (unmount(mntonname, fflag) != 0 ) {
440 warn("unmount of %s failed", mntonname);
444 printf("%s: unmount from %s\n", mntfromname, mntonname);
446 * Report to mountd-server which nfsname
447 * has been unmounted.
449 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
450 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
452 warnx("%s: %s", hostp,
453 clnt_spcreateerror("RPCPROG_MNT"));
456 clp->cl_auth = authsys_create_default();
459 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, (xdrproc_t)xdr_dir,
460 nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
461 if (clnt_stat != RPC_SUCCESS) {
462 warnx("%s: %s", hostp,
463 clnt_sperror(clp, "RPCMNT_UMOUNT"));
467 * Remove the unmounted entry from /var/db/mounttab.
470 clean_mtab(hostp, nfsdirname, vflag);
471 if(!write_mtab(vflag))
472 warnx("cannot remove mounttab entry %s:%s",
476 free(orignfsdirname);
477 auth_destroy(clp->cl_auth);
484 getmntname(const char *fromname, const char *onname,
485 mntwhat what, char **type, dowhat mark)
487 static struct statfs *mntbuf;
488 static int mntsize = 0;
489 static char *mntcheck = NULL;
490 static char *mntcount = NULL;
494 if ((mntsize = mntinfo(&mntbuf)) <= 0)
497 if (mntcheck == NULL) {
498 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
499 (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
503 * We want to get the file systems in the reverse order
504 * that they were mounted. Mounted and unmounted filesystems
505 * are marked or unmarked in a table called 'mntcheck'.
506 * Unmount(const char *dir, int flags) does only take the
507 * mountpoint as argument, not the destination. If we don't pay
508 * attention to the order, it can happen that a overlaying
509 * filesystem get's unmounted instead of the one the user
514 /* Return only the specific name */
515 for (i = mntsize - 1; i >= 0; i--) {
516 if (fromname != NULL && what == MNTON &&
517 !strcmp(mntbuf[i].f_mntfromname, fromname) &&
520 *type = mntbuf[i].f_fstypename;
521 return (mntbuf[i].f_mntonname);
523 if (fromname != NULL && what == MNTFROM &&
524 !strcmp(mntbuf[i].f_mntonname, fromname) &&
527 *type = mntbuf[i].f_fstypename;
528 return (mntbuf[i].f_mntfromname);
533 /* Mark current mount with '1' and return name */
534 for (i = mntsize - 1; i >= 0; i--) {
535 if (mntcheck[i] == 0 &&
536 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
537 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
539 return (mntbuf[i].f_mntonname);
544 /* Unmark current mount with '0' and return name */
545 for (i = 0; i < mntsize; i++) {
546 if (mntcheck[i] == 1 &&
547 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
548 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
550 return (mntbuf[i].f_mntonname);
555 /* Count the equal mntfromnames */
557 for (i = mntsize - 1; i >= 0; i--) {
558 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
561 /* Mark the already unmounted mounts and return
562 * mntfromname if count <= 1. Else return NULL.
564 for (i = mntsize - 1; i >= 0; i--) {
565 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
566 if (mntcount[i] == 1)
575 return (mntbuf[i].f_mntonname);
589 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
594 if (sa1->sa_family != sa2->sa_family)
597 switch (sa1->sa_family) {
599 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
600 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
604 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
605 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
607 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
608 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
615 return memcmp(p1, p2, len);
619 namematch(struct addrinfo *ai)
621 struct addrinfo *aip;
623 if (nfshost == NULL || nfshost_ai == NULL)
628 while (aip != NULL) {
629 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
640 checkmntlist(char *name, char **fromname, char **onname, char **type)
643 *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
644 if (*fromname == NULL) {
645 *onname = getmntname(name, NULL, MNTON, type, NAME);
653 mntinfo(struct statfs **mntbuf)
655 static struct statfs *origbuf;
659 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
662 bufsize = (mntsize + 1) * sizeof(struct statfs);
663 if ((origbuf = malloc(bufsize)) == NULL)
665 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
671 getrealname(char *name, char *realname)
681 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
682 strcpy(realname, "/");
684 if ((dirname = strrchr(name + 1, '/')) == NULL)
685 snprintf(realname, MAXPATHLEN, "%s", name);
690 if (ISDOT(name) || ISDOTDOT(name))
691 realpath(name, realname);
693 if ((dirname = strrchr(name, '/')) == NULL) {
694 if ((realpath(name, realname)) == NULL)
702 if (ISDOT(dirname)) {
704 if ((realpath(name, realname)) == NULL)
706 } else if (ISDOTDOT(dirname)) {
708 if ((realpath(name, realname)) == NULL)
711 if ((realpath(name, realname)) == NULL)
713 baselen = strlen(realname);
714 dirlen = strlen(dirname);
715 if (baselen + dirlen + 1 > MAXPATHLEN)
717 if (realname[1] == '\0') {
718 memmove(realname + 1, dirname, dirlen);
719 realname[dirlen + 1] = '\0';
721 realname[baselen] = '/';
722 memmove(realname + baselen + 1,
724 realname[baselen + dirlen + 1] = '\0';
732 * xdr routines for mount rpc's
735 xdr_dir(XDR *xdrsp, char *dirp)
738 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
745 fprintf(stderr, "%s\n%s\n",
746 "usage: umount [-fv] special | node",
747 " umount -a | -A [-F fstab] [-fv] [-h host] [-t type]");