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>
53 #include <nfs/rpcv2.h>
64 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
65 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
67 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
68 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
70 struct mtablist *mtabhead;
74 void checkmntlist (char *, char **, char **, char **);
75 int checkvfsname (const char *, char **);
76 char *getmntname (const char *, const char *,
77 mntwhat, char **, dowhat);
78 char *getrealname(char *, char *resolved_path);
79 char **makevfslist (const char *);
80 size_t mntinfo (struct statfs **);
81 int namematch (struct hostent *);
82 int umountall (char **);
83 int umountfs (char *, char **);
85 int xdr_dir (XDR *, char *);
88 main(int argc, char *argv[])
90 int all, errs, ch, mntsize;
91 char **typelist = NULL, *mntonname, *mntfromname;
92 char *type, *mntfromnamerev, *mntonnamerev;
93 struct statfs *mntbuf;
95 /* Start disks transferring immediately. */
99 while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1)
110 case 'h': /* -h implies -A. */
115 if (typelist != NULL)
116 err(1, "only one -t option may be specified");
117 typelist = makevfslist(optarg);
129 if ((argc == 0 && !all) || (argc != 0 && all))
132 /* -h implies "-t nfs" if no -t flag. */
133 if ((nfshost != NULL) && (typelist == NULL))
134 typelist = makevfslist("nfs");
138 if ((mntsize = mntinfo(&mntbuf)) <= 0)
141 * We unmount the nfs-mounts in the reverse order
142 * that they were mounted.
144 for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
145 if (checkvfsname(mntbuf[mntsize].f_fstypename,
149 * Check if a mountpoint is laid over by another mount.
150 * A warning will be printed to stderr if this is
151 * the case. The laid over mount remains unmounted.
153 mntonname = mntbuf[mntsize].f_mntonname;
154 mntfromname = mntbuf[mntsize].f_mntfromname;
155 mntonnamerev = getmntname(getmntname(mntonname,
156 NULL, MNTFROM, &type, NAME), NULL,
159 mntfromnamerev = getmntname(mntonnamerev,
160 NULL, MNTFROM, &type, NAME);
162 if (strcmp(mntonnamerev, mntonname) == 0 &&
163 strcmp(mntfromnamerev, mntfromname ) != 0)
164 warnx("cannot umount %s, %s\n "
165 "is mounted there, umount it first",
166 mntonname, mntfromnamerev);
168 if (umountfs(mntbuf[mntsize].f_mntonname,
176 err(1, "%s", _PATH_FSTAB);
177 errs = umountall(typelist);
180 for (errs = 0; *argv != NULL; ++argv)
181 if (umountfs(*argv, typelist) != 0)
185 (void)getmntname(NULL, NULL, NOTHING, NULL, FREE);
190 umountall(char **typelist)
196 static int firstcall = 1;
198 if ((fs = getfsent()) != NULL)
201 errx(1, "fstab reading failure");
205 /* Ignore the root. */
206 if (strcmp(fs->fs_file, "/") == 0)
210 * Historic practice: ignore unknown FSTAB_* fields.
212 if (strcmp(fs->fs_type, FSTAB_RW) &&
213 strcmp(fs->fs_type, FSTAB_RO) &&
214 strcmp(fs->fs_type, FSTAB_RQ))
216 /* If an unknown file system type, complain. */
217 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) {
218 warnx("%s: unknown mount type", fs->fs_vfstype);
221 if (checkvfsname(fs->fs_vfstype, typelist))
225 * We want to unmount the file systems in the reverse order
226 * that they were mounted. So, we save off the file name
227 * in some allocated memory, and then call recursively.
229 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
230 err(1, "malloc failed");
231 (void)strcpy(cp, fs->fs_file);
232 rval = umountall(typelist);
233 rval = umountfs(cp, typelist) || rval;
236 } while ((fs = getfsent()) != NULL);
241 umountfs(char *name, char **typelist)
243 enum clnt_stat clnt_stat;
245 struct mtablist *mtab;
246 struct sockaddr_in saddr;
247 struct timeval pertry, try;
250 int so, speclen, do_rpc;
251 char *mntonname, *mntfromname;
252 char *mntfromnamerev;
253 char *nfsdirname, *orignfsdirname;
254 char *resolved, realname[MAXPATHLEN];
255 char *type, *delimp, *hostp, *origname;
259 mntfromname = mntonname = delimp = hostp = orignfsdirname = NULL;
262 * 1. Check if the name exists in the mounttable.
264 (void)checkmntlist(name, &mntfromname, &mntonname, &type);
266 * 2. Remove trailing slashes if there are any. After that
267 * we look up the name in the mounttable again.
269 if (mntfromname == NULL && mntonname == NULL) {
270 speclen = strlen(name);
271 for (speclen = strlen(name);
272 speclen > 1 && name[speclen - 1] == '/';
274 name[speclen - 1] = '\0';
275 (void)checkmntlist(name, &mntfromname, &mntonname, &type);
277 /* Save off original name in origname */
278 if ((origname = strdup(name)) == NULL)
281 * 3. Check if the deprecated nfs-syntax with an '@'
282 * has been used and translate it to the ':' syntax.
283 * Look up the name in the mounttable again.
285 if (mntfromname == NULL && mntonname == NULL) {
286 if ((delimp = strrchr(name, '@')) != NULL) {
288 if (*hostp != '\0') {
290 * Make both '@' and ':'
293 char *host = strdup(hostp);
297 memmove(name + len + 1, name,
298 (size_t)(delimp - name));
300 memmove(name, host, len);
303 for (speclen = strlen(name);
304 speclen > 1 && name[speclen - 1] == '/';
306 name[speclen - 1] = '\0';
307 name[len + speclen + 1] = '\0';
308 (void)checkmntlist(name, &mntfromname,
313 * 4. Check if a relative mountpoint has been
314 * specified. This should happen as last check,
315 * the order is important. To prevent possible
316 * nfs-hangs, we just call realpath(3) on the
317 * basedir of mountpoint and add the dirname again.
318 * Check the name in mounttable one last time.
320 if (mntfromname == NULL && mntonname == NULL) {
321 (void)strcpy(name, origname);
322 if ((getrealname(name, realname)) != NULL) {
323 (void)checkmntlist(realname,
324 &mntfromname, &mntonname, &type);
328 * All tests failed, return to main()
330 if (mntfromname == NULL && mntonname == NULL) {
331 (void)strcpy(name, origname);
332 warnx("%s: not currently mounted",
343 if (checkvfsname(type, typelist))
348 if (!strcmp(type, "nfs")) {
349 if ((nfsdirname = strdup(mntfromname)) == NULL)
351 orignfsdirname = nfsdirname;
352 if ((delimp = strchr(nfsdirname, ':')) != NULL) {
355 if ((hp = gethostbyname(hostp)) == NULL) {
356 warnx("can't get net id for host");
358 nfsdirname = delimp + 1;
362 * Check if the reverse entrys of the mounttable are really the
363 * same as the normal ones.
365 if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
366 NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
369 * Mark the uppermost mount as unmounted.
371 (void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
373 * If several equal mounts are in the mounttable, check the order
374 * and warn the user if necessary.
376 if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
377 strcmp(resolved, mntonname) != 0) {
378 warnx("cannot umount %s, %s\n "
379 "is mounted there, umount it first",
380 mntonname, mntfromnamerev);
382 /* call getmntname again to set mntcheck[i] to 0 */
383 (void)getmntname(mntfromname, mntonname,
384 NOTHING, &type, UNMARK);
387 free(mntfromnamerev);
389 * Check if we have to start the rpc-call later.
390 * If there are still identical nfs-names mounted,
391 * we skip the rpc-call. Obviously this has to
392 * happen before unmount(2), but it should happen
393 * after the previous namecheck.
395 if (strcmp(type, "nfs") == 0 && getmntname(mntfromname, NULL, NOTHING,
396 &type, COUNT) != NULL)
402 if (unmount(mntonname, fflag) != 0 ) {
403 warn("unmount of %s failed", mntonname);
407 (void)printf("%s: unmount from %s\n", mntfromname, mntonname);
409 * Report to mountd-server which nfsname
410 * has been unmounted.
412 if (hp != NULL && !(fflag & MNT_FORCE) && do_rpc) {
413 memset(&saddr, 0, sizeof(saddr));
414 saddr.sin_family = AF_INET;
416 memmove(&saddr.sin_addr, hp->h_addr,
417 MIN(hp->h_length, sizeof(saddr.sin_addr)));
421 if ((clp = clntudp_create(&saddr,
422 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
423 clnt_pcreateerror("Cannot MNT PRC");
426 clp->cl_auth = authunix_create_default();
429 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
430 nfsdirname, xdr_void, (caddr_t)0, try);
431 if (clnt_stat != RPC_SUCCESS) {
432 clnt_perror(clp, "Bad MNT RPC");
436 * Remove the unmounted entry from /var/db/mounttab.
438 if (read_mtab(mtab)) {
440 clean_mtab(hostp, nfsdirname);
442 warnx("cannot remove entry %s:%s",
446 free(orignfsdirname);
447 auth_destroy(clp->cl_auth);
454 getmntname(const char *fromname, const char *onname,
455 mntwhat what, char **type, dowhat mark)
457 static struct statfs *mntbuf;
458 static size_t mntsize = 0;
459 static char *mntcheck = NULL;
460 static char *mntcount = NULL;
464 if ((mntsize = mntinfo(&mntbuf)) <= 0)
467 if (mntcheck == NULL) {
468 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
469 (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
473 * We want to get the file systems in the reverse order
474 * that they were mounted. Mounted and unmounted filesystems
475 * are marked or unmarked in a table called 'mntcheck'.
476 * Unmount(const char *dir, int flags) does only take the
477 * mountpoint as argument, not the destination. If we don't pay
478 * attention to the order, it can happen that a overlaying
479 * filesystem get's unmounted instead of the one the user
484 /* Return only the specific name */
485 for (i = mntsize - 1; i >= 0; i--) {
486 if (fromname != NULL && what == MNTON &&
487 !strcmp(mntbuf[i].f_mntfromname, fromname) &&
490 *type = mntbuf[i].f_fstypename;
491 return (mntbuf[i].f_mntonname);
493 if (fromname != NULL && what == MNTFROM &&
494 !strcmp(mntbuf[i].f_mntonname, fromname) &&
497 *type = mntbuf[i].f_fstypename;
498 return (mntbuf[i].f_mntfromname);
503 /* Mark current mount with '1' and return name */
504 for (i = mntsize - 1; i >= 0; i--) {
505 if (mntcheck[i] == 0 &&
506 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
507 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
509 return (mntbuf[i].f_mntonname);
514 /* Unmark current mount with '0' and return name */
515 for (i = 0; i < mntsize; i++) {
516 if (mntcheck[i] == 1 &&
517 (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
518 (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
520 return (mntbuf[i].f_mntonname);
525 /* Count the equal mntfromnames */
527 for (i = mntsize - 1; i >= 0; i--) {
528 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
531 /* Mark the already unmounted mounts and return
532 * mntfromname if count <= 1. Else return NULL.
534 for (i = mntsize - 1; i >= 0; i--) {
535 if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
536 if (mntcount[i] == 1)
545 return (mntbuf[i].f_mntonname);
559 namematch(struct hostent *hp)
563 if ((hp == NULL) || (nfshost == NULL))
566 if (strcasecmp(nfshost, hp->h_name) == 0)
569 if ((cp = strchr(hp->h_name, '.')) != NULL) {
571 if (strcasecmp(nfshost, hp->h_name) == 0)
574 for (np = hp->h_aliases; *np; np++) {
575 if (strcasecmp(nfshost, *np) == 0)
577 if ((cp = strchr(*np, '.')) != NULL) {
579 if (strcasecmp(nfshost, *np) == 0)
587 checkmntlist(char *name, char **fromname, char **onname, char **type)
590 *fromname = getmntname(name, NULL, MNTFROM, type, NAME);
591 if (*fromname == NULL) {
592 *onname = getmntname(name, NULL, MNTON, type, NAME);
600 mntinfo(struct statfs **mntbuf)
602 static struct statfs *origbuf;
606 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
609 bufsize = (mntsize + 1) * sizeof(struct statfs);
610 if ((origbuf = malloc(bufsize)) == NULL)
612 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
618 getrealname(char *name, char *realname)
628 if (ISDOT(name + 1) || ISDOTDOT(name + 1))
629 strcpy(realname, "/");
631 if ((dirname = strrchr(name + 1, '/')) == NULL)
632 snprintf(realname, MAXPATHLEN, "%s", name);
637 if (ISDOT(name) || ISDOTDOT(name))
638 (void)realpath(name, realname);
640 if ((dirname = strrchr(name, '/')) == NULL) {
641 if ((realpath(name, realname)) == NULL)
649 if (ISDOT(dirname)) {
651 if ((realpath(name, realname)) == NULL)
653 } else if (ISDOTDOT(dirname)) {
655 if ((realpath(name, realname)) == NULL)
658 if ((realpath(name, realname)) == NULL)
660 baselen = strlen(realname);
661 dirlen = strlen(dirname);
662 if (baselen + dirlen + 1 > MAXPATHLEN)
664 if (realname[1] == '\0') {
665 memmove(realname + 1, dirname, dirlen);
666 realname[dirlen + 1] = '\0';
668 realname[baselen] = '/';
669 memmove(realname + baselen + 1,
671 realname[baselen + dirlen + 1] = '\0';
679 * xdr routines for mount rpc's
682 xdr_dir(XDR *xdrsp, char *dirp)
685 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
692 (void)fprintf(stderr, "%s\n%s\n",
693 "usage: umount [-fv] special | node",
694 " umount -a | -A [-fv] [-h host] [-t type]");