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