Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.13
      1 /*	$NetBSD: umount.c,v 1.13 1995/03/18 15:01:37 cgd 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.13 1995/03/18 15:01:37 cgd 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 { 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 		if (setfsent() == 0)
    135 			err(1, "%s", _PATH_FSTAB);
    136 		errs = umountall();
    137 	} else
    138 		for (errs = 0; *argv != NULL; ++argv)
    139 			if (umountfs(*argv) != 0)
    140 				errs = 1;
    141 	exit(errs);
    142 }
    143 
    144 int
    145 umountall()
    146 {
    147 	struct fstab *fs;
    148 	int rval;
    149 	char *cp;
    150 
    151 	while ((fs = getfsent()) != NULL) {
    152 		/* Ignore the root. */
    153 		if (strcmp(fs->fs_file, "/") == 0)
    154 			continue;
    155 		/*
    156 		 * !!!
    157 		 * Historic practice: ignore unknown FSTAB_* fields.
    158 		 */
    159 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    160 		    strcmp(fs->fs_type, FSTAB_RO) &&
    161 		    strcmp(fs->fs_type, FSTAB_RQ))
    162 			continue;
    163 
    164 		if (!selected(fs->fs_vfstype))
    165 			continue;
    166 
    167 		/*
    168 		 * We want to unmount the file systems in the reverse order
    169 		 * that they were mounted.  So, we save off the file name
    170 		 * in some allocated memory, and then call recursively.
    171 		 */
    172 		if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
    173 			err(1, NULL);
    174 		(void)strcpy(cp, fs->fs_file);
    175 		rval = umountall();
    176 		return (umountfs(cp) || rval);
    177 	}
    178 	return (0);
    179 }
    180 
    181 int
    182 umountfs(name)
    183 	char *name;
    184 {
    185 	enum clnt_stat clnt_stat;
    186 	struct hostent *hp;
    187 	struct sockaddr_in saddr;
    188 	struct stat sb;
    189 	struct timeval pertry, try;
    190 	CLIENT *clp;
    191 	int so;
    192 	char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
    193 
    194 	if (realpath(name, rname) == NULL) {
    195 		warn("%s", rname);
    196 		return (1);
    197 	}
    198 
    199 	name = rname;
    200 
    201 	if (stat(name, &sb) < 0) {
    202 		if (((mntpt = getmntname(name, MNTFROM, type)) == NULL) &&
    203 		    ((mntpt = getmntname(name, MNTON, type)) == NULL)) {
    204 			warnx("%s: not currently mounted", name);
    205 			return (1);
    206 		}
    207 	} else if (S_ISBLK(sb.st_mode)) {
    208 		if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
    209 			warnx("%s: not currently mounted", name);
    210 			return (1);
    211 		}
    212 	} else if (S_ISDIR(sb.st_mode)) {
    213 		mntpt = name;
    214 		if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
    215 			warnx("%s: not currently mounted", mntpt);
    216 			return (1);
    217 		}
    218 	} else {
    219 		warnx("%s: not a directory or special device", name);
    220 		return (1);
    221 	}
    222 
    223 	if (!selected(type))
    224 		return (1);
    225 
    226 	if (!strcmp(type, MOUNT_NFS)) {
    227 		if ((delimp = strchr(name, '@')) != NULL) {
    228 			hostp = delimp + 1;
    229 			*delimp = '\0';
    230 			hp = gethostbyname(hostp);
    231 			*delimp = '@';
    232 		} else if ((delimp = strchr(name, ':')) != NULL) {
    233 			*delimp = '\0';
    234 			hostp = name;
    235 			hp = gethostbyname(hostp);
    236 			name = delimp + 1;
    237 			*delimp = ':';
    238 		} else
    239 			hp = NULL;
    240 		if (!namematch(hp))
    241 			return (1);
    242 	}
    243 
    244 	if (verbose)
    245 		(void)printf("%s: unmount from %s\n", name, mntpt);
    246 	if (fake)
    247 		return (0);
    248 
    249 	if (unmount(mntpt, fflag) < 0) {
    250 		warn("%s", mntpt);
    251 		return (1);
    252 	}
    253 
    254 	if (!strcmp(type, MOUNT_NFS) &&
    255 	    (hp != NULL) && !(fflag & MNT_FORCE)) {
    256 		*delimp = '\0';
    257 		memset(&saddr, 0, sizeof(saddr));
    258 		saddr.sin_family = AF_INET;
    259 		saddr.sin_port = 0;
    260 		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
    261 		pertry.tv_sec = 3;
    262 		pertry.tv_usec = 0;
    263 		so = RPC_ANYSOCK;
    264 		if ((clp = clntudp_create(&saddr,
    265 		    RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
    266 			clnt_pcreateerror("Cannot MNT PRC");
    267 			return (1);
    268 		}
    269 		clp->cl_auth = authunix_create_default();
    270 		try.tv_sec = 20;
    271 		try.tv_usec = 0;
    272 		clnt_stat = clnt_call(clp,
    273 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
    274 		if (clnt_stat != RPC_SUCCESS) {
    275 			clnt_perror(clp, "Bad MNT RPC");
    276 			return (1);
    277 		}
    278 		auth_destroy(clp->cl_auth);
    279 		clnt_destroy(clp);
    280 	}
    281 	return (0);
    282 }
    283 
    284 char *
    285 getmntname(name, what, type)
    286 	char *name;
    287 	mntwhat what;
    288 	char *type;
    289 {
    290 	struct statfs *mntbuf;
    291 	int i, mntsize;
    292 
    293 	if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    294 		warn("getmntinfo");
    295 		return (NULL);
    296 	}
    297 	for (i = 0; i < mntsize; i++) {
    298 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    299 			if (type)
    300 				memcpy(type, mntbuf[i].f_fstypename,
    301 				    sizeof(mntbuf[i].f_fstypename));
    302 			return (mntbuf[i].f_mntonname);
    303 		}
    304 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    305 			if (type)
    306 				memcpy(type, mntbuf[i].f_fstypename,
    307 				    sizeof(mntbuf[i].f_fstypename));
    308 			return (mntbuf[i].f_mntfromname);
    309 		}
    310 	}
    311 	return (NULL);
    312 }
    313 
    314 static enum { IN_LIST, NOT_IN_LIST } which;
    315 
    316 int
    317 selected(type)
    318 	const char *type;
    319 {
    320 	char **av;
    321 
    322 	/* If no type specified, it's always selected. */
    323 	if (typelist == NULL)
    324 		return (1);
    325 	for (av = typelist; *av != NULL; ++av)
    326 		if (!strcmp(type, *av))
    327 			return (which == IN_LIST ? 1 : 0);
    328 	return (which == IN_LIST ? 0 : 1);
    329 }
    330 
    331 void
    332 maketypelist(fslist)
    333 	char *fslist;
    334 {
    335 	int i;
    336 	char *nextcp, **av;
    337 
    338 	if ((fslist == NULL) || (fslist[0] == '\0'))
    339 		errx(1, "empty type list");
    340 
    341 	/*
    342 	 * XXX
    343 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
    344 	 * no yyy's, not the more intuitive "noyyy,noyyy".
    345 	 */
    346 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    347 		fslist += 2;
    348 		which = NOT_IN_LIST;
    349 	} else
    350 		which = IN_LIST;
    351 
    352 	/* Count the number of types. */
    353 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
    354 		++nextcp;
    355 
    356 	/* Build an array of that many types. */
    357 	if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
    358 		err(1, NULL);
    359 	av[0] = fslist;
    360 	for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
    361 		*nextcp = '\0';
    362 		av[i] = ++nextcp;
    363 	}
    364 	/* Terminate the array. */
    365 	av[i] = NULL;
    366 }
    367 
    368 int
    369 namematch(hp)
    370 	struct hostent *hp;
    371 {
    372 	char *cp, **np;
    373 
    374 	if ((hp == NULL) || (nfshost == NULL))
    375 		return (1);
    376 
    377 	if (strcasecmp(nfshost, hp->h_name) == 0)
    378 		return (1);
    379 
    380 	if ((cp = strchr(hp->h_name, '.')) != NULL) {
    381 		*cp = '\0';
    382 		if (strcasecmp(nfshost, hp->h_name) == 0)
    383 			return (1);
    384 	}
    385 	for (np = hp->h_aliases; *np; np++) {
    386 		if (strcasecmp(nfshost, *np) == 0)
    387 			return (1);
    388 		if ((cp = strchr(*np, '.')) != NULL) {
    389 			*cp = '\0';
    390 			if (strcasecmp(nfshost, *np) == 0)
    391 				return (1);
    392 		}
    393 	}
    394 	return (0);
    395 }
    396 
    397 /*
    398  * xdr routines for mount rpc's
    399  */
    400 int
    401 xdr_dir(xdrsp, dirp)
    402 	XDR *xdrsp;
    403 	char *dirp;
    404 {
    405 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    406 }
    407 
    408 void
    409 usage()
    410 {
    411 	(void)fprintf(stderr,
    412 	    "usage: %s\n       %s\n",
    413 	    "umount [-fv] [-t fstypelist] special | node",
    414 	    "umount -a[fv] [-h host] [-t fstypelist]");
    415 	exit(1);
    416 }
    417