Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.32
      1  1.32       dsl /*	$NetBSD: umount.c,v 1.32 2004/03/12 21:48:32 dsl 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.19     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\n\
     35  1.19     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.32       dsl __RCSID("$NetBSD: umount.c,v 1.32 2004/03/12 21:48:32 dsl 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.1       cgd #include <sys/socket.h>
     51   1.7   mycroft 
     52   1.1       cgd #include <netdb.h>
     53   1.1       cgd #include <rpc/rpc.h>
     54   1.1       cgd #include <rpc/pmap_clnt.h>
     55   1.1       cgd #include <rpc/pmap_prot.h>
     56   1.1       cgd #include <nfs/rpcv2.h>
     57   1.1       cgd 
     58   1.6       cgd #include <err.h>
     59   1.1       cgd #include <fstab.h>
     60   1.1       cgd #include <stdio.h>
     61   1.7   mycroft #include <stdlib.h>
     62   1.1       cgd #include <string.h>
     63   1.6       cgd #include <unistd.h>
     64   1.1       cgd 
     65  1.18        pk typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     66   1.7   mycroft 
     67  1.24      fair int	fake, fflag, verbose, raw;
     68   1.7   mycroft char	*nfshost;
     69  1.28      fvdl struct addrinfo *nfshost_ai = NULL;
     70   1.7   mycroft 
     71  1.31       dsl int	 checkvfsname(char *, char **);
     72  1.31       dsl char	*getmntname(const char *, mntwhat, char **);
     73  1.31       dsl char	**makevfslist(char *);
     74  1.31       dsl int	 main(int, char *[]);
     75  1.31       dsl int	 namematch(const struct addrinfo *);
     76  1.31       dsl int	 sacmp(const struct sockaddr *, const struct sockaddr *);
     77  1.31       dsl int	 selected(int);
     78  1.32       dsl int	 umountfs(const char *, char **);
     79  1.31       dsl void	 usage(void);
     80  1.31       dsl int	 xdr_dir(XDR *, char *);
     81   1.6       cgd 
     82   1.6       cgd int
     83  1.31       dsl main(int argc, char *argv[])
     84   1.1       cgd {
     85  1.20     lukem 	int all, ch, errs, mnts;
     86  1.20     lukem 	char **typelist = NULL;
     87  1.20     lukem 	struct statfs *mntbuf;
     88  1.28      fvdl 	struct addrinfo hints;
     89   1.1       cgd 
     90   1.7   mycroft 	/* Start disks transferring immediately. */
     91   1.1       cgd 	sync();
     92   1.7   mycroft 
     93   1.7   mycroft 	all = 0;
     94  1.24      fair 	while ((ch = getopt(argc, argv, "AaFfRh:t:v")) != -1)
     95   1.7   mycroft 		switch (ch) {
     96  1.20     lukem 		case 'A':
     97   1.7   mycroft 		case 'a':
     98   1.7   mycroft 			all = 1;
     99   1.7   mycroft 			break;
    100   1.7   mycroft 		case 'F':
    101   1.7   mycroft 			fake = 1;
    102   1.1       cgd 			break;
    103   1.1       cgd 		case 'f':
    104   1.1       cgd 			fflag = MNT_FORCE;
    105   1.1       cgd 			break;
    106  1.20     lukem 		case 'h':	/* -h implies -A. */
    107  1.27       chs 			all = 1;
    108   1.7   mycroft 			nfshost = optarg;
    109   1.1       cgd 			break;
    110  1.24      fair 		case 'R':
    111  1.24      fair 			raw = 1;
    112  1.24      fair 			break;
    113   1.1       cgd 		case 't':
    114  1.12   mycroft 			if (typelist != NULL)
    115  1.12   mycroft 				errx(1, "only one -t option may be specified.");
    116  1.20     lukem 			typelist = makevfslist(optarg);
    117   1.1       cgd 			break;
    118   1.7   mycroft 		case 'v':
    119  1.12   mycroft 			verbose = 1;
    120   1.1       cgd 			break;
    121   1.1       cgd 		default:
    122   1.1       cgd 			usage();
    123   1.1       cgd 			/* NOTREACHED */
    124   1.1       cgd 		}
    125   1.1       cgd 	argc -= optind;
    126   1.1       cgd 	argv += optind;
    127   1.1       cgd 
    128  1.24      fair 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
    129   1.1       cgd 		usage();
    130   1.7   mycroft 
    131   1.7   mycroft 	/* -h implies "-t nfs" if no -t flag. */
    132   1.7   mycroft 	if ((nfshost != NULL) && (typelist == NULL))
    133  1.20     lukem 		typelist = makevfslist("nfs");
    134  1.28      fvdl 
    135  1.28      fvdl 	if (nfshost != NULL) {
    136  1.28      fvdl 		memset(&hints, 0, sizeof hints);
    137  1.28      fvdl 		getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
    138  1.28      fvdl 	}
    139   1.7   mycroft 
    140  1.20     lukem 	errs = 0;
    141  1.27       chs 	if (all) {
    142  1.20     lukem 		if ((mnts = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    143  1.20     lukem 			warn("getmntinfo");
    144  1.20     lukem 			errs = 1;
    145  1.20     lukem 		}
    146  1.20     lukem 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    147  1.20     lukem 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    148  1.20     lukem 				continue;
    149  1.20     lukem 			if (umountfs(mntbuf[mnts].f_mntonname, typelist) != 0)
    150  1.20     lukem 				errs = 1;
    151  1.20     lukem 		}
    152  1.27       chs 	} else {
    153   1.7   mycroft 		for (errs = 0; *argv != NULL; ++argv)
    154  1.20     lukem 			if (umountfs(*argv, typelist) != 0)
    155   1.7   mycroft 				errs = 1;
    156  1.20     lukem 	}
    157   1.1       cgd 	exit(errs);
    158   1.1       cgd }
    159   1.1       cgd 
    160   1.6       cgd int
    161  1.32       dsl umountfs(const char *name, char **typelist)
    162   1.1       cgd {
    163   1.7   mycroft 	enum clnt_stat clnt_stat;
    164   1.7   mycroft 	struct stat sb;
    165  1.28      fvdl 	struct timeval try;
    166   1.7   mycroft 	CLIENT *clp;
    167  1.32       dsl 	char *type, *hostp, rname[MAXPATHLEN];
    168  1.18        pk 	mntwhat what;
    169  1.28      fvdl 	struct addrinfo *ai, hints;
    170  1.32       dsl 	const char *mntpt;
    171   1.7   mycroft 
    172  1.32       dsl 	hostp = NULL;
    173  1.32       dsl 	ai = NULL;
    174   1.7   mycroft 
    175  1.24      fair 	if (raw) {
    176  1.24      fair 		mntpt = name;
    177  1.24      fair 	} else {
    178   1.7   mycroft 
    179  1.24      fair 		what = MNTANY;
    180  1.30       dsl 		if (realpath(name, rname) != NULL) {
    181  1.30       dsl 			name = rname;
    182  1.24      fair 
    183  1.30       dsl 			if (stat(name, &sb) == 0) {
    184  1.30       dsl 				if (S_ISBLK(sb.st_mode))
    185  1.30       dsl 					what = MNTON;
    186  1.30       dsl 				else if (S_ISDIR(sb.st_mode))
    187  1.30       dsl 					what = MNTFROM;
    188  1.30       dsl 			}
    189  1.17        pk 		}
    190  1.30       dsl 		mntpt = name;
    191  1.24      fair 
    192  1.24      fair 		switch (what) {
    193  1.24      fair 		case MNTON:
    194  1.20     lukem 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    195  1.15   mycroft 				warnx("%s: not currently mounted", name);
    196  1.15   mycroft 				return (1);
    197  1.15   mycroft 			}
    198  1.24      fair 			break;
    199  1.24      fair 		case MNTFROM:
    200  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    201  1.24      fair 				warnx("%s: not currently mounted", mntpt);
    202  1.24      fair 				return (1);
    203  1.24      fair 			}
    204  1.24      fair 			break;
    205  1.24      fair 		default:
    206  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    207  1.30       dsl 				name = mntpt;
    208  1.24      fair 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    209  1.24      fair 					warnx("%s: not currently mounted", name);
    210  1.24      fair 					return (1);
    211  1.24      fair 				}
    212  1.24      fair 			}
    213   1.1       cgd 		}
    214   1.7   mycroft 
    215  1.24      fair 		if (checkvfsname(type, typelist))
    216  1.24      fair 			return (1);
    217   1.1       cgd 
    218  1.28      fvdl 		memset(&hints, 0, sizeof hints);
    219  1.24      fair 		if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
    220  1.32       dsl 			char *delimp;
    221  1.30       dsl 			/* look for host:mountpoint */
    222  1.30       dsl 			if ((delimp = strrchr(name, ':')) != NULL) {
    223  1.32       dsl 				int len = delimp - name;
    224  1.32       dsl 				hostp = malloc(len + 1);
    225  1.32       dsl 				if (hostp == NULL)
    226  1.32       dsl 				    	return 1;
    227  1.32       dsl 				memcpy(hostp, name, len);
    228  1.32       dsl 				hostp[len] = 0;
    229  1.32       dsl 				name += len + 1;
    230  1.28      fvdl 				getaddrinfo(hostp, NULL, &hints, &ai);
    231  1.24      fair 			}
    232  1.20     lukem 		}
    233  1.24      fair 
    234  1.28      fvdl 		if (!namematch(ai))
    235  1.24      fair 			return (1);
    236  1.10   mycroft 	}
    237   1.7   mycroft 
    238  1.12   mycroft 	if (verbose)
    239   1.7   mycroft 		(void)printf("%s: unmount from %s\n", name, mntpt);
    240   1.7   mycroft 	if (fake)
    241   1.7   mycroft 		return (0);
    242   1.1       cgd 
    243   1.7   mycroft 	if (unmount(mntpt, fflag) < 0) {
    244   1.6       cgd 		warn("%s", mntpt);
    245   1.7   mycroft 		return (1);
    246   1.1       cgd 	}
    247   1.1       cgd 
    248  1.32       dsl 	if (ai != NULL && !(fflag & MNT_FORCE)) {
    249  1.28      fvdl 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
    250  1.28      fvdl 		if (clp  == NULL) {
    251   1.1       cgd 			clnt_pcreateerror("Cannot MNT PRC");
    252   1.1       cgd 			return (1);
    253   1.1       cgd 		}
    254  1.28      fvdl 		clp->cl_auth = authsys_create_default();
    255   1.1       cgd 		try.tv_sec = 20;
    256   1.1       cgd 		try.tv_usec = 0;
    257   1.7   mycroft 		clnt_stat = clnt_call(clp,
    258   1.7   mycroft 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
    259   1.1       cgd 		if (clnt_stat != RPC_SUCCESS) {
    260   1.1       cgd 			clnt_perror(clp, "Bad MNT RPC");
    261   1.1       cgd 			return (1);
    262   1.1       cgd 		}
    263   1.1       cgd 		auth_destroy(clp->cl_auth);
    264   1.1       cgd 		clnt_destroy(clp);
    265   1.1       cgd 	}
    266   1.7   mycroft 	return (0);
    267   1.1       cgd }
    268   1.1       cgd 
    269   1.1       cgd char *
    270  1.31       dsl getmntname(const char *name, mntwhat what, char **type)
    271   1.1       cgd {
    272  1.20     lukem 	static struct statfs *mntbuf;
    273  1.20     lukem 	static int mntsize;
    274  1.20     lukem 	int i;
    275   1.1       cgd 
    276  1.20     lukem 	if (mntbuf == NULL &&
    277  1.20     lukem 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    278   1.7   mycroft 		warn("getmntinfo");
    279   1.7   mycroft 		return (NULL);
    280   1.1       cgd 	}
    281  1.21  drochner 	for (i = mntsize - 1; i >= 0; i--) {
    282   1.7   mycroft 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    283   1.7   mycroft 			if (type)
    284  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    285   1.1       cgd 			return (mntbuf[i].f_mntonname);
    286   1.1       cgd 		}
    287   1.7   mycroft 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    288   1.7   mycroft 			if (type)
    289  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    290   1.1       cgd 			return (mntbuf[i].f_mntfromname);
    291   1.1       cgd 		}
    292   1.1       cgd 	}
    293   1.7   mycroft 	return (NULL);
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.6       cgd int
    297  1.31       dsl sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    298   1.1       cgd {
    299  1.28      fvdl 	void *p1, *p2;
    300  1.28      fvdl 	int len;
    301  1.28      fvdl 
    302  1.28      fvdl 	if (sa1->sa_family != sa2->sa_family)
    303  1.28      fvdl 		return 1;
    304  1.28      fvdl 
    305  1.28      fvdl 	switch (sa1->sa_family) {
    306  1.28      fvdl 	case AF_INET:
    307  1.28      fvdl 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
    308  1.28      fvdl 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
    309  1.28      fvdl 		len = 4;
    310  1.28      fvdl 		break;
    311  1.28      fvdl 	case AF_INET6:
    312  1.28      fvdl 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
    313  1.28      fvdl 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
    314  1.28      fvdl 		len = 16;
    315  1.28      fvdl 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
    316  1.28      fvdl 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
    317  1.28      fvdl 			return 1;
    318  1.28      fvdl 		break;
    319  1.28      fvdl 	default:
    320  1.28      fvdl 		return 1;
    321  1.28      fvdl 	}
    322  1.28      fvdl 
    323  1.28      fvdl 	return memcmp(p1, p2, len);
    324  1.28      fvdl }
    325   1.7   mycroft 
    326  1.28      fvdl int
    327  1.31       dsl namematch(const struct addrinfo *ai)
    328  1.28      fvdl {
    329  1.28      fvdl 	struct addrinfo *aip;
    330   1.1       cgd 
    331  1.28      fvdl 	if (nfshost == NULL || nfshost_ai == NULL)
    332   1.7   mycroft 		return (1);
    333   1.7   mycroft 
    334  1.28      fvdl 	while (ai != NULL) {
    335  1.28      fvdl 		aip = nfshost_ai;
    336  1.28      fvdl 		while (aip != NULL) {
    337  1.28      fvdl 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
    338  1.28      fvdl 				return 1;
    339  1.28      fvdl 			aip = aip->ai_next;
    340   1.1       cgd 		}
    341  1.28      fvdl 		ai = ai->ai_next;
    342   1.1       cgd 	}
    343  1.28      fvdl 
    344  1.28      fvdl 	return 0;
    345   1.1       cgd }
    346   1.1       cgd 
    347   1.1       cgd /*
    348   1.1       cgd  * xdr routines for mount rpc's
    349   1.1       cgd  */
    350   1.6       cgd int
    351  1.31       dsl xdr_dir(XDR *xdrsp, char *dirp)
    352   1.1       cgd {
    353   1.1       cgd 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    354   1.1       cgd }
    355   1.7   mycroft 
    356   1.7   mycroft void
    357  1.31       dsl usage(void)
    358   1.7   mycroft {
    359   1.7   mycroft 	(void)fprintf(stderr,
    360   1.7   mycroft 	    "usage: %s\n       %s\n",
    361  1.24      fair 	    "umount [-fvFR] [-t fstypelist] special | node",
    362  1.22     mikel 	    "umount -a[fvF] [-h host] [-t fstypelist]");
    363   1.7   mycroft 	exit(1);
    364   1.7   mycroft }
    365