Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.24
      1  1.24      fair /*	$NetBSD: umount.c,v 1.24 1998/04/17 01:19:42 fair 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.24      fair __RCSID("$NetBSD: umount.c,v 1.24 1998/04/17 01:19:42 fair 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.7   mycroft 
     56   1.1       cgd #include <netdb.h>
     57   1.1       cgd #include <rpc/rpc.h>
     58   1.1       cgd #include <rpc/pmap_clnt.h>
     59   1.1       cgd #include <rpc/pmap_prot.h>
     60   1.1       cgd #include <nfs/rpcv2.h>
     61   1.1       cgd 
     62   1.6       cgd #include <err.h>
     63   1.1       cgd #include <fstab.h>
     64   1.1       cgd #include <stdio.h>
     65   1.7   mycroft #include <stdlib.h>
     66   1.1       cgd #include <string.h>
     67   1.6       cgd #include <unistd.h>
     68   1.1       cgd 
     69  1.18        pk typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     70   1.7   mycroft 
     71  1.24      fair int	fake, fflag, verbose, raw;
     72   1.7   mycroft char	*nfshost;
     73   1.7   mycroft 
     74  1.20     lukem int	 checkvfsname __P((const char *, char **));
     75  1.20     lukem char	*getmntname __P((char *, mntwhat, char **));
     76  1.20     lukem char	**makevfslist __P((char *));
     77  1.19     lukem int	 main __P((int, char *[]));
     78  1.19     lukem int	 namematch __P((struct hostent *));
     79  1.20     lukem int	 selected __P((int));
     80  1.20     lukem int	 umountall __P((char **));
     81  1.20     lukem int	 umountfs __P((char *, char **));
     82   1.7   mycroft void	 usage __P((void));
     83   1.7   mycroft int	 xdr_dir __P((XDR *, char *));
     84   1.6       cgd 
     85   1.6       cgd int
     86   1.1       cgd main(argc, argv)
     87   1.1       cgd 	int argc;
     88   1.7   mycroft 	char *argv[];
     89   1.1       cgd {
     90  1.20     lukem 	int all, ch, errs, mnts;
     91  1.20     lukem 	char **typelist = NULL;
     92  1.20     lukem 	struct statfs *mntbuf;
     93   1.1       cgd 
     94   1.7   mycroft 	/* Start disks transferring immediately. */
     95   1.1       cgd 	sync();
     96   1.7   mycroft 
     97   1.7   mycroft 	all = 0;
     98  1.24      fair 	while ((ch = getopt(argc, argv, "AaFfRh:t:v")) != -1)
     99   1.7   mycroft 		switch (ch) {
    100  1.20     lukem 		case 'A':
    101  1.20     lukem 			all = 2;
    102  1.20     lukem 			break;
    103   1.7   mycroft 		case 'a':
    104   1.7   mycroft 			all = 1;
    105   1.7   mycroft 			break;
    106   1.7   mycroft 		case 'F':
    107   1.7   mycroft 			fake = 1;
    108   1.1       cgd 			break;
    109   1.1       cgd 		case 'f':
    110   1.1       cgd 			fflag = MNT_FORCE;
    111   1.1       cgd 			break;
    112  1.20     lukem 		case 'h':	/* -h implies -A. */
    113  1.20     lukem 			all = 2;
    114   1.7   mycroft 			nfshost = optarg;
    115   1.1       cgd 			break;
    116  1.24      fair 		case 'R':
    117  1.24      fair 			raw = 1;
    118  1.24      fair 			break;
    119   1.1       cgd 		case 't':
    120  1.12   mycroft 			if (typelist != NULL)
    121  1.12   mycroft 				errx(1, "only one -t option may be specified.");
    122  1.20     lukem 			typelist = makevfslist(optarg);
    123   1.1       cgd 			break;
    124   1.7   mycroft 		case 'v':
    125  1.12   mycroft 			verbose = 1;
    126   1.1       cgd 			break;
    127   1.1       cgd 		default:
    128   1.1       cgd 			usage();
    129   1.1       cgd 			/* NOTREACHED */
    130   1.1       cgd 		}
    131   1.1       cgd 	argc -= optind;
    132   1.1       cgd 	argv += optind;
    133   1.1       cgd 
    134  1.24      fair 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
    135   1.1       cgd 		usage();
    136   1.7   mycroft 
    137   1.7   mycroft 	/* -h implies "-t nfs" if no -t flag. */
    138   1.7   mycroft 	if ((nfshost != NULL) && (typelist == NULL))
    139  1.20     lukem 		typelist = makevfslist("nfs");
    140   1.7   mycroft 
    141  1.20     lukem 	errs = 0;
    142  1.20     lukem 	switch (all) {
    143  1.20     lukem 	case 2:
    144  1.20     lukem 		if ((mnts = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    145  1.20     lukem 			warn("getmntinfo");
    146  1.20     lukem 			errs = 1;
    147  1.20     lukem 			break;
    148  1.20     lukem 		}
    149  1.20     lukem 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    150  1.20     lukem 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    151  1.20     lukem 				continue;
    152  1.20     lukem 			if (umountfs(mntbuf[mnts].f_mntonname, typelist) != 0)
    153  1.20     lukem 				errs = 1;
    154  1.20     lukem 		}
    155  1.20     lukem 		break;
    156  1.20     lukem 	case 1:
    157  1.20     lukem 		if (setfsent() == 0)
    158  1.20     lukem 			err(1, "%s", _PATH_FSTAB);
    159  1.20     lukem 		errs = umountall(typelist);
    160  1.20     lukem 		break;
    161  1.20     lukem 	case 0:
    162   1.7   mycroft 		for (errs = 0; *argv != NULL; ++argv)
    163  1.20     lukem 			if (umountfs(*argv, typelist) != 0)
    164   1.7   mycroft 				errs = 1;
    165  1.20     lukem 		break;
    166  1.20     lukem 	}
    167   1.1       cgd 	exit(errs);
    168   1.1       cgd }
    169   1.1       cgd 
    170  1.20     lukem 
    171   1.7   mycroft int
    172  1.20     lukem umountall(typelist)
    173  1.20     lukem 	char **typelist;
    174   1.1       cgd {
    175  1.16   mycroft 	struct statfs *fs;
    176  1.16   mycroft 	int n;
    177   1.7   mycroft 	int rval;
    178   1.7   mycroft 
    179  1.16   mycroft 	n = getmntinfo(&fs, MNT_NOWAIT);
    180  1.16   mycroft 	if (n == 0)
    181  1.19     lukem 		err(1, "%s", "");
    182  1.16   mycroft 
    183  1.16   mycroft 	rval = 0;
    184  1.16   mycroft 	while (--n >= 0) {
    185   1.7   mycroft 		/* Ignore the root. */
    186  1.16   mycroft 		if (strncmp(fs[n].f_mntonname, "/", MNAMELEN) == 0)
    187   1.7   mycroft 			continue;
    188   1.7   mycroft 
    189  1.20     lukem 		if (checkvfsname(fs[n].f_fstypename, typelist))
    190   1.7   mycroft 			continue;
    191   1.7   mycroft 
    192  1.20     lukem 		if (umountfs(fs[n].f_mntonname, typelist))
    193  1.16   mycroft 			rval = 1;
    194   1.1       cgd 	}
    195  1.16   mycroft 	return (rval);
    196   1.1       cgd }
    197   1.1       cgd 
    198   1.6       cgd int
    199  1.20     lukem umountfs(name, typelist)
    200   1.1       cgd 	char *name;
    201  1.20     lukem 	char **typelist;
    202   1.1       cgd {
    203   1.7   mycroft 	enum clnt_stat clnt_stat;
    204   1.7   mycroft 	struct hostent *hp;
    205   1.1       cgd 	struct sockaddr_in saddr;
    206   1.7   mycroft 	struct stat sb;
    207   1.1       cgd 	struct timeval pertry, try;
    208   1.7   mycroft 	CLIENT *clp;
    209   1.7   mycroft 	int so;
    210  1.20     lukem 	char *type, *delimp, *hostp, *mntpt, rname[MAXPATHLEN];
    211  1.18        pk 	mntwhat what;
    212   1.7   mycroft 
    213  1.19     lukem 	hp = NULL;
    214  1.19     lukem 	delimp = NULL;
    215   1.7   mycroft 
    216  1.24      fair 	if (raw) {
    217  1.24      fair 		mntpt = name;
    218  1.24      fair 	} else {
    219   1.7   mycroft 
    220  1.24      fair 		if (realpath(name, rname) == NULL) {
    221  1.24      fair 			warn("%s", rname);
    222  1.18        pk 			return (1);
    223  1.18        pk 		}
    224  1.24      fair 
    225  1.24      fair 		what = MNTANY;
    226  1.24      fair 		mntpt = name = rname;
    227  1.24      fair 
    228  1.24      fair 		if (stat(name, &sb) == 0) {
    229  1.24      fair 			if (S_ISBLK(sb.st_mode))
    230  1.24      fair 				what = MNTON;
    231  1.24      fair 			else if (S_ISDIR(sb.st_mode))
    232  1.24      fair 				what = MNTFROM;
    233  1.17        pk 		}
    234  1.24      fair 
    235  1.24      fair 		switch (what) {
    236  1.24      fair 		case MNTON:
    237  1.20     lukem 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    238  1.15   mycroft 				warnx("%s: not currently mounted", name);
    239  1.15   mycroft 				return (1);
    240  1.15   mycroft 			}
    241  1.24      fair 			break;
    242  1.24      fair 		case MNTFROM:
    243  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    244  1.24      fair 				warnx("%s: not currently mounted", mntpt);
    245  1.24      fair 				return (1);
    246  1.24      fair 			}
    247  1.24      fair 			break;
    248  1.24      fair 		default:
    249  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    250  1.24      fair 				name = rname;
    251  1.24      fair 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    252  1.24      fair 					warnx("%s: not currently mounted", name);
    253  1.24      fair 					return (1);
    254  1.24      fair 				}
    255  1.24      fair 			}
    256   1.1       cgd 		}
    257   1.7   mycroft 
    258  1.24      fair 		if (checkvfsname(type, typelist))
    259  1.24      fair 			return (1);
    260   1.1       cgd 
    261  1.24      fair 		hp = NULL;
    262  1.24      fair 		if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
    263  1.24      fair 			if ((delimp = strchr(name, '@')) != NULL) {
    264  1.24      fair 				hostp = delimp + 1;
    265  1.24      fair 				*delimp = '\0';
    266  1.24      fair 				hp = gethostbyname(hostp);
    267  1.24      fair 				*delimp = '@';
    268  1.24      fair 			} else if ((delimp = strchr(name, ':')) != NULL) {
    269  1.24      fair 				*delimp = '\0';
    270  1.24      fair 				hostp = name;
    271  1.24      fair 				hp = gethostbyname(hostp);
    272  1.24      fair 				name = delimp + 1;
    273  1.24      fair 				*delimp = ':';
    274  1.24      fair 			}
    275  1.20     lukem 		}
    276  1.24      fair 
    277  1.24      fair 		if (!namematch(hp))
    278  1.24      fair 			return (1);
    279  1.10   mycroft 	}
    280   1.7   mycroft 
    281  1.12   mycroft 	if (verbose)
    282   1.7   mycroft 		(void)printf("%s: unmount from %s\n", name, mntpt);
    283   1.7   mycroft 	if (fake)
    284   1.7   mycroft 		return (0);
    285   1.1       cgd 
    286   1.7   mycroft 	if (unmount(mntpt, fflag) < 0) {
    287   1.6       cgd 		warn("%s", mntpt);
    288   1.7   mycroft 		return (1);
    289   1.1       cgd 	}
    290   1.1       cgd 
    291  1.14       cgd 	if (!strncmp(type, MOUNT_NFS, MFSNAMELEN) &&
    292  1.10   mycroft 	    (hp != NULL) && !(fflag & MNT_FORCE)) {
    293   1.1       cgd 		*delimp = '\0';
    294   1.7   mycroft 		memset(&saddr, 0, sizeof(saddr));
    295   1.1       cgd 		saddr.sin_family = AF_INET;
    296   1.1       cgd 		saddr.sin_port = 0;
    297   1.7   mycroft 		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
    298   1.1       cgd 		pertry.tv_sec = 3;
    299   1.1       cgd 		pertry.tv_usec = 0;
    300   1.7   mycroft 		so = RPC_ANYSOCK;
    301   1.7   mycroft 		if ((clp = clntudp_create(&saddr,
    302   1.7   mycroft 		    RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
    303   1.1       cgd 			clnt_pcreateerror("Cannot MNT PRC");
    304   1.1       cgd 			return (1);
    305   1.1       cgd 		}
    306   1.1       cgd 		clp->cl_auth = authunix_create_default();
    307   1.1       cgd 		try.tv_sec = 20;
    308   1.1       cgd 		try.tv_usec = 0;
    309   1.7   mycroft 		clnt_stat = clnt_call(clp,
    310   1.7   mycroft 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
    311   1.1       cgd 		if (clnt_stat != RPC_SUCCESS) {
    312   1.1       cgd 			clnt_perror(clp, "Bad MNT RPC");
    313   1.1       cgd 			return (1);
    314   1.1       cgd 		}
    315   1.1       cgd 		auth_destroy(clp->cl_auth);
    316   1.1       cgd 		clnt_destroy(clp);
    317   1.1       cgd 	}
    318   1.7   mycroft 	return (0);
    319   1.1       cgd }
    320   1.1       cgd 
    321   1.1       cgd char *
    322   1.1       cgd getmntname(name, what, type)
    323   1.1       cgd 	char *name;
    324   1.7   mycroft 	mntwhat what;
    325  1.20     lukem 	char **type;
    326   1.1       cgd {
    327  1.20     lukem 	static struct statfs *mntbuf;
    328  1.20     lukem 	static int mntsize;
    329  1.20     lukem 	int i;
    330   1.1       cgd 
    331  1.20     lukem 	if (mntbuf == NULL &&
    332  1.20     lukem 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    333   1.7   mycroft 		warn("getmntinfo");
    334   1.7   mycroft 		return (NULL);
    335   1.1       cgd 	}
    336  1.21  drochner 	for (i = mntsize - 1; i >= 0; i--) {
    337   1.7   mycroft 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    338   1.7   mycroft 			if (type)
    339  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    340   1.1       cgd 			return (mntbuf[i].f_mntonname);
    341   1.1       cgd 		}
    342   1.7   mycroft 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    343   1.7   mycroft 			if (type)
    344  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    345   1.1       cgd 			return (mntbuf[i].f_mntfromname);
    346   1.1       cgd 		}
    347   1.1       cgd 	}
    348   1.7   mycroft 	return (NULL);
    349   1.1       cgd }
    350   1.1       cgd 
    351   1.6       cgd int
    352   1.7   mycroft namematch(hp)
    353   1.1       cgd 	struct hostent *hp;
    354   1.1       cgd {
    355   1.7   mycroft 	char *cp, **np;
    356   1.7   mycroft 
    357   1.7   mycroft 	if ((hp == NULL) || (nfshost == NULL))
    358   1.7   mycroft 		return (1);
    359   1.1       cgd 
    360   1.1       cgd 	if (strcasecmp(nfshost, hp->h_name) == 0)
    361   1.7   mycroft 		return (1);
    362   1.7   mycroft 
    363   1.6       cgd 	if ((cp = strchr(hp->h_name, '.')) != NULL) {
    364   1.1       cgd 		*cp = '\0';
    365   1.1       cgd 		if (strcasecmp(nfshost, hp->h_name) == 0)
    366   1.7   mycroft 			return (1);
    367   1.1       cgd 	}
    368   1.1       cgd 	for (np = hp->h_aliases; *np; np++) {
    369   1.1       cgd 		if (strcasecmp(nfshost, *np) == 0)
    370   1.7   mycroft 			return (1);
    371   1.6       cgd 		if ((cp = strchr(*np, '.')) != NULL) {
    372   1.1       cgd 			*cp = '\0';
    373   1.1       cgd 			if (strcasecmp(nfshost, *np) == 0)
    374   1.7   mycroft 				return (1);
    375   1.1       cgd 		}
    376   1.1       cgd 	}
    377   1.7   mycroft 	return (0);
    378   1.1       cgd }
    379   1.1       cgd 
    380   1.1       cgd /*
    381   1.1       cgd  * xdr routines for mount rpc's
    382   1.1       cgd  */
    383   1.6       cgd int
    384   1.1       cgd xdr_dir(xdrsp, dirp)
    385   1.1       cgd 	XDR *xdrsp;
    386   1.1       cgd 	char *dirp;
    387   1.1       cgd {
    388   1.1       cgd 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    389   1.1       cgd }
    390   1.7   mycroft 
    391   1.7   mycroft void
    392   1.7   mycroft usage()
    393   1.7   mycroft {
    394   1.7   mycroft 	(void)fprintf(stderr,
    395   1.7   mycroft 	    "usage: %s\n       %s\n",
    396  1.24      fair 	    "umount [-fvFR] [-t fstypelist] special | node",
    397  1.22     mikel 	    "umount -a[fvF] [-h host] [-t fstypelist]");
    398   1.7   mycroft 	exit(1);
    399   1.7   mycroft }
    400