Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.27
      1  1.27       chs /*	$NetBSD: umount.c,v 1.27 2000/06/06 07:09:15 chs 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.19     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\n\
     39  1.19     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43  1.13       cgd #if 0
     44  1.20     lukem static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
     45  1.13       cgd #else
     46  1.27       chs __RCSID("$NetBSD: umount.c,v 1.27 2000/06/06 07:09:15 chs Exp $");
     47  1.13       cgd #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/param.h>
     51   1.1       cgd #include <sys/stat.h>
     52   1.1       cgd #include <sys/mount.h>
     53   1.1       cgd #include <sys/time.h>
     54   1.1       cgd #include <sys/socket.h>
     55   1.7   mycroft 
     56   1.1       cgd #include <netdb.h>
     57   1.1       cgd #include <rpc/rpc.h>
     58   1.1       cgd #include <rpc/pmap_clnt.h>
     59   1.1       cgd #include <rpc/pmap_prot.h>
     60   1.1       cgd #include <nfs/rpcv2.h>
     61   1.1       cgd 
     62   1.6       cgd #include <err.h>
     63   1.1       cgd #include <fstab.h>
     64   1.1       cgd #include <stdio.h>
     65   1.7   mycroft #include <stdlib.h>
     66   1.1       cgd #include <string.h>
     67   1.6       cgd #include <unistd.h>
     68   1.1       cgd 
     69  1.18        pk typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
     70   1.7   mycroft 
     71  1.24      fair int	fake, fflag, verbose, raw;
     72   1.7   mycroft char	*nfshost;
     73   1.7   mycroft 
     74  1.20     lukem int	 checkvfsname __P((const char *, char **));
     75  1.20     lukem char	*getmntname __P((char *, mntwhat, char **));
     76  1.20     lukem char	**makevfslist __P((char *));
     77  1.19     lukem int	 main __P((int, char *[]));
     78  1.19     lukem int	 namematch __P((struct hostent *));
     79  1.20     lukem int	 selected __P((int));
     80  1.20     lukem int	 umountfs __P((char *, char **));
     81   1.7   mycroft void	 usage __P((void));
     82   1.7   mycroft int	 xdr_dir __P((XDR *, char *));
     83   1.6       cgd 
     84   1.6       cgd int
     85   1.1       cgd main(argc, argv)
     86   1.1       cgd 	int argc;
     87   1.7   mycroft 	char *argv[];
     88   1.1       cgd {
     89  1.20     lukem 	int all, ch, errs, mnts;
     90  1.20     lukem 	char **typelist = NULL;
     91  1.20     lukem 	struct statfs *mntbuf;
     92   1.1       cgd 
     93   1.7   mycroft 	/* Start disks transferring immediately. */
     94   1.1       cgd 	sync();
     95   1.7   mycroft 
     96   1.7   mycroft 	all = 0;
     97  1.24      fair 	while ((ch = getopt(argc, argv, "AaFfRh:t:v")) != -1)
     98   1.7   mycroft 		switch (ch) {
     99  1.20     lukem 		case 'A':
    100   1.7   mycroft 		case 'a':
    101   1.7   mycroft 			all = 1;
    102   1.7   mycroft 			break;
    103   1.7   mycroft 		case 'F':
    104   1.7   mycroft 			fake = 1;
    105   1.1       cgd 			break;
    106   1.1       cgd 		case 'f':
    107   1.1       cgd 			fflag = MNT_FORCE;
    108   1.1       cgd 			break;
    109  1.20     lukem 		case 'h':	/* -h implies -A. */
    110  1.27       chs 			all = 1;
    111   1.7   mycroft 			nfshost = optarg;
    112   1.1       cgd 			break;
    113  1.24      fair 		case 'R':
    114  1.24      fair 			raw = 1;
    115  1.24      fair 			break;
    116   1.1       cgd 		case 't':
    117  1.12   mycroft 			if (typelist != NULL)
    118  1.12   mycroft 				errx(1, "only one -t option may be specified.");
    119  1.20     lukem 			typelist = makevfslist(optarg);
    120   1.1       cgd 			break;
    121   1.7   mycroft 		case 'v':
    122  1.12   mycroft 			verbose = 1;
    123   1.1       cgd 			break;
    124   1.1       cgd 		default:
    125   1.1       cgd 			usage();
    126   1.1       cgd 			/* NOTREACHED */
    127   1.1       cgd 		}
    128   1.1       cgd 	argc -= optind;
    129   1.1       cgd 	argv += optind;
    130   1.1       cgd 
    131  1.24      fair 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
    132   1.1       cgd 		usage();
    133   1.7   mycroft 
    134   1.7   mycroft 	/* -h implies "-t nfs" if no -t flag. */
    135   1.7   mycroft 	if ((nfshost != NULL) && (typelist == NULL))
    136  1.20     lukem 		typelist = makevfslist("nfs");
    137   1.7   mycroft 
    138  1.20     lukem 	errs = 0;
    139  1.27       chs 	if (all) {
    140  1.20     lukem 		if ((mnts = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    141  1.20     lukem 			warn("getmntinfo");
    142  1.20     lukem 			errs = 1;
    143  1.20     lukem 		}
    144  1.20     lukem 		for (errs = 0, mnts--; mnts > 0; mnts--) {
    145  1.20     lukem 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
    146  1.20     lukem 				continue;
    147  1.20     lukem 			if (umountfs(mntbuf[mnts].f_mntonname, typelist) != 0)
    148  1.20     lukem 				errs = 1;
    149  1.20     lukem 		}
    150  1.27       chs 	} else {
    151   1.7   mycroft 		for (errs = 0; *argv != NULL; ++argv)
    152  1.20     lukem 			if (umountfs(*argv, typelist) != 0)
    153   1.7   mycroft 				errs = 1;
    154  1.20     lukem 	}
    155   1.1       cgd 	exit(errs);
    156   1.1       cgd }
    157   1.1       cgd 
    158   1.6       cgd int
    159  1.20     lukem umountfs(name, typelist)
    160   1.1       cgd 	char *name;
    161  1.20     lukem 	char **typelist;
    162   1.1       cgd {
    163   1.7   mycroft 	enum clnt_stat clnt_stat;
    164   1.7   mycroft 	struct hostent *hp;
    165   1.1       cgd 	struct sockaddr_in saddr;
    166   1.7   mycroft 	struct stat sb;
    167   1.1       cgd 	struct timeval pertry, try;
    168   1.7   mycroft 	CLIENT *clp;
    169   1.7   mycroft 	int so;
    170  1.20     lukem 	char *type, *delimp, *hostp, *mntpt, rname[MAXPATHLEN];
    171  1.18        pk 	mntwhat what;
    172   1.7   mycroft 
    173  1.19     lukem 	hp = NULL;
    174  1.19     lukem 	delimp = NULL;
    175   1.7   mycroft 
    176  1.24      fair 	if (raw) {
    177  1.24      fair 		mntpt = name;
    178  1.24      fair 	} else {
    179   1.7   mycroft 
    180  1.24      fair 		if (realpath(name, rname) == NULL) {
    181  1.24      fair 			warn("%s", rname);
    182  1.18        pk 			return (1);
    183  1.18        pk 		}
    184  1.24      fair 
    185  1.24      fair 		what = MNTANY;
    186  1.24      fair 		mntpt = name = rname;
    187  1.24      fair 
    188  1.24      fair 		if (stat(name, &sb) == 0) {
    189  1.24      fair 			if (S_ISBLK(sb.st_mode))
    190  1.24      fair 				what = MNTON;
    191  1.24      fair 			else if (S_ISDIR(sb.st_mode))
    192  1.24      fair 				what = MNTFROM;
    193  1.17        pk 		}
    194  1.24      fair 
    195  1.24      fair 		switch (what) {
    196  1.24      fair 		case MNTON:
    197  1.20     lukem 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    198  1.15   mycroft 				warnx("%s: not currently mounted", name);
    199  1.15   mycroft 				return (1);
    200  1.15   mycroft 			}
    201  1.24      fair 			break;
    202  1.24      fair 		case MNTFROM:
    203  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    204  1.24      fair 				warnx("%s: not currently mounted", mntpt);
    205  1.24      fair 				return (1);
    206  1.24      fair 			}
    207  1.24      fair 			break;
    208  1.24      fair 		default:
    209  1.24      fair 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
    210  1.24      fair 				name = rname;
    211  1.24      fair 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
    212  1.24      fair 					warnx("%s: not currently mounted", name);
    213  1.24      fair 					return (1);
    214  1.24      fair 				}
    215  1.24      fair 			}
    216   1.1       cgd 		}
    217   1.7   mycroft 
    218  1.24      fair 		if (checkvfsname(type, typelist))
    219  1.24      fair 			return (1);
    220   1.1       cgd 
    221  1.24      fair 		hp = NULL;
    222  1.24      fair 		if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
    223  1.24      fair 			if ((delimp = strchr(name, '@')) != NULL) {
    224  1.24      fair 				hostp = delimp + 1;
    225  1.24      fair 				*delimp = '\0';
    226  1.24      fair 				hp = gethostbyname(hostp);
    227  1.24      fair 				*delimp = '@';
    228  1.24      fair 			} else if ((delimp = strchr(name, ':')) != NULL) {
    229  1.24      fair 				*delimp = '\0';
    230  1.24      fair 				hostp = name;
    231  1.24      fair 				hp = gethostbyname(hostp);
    232  1.24      fair 				name = delimp + 1;
    233  1.24      fair 				*delimp = ':';
    234  1.24      fair 			}
    235  1.20     lukem 		}
    236  1.24      fair 
    237  1.24      fair 		if (!namematch(hp))
    238  1.24      fair 			return (1);
    239  1.10   mycroft 	}
    240   1.7   mycroft 
    241  1.12   mycroft 	if (verbose)
    242   1.7   mycroft 		(void)printf("%s: unmount from %s\n", name, mntpt);
    243   1.7   mycroft 	if (fake)
    244   1.7   mycroft 		return (0);
    245   1.1       cgd 
    246   1.7   mycroft 	if (unmount(mntpt, fflag) < 0) {
    247   1.6       cgd 		warn("%s", mntpt);
    248   1.7   mycroft 		return (1);
    249   1.1       cgd 	}
    250   1.1       cgd 
    251  1.25  sommerfe 	if (!raw && !strncmp(type, MOUNT_NFS, MFSNAMELEN) &&
    252  1.10   mycroft 	    (hp != NULL) && !(fflag & MNT_FORCE)) {
    253   1.1       cgd 		*delimp = '\0';
    254   1.7   mycroft 		memset(&saddr, 0, sizeof(saddr));
    255   1.1       cgd 		saddr.sin_family = AF_INET;
    256   1.1       cgd 		saddr.sin_port = 0;
    257   1.7   mycroft 		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
    258   1.1       cgd 		pertry.tv_sec = 3;
    259   1.1       cgd 		pertry.tv_usec = 0;
    260   1.7   mycroft 		so = RPC_ANYSOCK;
    261   1.7   mycroft 		if ((clp = clntudp_create(&saddr,
    262   1.7   mycroft 		    RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
    263   1.1       cgd 			clnt_pcreateerror("Cannot MNT PRC");
    264   1.1       cgd 			return (1);
    265   1.1       cgd 		}
    266   1.1       cgd 		clp->cl_auth = authunix_create_default();
    267   1.1       cgd 		try.tv_sec = 20;
    268   1.1       cgd 		try.tv_usec = 0;
    269   1.7   mycroft 		clnt_stat = clnt_call(clp,
    270   1.7   mycroft 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
    271   1.1       cgd 		if (clnt_stat != RPC_SUCCESS) {
    272   1.1       cgd 			clnt_perror(clp, "Bad MNT RPC");
    273   1.1       cgd 			return (1);
    274   1.1       cgd 		}
    275   1.1       cgd 		auth_destroy(clp->cl_auth);
    276   1.1       cgd 		clnt_destroy(clp);
    277   1.1       cgd 	}
    278   1.7   mycroft 	return (0);
    279   1.1       cgd }
    280   1.1       cgd 
    281   1.1       cgd char *
    282   1.1       cgd getmntname(name, what, type)
    283   1.1       cgd 	char *name;
    284   1.7   mycroft 	mntwhat what;
    285  1.20     lukem 	char **type;
    286   1.1       cgd {
    287  1.20     lukem 	static struct statfs *mntbuf;
    288  1.20     lukem 	static int mntsize;
    289  1.20     lukem 	int i;
    290   1.1       cgd 
    291  1.20     lukem 	if (mntbuf == NULL &&
    292  1.20     lukem 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    293   1.7   mycroft 		warn("getmntinfo");
    294   1.7   mycroft 		return (NULL);
    295   1.1       cgd 	}
    296  1.21  drochner 	for (i = mntsize - 1; i >= 0; i--) {
    297   1.7   mycroft 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
    298   1.7   mycroft 			if (type)
    299  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    300   1.1       cgd 			return (mntbuf[i].f_mntonname);
    301   1.1       cgd 		}
    302   1.7   mycroft 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
    303   1.7   mycroft 			if (type)
    304  1.20     lukem 				*type = mntbuf[i].f_fstypename;
    305   1.1       cgd 			return (mntbuf[i].f_mntfromname);
    306   1.1       cgd 		}
    307   1.1       cgd 	}
    308   1.7   mycroft 	return (NULL);
    309   1.1       cgd }
    310   1.1       cgd 
    311   1.6       cgd int
    312   1.7   mycroft namematch(hp)
    313   1.1       cgd 	struct hostent *hp;
    314   1.1       cgd {
    315   1.7   mycroft 	char *cp, **np;
    316   1.7   mycroft 
    317   1.7   mycroft 	if ((hp == NULL) || (nfshost == NULL))
    318   1.7   mycroft 		return (1);
    319   1.1       cgd 
    320   1.1       cgd 	if (strcasecmp(nfshost, hp->h_name) == 0)
    321   1.7   mycroft 		return (1);
    322   1.7   mycroft 
    323   1.6       cgd 	if ((cp = strchr(hp->h_name, '.')) != NULL) {
    324   1.1       cgd 		*cp = '\0';
    325   1.1       cgd 		if (strcasecmp(nfshost, hp->h_name) == 0)
    326   1.7   mycroft 			return (1);
    327   1.1       cgd 	}
    328   1.1       cgd 	for (np = hp->h_aliases; *np; np++) {
    329   1.1       cgd 		if (strcasecmp(nfshost, *np) == 0)
    330   1.7   mycroft 			return (1);
    331   1.6       cgd 		if ((cp = strchr(*np, '.')) != NULL) {
    332   1.1       cgd 			*cp = '\0';
    333   1.1       cgd 			if (strcasecmp(nfshost, *np) == 0)
    334   1.7   mycroft 				return (1);
    335   1.1       cgd 		}
    336   1.1       cgd 	}
    337   1.7   mycroft 	return (0);
    338   1.1       cgd }
    339   1.1       cgd 
    340   1.1       cgd /*
    341   1.1       cgd  * xdr routines for mount rpc's
    342   1.1       cgd  */
    343   1.6       cgd int
    344   1.1       cgd xdr_dir(xdrsp, dirp)
    345   1.1       cgd 	XDR *xdrsp;
    346   1.1       cgd 	char *dirp;
    347   1.1       cgd {
    348   1.1       cgd 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    349   1.1       cgd }
    350   1.7   mycroft 
    351   1.7   mycroft void
    352   1.7   mycroft usage()
    353   1.7   mycroft {
    354   1.7   mycroft 	(void)fprintf(stderr,
    355   1.7   mycroft 	    "usage: %s\n       %s\n",
    356  1.24      fair 	    "umount [-fvFR] [-t fstypelist] special | node",
    357  1.22     mikel 	    "umount -a[fvF] [-h host] [-t fstypelist]");
    358   1.7   mycroft 	exit(1);
    359   1.7   mycroft }
    360