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