Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.51
      1 /*	$NetBSD: umount.c,v 1.51 2016/06/26 03:51:28 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.51 2016/06/26 03:51:28 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 <errno.h>
     63 #include <fstab.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 
     69 typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     70 
     71 #ifndef SMALL
     72 #include "mountprog.h"
     73 
     74 static int	 fake, verbose;
     75 static char	*nfshost;
     76 static struct addrinfo *nfshost_ai = NULL;
     77 
     78 static int	 namematch(const struct addrinfo *);
     79 static int	 sacmp(const struct sockaddr *, const struct sockaddr *);
     80 static int	 xdr_dir(XDR *, char *);
     81 static const char *getmntproto(const char *);
     82 #endif /* !SMALL */
     83 
     84 static int	 fflag;
     85 static char	*getmntname(const char *, mntwhat, char **);
     86 static int	 umountfs(const char *, const char **, int);
     87 static void	 usage(void) __dead;
     88 
     89 int
     90 main(int argc, char *argv[])
     91 {
     92 	int ch, errs, all = 0, raw = 0;
     93 #ifndef SMALL
     94 	int mnts;
     95 	struct statvfs *mntbuf;
     96 	struct addrinfo hints;
     97 #endif /* SMALL */
     98 	const char **typelist = NULL;
     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 	const char *proto = NULL;
    185 #endif /* !SMALL */
    186 	const char *mntpt;
    187 	char *type, rname[MAXPATHLEN], umountprog[MAXPATHLEN];
    188 	mntwhat what;
    189 	struct stat sb;
    190 
    191 	if (raw) {
    192 		mntpt = name;
    193 	} else {
    194 
    195 		what = MNTANY;
    196 		if (realpath(name, rname) != NULL) {
    197 			name = rname;
    198 
    199 			if (stat(name, &sb) == 0) {
    200 				if (S_ISBLK(sb.st_mode))
    201 					what = MNTON;
    202 				else if (S_ISDIR(sb.st_mode))
    203 					what = MNTFROM;
    204 			}
    205 		}
    206 #ifdef SMALL
    207 		else {
    208  			warn("%s", name);
    209  			return 1;
    210 		}
    211 #endif /* SMALL */
    212 		mntpt = name;
    213 
    214 		switch (what) {
    215 		case MNTON:
    216 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    217 				warnx("%s: not currently mounted", name);
    218 				return (1);
    219 			}
    220 			break;
    221 		case MNTFROM:
    222 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    223 				warnx("%s: not currently mounted", mntpt);
    224 				return (1);
    225 			}
    226 			break;
    227 		default:
    228 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    229 				name = mntpt;
    230 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    231 					warnx("%s: not currently mounted", name);
    232 					return 1;
    233 				}
    234 			}
    235 		}
    236 
    237 #ifndef SMALL
    238 		if (checkvfsname(type, typelist))
    239 			return 1;
    240 
    241 		(void)memset(&hints, 0, sizeof hints);
    242 		if (!strncmp(type, MOUNT_NFS,
    243 		    sizeof(((struct statvfs *)NULL)->f_fstypename))) {
    244 			char *delimp;
    245 			proto = getmntproto(mntpt);
    246 			/* look for host:mountpoint */
    247 			if ((delimp = strrchr(name, ':')) != NULL) {
    248 				int len = delimp - name;
    249 				hostp = malloc(len + 1);
    250 				if (hostp == NULL)
    251 				    	return 1;
    252 				memcpy(hostp, name, len);
    253 				hostp[len] = 0;
    254 				name += len + 1;
    255 				getaddrinfo(hostp, NULL, &hints, &ai);
    256 			}
    257 		}
    258 
    259 		if (!namematch(ai))
    260 			return 1;
    261 #endif /* ! SMALL */
    262 		snprintf(umountprog, sizeof(umountprog), "umount_%s", type);
    263 	}
    264 
    265 #ifndef SMALL
    266 	if (verbose) {
    267 		(void)printf("%s: unmount from %s\n", name, mntpt);
    268 		/* put this before the test of FAKE */
    269 		if (!raw) {
    270 			(void)printf("Trying unmount program %s\n",
    271 			    umountprog);
    272 		}
    273 	}
    274 	if (fake)
    275 		return 0;
    276 #endif /* ! SMALL */
    277 
    278 	if (!raw) {
    279 		/*
    280 		 * The only options that need to be passed on are -f
    281 		 * and -v.
    282 		 */
    283 		char *args[3];
    284 		unsigned nargs = 0;
    285 
    286 		args[nargs++] = umountprog;
    287 		if (fflag == MNT_FORCE) {
    288 			args[nargs++] = __UNCONST("-f");
    289 		}
    290 #ifndef SMALL
    291 		if (verbose) {
    292 			args[nargs++] = __UNCONST("-v");
    293 		}
    294 #endif
    295 		execvp(umountprog, args);
    296 		if (errno != ENOENT) {
    297 			warn("%s: execvp", umountprog);
    298 		}
    299 	}
    300 
    301 #ifndef SMALL
    302 	if (verbose)
    303 		(void)printf("(No separate unmount program.)\n");
    304 #endif
    305 
    306 	if (unmount(mntpt, fflag) == -1) {
    307 		warn("%s", mntpt);
    308 		return 1;
    309 	}
    310 
    311 #ifndef SMALL
    312 	if (ai != NULL && !(fflag & MNT_FORCE)) {
    313 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto);
    314 		if (clp  == NULL) {
    315 			clnt_pcreateerror("Cannot MNT PRC");
    316 			return 1;
    317 		}
    318 		clp->cl_auth = authsys_create_default();
    319 		try.tv_sec = 20;
    320 		try.tv_usec = 0;
    321 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
    322 		    __UNCONST(name), xdr_void, NULL, try);
    323 		if (clnt_stat != RPC_SUCCESS) {
    324 			clnt_perror(clp, "Bad MNT RPC");
    325 			return 1;
    326 		}
    327 		auth_destroy(clp->cl_auth);
    328 		clnt_destroy(clp);
    329 	}
    330 #endif /* ! SMALL */
    331 	return 0;
    332 }
    333 
    334 static char *
    335 getmntname(const char *name, mntwhat what, char **type)
    336 {
    337 	static struct statvfs *mntbuf;
    338 	static int mntsize;
    339 	int i;
    340 
    341 	if (mntbuf == NULL &&
    342 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    343 		warn("getmntinfo");
    344 		return (NULL);
    345 	}
    346 	for (i = mntsize - 1; i >= 0; i--) {
    347 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    348 			if (type)
    349 				*type = mntbuf[i].f_fstypename;
    350 			return (mntbuf[i].f_mntonname);
    351 		}
    352 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    353 			if (type)
    354 				*type = mntbuf[i].f_fstypename;
    355 			return (mntbuf[i].f_mntfromname);
    356 		}
    357 	}
    358 	return (NULL);
    359 }
    360 
    361 #ifndef SMALL
    362 static int
    363 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    364 {
    365 	const void *p1, *p2;
    366 	size_t len;
    367 
    368 	if (sa1->sa_family != sa2->sa_family)
    369 		return 1;
    370 
    371 	switch (sa1->sa_family) {
    372 	case AF_INET:
    373 		p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
    374 		p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
    375 		len = 4;
    376 		break;
    377 	case AF_INET6:
    378 		p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
    379 		p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
    380 		len = 16;
    381 		if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
    382 		    ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
    383 			return 1;
    384 		break;
    385 	default:
    386 		return 1;
    387 	}
    388 
    389 	return memcmp(p1, p2, len);
    390 }
    391 
    392 static int
    393 namematch(const struct addrinfo *ai)
    394 {
    395 	struct addrinfo *aip;
    396 
    397 	if (nfshost == NULL || nfshost_ai == NULL)
    398 		return (1);
    399 
    400 	while (ai != NULL) {
    401 		aip = nfshost_ai;
    402 		while (aip != NULL) {
    403 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
    404 				return 1;
    405 			aip = aip->ai_next;
    406 		}
    407 		ai = ai->ai_next;
    408 	}
    409 
    410 	return 0;
    411 }
    412 
    413 /*
    414  * xdr routines for mount rpc's
    415  */
    416 static int
    417 xdr_dir(XDR *xdrsp, char *dirp)
    418 {
    419 	return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
    420 }
    421 
    422 static const char *
    423 getmntproto(const char *name)
    424 {
    425 	struct nfs_args nfsargs;
    426 	struct sockaddr_storage ss;
    427 
    428 	nfsargs.sotype = SOCK_DGRAM;
    429 	nfsargs.addr = (struct sockaddr *)&ss;
    430 	nfsargs.addrlen = sizeof(ss);
    431 	(void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
    432 	return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
    433 }
    434 #endif /* !SMALL */
    435 
    436 static void
    437 usage(void)
    438 {
    439 #ifdef SMALL
    440 	(void)fprintf(stderr,
    441 	    "Usage: %s [-fR]  special | node\n", getprogname());
    442 #else
    443 	(void)fprintf(stderr,
    444 	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
    445 	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
    446 	    getprogname());
    447 #endif /* SMALL */
    448 	exit(1);
    449 }
    450