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
33 * @(#) Copyright (c) 1980, 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)umount.c 8.8 (Berkeley) 5/8/95
35 * $FreeBSD: src/sbin/umount/umount.c,v 1.22.2.1 2001/12/13 01:27:15 iedowse Exp $
36 * $DragonFly: src/sbin/umount/umount.c,v 1.4 2005/11/06 12:51:01 swildner Exp $
39 #include <sys/param.h>
40 #include <sys/mount.h>
44 #include <nfs/rpcv2.h>
55 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
56 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
58 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
59 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
64 void checkmntlist (char *, char **, char **, char **);
65 int checkvfsname (const char *, char **);
66 char *getmntname (const char *, const char *,
67 mntwhat, char **, dowhat);
68 char *getrealname(char *, char *resolved_path);
69 char **makevfslist (const char *);
70 size_t mntinfo (struct statfs **);
71 int namematch (struct hostent *);
72 int umountall (char **);
73 int umountfs (char *, char **);
75 int xdr_dir (XDR *, char *);
78 main(int argc, char *argv[])
80 int all, errs, ch, mntsize;
81 char **typelist = NULL, *mntonname, *mntfromname;
82 char *type, *mntfromnamerev, *mntonnamerev;
83 struct statfs *mntbuf;
85 /* Start disks transferring immediately. */
89 while ((ch = getopt(argc, argv, "Aafh: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);
119 if ((argc == 0 && !all) || (argc != 0 && all))
122 /* -h implies "-t nfs" if no -t flag. */
123 if ((nfshost != NULL) && (typelist == NULL))
124 typelist = makevfslist("nfs");
128 if ((mntsize = mntinfo(&mntbuf)) <= 0)
131 * We unmount the nfs-mounts in the reverse order
132 * that they were mounted.
134 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
135 if (checkvfsname(mntbuf[mntsize].f_fstypename,
139 * Check if a mountpoint is laid over by another mount.
140 * A warning will be printed to stderr if this is
141 * the case. The laid over mount remains unmounted.
143 mntonname = mntbuf[mntsize].f_mntonname;
144 mntfromname = mntbuf[mntsize].f_mntfromname;
145 mntonnamerev = getmntname(getmntname(mntonname,
146 NULL, MNTFROM, &type, NAME), NULL,
149 mntfromnamerev = getmntname(mntonnamerev,
150 NULL, MNTFROM, &type, NAME);
152 if (strcmp(mntonnamerev, mntonname) == 0 &&
153 strcmp(mntfromnamerev, mntfromname ) != 0)
154 warnx("cannot umount %s, %s\n "
155 "is mounted there, umount it first",
156 mntonname, mntfromnamerev);
158 if (umountfs(mntbuf[mntsize].f_mntonname,
166 err(1, "%s", _PATH_FSTAB);
167 errs = umountall(typelist);
170 for (errs = 0; *argv != NULL; ++argv)
171 if (umountfs(*argv, typelist) != 0)
175 getmntname(NULL, NULL, NOTHING, NULL, FREE);
180 umountall(char **typelist)
186 static int firstcall = 1;
188 if ((fs = getfsent()) != NULL)
191 errx(1, "fstab reading failure");
195 /* Ignore the root. */
196 if (strcmp(fs->fs_file, "/") == 0)
200 * Historic practice: ignore unknown FSTAB_* fields.
202 if (strcmp(fs->fs_type, FSTAB_RW) &&
203 strcmp(fs->fs_type, FSTAB_RO) &&
204 strcmp(fs->fs_type, FSTAB_RQ))
206 /* If an unknown file system type, complain. */
207 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) {
208 warnx("%s: unknown mount type", fs->fs_vfstype);
211 if (checkvfsname(fs->fs_vfstype, typelist))
215 * We want to unmount the file systems in the reverse order
216 * that they were mounted. So, we save off the file name
217 * in some allocated memory, and then call recursively.
219 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
220 err(1, "malloc failed");
221 strcpy(cp, fs->fs_file);
222 rval = umountall(typelist);
223 rval = umountfs(cp, typelist) || rval;
226 } while ((fs = getfsent()) != NULL);
231 umountfs(char *name, char **typelist)
233 enum clnt_stat clnt_stat;
235 struct mtablist *mtab;
236 struct sockaddr_in saddr;
237 struct timeval pertry, try;
240 int so, speclen, do_rpc;
241 char *mntonname, *mntfromname;
242 char *mntfromnamerev;
243 char *nfsdirname, *orignfsdirname;
244 char *resolved, realname[MAXPATHLEN];
245 char *type, *delimp, *hostp, *origname;
249 mntfromname = mntonname = delimp = hostp = orignfsdirname = NULL;
252 * 1. Check if the name exists in the mounttable.
254 checkmntlist(name, &mntfromname, &mntonname, &type);
256 * 2. Remove trailing slashes if there are any. After that
257 * we look up the name in the mounttable again.
259 if (mntfromname == NULL && mntonname == NULL) {
260 speclen = strlen(name);
261 for (speclen = strlen(name);
262 speclen > 1 && name[speclen - 1] == '/';
264 name[speclen - 1] = '\0';
265 checkmntlist(name, &mntfromname, &mntonname, &type);
267 /* Save off original name in origname */
268 if ((origname = strdup(name)) == NULL)
271 * 3. Check if the deprecated nfs-syntax with an '@'
272 * has been used and translate it to the ':' syntax.
273 * Look up the name in the mounttable again.
275 if (mntfromname == NULL && mntonname == NULL) {
276 if ((delimp = strrchr(name, '@')) != NULL) {
278 if (*hostp != '\0') {
280 * Make both '@' and ':'
283 char *host = strdup(hostp);
287 memmove(name + len + 1, name,
288 (size_t)(delimp - name));
290 memmove(name, host, len);
293 for (speclen = strlen(name);
294 speclen > 1 && name[speclen - 1] == '/';
296 name[speclen - 1] = '\0';
297 name[len + speclen + 1] = '\0';
298 checkmntlist(name, &mntfromname,
303 * 4. Check if a relative mountpoint has been
304 * specified. This should happen as last check,
305 * the order is important. To prevent possible
306 * nfs-hangs, we just call realpath(3) on the
307 * basedir of mountpoint and add the dirname again.
308 * Check the name in mounttable one last time.
310 if (mntfromname == NULL && mntonname == NULL) {
311 strcpy(name, origname);
312 if ((getrealname(name, realname)) != NULL) {
313 checkmntlist(realname,
314 &mntfromname, &mntonname, &type);
318 * All tests failed, return to main()
320 if (mntfromname == NULL && mntonname == NULL) {
321 strcpy(name, origname);
322 warnx("%s: not currently mounted",
333 if (checkvfsname(type, typelist))
338 if (!strcmp(type, "nfs")) {
339 if ((nfsdirname = strdup(mntfromname)) == NULL)
341 orignfsdirname = nfsdirname;
342 if ((delimp = strchr(nfsdirname, ':')) != NULL) {
345 if ((hp = gethostbyname(hostp)) == NULL) {
346 warnx("can't get net id for host");
348 nfsdirname = delimp + 1;
352 * Check if the reverse entrys of the mounttable are really the
353 * same as the normal ones.
355 if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
356 NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
359 * Mark the uppermost mount as unmounted.
361 getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
363 * If several equal mounts are in the mounttable, check the order
364 * and warn the user if necessary.
366 if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
367 strcmp(resolved, mntonname) != 0) {
368 warnx("cannot umount %s, %s\n "
369 "is mounted there, umount it first",
370 mntonname, mntfromnamerev);
372 /* call getmntname again to set mntcheck[i] to 0 */
373 getmntname(mntfromname, mntonname,
374 NOTHING, &type, UNMARK);
377 free(mntfromnamerev);
379 * Check if we have to start the rpc-call later.
380 * If there are still identical nfs-names mounted,
381 * we skip the rpc-call. Obviously this has to
382 * happen before unmount(2), but it should happen
383 * after the previous namecheck.
385 if (strcmp(type, "nfs") == 0 && getmntname(mntfromname, NULL, NOTHING,
386 &type, COUNT) != NULL)
392 if (unmount(mntonname, fflag) != 0 ) {
393 warn("unmount of %s failed", mntonname);
397 printf("%s: unmount from %s\n", mntfromname, mntonname);
399 * Report to mountd-server which nfsname
400 * has been unmounted.
402 if (hp != NULL && !(fflag & MNT_FORCE) && do_rpc) {
403 memset(&saddr, 0, sizeof(saddr));
404 saddr.sin_family = AF_INET;
406 memmove(&saddr.sin_addr, hp->h_addr,
407 MIN(hp->h_length, sizeof(saddr.sin_addr)));
411 if ((clp = clntudp_create(&saddr,
412 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
413 clnt_pcreateerror("Cannot MNT PRC");
416 clp->cl_auth = authunix_create_default();
419 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
420 nfsdirname, xdr_void, (caddr_t)0, try);
421 if (clnt_stat != RPC_SUCCESS) {
422 clnt_perror(clp, "Bad MNT RPC");
426 * Remove the unmounted entry from /var/db/mounttab.
429 clean_mtab(hostp, nfsdirname, vflag);
430 if(!write_mtab(vflag))
431 warnx("cannot remove mounttab entry %s:%s",
435 free(orignfsdirname);
436 auth_destroy(clp->cl_auth);
443 getmntname(const char *fromname, const char *onname,
444 mntwhat what, char **type, dowhat mark)
446 static struct statfs *mntbuf;
447 static size_t mntsize = 0;
448 static char *mntcheck = NULL;
449 static char *mntcount = NULL;
453 if ((mntsize = mntinfo(&mntbuf)) <= 0)
456 if (mntcheck == NULL) {
457 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
458 (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
462 * We want to get the file systems in the reverse order
463 * that they were mounted. Mounted and unmounted filesystems
464 * are marked or unmarked in a table called 'mntcheck'.
465 * Unmount(const char *dir, int flags) does only take the
466 * mountpoint as argument, not the destination. If we don't pay
467 * attention to the order, it can happen that a overlaying
468 * filesystem get's unmounted instead of the one the user
473 /* Return only the specific name */
474 for (i = mntsize - 1; i >= 0; i--) {
475 if (fromname != NULL && what == MNTON &&
476 !strcmp(mntbuf[i].f_mntfromname, fromname) &&
479 *type = mntbuf[i].f_fstypename;
480 return (mntbuf[i].f_mntonname);
482 if (fromname != NULL && what == MNTFROM &&
483 !strcmp(mntbuf[i].f_mntonname, fromname) &&
486 *type = mntbuf[i].f_fstypename;
487 return (mntbuf[i].f_mntfromname);
492 /* Mark current mount with '1' and return name */
493 for (i = mntsize - 1; i >= 0; i--) {
494 if (mntcheck[i] == 0 &&
495 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
496 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
498 return (mntbuf[i].f_mntonname);
503 /* Unmark current mount with '0' and return name */
504 for (i = 0; i < mntsize; i++) {
505 if (mntcheck[i] == 1 &&
506 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
507 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
509 return (mntbuf[i].f_mntonname);
514 /* Count the equal mntfromnames */
516 for (i = mntsize - 1; i >= 0; i--) {
517 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
520 /* Mark the already unmounted mounts and return
521 * mntfromname if count <= 1. Else return NULL.
523 for (i = mntsize - 1; i >= 0; i--) {
524 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
525 if (mntcount[i] == 1)
534 return (mntbuf[i].f_mntonname);
548 namematch(struct hostent *hp)
552 if ((hp == NULL) || (nfshost == NULL))
555 if (strcasecmp(nfshost, hp->h_name) == 0)
558 if ((cp = strchr(hp->h_name, '.')) != NULL) {
560 if (strcasecmp(nfshost, hp->h_name) == 0)
563 for (np = hp->h_aliases; *np; np++) {
564 if (strcasecmp(nfshost, *np) == 0)
566 if ((cp = strchr(*np, '.')) != NULL) {
568 if (strcasecmp(nfshost, *np) == 0)
576 checkmntlist(char *name, char **fromname, char **onname, char **type)
579 *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
580 if (*fromname == NULL) {
581 *onname = getmntname(name, NULL, MNTON, type, NAME);
589 mntinfo(struct statfs **mntbuf)
591 static struct statfs *origbuf;
595 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
598 bufsize = (mntsize + 1) * sizeof(struct statfs);
599 if ((origbuf = malloc(bufsize)) == NULL)
601 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
607 getrealname(char *name, char *realname)
617 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
618 strcpy(realname, "/");
620 if ((dirname = strrchr(name + 1, '/')) == NULL)
621 snprintf(realname, MAXPATHLEN, "%s", name);
626 if (ISDOT(name) || ISDOTDOT(name))
627 realpath(name, realname);
629 if ((dirname = strrchr(name, '/')) == NULL) {
630 if ((realpath(name, realname)) == NULL)
638 if (ISDOT(dirname)) {
640 if ((realpath(name, realname)) == NULL)
642 } else if (ISDOTDOT(dirname)) {
644 if ((realpath(name, realname)) == NULL)
647 if ((realpath(name, realname)) == NULL)
649 baselen = strlen(realname);
650 dirlen = strlen(dirname);
651 if (baselen + dirlen + 1 > MAXPATHLEN)
653 if (realname[1] == '\0') {
654 memmove(realname + 1, dirname, dirlen);
655 realname[dirlen + 1] = '\0';
657 realname[baselen] = '/';
658 memmove(realname + baselen + 1,
660 realname[baselen + dirlen + 1] = '\0';
668 * xdr routines for mount rpc's
671 xdr_dir(XDR *xdrsp, char *dirp)
674 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
681 fprintf(stderr, "%s\n%s\n",
682 "usage: umount [-fv] special | node",
683 " umount -a | -A [-fv] [-h host] [-t type]");