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 *);
73 void usage (void) __dead2
;
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",
357 if (checkvfsname(type
, typelist
))
361 * Check if the reverse entrys of the mounttable are really the
362 * same as the normal ones.
364 if ((mntfromnamerev
= strdup(getmntname(getmntname(mntfromname
,
365 NULL
, MNTON
, &type
, NAME
), NULL
, MNTFROM
, &type
, NAME
))) == NULL
)
368 * Mark the uppermost mount as unmounted.
370 getmntname(mntfromname
, mntonname
, NOTHING
, &type
, MARK
);
372 * If several equal mounts are in the mounttable, check the order
373 * and warn the user if necessary.
375 if (strcmp(mntfromnamerev
, mntfromname
) != 0 &&
376 strcmp(resolved
, mntonname
) != 0) {
377 warnx("cannot umount %s, %s\n "
378 "is mounted there, umount it first",
379 mntonname
, mntfromnamerev
);
381 /* call getmntname again to set mntcheck[i] to 0 */
382 getmntname(mntfromname
, mntonname
,
383 NOTHING
, &type
, UNMARK
);
386 free(mntfromnamerev
);
387 return (umountfs(mntfromname
, mntonname
, type
));
391 * NFS stuff and unmount(2) call
394 umountfs(char *mntfromname
, char *mntonname
, char *type
)
396 enum clnt_stat clnt_stat
;
398 struct addrinfo
*ai
, hints
;
401 char *nfsdirname
, *orignfsdirname
;
402 char *hostp
, *delimp
;
407 nfsdirname
= delimp
= orignfsdirname
= NULL
;
408 memset(&hints
, 0, sizeof hints
);
410 if (strcmp(type
, "nfs") == 0) {
411 if ((nfsdirname
= strdup(mntfromname
)) == NULL
)
413 orignfsdirname
= nfsdirname
;
414 if ((delimp
= strrchr(nfsdirname
, ':')) != NULL
) {
417 getaddrinfo(hostp
, NULL
, &hints
, &ai
);
419 warnx("can't get net id for host");
421 nfsdirname
= delimp
+ 1;
425 * Check if we have to start the rpc-call later.
426 * If there are still identical nfs-names mounted,
427 * we skip the rpc-call. Obviously this has to
428 * happen before unmount(2), but it should happen
429 * after the previous namecheck.
430 * A non-NULL return means that this is the last
431 * mount from mntfromname that is still mounted.
433 if (getmntname(mntfromname
, NULL
, NOTHING
, &type
, COUNT
)
440 if (unmount(mntonname
, fflag
) != 0 ) {
441 warn("unmount of %s failed", mntonname
);
445 printf("%s: unmount from %s\n", mntfromname
, mntonname
);
447 * Report to mountd-server which nfsname
448 * has been unmounted.
450 if (ai
!= NULL
&& !(fflag
& MNT_FORCE
) && do_rpc
) {
451 clp
= clnt_create(hostp
, RPCPROG_MNT
, RPCMNT_VER1
, "udp");
453 warnx("%s: %s", hostp
,
454 clnt_spcreateerror("RPCPROG_MNT"));
457 clp
->cl_auth
= authsys_create_default();
460 clnt_stat
= clnt_call(clp
, RPCMNT_UMOUNT
, (xdrproc_t
)xdr_dir
,
461 nfsdirname
, (xdrproc_t
)xdr_void
, (caddr_t
)0, try);
462 if (clnt_stat
!= RPC_SUCCESS
) {
463 warnx("%s: %s", hostp
,
464 clnt_sperror(clp
, "RPCMNT_UMOUNT"));
468 * Remove the unmounted entry from /var/db/mounttab.
471 clean_mtab(hostp
, nfsdirname
, vflag
);
472 if(!write_mtab(vflag
))
473 warnx("cannot remove mounttab entry %s:%s",
477 free(orignfsdirname
);
478 auth_destroy(clp
->cl_auth
);
485 getmntname(const char *fromname
, const char *onname
,
486 mntwhat what
, char **type
, dowhat mark
)
488 static struct statfs
*mntbuf
;
489 static int mntsize
= 0;
490 static char *mntcheck
= NULL
;
491 static char *mntcount
= NULL
;
495 if ((mntsize
= mntinfo(&mntbuf
)) <= 0)
498 if (mntcheck
== NULL
) {
499 if ((mntcheck
= calloc(mntsize
+ 1, sizeof(int))) == NULL
||
500 (mntcount
= calloc(mntsize
+ 1, sizeof(int))) == NULL
)
504 * We want to get the file systems in the reverse order
505 * that they were mounted. Mounted and unmounted filesystems
506 * are marked or unmarked in a table called 'mntcheck'.
507 * Unmount(const char *dir, int flags) does only take the
508 * mountpoint as argument, not the destination. If we don't pay
509 * attention to the order, it can happen that a overlaying
510 * filesystem get's unmounted instead of the one the user
515 /* Return only the specific name */
516 for (i
= mntsize
- 1; i
>= 0; i
--) {
517 if (fromname
!= NULL
&& what
== MNTON
&&
518 !strcmp(mntbuf
[i
].f_mntfromname
, fromname
) &&
521 *type
= mntbuf
[i
].f_fstypename
;
522 return (mntbuf
[i
].f_mntonname
);
524 if (fromname
!= NULL
&& what
== MNTFROM
&&
525 !strcmp(mntbuf
[i
].f_mntonname
, fromname
) &&
528 *type
= mntbuf
[i
].f_fstypename
;
529 return (mntbuf
[i
].f_mntfromname
);
534 /* Mark current mount with '1' and return name */
535 for (i
= mntsize
- 1; i
>= 0; i
--) {
536 if (mntcheck
[i
] == 0 &&
537 (strcmp(mntbuf
[i
].f_mntonname
, onname
) == 0) &&
538 (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)) {
540 return (mntbuf
[i
].f_mntonname
);
545 /* Unmark current mount with '0' and return name */
546 for (i
= 0; i
< mntsize
; i
++) {
547 if (mntcheck
[i
] == 1 &&
548 (strcmp(mntbuf
[i
].f_mntonname
, onname
) == 0) &&
549 (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)) {
551 return (mntbuf
[i
].f_mntonname
);
556 /* Count the equal mntfromnames */
558 for (i
= mntsize
- 1; i
>= 0; i
--) {
559 if (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)
562 /* Mark the already unmounted mounts and return
563 * mntfromname if count <= 1. Else return NULL.
565 for (i
= mntsize
- 1; i
>= 0; i
--) {
566 if (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0) {
567 if (mntcount
[i
] == 1)
576 return (mntbuf
[i
].f_mntonname
);
590 sacmp(struct sockaddr
*sa1
, struct sockaddr
*sa2
)
595 if (sa1
->sa_family
!= sa2
->sa_family
)
598 switch (sa1
->sa_family
) {
600 p1
= &((struct sockaddr_in
*)sa1
)->sin_addr
;
601 p2
= &((struct sockaddr_in
*)sa2
)->sin_addr
;
605 p1
= &((struct sockaddr_in6
*)sa1
)->sin6_addr
;
606 p2
= &((struct sockaddr_in6
*)sa2
)->sin6_addr
;
608 if (((struct sockaddr_in6
*)sa1
)->sin6_scope_id
!=
609 ((struct sockaddr_in6
*)sa2
)->sin6_scope_id
)
616 return memcmp(p1
, p2
, len
);
620 namematch(struct addrinfo
*ai
)
622 struct addrinfo
*aip
;
624 if (nfshost
== NULL
|| nfshost_ai
== NULL
)
629 while (aip
!= NULL
) {
630 if (sacmp(ai
->ai_addr
, aip
->ai_addr
) == 0)
641 checkmntlist(char *name
, char **fromname
, char **onname
, char **type
)
644 *fromname
= getmntname(name
, NULL
, MNTFROM
, type
, NAME
);
645 if (*fromname
== NULL
) {
646 *onname
= getmntname(name
, NULL
, MNTON
, type
, NAME
);
654 mntinfo(struct statfs
**mntbuf
)
656 static struct statfs
*origbuf
;
660 mntsize
= getfsstat(NULL
, 0, MNT_NOWAIT
);
663 bufsize
= (mntsize
+ 1) * sizeof(struct statfs
);
664 if ((origbuf
= malloc(bufsize
)) == NULL
)
666 mntsize
= getfsstat(origbuf
, (long)bufsize
, MNT_NOWAIT
);
672 getrealname(char *name
, char *realname
)
682 if (ISDOT(name
+ 1) || ISDOTDOT(name
+ 1))
683 strcpy(realname
, "/");
685 if ((dirname
= strrchr(name
+ 1, '/')) == NULL
)
686 snprintf(realname
, MAXPATHLEN
, "%s", name
);
691 if (ISDOT(name
) || ISDOTDOT(name
))
692 realpath(name
, realname
);
694 if ((dirname
= strrchr(name
, '/')) == NULL
) {
695 if ((realpath(name
, realname
)) == NULL
)
703 if (ISDOT(dirname
)) {
705 if ((realpath(name
, realname
)) == NULL
)
707 } else if (ISDOTDOT(dirname
)) {
709 if ((realpath(name
, realname
)) == NULL
)
712 if ((realpath(name
, realname
)) == NULL
)
714 baselen
= strlen(realname
);
715 dirlen
= strlen(dirname
);
716 if (baselen
+ dirlen
+ 1 > MAXPATHLEN
)
718 if (realname
[1] == '\0') {
719 memmove(realname
+ 1, dirname
, dirlen
);
720 realname
[dirlen
+ 1] = '\0';
722 realname
[baselen
] = '/';
723 memmove(realname
+ baselen
+ 1,
725 realname
[baselen
+ dirlen
+ 1] = '\0';
733 * xdr routines for mount rpc's
736 xdr_dir(XDR
*xdrsp
, char *dirp
)
739 return (xdr_string(xdrsp
, &dirp
, RPCMNT_PATHLEN
));
746 fprintf(stderr
, "%s\n%s\n",
747 "usage: umount [-fv] special | node",
748 " umount -a | -A [-F fstab] [-fv] [-h host] [-t type]");