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.28 2001/10/13 02:04:54 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>
41 #include <sys/socket.h>
45 #include <nfs/rpcv2.h>
56 #define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
57 #define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
59 typedef enum { MNTON
, MNTFROM
, NOTHING
} mntwhat
;
60 typedef enum { MARK
, UNMARK
, NAME
, COUNT
, FREE
} dowhat
;
62 struct addrinfo
*nfshost_ai
= NULL
;
66 void checkmntlist (char *, char **, char **, char **);
67 int checkvfsname (const char *, char **);
68 char *getmntname (const char *, const char *,
69 mntwhat
, char **, dowhat
);
70 char *getrealname(char *, char *resolved_path
);
71 char **makevfslist (const char *);
72 int mntinfo (struct statfs
**);
73 int namematch (struct addrinfo
*);
74 int sacmp (struct sockaddr
*, struct sockaddr
*);
75 int umountall (char **);
76 int checkname (char *, char **);
77 int umountfs (char *, char *, char *);
79 int xdr_dir (XDR
*, char *);
82 main(int argc
, char *argv
[])
84 int all
, errs
, ch
, mntsize
, error
;
85 char **typelist
= NULL
, *mntonname
, *mntfromname
;
86 char *type
, *mntfromnamerev
, *mntonnamerev
;
87 struct statfs
*mntbuf
;
88 struct addrinfo hints
;
90 /* Start disks transferring immediately. */
94 while ((ch
= getopt(argc
, argv
, "Aafh:t:v")) != -1)
105 case 'h': /* -h implies -A. */
110 if (typelist
!= NULL
)
111 err(1, "only one -t option may be specified");
112 typelist
= makevfslist(optarg
);
124 if ((argc
== 0 && !all
) || (argc
!= 0 && all
))
127 /* -h implies "-t nfs" if no -t flag. */
128 if ((nfshost
!= NULL
) && (typelist
== NULL
))
129 typelist
= makevfslist("nfs");
131 if (nfshost
!= NULL
) {
132 memset(&hints
, 0, sizeof hints
);
133 error
= getaddrinfo(nfshost
, NULL
, &hints
, &nfshost_ai
);
135 errx(1, "%s: %s", nfshost
, gai_strerror(error
));
140 if ((mntsize
= mntinfo(&mntbuf
)) <= 0)
143 * We unmount the nfs-mounts in the reverse order
144 * that they were mounted.
146 for (errs
= 0, mntsize
--; mntsize
> 0; mntsize
--) {
147 if (checkvfsname(mntbuf
[mntsize
].f_fstypename
,
151 * Check if a mountpoint is laid over by another mount.
152 * A warning will be printed to stderr if this is
153 * the case. The laid over mount remains unmounted.
155 mntonname
= mntbuf
[mntsize
].f_mntonname
;
156 mntfromname
= mntbuf
[mntsize
].f_mntfromname
;
157 mntonnamerev
= getmntname(getmntname(mntonname
,
158 NULL
, MNTFROM
, &type
, NAME
), NULL
,
161 mntfromnamerev
= getmntname(mntonnamerev
,
162 NULL
, MNTFROM
, &type
, NAME
);
164 if (strcmp(mntonnamerev
, mntonname
) == 0 &&
165 strcmp(mntfromnamerev
, mntfromname
) != 0)
166 warnx("cannot umount %s, %s\n "
167 "is mounted there, umount it first",
168 mntonname
, mntfromnamerev
);
170 if (checkname(mntbuf
[mntsize
].f_mntonname
,
178 err(1, "%s", _PATH_FSTAB
);
179 errs
= umountall(typelist
);
182 for (errs
= 0; *argv
!= NULL
; ++argv
)
183 if (checkname(*argv
, typelist
) != 0)
187 getmntname(NULL
, NULL
, NOTHING
, NULL
, FREE
);
192 umountall(char **typelist
)
198 static int firstcall
= 1;
200 if ((fs
= getfsent()) != NULL
)
203 errx(1, "fstab reading failure");
207 /* Ignore the root. */
208 if (strcmp(fs
->fs_file
, "/") == 0)
212 * Historic practice: ignore unknown FSTAB_* fields.
214 if (strcmp(fs
->fs_type
, FSTAB_RW
) &&
215 strcmp(fs
->fs_type
, FSTAB_RO
) &&
216 strcmp(fs
->fs_type
, FSTAB_RQ
))
218 /* Ignore unknown file system types. */
219 if (getvfsbyname(fs
->fs_vfstype
, &vfc
) == -1)
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 strcpy(cp
, fs
->fs_file
);
232 rval
= umountall(typelist
);
233 rval
= checkname(cp
, typelist
) || rval
;
236 } while ((fs
= getfsent()) != NULL
);
241 * Do magic checks on mountpoint and device or hand over
242 * it to unmount(2) if everything fails.
245 checkname(char *name
, char **typelist
)
249 char *mntonname
, *mntfromname
;
250 char *mntfromnamerev
;
251 char *resolved
, realname
[MAXPATHLEN
];
252 char *type
, *hostp
, *delimp
, *origname
;
253 char none
[] = "none";
256 mntfromname
= mntonname
= delimp
= hostp
= NULL
;
259 * 1. Check if the name exists in the mounttable.
261 checkmntlist(name
, &mntfromname
, &mntonname
, &type
);
263 * 2. Remove trailing slashes if there are any. After that
264 * we look up the name in the mounttable again.
266 if (mntfromname
== NULL
&& mntonname
== NULL
) {
267 speclen
= strlen(name
);
268 for (speclen
= strlen(name
);
269 speclen
> 1 && name
[speclen
- 1] == '/';
271 name
[speclen
- 1] = '\0';
272 checkmntlist(name
, &mntfromname
, &mntonname
, &type
);
274 /* Save off original name in origname */
275 if ((origname
= strdup(name
)) == NULL
)
278 * 3. Check if the deprecated nfs-syntax with an '@'
279 * has been used and translate it to the ':' syntax.
280 * Look up the name in the mounttable again.
282 if (mntfromname
== NULL
&& mntonname
== NULL
) {
283 if ((delimp
= strrchr(name
, '@')) != NULL
) {
285 if (*hostp
!= '\0') {
287 * Make both '@' and ':'
290 char *host
= strdup(hostp
);
294 memmove(name
+ len
+ 1, name
,
295 (size_t)(delimp
- name
));
297 memmove(name
, host
, len
);
300 for (speclen
= strlen(name
);
301 speclen
> 1 && name
[speclen
- 1] == '/';
303 name
[speclen
- 1] = '\0';
304 name
[len
+ speclen
+ 1] = '\0';
305 checkmntlist(name
, &mntfromname
,
310 * 4. Check if a relative mountpoint has been
311 * specified. This should happen as last check,
312 * the order is important. To prevent possible
313 * nfs-hangs, we just call realpath(3) on the
314 * basedir of mountpoint and add the dirname again.
315 * Check the name in mounttable one last time.
317 if (mntfromname
== NULL
&& mntonname
== NULL
) {
318 strcpy(name
, origname
);
319 if ((getrealname(name
, realname
)) != NULL
) {
320 checkmntlist(realname
,
321 &mntfromname
, &mntonname
, &type
);
325 * 5. All tests failed, just hand over the
326 * mountpoint to the kernel, maybe the statfs
327 * structure has been truncated or is not
328 * useful anymore because of a chroot(2).
329 * Please note that nfs will not be able to
330 * notify the nfs-server about unmounting.
331 * These things can change in future when the
332 * fstat structure get's more reliable,
333 * but at the moment we cannot thrust it.
335 if (mntfromname
== NULL
&& mntonname
== NULL
) {
336 strcpy(name
, origname
);
337 if (umountfs(NULL
, origname
,
339 warnx("%s not found in "
341 "unmounted it anyway",
355 if (checkvfsname(type
, typelist
))
359 * Check if the reverse entrys of the mounttable are really the
360 * same as the normal ones.
362 if ((mntfromnamerev
= strdup(getmntname(getmntname(mntfromname
,
363 NULL
, MNTON
, &type
, NAME
), NULL
, MNTFROM
, &type
, NAME
))) == NULL
)
366 * Mark the uppermost mount as unmounted.
368 getmntname(mntfromname
, mntonname
, NOTHING
, &type
, MARK
);
370 * If several equal mounts are in the mounttable, check the order
371 * and warn the user if necessary.
373 if (strcmp(mntfromnamerev
, mntfromname
) != 0 &&
374 strcmp(resolved
, mntonname
) != 0) {
375 warnx("cannot umount %s, %s\n "
376 "is mounted there, umount it first",
377 mntonname
, mntfromnamerev
);
379 /* call getmntname again to set mntcheck[i] to 0 */
380 getmntname(mntfromname
, mntonname
,
381 NOTHING
, &type
, UNMARK
);
384 free(mntfromnamerev
);
385 return (umountfs(mntfromname
, mntonname
, type
));
389 * NFS stuff and unmount(2) call
392 umountfs(char *mntfromname
, char *mntonname
, char *type
)
394 enum clnt_stat clnt_stat
;
396 struct addrinfo
*ai
, hints
;
399 char *nfsdirname
, *orignfsdirname
;
400 char *hostp
, *delimp
;
405 nfsdirname
= delimp
= orignfsdirname
= NULL
;
406 memset(&hints
, 0, sizeof hints
);
408 if (strcmp(type
, "nfs") == 0) {
409 if ((nfsdirname
= strdup(mntfromname
)) == NULL
)
411 orignfsdirname
= nfsdirname
;
412 if ((delimp
= strrchr(nfsdirname
, ':')) != NULL
) {
415 getaddrinfo(hostp
, NULL
, &hints
, &ai
);
417 warnx("can't get net id for host");
419 nfsdirname
= delimp
+ 1;
423 * Check if we have to start the rpc-call later.
424 * If there are still identical nfs-names mounted,
425 * we skip the rpc-call. Obviously this has to
426 * happen before unmount(2), but it should happen
427 * after the previous namecheck.
428 * A non-NULL return means that this is the last
429 * mount from mntfromname that is still mounted.
431 if (getmntname(mntfromname
, NULL
, NOTHING
, &type
, COUNT
)
438 if (unmount(mntonname
, fflag
) != 0 ) {
439 warn("unmount of %s failed", mntonname
);
443 printf("%s: unmount from %s\n", mntfromname
, mntonname
);
445 * Report to mountd-server which nfsname
446 * has been unmounted.
448 if (ai
!= NULL
&& !(fflag
& MNT_FORCE
) && do_rpc
) {
449 clp
= clnt_create(hostp
, RPCPROG_MNT
, RPCMNT_VER1
, "udp");
451 warnx("%s: %s", hostp
,
452 clnt_spcreateerror("RPCPROG_MNT"));
455 clp
->cl_auth
= authsys_create_default();
458 clnt_stat
= clnt_call(clp
, RPCMNT_UMOUNT
, (xdrproc_t
)xdr_dir
,
459 nfsdirname
, (xdrproc_t
)xdr_void
, (caddr_t
)0, try);
460 if (clnt_stat
!= RPC_SUCCESS
) {
461 warnx("%s: %s", hostp
,
462 clnt_sperror(clp
, "RPCMNT_UMOUNT"));
466 * Remove the unmounted entry from /var/db/mounttab.
469 clean_mtab(hostp
, nfsdirname
, vflag
);
470 if(!write_mtab(vflag
))
471 warnx("cannot remove mounttab entry %s:%s",
475 free(orignfsdirname
);
476 auth_destroy(clp
->cl_auth
);
483 getmntname(const char *fromname
, const char *onname
,
484 mntwhat what
, char **type
, dowhat mark
)
486 static struct statfs
*mntbuf
;
487 static int mntsize
= 0;
488 static char *mntcheck
= NULL
;
489 static char *mntcount
= NULL
;
493 if ((mntsize
= mntinfo(&mntbuf
)) <= 0)
496 if (mntcheck
== NULL
) {
497 if ((mntcheck
= calloc(mntsize
+ 1, sizeof(int))) == NULL
||
498 (mntcount
= calloc(mntsize
+ 1, sizeof(int))) == NULL
)
502 * We want to get the file systems in the reverse order
503 * that they were mounted. Mounted and unmounted filesystems
504 * are marked or unmarked in a table called 'mntcheck'.
505 * Unmount(const char *dir, int flags) does only take the
506 * mountpoint as argument, not the destination. If we don't pay
507 * attention to the order, it can happen that a overlaying
508 * filesystem get's unmounted instead of the one the user
513 /* Return only the specific name */
514 for (i
= mntsize
- 1; i
>= 0; i
--) {
515 if (fromname
!= NULL
&& what
== MNTON
&&
516 !strcmp(mntbuf
[i
].f_mntfromname
, fromname
) &&
519 *type
= mntbuf
[i
].f_fstypename
;
520 return (mntbuf
[i
].f_mntonname
);
522 if (fromname
!= NULL
&& what
== MNTFROM
&&
523 !strcmp(mntbuf
[i
].f_mntonname
, fromname
) &&
526 *type
= mntbuf
[i
].f_fstypename
;
527 return (mntbuf
[i
].f_mntfromname
);
532 /* Mark current mount with '1' and return name */
533 for (i
= mntsize
- 1; i
>= 0; i
--) {
534 if (mntcheck
[i
] == 0 &&
535 (strcmp(mntbuf
[i
].f_mntonname
, onname
) == 0) &&
536 (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)) {
538 return (mntbuf
[i
].f_mntonname
);
543 /* Unmark current mount with '0' and return name */
544 for (i
= 0; i
< mntsize
; i
++) {
545 if (mntcheck
[i
] == 1 &&
546 (strcmp(mntbuf
[i
].f_mntonname
, onname
) == 0) &&
547 (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)) {
549 return (mntbuf
[i
].f_mntonname
);
554 /* Count the equal mntfromnames */
556 for (i
= mntsize
- 1; i
>= 0; i
--) {
557 if (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0)
560 /* Mark the already unmounted mounts and return
561 * mntfromname if count <= 1. Else return NULL.
563 for (i
= mntsize
- 1; i
>= 0; i
--) {
564 if (strcmp(mntbuf
[i
].f_mntfromname
, fromname
) == 0) {
565 if (mntcount
[i
] == 1)
574 return (mntbuf
[i
].f_mntonname
);
588 sacmp(struct sockaddr
*sa1
, struct sockaddr
*sa2
)
593 if (sa1
->sa_family
!= sa2
->sa_family
)
596 switch (sa1
->sa_family
) {
598 p1
= &((struct sockaddr_in
*)sa1
)->sin_addr
;
599 p2
= &((struct sockaddr_in
*)sa2
)->sin_addr
;
603 p1
= &((struct sockaddr_in6
*)sa1
)->sin6_addr
;
604 p2
= &((struct sockaddr_in6
*)sa2
)->sin6_addr
;
606 if (((struct sockaddr_in6
*)sa1
)->sin6_scope_id
!=
607 ((struct sockaddr_in6
*)sa2
)->sin6_scope_id
)
614 return memcmp(p1
, p2
, len
);
618 namematch(struct addrinfo
*ai
)
620 struct addrinfo
*aip
;
622 if (nfshost
== NULL
|| nfshost_ai
== NULL
)
627 while (aip
!= NULL
) {
628 if (sacmp(ai
->ai_addr
, aip
->ai_addr
) == 0)
639 checkmntlist(char *name
, char **fromname
, char **onname
, char **type
)
642 *fromname
= getmntname(name
, NULL
, MNTFROM
, type
, NAME
);
643 if (*fromname
== NULL
) {
644 *onname
= getmntname(name
, NULL
, MNTON
, type
, NAME
);
652 mntinfo(struct statfs
**mntbuf
)
654 static struct statfs
*origbuf
;
658 mntsize
= getfsstat(NULL
, 0, MNT_NOWAIT
);
661 bufsize
= (mntsize
+ 1) * sizeof(struct statfs
);
662 if ((origbuf
= malloc(bufsize
)) == NULL
)
664 mntsize
= getfsstat(origbuf
, (long)bufsize
, MNT_NOWAIT
);
670 getrealname(char *name
, char *realname
)
680 if (ISDOT(name
+ 1) || ISDOTDOT(name
+ 1))
681 strcpy(realname
, "/");
683 if ((dirname
= strrchr(name
+ 1, '/')) == NULL
)
684 snprintf(realname
, MAXPATHLEN
, "%s", name
);
689 if (ISDOT(name
) || ISDOTDOT(name
))
690 realpath(name
, realname
);
692 if ((dirname
= strrchr(name
, '/')) == NULL
) {
693 if ((realpath(name
, realname
)) == NULL
)
701 if (ISDOT(dirname
)) {
703 if ((realpath(name
, realname
)) == NULL
)
705 } else if (ISDOTDOT(dirname
)) {
707 if ((realpath(name
, realname
)) == NULL
)
710 if ((realpath(name
, realname
)) == NULL
)
712 baselen
= strlen(realname
);
713 dirlen
= strlen(dirname
);
714 if (baselen
+ dirlen
+ 1 > MAXPATHLEN
)
716 if (realname
[1] == '\0') {
717 memmove(realname
+ 1, dirname
, dirlen
);
718 realname
[dirlen
+ 1] = '\0';
720 realname
[baselen
] = '/';
721 memmove(realname
+ baselen
+ 1,
723 realname
[baselen
+ dirlen
+ 1] = '\0';
731 * xdr routines for mount rpc's
734 xdr_dir(XDR
*xdrsp
, char *dirp
)
737 return (xdr_string(xdrsp
, &dirp
, RPCMNT_PATHLEN
));
744 fprintf(stderr
, "%s\n%s\n",
745 "usage: umount [-fv] special | node",
746 " umount -a | -A [-fv] [-h host] [-t type]");