NetBSD/sbin/umount/umount.c

409 lines
9.4 KiB
C
Raw Normal View History

1993-03-21 12:45:37 +03:00
/*-
* Copyright (c) 1980, 1989, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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
static char copyright[] =
"@(#) Copyright (c) 1980, 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
/*static char sccsid[] = "from: @(#)umount.c 8.3 (Berkeley) 2/20/94";*/
static char *rcsid = "$Id: umount.c,v 1.8 1994/08/23 20:57:04 cgd Exp $";
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
1993-03-21 12:45:37 +03:00
#include <netdb.h>
#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_prot.h>
#include <nfs/rpcv2.h>
1994-04-14 07:25:14 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <fstab.h>
#include <stdio.h>
1994-04-14 07:25:14 +04:00
#include <stdlib.h>
#include <string.h>
1994-04-14 07:25:14 +04:00
#include <unistd.h>
1993-03-21 12:45:37 +03:00
typedef enum { MNTON, MNTFROM } mntwhat;
1993-03-21 12:45:37 +03:00
int fake, fflag, vflag;
char **typelist;
char *nfshost;
1993-03-21 12:45:37 +03:00
char *getmntname __P((char *, mntwhat, char *));
void maketypelist __P((char *));
int selected __P((char *));
int namematch __P((struct hostent *));
int umountall __P((void));
int umountfs __P((char *));
void usage __P((void));
int xdr_dir __P((XDR *, char *));
1994-04-14 07:25:14 +04:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
char *argv[];
1993-03-21 12:45:37 +03:00
{
int all, ch, errs;
1993-03-21 12:45:37 +03:00
/* Start disks transferring immediately. */
1993-03-21 12:45:37 +03:00
sync();
all = 0;
while ((ch = getopt(argc, argv, "aFfh:t:v")) != EOF)
switch (ch) {
case 'a':
all = 1;
break;
case 'F':
fake = 1;
1993-03-21 12:45:37 +03:00
break;
case 'f':
fflag = MNT_FORCE;
break;
case 'h': /* -h implies -a. */
all = 1;
nfshost = optarg;
1993-03-21 12:45:37 +03:00
break;
case 't':
maketypelist(optarg);
1993-03-21 12:45:37 +03:00
break;
case 'v':
vflag = 1;
1993-03-21 12:45:37 +03:00
break;
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (argc == 0 && !all || argc != 0 && all)
1993-03-21 12:45:37 +03:00
usage();
/* -h implies "-t nfs" if no -t flag. */
if ((nfshost != NULL) && (typelist == NULL))
maketypelist("nfs");
1993-03-21 12:45:37 +03:00
if (all) {
if (setfsent() == 0)
err(1, "%s", _PATH_FSTAB);
errs = umountall();
1993-03-21 12:45:37 +03:00
} else
for (errs = 0; *argv != NULL; ++argv)
if (umountfs(*argv) != 0)
errs = 1;
1993-03-21 12:45:37 +03:00
exit(errs);
}
int
umountall()
1993-03-21 12:45:37 +03:00
{
struct fstab *fs;
int rval;
char *cp;
while ((fs = getfsent()) != NULL) {
/* 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 (!selected(fs->fs_vfstype))
continue;
/*
* 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)
err(1, NULL);
(void)strcpy(cp, fs->fs_file);
rval = umountall();
return (umountfs(cp) || rval);
1993-03-21 12:45:37 +03:00
}
return (0);
1993-03-21 12:45:37 +03:00
}
1994-04-14 07:25:14 +04:00
int
umountfs(name)
1993-03-21 12:45:37 +03:00
char *name;
{
enum clnt_stat clnt_stat;
struct hostent *hp;
1993-03-21 12:45:37 +03:00
struct sockaddr_in saddr;
struct stat sb;
1993-03-21 12:45:37 +03:00
struct timeval pertry, try;
CLIENT *clp;
int so;
char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
if (realpath(name, rname) == NULL) {
warn("%s", rname);
return (0);
}
name = rname;
if (stat(name, &sb) < 0) {
if (((mntpt = getmntname(name, MNTFROM, type)) == NULL) &&
((mntpt = getmntname(name, MNTON, type)) == NULL)) {
warnx("%s: not currently mounted", name);
return (1);
1993-03-21 12:45:37 +03:00
}
} else if (S_ISBLK(sb.st_mode)) {
if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
warnx("%s: not currently mounted", name);
return (1);
1993-03-21 12:45:37 +03:00
}
} else if (S_ISDIR(sb.st_mode)) {
1993-03-21 12:45:37 +03:00
mntpt = name;
if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
warnx("%s: not currently mounted", mntpt);
return (1);
1993-03-21 12:45:37 +03:00
}
} else {
warnx("%s: not a directory or special device", name);
return (1);
1993-03-21 12:45:37 +03:00
}
if (!selected(type))
return (0);
1994-04-14 07:25:14 +04:00
if ((delimp = strchr(name, '@')) != NULL) {
1993-03-21 12:45:37 +03:00
hostp = delimp + 1;
*delimp = '\0';
hp = gethostbyname(hostp);
*delimp = '@';
1994-04-14 07:25:14 +04:00
} else if ((delimp = strchr(name, ':')) != NULL) {
1993-03-21 12:45:37 +03:00
*delimp = '\0';
hostp = name;
hp = gethostbyname(hostp);
name = delimp + 1;
1993-03-21 12:45:37 +03:00
*delimp = ':';
} else
hp = NULL;
if (!namematch(hp))
return (0);
1993-03-21 12:45:37 +03:00
if (vflag)
(void)printf("%s: unmount from %s\n", name, mntpt);
if (fake)
1993-03-21 12:45:37 +03:00
return (0);
if (unmount(mntpt, fflag) < 0) {
warn("%s", mntpt);
return (1);
1993-03-21 12:45:37 +03:00
}
if ((hp != NULL) && !(fflag & MNT_FORCE)) {
1993-03-21 12:45:37 +03:00
*delimp = '\0';
memset(&saddr, 0, sizeof(saddr));
1993-03-21 12:45:37 +03:00
saddr.sin_family = AF_INET;
saddr.sin_port = 0;
memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
1993-03-21 12:45:37 +03:00
pertry.tv_sec = 3;
pertry.tv_usec = 0;
so = RPC_ANYSOCK;
if ((clp = clntudp_create(&saddr,
RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
1993-03-21 12:45:37 +03:00
clnt_pcreateerror("Cannot MNT PRC");
return (1);
}
clp->cl_auth = authunix_create_default();
try.tv_sec = 20;
try.tv_usec = 0;
clnt_stat = clnt_call(clp,
RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
1993-03-21 12:45:37 +03:00
if (clnt_stat != RPC_SUCCESS) {
clnt_perror(clp, "Bad MNT RPC");
return (1);
}
auth_destroy(clp->cl_auth);
clnt_destroy(clp);
}
return (0);
1993-03-21 12:45:37 +03:00
}
char *
getmntname(name, what, type)
char *name;
mntwhat what;
1994-04-14 07:25:14 +04:00
char *type;
1993-03-21 12:45:37 +03:00
{
struct statfs *mntbuf;
int i, mntsize;
1993-03-21 12:45:37 +03:00
if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
warn("getmntinfo");
return (NULL);
1993-03-21 12:45:37 +03:00
}
for (i = 0; i < mntsize; i++) {
if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
if (type)
memcpy(type, mntbuf[i].f_fstypename,
sizeof(mntbuf[i].f_fstypename));
1993-03-21 12:45:37 +03:00
return (mntbuf[i].f_mntonname);
}
if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
if (type)
memcpy(type, mntbuf[i].f_fstypename,
sizeof(mntbuf[i].f_fstypename));
1993-03-21 12:45:37 +03:00
return (mntbuf[i].f_mntfromname);
}
}
return (NULL);
1993-03-21 12:45:37 +03:00
}
static enum { IN_LIST, NOT_IN_LIST } which;
1993-03-21 12:45:37 +03:00
1994-04-14 07:25:14 +04:00
int
selected(type)
1994-04-14 07:25:14 +04:00
char *type;
1993-03-21 12:45:37 +03:00
{
char **av;
/* If no type specified, it's always selected. */
1994-04-14 07:25:14 +04:00
if (typelist == NULL)
return (1);
for (av = typelist; *av != NULL; ++av)
if (!strcmp(type, *av))
return (which == IN_LIST ? 1 : 0);
return (which == IN_LIST ? 0 : 1);
1993-03-21 12:45:37 +03:00
}
void
1993-03-21 12:45:37 +03:00
maketypelist(fslist)
char *fslist;
{
int i;
char *nextcp, **av;
if ((fslist == NULL) || (fslist[0] == '\0'))
errx(1, "empty type list");
1993-03-21 12:45:37 +03:00
/*
* XXX
* Note: the syntax is "noxxx,yyy" for no xxx's and
* no yyy's, not the more intuitive "noyyy,noyyy".
*/
1993-03-21 12:45:37 +03:00
if (fslist[0] == 'n' && fslist[1] == 'o') {
fslist += 2;
which = NOT_IN_LIST;
1993-03-21 12:45:37 +03:00
} else
which = IN_LIST;
/* Count the number of types. */
for (i = 0, nextcp = fslist; *nextcp != NULL; ++nextcp)
1993-03-21 12:45:37 +03:00
if (*nextcp == ',')
i++;
/* Build an array of that many types. */
if ((av = typelist = malloc((i + 2) * sizeof(char *))) == NULL)
err(1, NULL);
for (i = 0; fslist != NULL; fslist = nextcp, ++i) {
1994-04-14 07:25:14 +04:00
if ((nextcp = strchr(fslist, ',')) != NULL)
1993-03-21 12:45:37 +03:00
*nextcp++ = '\0';
/* Note that we're keeping pointers into the input string. */
av[i] = fslist;
1993-03-21 12:45:37 +03:00
}
/* Terminate the array. */
av[i++] = NULL;
1993-03-21 12:45:37 +03:00
}
1994-04-14 07:25:14 +04:00
int
namematch(hp)
1993-03-21 12:45:37 +03:00
struct hostent *hp;
{
char *cp, **np;
if ((hp == NULL) || (nfshost == NULL))
return (1);
1993-03-21 12:45:37 +03:00
if (strcasecmp(nfshost, hp->h_name) == 0)
return (1);
1994-04-14 07:25:14 +04:00
if ((cp = strchr(hp->h_name, '.')) != NULL) {
1993-03-21 12:45:37 +03:00
*cp = '\0';
if (strcasecmp(nfshost, hp->h_name) == 0)
return (1);
1993-03-21 12:45:37 +03:00
}
for (np = hp->h_aliases; *np; np++) {
if (strcasecmp(nfshost, *np) == 0)
return (1);
1994-04-14 07:25:14 +04:00
if ((cp = strchr(*np, '.')) != NULL) {
1993-03-21 12:45:37 +03:00
*cp = '\0';
if (strcasecmp(nfshost, *np) == 0)
return (1);
1993-03-21 12:45:37 +03:00
}
}
return (0);
1993-03-21 12:45:37 +03:00
}
/*
* xdr routines for mount rpc's
*/
1994-04-14 07:25:14 +04:00
int
1993-03-21 12:45:37 +03:00
xdr_dir(xdrsp, dirp)
XDR *xdrsp;
char *dirp;
{
return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
}
void
usage()
{
(void)fprintf(stderr,
"usage: %s\n %s\n",
"umount [-fv] [-t fstypelist] special | node",
"umount -a[fv] [-h host] [-t fstypelist]");
exit(1);
}