1994-05-26 06:35:07 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-08-03 06:44:46 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-08-03 06:44:46 +00:00
|
|
|
#if 0
|
1997-06-16 11:20:05 +00:00
|
|
|
static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95";
|
1998-08-03 06:44:46 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <nfs/rpcv2.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-10-17 16:26:58 +00:00
|
|
|
#define ISDOT(x) ((x)[0] == '.' && (x)[1] == '\0')
|
|
|
|
#define ISDOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
|
|
|
|
|
|
|
|
typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
|
|
|
|
typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
|
|
|
|
|
|
|
|
int fflag, vflag, count;
|
|
|
|
char *nfshost;
|
|
|
|
|
|
|
|
void checkmntlist (char *, char **, char **, char **);
|
|
|
|
int checkvfsname (const char *, char **);
|
|
|
|
char *getmntname (const char *, const char *,
|
|
|
|
mntwhat, char **, dowhat);
|
|
|
|
char *getrealname(char *, char *resolved_path);
|
|
|
|
char **makevfslist (const char *);
|
|
|
|
size_t mntinfo (struct statfs **);
|
|
|
|
int namematch (struct hostent *);
|
|
|
|
int selected (int);
|
|
|
|
int umountall (char **);
|
|
|
|
int umountfs (char *, char **);
|
|
|
|
void usage (void);
|
|
|
|
int xdr_dir (XDR *, char *);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
int
|
1999-10-17 16:26:58 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1999-10-17 16:26:58 +00:00
|
|
|
int all, errs, ch, mntsize;
|
|
|
|
char **typelist = NULL, *mntonname, *mntfromname;
|
|
|
|
char *type, *mntfromnamerev, *mntonnamerev;
|
1997-06-16 11:20:05 +00:00
|
|
|
struct statfs *mntbuf;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
/* Start disks transferring immediately. */
|
|
|
|
sync();
|
|
|
|
|
1999-10-17 16:26:58 +00:00
|
|
|
all = count = errs = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (ch) {
|
1997-06-16 11:20:05 +00:00
|
|
|
case 'A':
|
|
|
|
all = 2;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'a':
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = MNT_FORCE;
|
|
|
|
break;
|
1997-06-16 11:20:05 +00:00
|
|
|
case 'h': /* -h implies -A. */
|
|
|
|
all = 2;
|
1994-05-26 06:35:07 +00:00
|
|
|
nfshost = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
1997-06-16 11:20:05 +00:00
|
|
|
if (typelist != NULL)
|
1999-10-17 16:26:58 +00:00
|
|
|
err(1, "only one -t option may be specified");
|
1997-06-16 11:20:05 +00:00
|
|
|
typelist = makevfslist(optarg);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1998-08-03 06:44:46 +00:00
|
|
|
if ((argc == 0 && !all) || (argc != 0 && all))
|
1994-05-26 06:35:07 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
/* -h implies "-t nfs" if no -t flag. */
|
|
|
|
if ((nfshost != NULL) && (typelist == NULL))
|
1997-06-16 11:20:05 +00:00
|
|
|
typelist = makevfslist("nfs");
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1997-06-16 11:20:05 +00:00
|
|
|
switch (all) {
|
|
|
|
case 2:
|
1999-10-17 16:26:58 +00:00
|
|
|
if ((mntsize = mntinfo(&mntbuf)) <= 0)
|
1997-06-16 11:20:05 +00:00
|
|
|
break;
|
1999-10-17 16:26:58 +00:00
|
|
|
/*
|
|
|
|
* We unmount the nfs-mounts in the reverse order
|
|
|
|
* that they were mounted.
|
|
|
|
*/
|
|
|
|
for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
|
|
|
|
if (checkvfsname(mntbuf[mntsize].f_fstypename,
|
|
|
|
typelist))
|
1997-06-16 11:20:05 +00:00
|
|
|
continue;
|
1999-10-17 16:26:58 +00:00
|
|
|
/*
|
|
|
|
* Check if a mountpoint is laid over by another mount.
|
|
|
|
* A warning will be printed to stderr if this is
|
|
|
|
* the case. The laid over mount remains unmounted.
|
|
|
|
*/
|
|
|
|
mntonname = mntbuf[mntsize].f_mntonname;
|
|
|
|
mntfromname = mntbuf[mntsize].f_mntfromname;
|
|
|
|
mntonnamerev = getmntname(getmntname(mntonname,
|
|
|
|
NULL, MNTFROM, &type, NAME), NULL,
|
|
|
|
MNTON, &type, NAME);
|
|
|
|
|
|
|
|
mntfromnamerev = getmntname(mntonnamerev,
|
|
|
|
NULL, MNTFROM, &type, NAME);
|
|
|
|
|
|
|
|
if (strcmp(mntonnamerev, mntonname) == 0 &&
|
|
|
|
strcmp(mntfromnamerev, mntfromname ) != 0)
|
|
|
|
warnx("cannot umount %s, %s\n "
|
|
|
|
"is mounted there, umount it first",
|
|
|
|
mntonname, mntfromnamerev);
|
|
|
|
|
|
|
|
if (umountfs(mntbuf[mntsize].f_mntonname,
|
|
|
|
typelist) != 0)
|
1997-06-16 11:20:05 +00:00
|
|
|
errs = 1;
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
free(mntbuf);
|
1997-06-16 11:20:05 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
1994-05-26 06:35:07 +00:00
|
|
|
if (setfsent() == 0)
|
|
|
|
err(1, "%s", _PATH_FSTAB);
|
1997-06-16 11:20:05 +00:00
|
|
|
errs = umountall(typelist);
|
|
|
|
break;
|
|
|
|
case 0:
|
1994-05-26 06:35:07 +00:00
|
|
|
for (errs = 0; *argv != NULL; ++argv)
|
1997-06-16 11:20:05 +00:00
|
|
|
if (umountfs(*argv, typelist) != 0)
|
|
|
|
errs = 1;
|
|
|
|
break;
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
(void)getmntname(NULL, NULL, NOTHING, NULL, FREE);
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(errs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-10-17 16:26:58 +00:00
|
|
|
umountall(char **typelist)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1999-10-18 03:52:20 +00:00
|
|
|
struct vfsconf vfc;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct fstab *fs;
|
1998-08-03 06:44:46 +00:00
|
|
|
int rval;
|
1994-05-26 06:35:07 +00:00
|
|
|
char *cp;
|
1999-10-18 03:52:20 +00:00
|
|
|
static int firstcall = 1;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1999-10-18 03:52:20 +00:00
|
|
|
if ((fs = getfsent()) != NULL) {
|
|
|
|
if (firstcall)
|
|
|
|
errx(1, "fstab reading failure");
|
|
|
|
firstcall = 0;
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
do {
|
1994-05-26 06:35:07 +00:00
|
|
|
/* Ignore the root. */
|
|
|
|
if (strcmp(fs->fs_file, "/") == 0)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Historic practice: ignore unknown FSTAB_* fields.
|
|
|
|
*/
|
|
|
|
if (strcmp(fs->fs_type, FSTAB_RW) &&
|
|
|
|
strcmp(fs->fs_type, FSTAB_RO) &&
|
|
|
|
strcmp(fs->fs_type, FSTAB_RQ))
|
|
|
|
continue;
|
|
|
|
/* If an unknown file system type, complain. */
|
1999-10-17 16:26:58 +00:00
|
|
|
if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) {
|
1994-05-26 06:35:07 +00:00
|
|
|
warnx("%s: unknown mount type", fs->fs_vfstype);
|
|
|
|
continue;
|
|
|
|
}
|
1997-06-16 11:20:05 +00:00
|
|
|
if (checkvfsname(fs->fs_vfstype, typelist))
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
|
1995-05-30 06:12:45 +00:00
|
|
|
/*
|
1994-05-26 06:35:07 +00:00
|
|
|
* We want to unmount the file systems in the reverse order
|
|
|
|
* that they were mounted. So, we save off the file name
|
|
|
|
* in some allocated memory, and then call recursively.
|
|
|
|
*/
|
|
|
|
if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
|
1999-10-17 16:26:58 +00:00
|
|
|
err(1, "malloc failed");
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)strcpy(cp, fs->fs_file);
|
1997-06-16 11:20:05 +00:00
|
|
|
rval = umountall(typelist);
|
1999-10-18 03:52:20 +00:00
|
|
|
rval = umountfs(cp, typelist) || rval;
|
|
|
|
free(cp);
|
|
|
|
return (rval);
|
1999-10-17 16:26:58 +00:00
|
|
|
} while ((fs = getfsent()) != NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-10-17 16:26:58 +00:00
|
|
|
umountfs(char *name, char **typelist)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
enum clnt_stat clnt_stat;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
struct timeval pertry, try;
|
|
|
|
CLIENT *clp;
|
1999-10-17 16:26:58 +00:00
|
|
|
size_t len;
|
|
|
|
int so, speclen;
|
|
|
|
char *mntonname, *mntfromname;
|
1999-10-18 03:52:20 +00:00
|
|
|
char *mntfromnamerev;
|
1999-10-17 16:26:58 +00:00
|
|
|
char *nfsdirname, *orignfsdirname;
|
|
|
|
char *resolved, realname[MAXPATHLEN + 1];
|
|
|
|
char *type, *delimp, *hostp, *origname;
|
|
|
|
|
1999-10-18 03:52:20 +00:00
|
|
|
len = 0;
|
1999-10-17 16:26:58 +00:00
|
|
|
mntfromname = mntonname = delimp = hostp = orignfsdirname = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1. Check if the name exists in the mounttable.
|
|
|
|
*/
|
|
|
|
(void)checkmntlist(name, &mntfromname, &mntonname, &type);
|
|
|
|
/*
|
|
|
|
* 2. Remove trailing slashes if there are any. After that
|
|
|
|
* we look up the name in the mounttable again.
|
|
|
|
*/
|
|
|
|
if (mntfromname == NULL && mntonname == NULL) {
|
|
|
|
speclen = strlen(name);
|
|
|
|
for (speclen = strlen(name);
|
|
|
|
speclen > 1 && name[speclen - 1] == '/';
|
|
|
|
speclen--)
|
|
|
|
name[speclen - 1] = '\0';
|
|
|
|
(void)checkmntlist(name, &mntfromname, &mntonname, &type);
|
|
|
|
resolved = name;
|
|
|
|
/* Save off original name in origname */
|
|
|
|
if ((origname = strdup(name)) == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
/*
|
|
|
|
* 3. Check if the deprecated nfs-syntax with an '@'
|
|
|
|
* has been used and translate it to the ':' syntax.
|
|
|
|
* Look up the name in the mounttable again.
|
|
|
|
*/
|
|
|
|
if (mntfromname == NULL && mntonname == NULL) {
|
|
|
|
if ((delimp = strrchr(name, '@')) != NULL) {
|
|
|
|
hostp = delimp + 1;
|
|
|
|
if (*hostp != '\0') {
|
|
|
|
/*
|
|
|
|
* Make both '@' and ':'
|
|
|
|
* notations equal
|
|
|
|
*/
|
|
|
|
char *host = strdup(hostp);
|
|
|
|
len = strlen(hostp);
|
|
|
|
if (host == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
memmove(name + len + 1, name,
|
|
|
|
(size_t)(delimp - name));
|
|
|
|
name[len] = ':';
|
|
|
|
memmove(name, host, len);
|
|
|
|
free(host);
|
|
|
|
}
|
|
|
|
for (speclen = strlen(name);
|
|
|
|
speclen > 1 && name[speclen - 1] == '/';
|
|
|
|
speclen--)
|
|
|
|
name[speclen - 1] = '\0';
|
|
|
|
name[len + speclen + 1] = '\0';
|
|
|
|
(void)checkmntlist(name, &mntfromname,
|
|
|
|
&mntonname, &type);
|
|
|
|
resolved = name;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 4. Check if a relative mountpoint has been
|
|
|
|
* specified. This should happen as last check,
|
|
|
|
* the order is important. To prevent possible
|
|
|
|
* nfs-hangs, we just call realpath(3) on the
|
|
|
|
* basedir of mountpoint and add the dirname again.
|
|
|
|
* Check the name in mounttable one last time.
|
|
|
|
*/
|
|
|
|
if (mntfromname == NULL && mntonname == NULL) {
|
|
|
|
(void)strcpy(name, origname);
|
|
|
|
if ((getrealname(name, realname)) != NULL) {
|
|
|
|
(void)checkmntlist(realname,
|
|
|
|
&mntfromname, &mntonname, &type);
|
|
|
|
resolved = realname;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* All tests failed, return to main()
|
|
|
|
*/
|
|
|
|
if (mntfromname == NULL && mntonname == NULL) {
|
|
|
|
(void)strcpy(name, origname);
|
|
|
|
warnx("%s: not currently mounted",
|
|
|
|
origname);
|
|
|
|
free(origname);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
free(origname);
|
|
|
|
} else
|
|
|
|
resolved = name;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1997-06-16 11:20:05 +00:00
|
|
|
if (checkvfsname(type, typelist))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
hp = NULL;
|
1999-10-17 16:26:58 +00:00
|
|
|
nfsdirname = NULL;
|
1997-06-16 11:20:05 +00:00
|
|
|
if (!strcmp(type, "nfs")) {
|
1999-10-17 16:26:58 +00:00
|
|
|
if ((nfsdirname = strdup(mntfromname)) == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
orignfsdirname = nfsdirname;
|
|
|
|
if ((delimp = strchr(nfsdirname, ':')) != NULL) {
|
1997-06-16 11:20:05 +00:00
|
|
|
*delimp = '\0';
|
1999-10-17 16:26:58 +00:00
|
|
|
hostp = nfsdirname;
|
|
|
|
if ((hp = gethostbyname(hostp)) == NULL) {
|
|
|
|
warnx("can't get net id for host");
|
|
|
|
}
|
|
|
|
nfsdirname = delimp + 1;
|
1997-06-16 11:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
/*
|
|
|
|
* Check if the reverse entrys of the mounttable are really the
|
|
|
|
* same as the normal ones.
|
|
|
|
*/
|
|
|
|
if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
|
1999-10-18 03:52:20 +00:00
|
|
|
NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
|
1999-10-17 16:26:58 +00:00
|
|
|
err(1, "strdup");
|
|
|
|
/*
|
|
|
|
* Mark the uppermost mount as unmounted.
|
|
|
|
*/
|
1999-10-18 03:52:20 +00:00
|
|
|
(void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
|
1999-10-17 16:26:58 +00:00
|
|
|
/*
|
|
|
|
* If several equal mounts are in the mounttable, check the order
|
|
|
|
* and warn the user if necessary.
|
|
|
|
*/
|
|
|
|
if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
|
|
|
|
strcmp(resolved, mntonname) != 0) {
|
|
|
|
warnx("cannot umount %s, %s\n "
|
|
|
|
"is mounted there, umount it first",
|
|
|
|
mntonname, mntfromnamerev);
|
|
|
|
|
|
|
|
/* call getmntname again to set mntcheck[i] to 0 */
|
1999-10-18 03:52:20 +00:00
|
|
|
(void)getmntname(mntfromname, mntonname,
|
1999-10-17 16:26:58 +00:00
|
|
|
NOTHING, &type, UNMARK);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
free(mntfromnamerev);
|
|
|
|
/*
|
|
|
|
* Check if we have to start the rpc-call later.
|
|
|
|
* If there are still identical nfs-names mounted,
|
|
|
|
* we skip the rpc-call. Obviously this has to
|
|
|
|
* happen before unmount(2), but it should happen
|
|
|
|
* after the previous namecheck.
|
|
|
|
*/
|
|
|
|
if (!strcmp(type, "nfs")) {
|
|
|
|
if (getmntname(mntfromname, NULL, NOTHING,
|
|
|
|
&type, COUNT) == NULL)
|
|
|
|
count = 1;
|
1999-10-18 03:52:20 +00:00
|
|
|
else
|
|
|
|
count = 0;
|
1999-10-17 16:26:58 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
if (!namematch(hp))
|
1997-06-16 11:20:05 +00:00
|
|
|
return (1);
|
1999-10-17 16:26:58 +00:00
|
|
|
if (unmount(mntonname, fflag) != 0 ) {
|
|
|
|
warn("unmount of %s failed", mntonname);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
if (vflag)
|
|
|
|
(void)printf("%s: unmount from %s\n", mntfromname, mntonname);
|
|
|
|
/*
|
|
|
|
* Report to mountd-server which nfsname
|
|
|
|
* has been unmounted.
|
|
|
|
*/
|
|
|
|
if (hp != NULL && !(fflag & MNT_FORCE) && count == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
memset(&saddr, 0, sizeof(saddr));
|
|
|
|
saddr.sin_family = AF_INET;
|
|
|
|
saddr.sin_port = 0;
|
1997-12-26 23:28:17 +00:00
|
|
|
memmove(&saddr.sin_addr, hp->h_addr,
|
|
|
|
MIN(hp->h_length, sizeof(saddr.sin_addr)));
|
1994-05-26 06:35:07 +00:00
|
|
|
pertry.tv_sec = 3;
|
|
|
|
pertry.tv_usec = 0;
|
|
|
|
so = RPC_ANYSOCK;
|
|
|
|
if ((clp = clntudp_create(&saddr,
|
|
|
|
RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
|
|
|
|
clnt_pcreateerror("Cannot MNT PRC");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
clp->cl_auth = authunix_create_default();
|
|
|
|
try.tv_sec = 20;
|
|
|
|
try.tv_usec = 0;
|
1999-10-17 16:26:58 +00:00
|
|
|
clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
|
|
|
|
nfsdirname, xdr_void, (caddr_t)0, try);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (clnt_stat != RPC_SUCCESS) {
|
|
|
|
clnt_perror(clp, "Bad MNT RPC");
|
|
|
|
return (1);
|
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
free(orignfsdirname);
|
1994-05-26 06:35:07 +00:00
|
|
|
auth_destroy(clp->cl_auth);
|
|
|
|
clnt_destroy(clp);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1999-10-17 16:26:58 +00:00
|
|
|
getmntname(const char *fromname, const char *onname,
|
|
|
|
mntwhat what, char **type, dowhat mark)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1997-06-16 11:20:05 +00:00
|
|
|
static struct statfs *mntbuf;
|
1999-10-17 16:26:58 +00:00
|
|
|
static size_t mntsize = 0;
|
|
|
|
static char *mntcheck = NULL;
|
|
|
|
static char *mntcount = NULL;
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
if (mntsize <= 0) {
|
|
|
|
if ((mntsize = mntinfo(&mntbuf)) <= 0)
|
|
|
|
return (NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
if (mntcheck == NULL) {
|
|
|
|
if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
|
|
|
|
(mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
|
|
|
|
err(1, "calloc");
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We want to get the file systems in the reverse order
|
|
|
|
* that they were mounted. Mounted and unmounted filesystems
|
|
|
|
* are marked or unmarked in a table called 'mntcheck'.
|
|
|
|
* Unmount(const char *dir, int flags) does only take the
|
|
|
|
* mountpoint as argument, not the destination. If we don't pay
|
|
|
|
* attention to the order, it can happen that a overlaying
|
|
|
|
* filesystem get's unmounted instead of the one the user
|
|
|
|
* has choosen.
|
|
|
|
*/
|
|
|
|
switch (mark) {
|
|
|
|
case NAME:
|
|
|
|
/* Return only the specific name */
|
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
|
|
|
if (fromname != NULL && what == MNTON &&
|
|
|
|
!strcmp(mntbuf[i].f_mntfromname, fromname) &&
|
|
|
|
mntcheck[i] != 1) {
|
|
|
|
if (type)
|
|
|
|
*type = mntbuf[i].f_fstypename;
|
|
|
|
return (mntbuf[i].f_mntonname);
|
|
|
|
}
|
|
|
|
if (fromname != NULL && what == MNTFROM &&
|
|
|
|
!strcmp(mntbuf[i].f_mntonname, fromname) &&
|
|
|
|
mntcheck[i] != 1) {
|
|
|
|
if (type)
|
|
|
|
*type = mntbuf[i].f_fstypename;
|
|
|
|
return (mntbuf[i].f_mntfromname);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
return (NULL);
|
|
|
|
case MARK:
|
|
|
|
/* Mark current mount with '1' and return name */
|
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
|
|
|
if (mntcheck[i] == 0 &&
|
|
|
|
(strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
|
|
|
|
(strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
|
|
|
|
mntcheck[i] = 1;
|
|
|
|
return (mntbuf[i].f_mntonname);
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1999-10-17 16:26:58 +00:00
|
|
|
return (NULL);
|
|
|
|
case UNMARK:
|
|
|
|
/* Unmark current mount with '0' and return name */
|
|
|
|
for (i = 0; i < mntsize; i++) {
|
|
|
|
if (mntcheck[i] == 1 &&
|
|
|
|
(strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
|
|
|
|
(strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
|
|
|
|
mntcheck[i] = 0;
|
|
|
|
return (mntbuf[i].f_mntonname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
case COUNT:
|
|
|
|
/* Count the equal mntfromnames */
|
|
|
|
count = 0;
|
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
|
|
|
if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
/* Mark the already unmounted mounts and return
|
1999-10-18 03:52:20 +00:00
|
|
|
* mntfromname if count <= 1. Else return NULL.
|
1999-10-17 16:26:58 +00:00
|
|
|
*/
|
|
|
|
for (i = mntsize - 1; i >= 0; i--) {
|
|
|
|
if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
|
|
|
|
if (mntcount[i] == 1)
|
|
|
|
count--;
|
|
|
|
else
|
|
|
|
mntcount[i] = 1;
|
|
|
|
}
|
|
|
|
}
|
1999-10-18 03:52:20 +00:00
|
|
|
if (count <= 1)
|
|
|
|
return (mntbuf[i].f_mntonname);
|
|
|
|
else
|
|
|
|
return (NULL);
|
1999-10-17 16:26:58 +00:00
|
|
|
case FREE:
|
|
|
|
free(mntbuf);
|
|
|
|
free(mntcheck);
|
|
|
|
return (NULL);
|
|
|
|
default:
|
|
|
|
return (NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-10-17 16:26:58 +00:00
|
|
|
namematch(struct hostent *hp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
char *cp, **np;
|
|
|
|
|
|
|
|
if ((hp == NULL) || (nfshost == NULL))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (strcasecmp(nfshost, hp->h_name) == 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if ((cp = strchr(hp->h_name, '.')) != NULL) {
|
|
|
|
*cp = '\0';
|
|
|
|
if (strcasecmp(nfshost, hp->h_name) == 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (np = hp->h_aliases; *np; np++) {
|
|
|
|
if (strcasecmp(nfshost, *np) == 0)
|
|
|
|
return (1);
|
|
|
|
if ((cp = strchr(*np, '.')) != NULL) {
|
|
|
|
*cp = '\0';
|
|
|
|
if (strcasecmp(nfshost, *np) == 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-10-17 16:26:58 +00:00
|
|
|
void
|
|
|
|
checkmntlist(char *name, char **fromname, char **onname, char **type)
|
|
|
|
{
|
|
|
|
|
|
|
|
*fromname = getmntname(name, NULL, MNTFROM, type, NAME);
|
|
|
|
if (*fromname == NULL) {
|
|
|
|
*onname = getmntname(name, NULL, MNTON, type, NAME);
|
|
|
|
if (*onname != NULL)
|
|
|
|
*fromname = name;
|
|
|
|
} else
|
|
|
|
*onname = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
mntinfo(struct statfs **mntbuf)
|
|
|
|
{
|
|
|
|
static struct statfs *origbuf;
|
|
|
|
size_t bufsize;
|
|
|
|
int mntsize;
|
|
|
|
|
|
|
|
mntsize = getfsstat(NULL, 0l, MNT_NOWAIT);
|
|
|
|
if (mntsize <= 0)
|
|
|
|
return (0);
|
|
|
|
bufsize = (mntsize + 1) * sizeof(struct statfs);
|
|
|
|
if ((origbuf = malloc(bufsize)) == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
|
|
|
|
*mntbuf = origbuf;
|
|
|
|
return (mntsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
getrealname(char *name, char *realname)
|
|
|
|
{
|
|
|
|
char *dirname;
|
|
|
|
int havedir;
|
|
|
|
size_t baselen;
|
|
|
|
size_t dirlen;
|
|
|
|
|
|
|
|
dirname = '\0';
|
|
|
|
havedir = 0;
|
|
|
|
if (*name == '/') {
|
|
|
|
if (ISDOT(name + 1) || ISDOTDOT(name + 1))
|
|
|
|
strcpy(realname, "/");
|
|
|
|
else {
|
|
|
|
if ((dirname = strrchr(name + 1, '/')) == NULL)
|
|
|
|
strncpy(realname, name, MAXPATHLEN);
|
|
|
|
else
|
|
|
|
havedir = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ISDOT(name) || ISDOTDOT(name))
|
|
|
|
(void)realpath(name, realname);
|
|
|
|
else {
|
|
|
|
if ((dirname = strrchr(name, '/')) == NULL) {
|
|
|
|
if ((realpath(name, realname)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
} else
|
|
|
|
havedir = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (havedir) {
|
|
|
|
*dirname++ = '\0';
|
|
|
|
if (ISDOT(dirname)) {
|
|
|
|
*dirname = '\0';
|
|
|
|
if ((realpath(name, realname)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
} else if (ISDOTDOT(dirname)) {
|
|
|
|
*--dirname = '/';
|
|
|
|
if ((realpath(name, realname)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
} else {
|
|
|
|
if ((realpath(name, realname)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
baselen = strlen(realname);
|
|
|
|
dirlen = strlen(dirname);
|
|
|
|
if (baselen + dirlen + 1 > MAXPATHLEN)
|
|
|
|
return (NULL);
|
|
|
|
if (realname[1] == '\0') {
|
|
|
|
memmove(realname + 1, dirname, dirlen);
|
|
|
|
realname[dirlen + 1] = '\0';
|
|
|
|
} else {
|
|
|
|
realname[baselen] = '/';
|
|
|
|
memmove(realname + baselen + 1,
|
|
|
|
dirname, dirlen);
|
|
|
|
realname[baselen + dirlen + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (realname);
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* xdr routines for mount rpc's
|
|
|
|
*/
|
|
|
|
int
|
1999-10-17 16:26:58 +00:00
|
|
|
xdr_dir(XDR *xdrsp, char *dirp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1999-10-17 16:26:58 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
1999-10-17 16:26:58 +00:00
|
|
|
|
1997-06-19 14:44:24 +00:00
|
|
|
(void)fprintf(stderr, "%s\n%s\n",
|
|
|
|
"usage: umount [-fv] special | node",
|
|
|
|
" umount -a | -A [-fv] [-h host] [-t type]");
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|