Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.45
      1 /*	$NetBSD: umount.c,v 1.45 2013/06/29 23:06:29 christos 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.45 2013/06/29 23:06:29 christos 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 "mount_nfs.h"
     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 	/* Start disks transferring immediately. */
    100 	sync();
    101 
    102 #ifdef SMALL
    103 #define OPTS "fR"
    104 #else
    105 #define OPTS "AaFfh:Rt:v"
    106 #endif
    107 	while ((ch = getopt(argc, argv, OPTS)) != -1)
    108 		switch (ch) {
    109 		case 'f':
    110 			fflag = MNT_FORCE;
    111 			break;
    112 		case 'R':
    113 			raw = 1;
    114 			break;
    115 #ifndef SMALL
    116 		case 'A':
    117 		case 'a':
    118 			all = 1;
    119 			break;
    120 		case 'F':
    121 			fake = 1;
    122 			break;
    123 		case 'h':	/* -h implies -A. */
    124 			all = 1;
    125 			nfshost = optarg;
    126 			break;
    127 		case 't':
    128 			if (typelist != NULL)
    129 				errx(1, "only one -t option may be specified.");
    130 			typelist = makevfslist(optarg);
    131 			break;
    132 		case 'v':
    133 			verbose = 1;
    134 			break;
    135 #endif /* !SMALL */
    136 		default:
    137 			usage();
    138 			/* NOTREACHED */
    139 		}
    140 	argc -= optind;
    141 	argv += optind;
    142 
    143 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
    144 		usage();
    145 
    146 #ifndef SMALL
    147 	/* -h implies "-t nfs" if no -t flag. */
    148 	if ((nfshost != NULL) && (typelist == NULL))
    149 		typelist = makevfslist("nfs");
    150 
    151 	if (nfshost != NULL) {
    152 		memset(&hints, 0, sizeof hints);
    153 		getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
    154 	}
    155 
    156 	errs = 0;
    157 	if (all) {
    158 		if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) {
    159 			warn("getmntinfo");
    160 			errs = 1;
    161 		}
    162 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    163 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    164 				continue;
    165 			if (umountfs(mntbuf[mnts].f_mntonname, typelist,
    166 			             1) != 0)
    167 				errs = 1;
    168 		}
    169 	} else
    170 #endif /* !SMALL */
    171 		for (errs = 0; *argv != NULL; ++argv)
    172 			if (umountfs(*argv, typelist, raw) != 0)
    173 				errs = 1;
    174 	return errs;
    175 }
    176 
    177 static int
    178 umountfs(const char *name, const char **typelist, int raw)
    179 {
    180 #ifndef SMALL
    181 	enum clnt_stat clnt_stat;
    182 	struct timeval try;
    183 	CLIENT *clp;
    184 	char *hostp = NULL;
    185 	struct addrinfo *ai = NULL, hints;
    186 #endif /* !SMALL */
    187 	const char *mntpt;
    188 	char *type, rname[MAXPATHLEN];
    189 	mntwhat what;
    190 	struct stat sb;
    191 
    192 	if (raw) {
    193 		mntpt = name;
    194 	} else {
    195 
    196 		what = MNTANY;
    197 		if (realpath(name, rname) != NULL) {
    198 			name = rname;
    199 
    200 			if (stat(name, &sb) == 0) {
    201 				if (S_ISBLK(sb.st_mode))
    202 					what = MNTON;
    203 				else if (S_ISDIR(sb.st_mode))
    204 					what = MNTFROM;
    205 			}
    206 		}
    207 #ifdef SMALL
    208 		else {
    209  			warn("%s", rname);
    210  			return 1;
    211 		}
    212 #endif /* SMALL */
    213 		mntpt = name;
    214 
    215 		switch (what) {
    216 		case MNTON:
    217 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    218 				warnx("%s: not currently mounted", name);
    219 				return (1);
    220 			}
    221 			break;
    222 		case MNTFROM:
    223 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    224 				warnx("%s: not currently mounted", mntpt);
    225 				return (1);
    226 			}
    227 			break;
    228 		default:
    229 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    230 				name = mntpt;
    231 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    232 					warnx("%s: not currently mounted", name);
    233 					return 1;
    234 				}
    235 			}
    236 		}
    237 
    238 #ifndef SMALL
    239 		if (checkvfsname(type, typelist))
    240 			return 1;
    241 
    242 		(void)memset(&hints, 0, sizeof hints);
    243 		if (!strncmp(type, MOUNT_NFS,
    244 		    sizeof(((struct statvfs *)NULL)->f_fstypename))) {
    245 			char *delimp;
    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 	}
    263 
    264 #ifndef SMALL
    265 	if (verbose)
    266 		(void)printf("%s: unmount from %s\n", name, mntpt);
    267 	if (fake)
    268 		return 0;
    269 #endif /* ! SMALL */
    270 
    271 	if (unmount(mntpt, fflag) == -1) {
    272 		warn("%s", mntpt);
    273 		return 1;
    274 	}
    275 
    276 #ifndef SMALL
    277 	if (ai != NULL && !(fflag & MNT_FORCE)) {
    278 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1,
    279 		    getmntproto(mntpt));
    280 		if (clp  == NULL) {
    281 			clnt_pcreateerror("Cannot MNT PRC");
    282 			return 1;
    283 		}
    284 		clp->cl_auth = authsys_create_default();
    285 		try.tv_sec = 20;
    286 		try.tv_usec = 0;
    287 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
    288 		    __UNCONST(name), xdr_void, NULL, try);
    289 		if (clnt_stat != RPC_SUCCESS) {
    290 			clnt_perror(clp, "Bad MNT RPC");
    291 			return 1;
    292 		}
    293 		auth_destroy(clp->cl_auth);
    294 		clnt_destroy(clp);
    295 	}
    296 #endif /* ! SMALL */
    297 	return 0;
    298 }
    299 
    300 static char *
    301 getmntname(const char *name, mntwhat what, char **type)
    302 {
    303 	static struct statvfs *mntbuf;
    304 	static int mntsize;
    305 	int i;
    306 
    307 	if (mntbuf == NULL &&
    308 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    309 		warn("getmntinfo");
    310 		return (NULL);
    311 	}
    312 	for (i = mntsize - 1; i >= 0; i--) {
    313 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    314 			if (type)
    315 				*type = mntbuf[i].f_fstypename;
    316 			return (mntbuf[i].f_mntonname);
    317 		}
    318 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    319 			if (type)
    320 				*type = mntbuf[i].f_fstypename;
    321 			return (mntbuf[i].f_mntfromname);
    322 		}
    323 	}
    324 	return (NULL);
    325 }
    326 
    327 #ifndef SMALL
    328 static int
    329 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    330 {
    331 	const void *p1, *p2;
    332 	size_t len;
    333 
    334 	if (sa1->sa_family != sa2->sa_family)
    335 		return 1;
    336 
    337 	switch (sa1->sa_family) {
    338 	case AF_INET:
    339 		p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
    340 		p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
    341 		len = 4;
    342 		break;
    343 	case AF_INET6:
    344 		p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
    345 		p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
    346 		len = 16;
    347 		if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
    348 		    ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
    349 			return 1;
    350 		break;
    351 	default:
    352 		return 1;
    353 	}
    354 
    355 	return memcmp(p1, p2, len);
    356 }
    357 
    358 static int
    359 namematch(const struct addrinfo *ai)
    360 {
    361 	struct addrinfo *aip;
    362 
    363 	if (nfshost == NULL || nfshost_ai == NULL)
    364 		return (1);
    365 
    366 	while (ai != NULL) {
    367 		aip = nfshost_ai;
    368 		while (aip != NULL) {
    369 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
    370 				return 1;
    371 			aip = aip->ai_next;
    372 		}
    373 		ai = ai->ai_next;
    374 	}
    375 
    376 	return 0;
    377 }
    378 
    379 /*
    380  * xdr routines for mount rpc's
    381  */
    382 static int
    383 xdr_dir(XDR *xdrsp, char *dirp)
    384 {
    385 	return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
    386 }
    387 
    388 static const char *
    389 getmntproto(const char *mntpt)
    390 {
    391 	struct nfs_args nfsargs;
    392 	struct sockaddr_storage sa;
    393 	int proto;
    394 	char *name;
    395 
    396 	memset(&sa, 0, sizeof(sa));
    397 	nfsargs.addr = (struct sockaddr *)&sa;
    398 	nfsargs.addrlen = sizeof(sa);
    399 	if ((name = strdup(mntpt)) == NULL)
    400 		err(EXIT_FAILURE, "strdup");
    401 	if (!getnfsargs(name, &nfsargs))
    402 		proto = IPPROTO_UDP;
    403 	else
    404 		proto = nfsargs.proto;
    405 	free(name);
    406 
    407 	// XXX: Return udp6/tcp6 too?
    408 	return proto == IPPROTO_UDP ? "udp" : "tcp";
    409 }
    410 #endif /* !SMALL */
    411 
    412 static void
    413 usage(void)
    414 {
    415 #ifdef SMALL
    416 	(void)fprintf(stderr,
    417 	    "Usage: %s [-fR]  special | node\n", getprogname());
    418 #else
    419 	(void)fprintf(stderr,
    420 	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
    421 	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
    422 	    getprogname());
    423 #endif /* SMALL */
    424 	exit(1);
    425 }
    426