Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.20.2.1
      1  1.20.2.1   mellon /*	$NetBSD: umount.c,v 1.20.2.1 1997/11/02 00:21:01 mellon Exp $	*/
      2      1.13      cgd 
      3       1.1      cgd /*-
      4       1.7  mycroft  * Copyright (c) 1980, 1989, 1993
      5       1.7  mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1      cgd  *
      7       1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1      cgd  * modification, are permitted provided that the following conditions
      9       1.1      cgd  * are met:
     10       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1      cgd  *    must display the following acknowledgement:
     17       1.1      cgd  *	This product includes software developed by the University of
     18       1.1      cgd  *	California, Berkeley and its contributors.
     19       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1      cgd  *    may be used to endorse or promote products derived from this software
     21       1.1      cgd  *    without specific prior written permission.
     22       1.1      cgd  *
     23       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1      cgd  * SUCH DAMAGE.
     34       1.1      cgd  */
     35       1.1      cgd 
     36      1.19    lukem #include <sys/cdefs.h>
     37       1.1      cgd #ifndef lint
     38      1.19    lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\n\
     39      1.19    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40       1.1      cgd #endif /* not lint */
     41       1.1      cgd 
     42       1.1      cgd #ifndef lint
     43      1.13      cgd #if 0
     44      1.20    lukem static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
     45      1.13      cgd #else
     46  1.20.2.1   mellon __RCSID("$NetBSD: umount.c,v 1.20.2.1 1997/11/02 00:21:01 mellon Exp $");
     47      1.13      cgd #endif
     48       1.1      cgd #endif /* not lint */
     49       1.1      cgd 
     50       1.1      cgd #include <sys/param.h>
     51       1.1      cgd #include <sys/stat.h>
     52       1.1      cgd #include <sys/mount.h>
     53       1.1      cgd #include <sys/time.h>
     54       1.1      cgd #include <sys/socket.h>
     55       1.1      cgd #include <sys/socketvar.h>
     56       1.7  mycroft 
     57       1.1      cgd #include <netdb.h>
     58       1.1      cgd #include <rpc/rpc.h>
     59       1.1      cgd #include <rpc/pmap_clnt.h>
     60       1.1      cgd #include <rpc/pmap_prot.h>
     61       1.1      cgd #include <nfs/rpcv2.h>
     62       1.1      cgd 
     63       1.6      cgd #include <err.h>
     64       1.1      cgd #include <fstab.h>
     65       1.1      cgd #include <stdio.h>
     66       1.7  mycroft #include <stdlib.h>
     67       1.1      cgd #include <string.h>
     68       1.6      cgd #include <unistd.h>
     69       1.1      cgd 
     70      1.18       pk typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     71       1.7  mycroft 
     72      1.12  mycroft int	fake, fflag, verbose;
     73       1.7  mycroft char	*nfshost;
     74       1.7  mycroft 
     75      1.20    lukem int	 checkvfsname __P((const char *, char **));
     76      1.20    lukem char	*getmntname __P((char *, mntwhat, char **));
     77      1.20    lukem char	**makevfslist __P((char *));
     78      1.19    lukem int	 main __P((int, char *[]));
     79      1.19    lukem int	 namematch __P((struct hostent *));
     80      1.20    lukem int	 selected __P((int));
     81      1.20    lukem int	 umountall __P((char **));
     82      1.20    lukem int	 umountfs __P((char *, char **));
     83       1.7  mycroft void	 usage __P((void));
     84       1.7  mycroft int	 xdr_dir __P((XDR *, char *));
     85       1.6      cgd 
     86       1.6      cgd int
     87       1.1      cgd main(argc, argv)
     88       1.1      cgd 	int argc;
     89       1.7  mycroft 	char *argv[];
     90       1.1      cgd {
     91      1.20    lukem 	int all, ch, errs, mnts;
     92      1.20    lukem 	char **typelist = NULL;
     93      1.20    lukem 	struct statfs *mntbuf;
     94       1.1      cgd 
     95       1.7  mycroft 	/* Start disks transferring immediately. */
     96       1.1      cgd 	sync();
     97       1.7  mycroft 
     98       1.7  mycroft 	all = 0;
     99      1.20    lukem 	while ((ch = getopt(argc, argv, "AaFfh:t:v")) != -1)
    100       1.7  mycroft 		switch (ch) {
    101      1.20    lukem 		case 'A':
    102      1.20    lukem 			all = 2;
    103      1.20    lukem 			break;
    104       1.7  mycroft 		case 'a':
    105       1.7  mycroft 			all = 1;
    106       1.7  mycroft 			break;
    107       1.7  mycroft 		case 'F':
    108       1.7  mycroft 			fake = 1;
    109       1.1      cgd 			break;
    110       1.1      cgd 		case 'f':
    111       1.1      cgd 			fflag = MNT_FORCE;
    112       1.1      cgd 			break;
    113      1.20    lukem 		case 'h':	/* -h implies -A. */
    114      1.20    lukem 			all = 2;
    115       1.7  mycroft 			nfshost = optarg;
    116       1.1      cgd 			break;
    117       1.1      cgd 		case 't':
    118      1.12  mycroft 			if (typelist != NULL)
    119      1.12  mycroft 				errx(1, "only one -t option may be specified.");
    120      1.20    lukem 			typelist = makevfslist(optarg);
    121       1.1      cgd 			break;
    122       1.7  mycroft 		case 'v':
    123      1.12  mycroft 			verbose = 1;
    124       1.1      cgd 			break;
    125       1.1      cgd 		default:
    126       1.1      cgd 			usage();
    127       1.1      cgd 			/* NOTREACHED */
    128       1.1      cgd 		}
    129       1.1      cgd 	argc -= optind;
    130       1.1      cgd 	argv += optind;
    131       1.1      cgd 
    132      1.19    lukem 	if ((argc == 0 && !all) || (argc != 0 && all))
    133       1.1      cgd 		usage();
    134       1.7  mycroft 
    135       1.7  mycroft 	/* -h implies "-t nfs" if no -t flag. */
    136       1.7  mycroft 	if ((nfshost != NULL) && (typelist == NULL))
    137      1.20    lukem 		typelist = makevfslist("nfs");
    138       1.7  mycroft 
    139      1.20    lukem 	errs = 0;
    140      1.20    lukem 	switch (all) {
    141      1.20    lukem 	case 2:
    142      1.20    lukem 		if ((mnts = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    143      1.20    lukem 			warn("getmntinfo");
    144      1.20    lukem 			errs = 1;
    145      1.20    lukem 			break;
    146      1.20    lukem 		}
    147      1.20    lukem 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    148      1.20    lukem 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    149      1.20    lukem 				continue;
    150      1.20    lukem 			if (umountfs(mntbuf[mnts].f_mntonname, typelist) != 0)
    151      1.20    lukem 				errs = 1;
    152      1.20    lukem 		}
    153      1.20    lukem 		break;
    154      1.20    lukem 	case 1:
    155      1.20    lukem 		if (setfsent() == 0)
    156      1.20    lukem 			err(1, "%s", _PATH_FSTAB);
    157      1.20    lukem 		errs = umountall(typelist);
    158      1.20    lukem 		break;
    159      1.20    lukem 	case 0:
    160       1.7  mycroft 		for (errs = 0; *argv != NULL; ++argv)
    161      1.20    lukem 			if (umountfs(*argv, typelist) != 0)
    162       1.7  mycroft 				errs = 1;
    163      1.20    lukem 		break;
    164      1.20    lukem 	}
    165       1.1      cgd 	exit(errs);
    166       1.1      cgd }
    167       1.1      cgd 
    168      1.20    lukem 
    169       1.7  mycroft int
    170      1.20    lukem umountall(typelist)
    171      1.20    lukem 	char **typelist;
    172       1.1      cgd {
    173      1.16  mycroft 	struct statfs *fs;
    174      1.16  mycroft 	int n;
    175       1.7  mycroft 	int rval;
    176       1.7  mycroft 
    177      1.16  mycroft 	n = getmntinfo(&fs, MNT_NOWAIT);
    178      1.16  mycroft 	if (n == 0)
    179      1.19    lukem 		err(1, "%s", "");
    180      1.16  mycroft 
    181      1.16  mycroft 	rval = 0;
    182      1.16  mycroft 	while (--n >= 0) {
    183       1.7  mycroft 		/* Ignore the root. */
    184      1.16  mycroft 		if (strncmp(fs[n].f_mntonname, "/", MNAMELEN) == 0)
    185       1.7  mycroft 			continue;
    186       1.7  mycroft 
    187      1.20    lukem 		if (checkvfsname(fs[n].f_fstypename, typelist))
    188       1.7  mycroft 			continue;
    189       1.7  mycroft 
    190      1.20    lukem 		if (umountfs(fs[n].f_mntonname, typelist))
    191      1.16  mycroft 			rval = 1;
    192       1.1      cgd 	}
    193      1.16  mycroft 	return (rval);
    194       1.1      cgd }
    195       1.1      cgd 
    196       1.6      cgd int
    197      1.20    lukem umountfs(name, typelist)
    198       1.1      cgd 	char *name;
    199      1.20    lukem 	char **typelist;
    200       1.1      cgd {
    201       1.7  mycroft 	enum clnt_stat clnt_stat;
    202       1.7  mycroft 	struct hostent *hp;
    203       1.1      cgd 	struct sockaddr_in saddr;
    204       1.7  mycroft 	struct stat sb;
    205       1.1      cgd 	struct timeval pertry, try;
    206       1.7  mycroft 	CLIENT *clp;
    207       1.7  mycroft 	int so;
    208      1.20    lukem 	char *type, *delimp, *hostp, *mntpt, rname[MAXPATHLEN];
    209      1.18       pk 	mntwhat what;
    210       1.7  mycroft 
    211      1.19    lukem 	hp = NULL;
    212      1.19    lukem 	delimp = NULL;
    213       1.7  mycroft 	if (realpath(name, rname) == NULL) {
    214       1.7  mycroft 		warn("%s", rname);
    215       1.9      cgd 		return (1);
    216       1.7  mycroft 	}
    217       1.7  mycroft 
    218      1.18       pk 	what = MNTANY;
    219      1.15  mycroft 	mntpt = name = rname;
    220       1.7  mycroft 
    221      1.18       pk 	if (stat(name, &sb) == 0) {
    222      1.18       pk 		if (S_ISBLK(sb.st_mode))
    223      1.18       pk 			what = MNTON;
    224      1.18       pk 		else if (S_ISDIR(sb.st_mode))
    225      1.18       pk 			what = MNTFROM;
    226      1.18       pk 	}
    227      1.18       pk 
    228      1.18       pk 	switch (what) {
    229      1.18       pk 	case MNTON:
    230      1.20    lukem 		if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    231      1.18       pk 			warnx("%s: not currently mounted", name);
    232      1.18       pk 			return (1);
    233      1.18       pk 		}
    234      1.18       pk 		break;
    235      1.18       pk 	case MNTFROM:
    236      1.20    lukem 		if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    237      1.18       pk 			warnx("%s: not currently mounted", mntpt);
    238      1.18       pk 			return (1);
    239      1.17       pk 		}
    240      1.18       pk 		break;
    241      1.18       pk 	default:
    242      1.20    lukem 		if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    243      1.15  mycroft 			name = rname;
    244      1.20    lukem 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    245      1.15  mycroft 				warnx("%s: not currently mounted", name);
    246      1.15  mycroft 				return (1);
    247      1.15  mycroft 			}
    248       1.1      cgd 		}
    249       1.7  mycroft 	}
    250       1.7  mycroft 
    251      1.20    lukem 	if (checkvfsname(type, typelist))
    252       1.9      cgd 		return (1);
    253       1.1      cgd 
    254      1.20    lukem 	hp = NULL;
    255      1.14      cgd 	if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
    256      1.10  mycroft 		if ((delimp = strchr(name, '@')) != NULL) {
    257      1.10  mycroft 			hostp = delimp + 1;
    258      1.10  mycroft 			*delimp = '\0';
    259      1.10  mycroft 			hp = gethostbyname(hostp);
    260      1.10  mycroft 			*delimp = '@';
    261      1.10  mycroft 		} else if ((delimp = strchr(name, ':')) != NULL) {
    262      1.10  mycroft 			*delimp = '\0';
    263      1.10  mycroft 			hostp = name;
    264      1.10  mycroft 			hp = gethostbyname(hostp);
    265      1.10  mycroft 			name = delimp + 1;
    266      1.10  mycroft 			*delimp = ':';
    267      1.20    lukem 		}
    268      1.10  mycroft 	}
    269       1.7  mycroft 
    270      1.20    lukem 	if (!namematch(hp))
    271      1.20    lukem 		return (1);
    272      1.20    lukem 
    273      1.12  mycroft 	if (verbose)
    274       1.7  mycroft 		(void)printf("%s: unmount from %s\n", name, mntpt);
    275       1.7  mycroft 	if (fake)
    276       1.7  mycroft 		return (0);
    277       1.1      cgd 
    278       1.7  mycroft 	if (unmount(mntpt, fflag) < 0) {
    279       1.6      cgd 		warn("%s", mntpt);
    280       1.7  mycroft 		return (1);
    281       1.1      cgd 	}
    282       1.1      cgd 
    283      1.14      cgd 	if (!strncmp(type, MOUNT_NFS, MFSNAMELEN) &&
    284      1.10  mycroft 	    (hp != NULL) && !(fflag & MNT_FORCE)) {
    285       1.1      cgd 		*delimp = '\0';
    286       1.7  mycroft 		memset(&saddr, 0, sizeof(saddr));
    287       1.1      cgd 		saddr.sin_family = AF_INET;
    288       1.1      cgd 		saddr.sin_port = 0;
    289       1.7  mycroft 		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
    290       1.1      cgd 		pertry.tv_sec = 3;
    291       1.1      cgd 		pertry.tv_usec = 0;
    292       1.7  mycroft 		so = RPC_ANYSOCK;
    293       1.7  mycroft 		if ((clp = clntudp_create(&saddr,
    294       1.7  mycroft 		    RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
    295       1.1      cgd 			clnt_pcreateerror("Cannot MNT PRC");
    296       1.1      cgd 			return (1);
    297       1.1      cgd 		}
    298       1.1      cgd 		clp->cl_auth = authunix_create_default();
    299       1.1      cgd 		try.tv_sec = 20;
    300       1.1      cgd 		try.tv_usec = 0;
    301       1.7  mycroft 		clnt_stat = clnt_call(clp,
    302       1.7  mycroft 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
    303       1.1      cgd 		if (clnt_stat != RPC_SUCCESS) {
    304       1.1      cgd 			clnt_perror(clp, "Bad MNT RPC");
    305       1.1      cgd 			return (1);
    306       1.1      cgd 		}
    307       1.1      cgd 		auth_destroy(clp->cl_auth);
    308       1.1      cgd 		clnt_destroy(clp);
    309       1.1      cgd 	}
    310       1.7  mycroft 	return (0);
    311       1.1      cgd }
    312       1.1      cgd 
    313       1.1      cgd char *
    314       1.1      cgd getmntname(name, what, type)
    315       1.1      cgd 	char *name;
    316       1.7  mycroft 	mntwhat what;
    317      1.20    lukem 	char **type;
    318       1.1      cgd {
    319      1.20    lukem 	static struct statfs *mntbuf;
    320      1.20    lukem 	static int mntsize;
    321      1.20    lukem 	int i;
    322       1.1      cgd 
    323      1.20    lukem 	if (mntbuf == NULL &&
    324      1.20    lukem 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    325       1.7  mycroft 		warn("getmntinfo");
    326       1.7  mycroft 		return (NULL);
    327       1.1      cgd 	}
    328  1.20.2.1   mellon 	for (i = mntsize - 1; i >= 0; i--) {
    329       1.7  mycroft 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    330       1.7  mycroft 			if (type)
    331      1.20    lukem 				*type = mntbuf[i].f_fstypename;
    332       1.1      cgd 			return (mntbuf[i].f_mntonname);
    333       1.1      cgd 		}
    334       1.7  mycroft 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    335       1.7  mycroft 			if (type)
    336      1.20    lukem 				*type = mntbuf[i].f_fstypename;
    337       1.1      cgd 			return (mntbuf[i].f_mntfromname);
    338       1.1      cgd 		}
    339       1.1      cgd 	}
    340       1.7  mycroft 	return (NULL);
    341       1.1      cgd }
    342       1.1      cgd 
    343       1.6      cgd int
    344       1.7  mycroft namematch(hp)
    345       1.1      cgd 	struct hostent *hp;
    346       1.1      cgd {
    347       1.7  mycroft 	char *cp, **np;
    348       1.7  mycroft 
    349       1.7  mycroft 	if ((hp == NULL) || (nfshost == NULL))
    350       1.7  mycroft 		return (1);
    351       1.1      cgd 
    352       1.1      cgd 	if (strcasecmp(nfshost, hp->h_name) == 0)
    353       1.7  mycroft 		return (1);
    354       1.7  mycroft 
    355       1.6      cgd 	if ((cp = strchr(hp->h_name, '.')) != NULL) {
    356       1.1      cgd 		*cp = '\0';
    357       1.1      cgd 		if (strcasecmp(nfshost, hp->h_name) == 0)
    358       1.7  mycroft 			return (1);
    359       1.1      cgd 	}
    360       1.1      cgd 	for (np = hp->h_aliases; *np; np++) {
    361       1.1      cgd 		if (strcasecmp(nfshost, *np) == 0)
    362       1.7  mycroft 			return (1);
    363       1.6      cgd 		if ((cp = strchr(*np, '.')) != NULL) {
    364       1.1      cgd 			*cp = '\0';
    365       1.1      cgd 			if (strcasecmp(nfshost, *np) == 0)
    366       1.7  mycroft 				return (1);
    367       1.1      cgd 		}
    368       1.1      cgd 	}
    369       1.7  mycroft 	return (0);
    370       1.1      cgd }
    371       1.1      cgd 
    372       1.1      cgd /*
    373       1.1      cgd  * xdr routines for mount rpc's
    374       1.1      cgd  */
    375       1.6      cgd int
    376       1.1      cgd xdr_dir(xdrsp, dirp)
    377       1.1      cgd 	XDR *xdrsp;
    378       1.1      cgd 	char *dirp;
    379       1.1      cgd {
    380       1.1      cgd 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    381       1.1      cgd }
    382       1.7  mycroft 
    383       1.7  mycroft void
    384       1.7  mycroft usage()
    385       1.7  mycroft {
    386       1.7  mycroft 	(void)fprintf(stderr,
    387       1.7  mycroft 	    "usage: %s\n       %s\n",
    388       1.7  mycroft 	    "umount [-fv] [-t fstypelist] special | node",
    389       1.7  mycroft 	    "umount -a[fv] [-h host] [-t fstypelist]");
    390       1.7  mycroft 	exit(1);
    391       1.7  mycroft }
    392