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