Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.49
      1 /*	$NetBSD: umount.c,v 1.49 2016/06/26 03:05:52 dholland 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\
     35  The Regents of the University of California.  All rights reserved.");
     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.49 2016/06/26 03:05:52 dholland 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 #include <nfs/nfsmount.h>
     59 #endif /* !SMALL */
     60 
     61 #include <err.h>
     62 #include <fstab.h>
     63 #include <stdio.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 
     68 typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     69 
     70 #ifndef SMALL
     71 #include "mountprog.h"
     72 
     73 static int	 fake, verbose;
     74 static char	*nfshost;
     75 static struct addrinfo *nfshost_ai = NULL;
     76 
     77 static int	 namematch(const struct addrinfo *);
     78 static int	 sacmp(const struct sockaddr *, const struct sockaddr *);
     79 static int	 xdr_dir(XDR *, char *);
     80 static const char *getmntproto(const char *);
     81 #endif /* !SMALL */
     82 
     83 static int	 fflag;
     84 static char	*getmntname(const char *, mntwhat, char **);
     85 static int	 umountfs(const char *, const char **, int);
     86 static void	 usage(void) __dead;
     87 
     88 int
     89 main(int argc, char *argv[])
     90 {
     91 	int ch, errs, all = 0, raw = 0;
     92 #ifndef SMALL
     93 	int mnts;
     94 	struct statvfs *mntbuf;
     95 	struct addrinfo hints;
     96 #endif /* SMALL */
     97 	const char **typelist = NULL;
     98 
     99 #ifdef SMALL
    100 #define OPTS "fR"
    101 #else
    102 #define OPTS "AaFfh:Rt:v"
    103 #endif
    104 	while ((ch = getopt(argc, argv, OPTS)) != -1)
    105 		switch (ch) {
    106 		case 'f':
    107 			fflag = MNT_FORCE;
    108 			break;
    109 		case 'R':
    110 			raw = 1;
    111 			break;
    112 #ifndef SMALL
    113 		case 'A':
    114 		case 'a':
    115 			all = 1;
    116 			break;
    117 		case 'F':
    118 			fake = 1;
    119 			break;
    120 		case 'h':	/* -h implies -A. */
    121 			all = 1;
    122 			nfshost = optarg;
    123 			break;
    124 		case 't':
    125 			if (typelist != NULL)
    126 				errx(1, "only one -t option may be specified.");
    127 			typelist = makevfslist(optarg);
    128 			break;
    129 		case 'v':
    130 			verbose = 1;
    131 			break;
    132 #endif /* !SMALL */
    133 		default:
    134 			usage();
    135 			/* NOTREACHED */
    136 		}
    137 	argc -= optind;
    138 	argv += optind;
    139 
    140 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
    141 		usage();
    142 
    143 #ifndef SMALL
    144 	/* -h implies "-t nfs" if no -t flag. */
    145 	if ((nfshost != NULL) && (typelist == NULL))
    146 		typelist = makevfslist("nfs");
    147 
    148 	if (nfshost != NULL) {
    149 		memset(&hints, 0, sizeof hints);
    150 		getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
    151 	}
    152 
    153 	errs = 0;
    154 	if (all) {
    155 		if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) {
    156 			warn("getmntinfo");
    157 			errs = 1;
    158 		}
    159 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    160 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    161 				continue;
    162 			if (umountfs(mntbuf[mnts].f_mntonname, typelist,
    163 			             1) != 0)
    164 				errs = 1;
    165 		}
    166 	} else
    167 #endif /* !SMALL */
    168 		for (errs = 0; *argv != NULL; ++argv)
    169 			if (umountfs(*argv, typelist, raw) != 0)
    170 				errs = 1;
    171 	return errs;
    172 }
    173 
    174 static int
    175 umountfs(const char *name, const char **typelist, int raw)
    176 {
    177 #ifndef SMALL
    178 	enum clnt_stat clnt_stat;
    179 	struct timeval try;
    180 	CLIENT *clp;
    181 	char *hostp = NULL;
    182 	struct addrinfo *ai = NULL, hints;
    183 	const char *proto = NULL;
    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", name);
    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,
    242 		    sizeof(((struct statvfs *)NULL)->f_fstypename))) {
    243 			char *delimp;
    244 			proto = getmntproto(mntpt);
    245 			/* look for host:mountpoint */
    246 			if ((delimp = strrchr(name, ':')) != NULL) {
    247 				int len = delimp - name;
    248 				hostp = malloc(len + 1);
    249 				if (hostp == NULL)
    250 				    	return 1;
    251 				memcpy(hostp, name, len);
    252 				hostp[len] = 0;
    253 				name += len + 1;
    254 				getaddrinfo(hostp, NULL, &hints, &ai);
    255 			}
    256 		}
    257 
    258 		if (!namematch(ai))
    259 			return 1;
    260 #endif /* ! SMALL */
    261 	}
    262 
    263 #ifndef SMALL
    264 	if (verbose)
    265 		(void)printf("%s: unmount from %s\n", name, mntpt);
    266 	if (fake)
    267 		return 0;
    268 #endif /* ! SMALL */
    269 
    270 	if (unmount(mntpt, fflag) == -1) {
    271 		warn("%s", mntpt);
    272 		return 1;
    273 	}
    274 
    275 #ifndef SMALL
    276 	if (ai != NULL && !(fflag & MNT_FORCE)) {
    277 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto);
    278 		if (clp  == NULL) {
    279 			clnt_pcreateerror("Cannot MNT PRC");
    280 			return 1;
    281 		}
    282 		clp->cl_auth = authsys_create_default();
    283 		try.tv_sec = 20;
    284 		try.tv_usec = 0;
    285 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
    286 		    __UNCONST(name), xdr_void, NULL, try);
    287 		if (clnt_stat != RPC_SUCCESS) {
    288 			clnt_perror(clp, "Bad MNT RPC");
    289 			return 1;
    290 		}
    291 		auth_destroy(clp->cl_auth);
    292 		clnt_destroy(clp);
    293 	}
    294 #endif /* ! SMALL */
    295 	return 0;
    296 }
    297 
    298 static char *
    299 getmntname(const char *name, mntwhat what, char **type)
    300 {
    301 	static struct statvfs *mntbuf;
    302 	static int mntsize;
    303 	int i;
    304 
    305 	if (mntbuf == NULL &&
    306 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    307 		warn("getmntinfo");
    308 		return (NULL);
    309 	}
    310 	for (i = mntsize - 1; i >= 0; i--) {
    311 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    312 			if (type)
    313 				*type = mntbuf[i].f_fstypename;
    314 			return (mntbuf[i].f_mntonname);
    315 		}
    316 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    317 			if (type)
    318 				*type = mntbuf[i].f_fstypename;
    319 			return (mntbuf[i].f_mntfromname);
    320 		}
    321 	}
    322 	return (NULL);
    323 }
    324 
    325 #ifndef SMALL
    326 static int
    327 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    328 {
    329 	const void *p1, *p2;
    330 	size_t len;
    331 
    332 	if (sa1->sa_family != sa2->sa_family)
    333 		return 1;
    334 
    335 	switch (sa1->sa_family) {
    336 	case AF_INET:
    337 		p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
    338 		p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
    339 		len = 4;
    340 		break;
    341 	case AF_INET6:
    342 		p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
    343 		p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
    344 		len = 16;
    345 		if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
    346 		    ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
    347 			return 1;
    348 		break;
    349 	default:
    350 		return 1;
    351 	}
    352 
    353 	return memcmp(p1, p2, len);
    354 }
    355 
    356 static int
    357 namematch(const struct addrinfo *ai)
    358 {
    359 	struct addrinfo *aip;
    360 
    361 	if (nfshost == NULL || nfshost_ai == NULL)
    362 		return (1);
    363 
    364 	while (ai != NULL) {
    365 		aip = nfshost_ai;
    366 		while (aip != NULL) {
    367 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
    368 				return 1;
    369 			aip = aip->ai_next;
    370 		}
    371 		ai = ai->ai_next;
    372 	}
    373 
    374 	return 0;
    375 }
    376 
    377 /*
    378  * xdr routines for mount rpc's
    379  */
    380 static int
    381 xdr_dir(XDR *xdrsp, char *dirp)
    382 {
    383 	return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
    384 }
    385 
    386 static const char *
    387 getmntproto(const char *name)
    388 {
    389 	struct nfs_args nfsargs;
    390 	struct sockaddr_storage ss;
    391 
    392 	nfsargs.sotype = SOCK_DGRAM;
    393 	nfsargs.addr = (struct sockaddr *)&ss;
    394 	nfsargs.addrlen = sizeof(ss);
    395 	(void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
    396 	return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
    397 }
    398 #endif /* !SMALL */
    399 
    400 static void
    401 usage(void)
    402 {
    403 #ifdef SMALL
    404 	(void)fprintf(stderr,
    405 	    "Usage: %s [-fR]  special | node\n", getprogname());
    406 #else
    407 	(void)fprintf(stderr,
    408 	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
    409 	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
    410 	    getprogname());
    411 #endif /* SMALL */
    412 	exit(1);
    413 }
    414