Home | History | Annotate | Line # | Download | only in umount
umount.c revision 1.4
      1 /*-
      2  * Copyright (c) 1980, 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)umount.c	5.16 (Berkeley) 6/3/91";*/
     42 static char rcsid[] = "$Id: umount.c,v 1.4 1993/08/01 18:23:43 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <sys/mount.h>
     48 
     49 #ifdef NFS
     50 #include <sys/time.h>
     51 #include <sys/socket.h>
     52 #include <sys/socketvar.h>
     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
     59 
     60 #include <fstab.h>
     61 #include <stdio.h>
     62 #include <string.h>
     63 
     64 #ifdef NFS
     65 int xdr_dir();
     66 char *nfshost;
     67 #endif
     68 
     69 int	vflag, all, errs, fake;
     70 int	fflag = MNT_NOFORCE;
     71 char	*getmntname();
     72 
     73 #define	MNTON	1
     74 #define	MNTFROM	2
     75 #define	MNTTYPE 3
     76 
     77 int *typelist, *maketypelist();
     78 
     79 main(argc, argv)
     80 	int argc;
     81 	char **argv;
     82 {
     83 	extern char *optarg;
     84 	extern int optind;
     85 	int ch;
     86 
     87 	sync();
     88 	while ((ch = getopt(argc, argv, "afFh:t:v")) != EOF)
     89 		switch((char)ch) {
     90 		case 'v':
     91 			vflag++;
     92 			break;
     93 		case 'f':
     94 			fflag = MNT_FORCE;
     95 			break;
     96 		case 'F':
     97 			fake++;
     98 			break;
     99 		case 'a':
    100 			all++;
    101 			break;
    102 		case 't':
    103 			typelist = maketypelist(optarg);
    104 			break;
    105 #ifdef	NFS
    106 		case 'h':
    107 			/* -h flag implies -a, and "-t nfs" if no -t flag */
    108 			nfshost = optarg;
    109 			all++;
    110 			if (typelist == NULL)
    111 				typelist = maketypelist("nfs");
    112 			break;
    113 #endif /* NFS */
    114 		case '?':
    115 		default:
    116 			usage();
    117 			/* NOTREACHED */
    118 		}
    119 	argc -= optind;
    120 	argv += optind;
    121 
    122 	if (argc == 0 && !all)
    123 		usage();
    124 	if (all) {
    125 		if (argc > 0)
    126 			usage();
    127 		if (setfsent() == 0)
    128 			perror(FSTAB), exit(1);
    129 		umountall(typelist);
    130 		exit(0);
    131 	} else
    132 		setfsent();
    133 	while (argc > 0) {
    134 		if (umountfs(*argv++, 0) == 0)
    135 			errs++;
    136 		argc--;
    137 	}
    138 	exit(errs);
    139 }
    140 
    141 usage()
    142 {
    143 	fprintf(stderr,
    144 		"%s\n%s\n",
    145 		"Usage: umount [-fv] special | node",
    146 #ifndef	NFS
    147 		"    or umount -a[fv] [-t fstypelist]"
    148 #else
    149 		"    or umount -a[fv] [-h host] [-t fstypelist]"
    150 #endif
    151 	      );
    152 	exit(1);
    153 }
    154 
    155 umountall(typelist)
    156 	char **typelist;
    157 {
    158 	register struct fstab *fs;
    159 	struct fstab *allocfsent();
    160 
    161 	if ((fs = getfsent()) == (struct fstab *)0)
    162 		return;
    163 	fs = allocfsent(fs);
    164 	umountall(typelist);
    165 	if (strcmp(fs->fs_file, "/") == 0) {
    166 		freefsent(fs);
    167 		return;
    168 	}
    169 	if (strcmp(fs->fs_type, FSTAB_RW) &&
    170 	    strcmp(fs->fs_type, FSTAB_RO) &&
    171 	    strcmp(fs->fs_type, FSTAB_RQ)) {
    172 		freefsent(fs);
    173 		return;
    174 	}
    175 	(void) umountfs(fs->fs_file, typelist);
    176 	freefsent(fs);
    177 }
    178 
    179 struct fstab *
    180 allocfsent(fs)
    181 	register struct fstab *fs;
    182 {
    183 	register struct fstab *new;
    184 	register char *cp;
    185 
    186 	new = (struct fstab *)malloc((unsigned)sizeof (*fs));
    187 	cp = (char *)malloc((unsigned)strlen(fs->fs_file) + 1);
    188 	strcpy(cp, fs->fs_file);
    189 	new->fs_file = cp;
    190 	cp = (char *)malloc((unsigned)strlen(fs->fs_type) + 1);
    191 	strcpy(cp, fs->fs_type);
    192 	new->fs_type = cp;
    193 	cp = (char *)malloc((unsigned)strlen(fs->fs_spec) + 1);
    194 	strcpy(cp, fs->fs_spec);
    195 	new->fs_spec = cp;
    196 	new->fs_passno = fs->fs_passno;
    197 	new->fs_freq = fs->fs_freq;
    198 	return (new);
    199 }
    200 
    201 freefsent(fs)
    202 	register struct fstab *fs;
    203 {
    204 
    205 	if (fs->fs_file)
    206 		free(fs->fs_file);
    207 	if (fs->fs_spec)
    208 		free(fs->fs_spec);
    209 	if (fs->fs_type)
    210 		free(fs->fs_type);
    211 	free((char *)fs);
    212 }
    213 
    214 umountfs(name, typelist)
    215 	char *name;
    216 	int *typelist;
    217 {
    218 	char *mntpt;
    219 	struct stat stbuf;
    220 	int type;
    221 #ifdef NFS
    222 	register CLIENT *clp;
    223 	struct hostent *hp = 0;
    224 	struct sockaddr_in saddr;
    225 	struct timeval pertry, try;
    226 	enum clnt_stat clnt_stat;
    227 	int so = RPC_ANYSOCK;
    228 	char *hostp, *delimp;
    229 #endif /* NFS */
    230 
    231 	if (stat(name, &stbuf) < 0) {
    232 		if (getmntname(name, MNTFROM, &type) != 0)
    233 			mntpt = name;
    234 		else if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
    235 			fprintf(stderr, "%s: not currently mounted\n", name);
    236 			return (0);
    237 		}
    238 	} else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
    239 		if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
    240 			fprintf(stderr, "%s: not currently mounted\n", name);
    241 			return (0);
    242 		}
    243 	} else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
    244 		mntpt = name;
    245 		if (getmntname(mntpt, MNTFROM, &type) == 0) {
    246 			fprintf(stderr, "%s: not currently mounted\n", name);
    247 			return (0);
    248 		}
    249 	} else {
    250 		fprintf(stderr, "%s: not a directory or special device\n",
    251 			name);
    252 		return (0);
    253 	}
    254 
    255 	if (badtype(type, typelist))
    256 		return(1);
    257 #ifdef NFS
    258 	if ((delimp = index(name, '@')) != NULL) {
    259 		hostp = delimp + 1;
    260 		*delimp = '\0';
    261 		hp = gethostbyname(hostp);
    262 		*delimp = '@';
    263 	} else if ((delimp = index(name, ':')) != NULL) {
    264 		*delimp = '\0';
    265 		hostp = name;
    266 		hp = gethostbyname(hostp);
    267 		name = delimp+1;
    268 		*delimp = ':';
    269 	}
    270 
    271 	if (!namematch(hp, nfshost))
    272 		return(1);
    273 #endif	/* NFS */
    274 	if (!fake && unmount(mntpt, fflag) < 0) {
    275 		perror(mntpt);
    276 		return (0);
    277 	}
    278 	if (vflag)
    279 		fprintf(stderr, "%s: Unmounted from %s\n", name, mntpt);
    280 
    281 #ifdef	NFS
    282 	if (!fake && hp != NULL && (fflag & MNT_FORCE) == 0) {
    283 		*delimp = '\0';
    284 		bcopy(hp->h_addr,(caddr_t)&saddr.sin_addr,hp->h_length);
    285 		saddr.sin_family = AF_INET;
    286 		saddr.sin_port = 0;
    287 		pertry.tv_sec = 3;
    288 		pertry.tv_usec = 0;
    289 		if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
    290 		    pertry, &so)) == NULL) {
    291 			clnt_pcreateerror("Cannot MNT PRC");
    292 			return (1);
    293 		}
    294 		clp->cl_auth = authunix_create_default();
    295 		try.tv_sec = 20;
    296 		try.tv_usec = 0;
    297 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, name,
    298 			xdr_void, (caddr_t)0, try);
    299 		if (clnt_stat != RPC_SUCCESS) {
    300 			clnt_perror(clp, "Bad MNT RPC");
    301 			return (1);
    302 		}
    303 		auth_destroy(clp->cl_auth);
    304 		clnt_destroy(clp);
    305 	}
    306 #endif /* NFS */
    307 	return (1);
    308 }
    309 
    310 char *
    311 getmntname(name, what, type)
    312 	char *name;
    313 	int what;
    314 	int *type;
    315 {
    316 	int mntsize, i;
    317 	struct statfs *mntbuf;
    318 
    319 	if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
    320 		perror("umount");
    321 		return (0);
    322 	}
    323 	for (i = 0; i < mntsize; i++) {
    324 		if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name)) {
    325 			if (type)
    326 				*type = mntbuf[i].f_type;
    327 			return (mntbuf[i].f_mntonname);
    328 		}
    329 		if (what == MNTFROM && !strcmp(mntbuf[i].f_mntonname, name)) {
    330 			if (type)
    331 				*type = mntbuf[i].f_type;
    332 			return (mntbuf[i].f_mntfromname);
    333 		}
    334 	}
    335 	return (0);
    336 }
    337 
    338 static int skipvfs;
    339 
    340 badtype(type, typelist)
    341 	int type;
    342 	int *typelist;
    343 {
    344 	if (typelist == 0)
    345 		return(0);
    346 	while (*typelist) {
    347 		if (type == *typelist)
    348 			return(skipvfs);
    349 		typelist++;
    350 	}
    351 	return(!skipvfs);
    352 }
    353 
    354 int *
    355 maketypelist(fslist)
    356 	char *fslist;
    357 {
    358 	register char *nextcp;
    359 	register int *av, i;
    360 
    361 	if (fslist == NULL)
    362 		return(NULL);
    363 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    364 		fslist += 2;
    365 		skipvfs = 1;
    366 	} else
    367 		skipvfs = 0;
    368 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
    369 		if (*nextcp == ',')
    370 			i++;
    371 	av = (int *)malloc((i+2) * sizeof(int));
    372 	if (av == NULL)
    373 		return(NULL);
    374 	for (i = 0; fslist; fslist = nextcp) {
    375 		if (nextcp = index(fslist, ','))
    376 			*nextcp++ = '\0';
    377 		if (strcmp(fslist, "ufs") == 0)
    378 			av[i++] = MOUNT_UFS;
    379 		else if (strcmp(fslist, "nfs") == 0)
    380 			av[i++] = MOUNT_NFS;
    381 		else if (strcmp(fslist, "mfs") == 0)
    382 			av[i++] = MOUNT_MFS;
    383 		else if (strcmp(fslist, "msdos") == 0)
    384 			av[i++] = MOUNT_MSDOS;
    385 		else if (strcmp(fslist, "isofs") == 0)
    386 			av[i++] = MOUNT_ISOFS;
    387 	}
    388 	av[i++] = 0;
    389 	return(av);
    390 }
    391 
    392 #ifdef	NFS
    393 namematch(hp, nfshost)
    394 	struct hostent *hp;
    395 	char *nfshost;
    396 {
    397 	register char *cp;
    398 	register char **np;
    399 
    400 	if (hp == NULL || nfshost == NULL)
    401 		return(1);
    402 	if (strcasecmp(nfshost, hp->h_name) == 0)
    403 		return(1);
    404 	if (cp = index(hp->h_name, '.')) {
    405 		*cp = '\0';
    406 		if (strcasecmp(nfshost, hp->h_name) == 0)
    407 			return(1);
    408 	}
    409 	for (np = hp->h_aliases; *np; np++) {
    410 		if (strcasecmp(nfshost, *np) == 0)
    411 			return(1);
    412 		if (cp = index(*np, '.')) {
    413 			*cp = '\0';
    414 			if (strcasecmp(nfshost, *np) == 0)
    415 				return(1);
    416 		}
    417 	}
    418 	return(0);
    419 }
    420 
    421 /*
    422  * xdr routines for mount rpc's
    423  */
    424 xdr_dir(xdrsp, dirp)
    425 	XDR *xdrsp;
    426 	char *dirp;
    427 {
    428 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    429 }
    430 #endif /* NFS */
    431