Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.35
      1 /*	$NetBSD: umount.c,v 1.35 2004/09/01 01:47:09 chs 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.35 2004/09/01 01:47:09 chs 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	 main(int, char *[]);
     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 				     raw) != 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, MFSNAMELEN)) {
    244 			char *delimp;
    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) < 0) {
    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, "udp");
    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,
    286 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, 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 	void *p1, *p2;
    330 	int 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 = &((struct sockaddr_in *)sa1)->sin_addr;
    338 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
    339 		len = 4;
    340 		break;
    341 	case AF_INET6:
    342 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
    343 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
    344 		len = 16;
    345 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
    346 		    ((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 #endif /* !SMALL */
    386 
    387 static void
    388 usage(void)
    389 {
    390 #ifdef SMALL
    391 	(void)fprintf(stderr,
    392 	    "Usage: %s [-fR]  special | node\n", getprogname());
    393 #else
    394 	(void)fprintf(stderr,
    395 	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
    396 	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
    397 	    getprogname());
    398 #endif /* SMALL */
    399 	exit(1);
    400 }
    401