xref: /onnv-gate/usr/src/cmd/fs.d/nfs/mountd/rmtab.c (revision 11211:a6230133d60c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53957Sth199096  * Common Development and Distribution License (the "License").
63957Sth199096  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213957Sth199096 
220Sstevel@tonic-gate /*
23*11211SThomas.Haynes@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <ctype.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <rpcsvc/mount.h>
380Sstevel@tonic-gate #include <sys/pathconf.h>
390Sstevel@tonic-gate #include <sys/systeminfo.h>
400Sstevel@tonic-gate #include <sys/utsname.h>
410Sstevel@tonic-gate #include <signal.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <thread.h>
450Sstevel@tonic-gate #include <syslog.h>
460Sstevel@tonic-gate #include <sys/socket.h>
470Sstevel@tonic-gate #include <netinet/in.h>
480Sstevel@tonic-gate #include <arpa/inet.h>
493957Sth199096 #include <sharefs/share.h>
500Sstevel@tonic-gate #include "../lib/sharetab.h"
510Sstevel@tonic-gate #include "hashset.h"
520Sstevel@tonic-gate #include "mountd.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static char RMTAB[] = "/etc/rmtab";
550Sstevel@tonic-gate static FILE *rmtabf = NULL;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * There is nothing magic about the value selected here. Too low,
590Sstevel@tonic-gate  * and mountd might spend too much time rewriting the rmtab file.
600Sstevel@tonic-gate  * Too high, it won't do it frequently enough.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate static int rmtab_del_thresh = 250;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	RMTAB_TOOMANY_DELETED()	\
650Sstevel@tonic-gate 	((rmtab_deleted > rmtab_del_thresh) && (rmtab_deleted > rmtab_inuse))
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * mountd's version of a "struct mountlist". It is the same except
690Sstevel@tonic-gate  * for the added ml_pos field.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate struct mntentry {
720Sstevel@tonic-gate 	char  *m_host;
730Sstevel@tonic-gate 	char  *m_path;
740Sstevel@tonic-gate 	long   m_pos;
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static HASHSET mntlist;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static int mntentry_equal(const void *, const void *);
800Sstevel@tonic-gate static uint32_t mntentry_hash(const void *);
810Sstevel@tonic-gate static int mntlist_contains(char *, char *);
820Sstevel@tonic-gate static void rmtab_delete(long);
830Sstevel@tonic-gate static long rmtab_insert(char *, char *);
840Sstevel@tonic-gate static void rmtab_rewrite(void);
850Sstevel@tonic-gate static void rmtab_parse(char *buf);
860Sstevel@tonic-gate static bool_t xdr_mntlistencode(XDR * xdrs, HASHSET * mntlist);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	exstrdup(s) \
890Sstevel@tonic-gate 	strcpy(exmalloc(strlen(s)+1), s)
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static int rmtab_inuse;
930Sstevel@tonic-gate static int rmtab_deleted;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static rwlock_t rmtab_lock;	/* lock to protect rmtab list */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Check whether the given client/path combination
1000Sstevel@tonic-gate  * already appears in the mount list.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static int
mntlist_contains(char * host,char * path)1040Sstevel@tonic-gate mntlist_contains(char *host, char *path)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	struct mntentry m;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	m.m_host = host;
1090Sstevel@tonic-gate 	m.m_path = path;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	return (h_get(mntlist, &m) != NULL);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  *  Add an entry to the mount list.
1170Sstevel@tonic-gate  *  First check whether it's there already - the client
1180Sstevel@tonic-gate  *  may have crashed and be rebooting.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static void
mntlist_insert(char * host,char * path)1220Sstevel@tonic-gate mntlist_insert(char *host, char *path)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	if (!mntlist_contains(host, path)) {
1250Sstevel@tonic-gate 		struct mntentry *m;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		m = exmalloc(sizeof (struct mntentry));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		m->m_host = exstrdup(host);
1300Sstevel@tonic-gate 		m->m_path = exstrdup(path);
1310Sstevel@tonic-gate 		m->m_pos = rmtab_insert(host, path);
1320Sstevel@tonic-gate 		(void) h_put(mntlist, m);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate void
mntlist_new(char * host,char * path)1370Sstevel@tonic-gate mntlist_new(char *host, char *path)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	(void) rw_wrlock(&rmtab_lock);
1400Sstevel@tonic-gate 	mntlist_insert(host, path);
1410Sstevel@tonic-gate 	(void) rw_unlock(&rmtab_lock);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * Delete an entry from the mount list.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate void
mntlist_delete(char * host,char * path)1490Sstevel@tonic-gate mntlist_delete(char *host, char *path)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	struct mntentry *m, mm;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	mm.m_host = host;
1540Sstevel@tonic-gate 	mm.m_path = path;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	(void) rw_wrlock(&rmtab_lock);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (m = (struct mntentry *)h_get(mntlist, &mm)) {
1590Sstevel@tonic-gate 		rmtab_delete(m->m_pos);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		(void) h_delete(mntlist, m);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		free(m->m_path);
1640Sstevel@tonic-gate 		free(m->m_host);
1650Sstevel@tonic-gate 		free(m);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		if (RMTAB_TOOMANY_DELETED())
1680Sstevel@tonic-gate 			rmtab_rewrite();
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	(void) rw_unlock(&rmtab_lock);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Delete all entries for a host from the mount list
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate void
mntlist_delete_all(char * host)1780Sstevel@tonic-gate mntlist_delete_all(char *host)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	HASHSET_ITERATOR iterator;
1810Sstevel@tonic-gate 	struct mntentry *m;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	(void) rw_wrlock(&rmtab_lock);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	iterator = h_iterator(mntlist);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	while (m = (struct mntentry *)h_next(iterator)) {
1880Sstevel@tonic-gate 		if (strcasecmp(m->m_host, host))
1890Sstevel@tonic-gate 			continue;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		rmtab_delete(m->m_pos);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		(void) h_delete(mntlist, m);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		free(m->m_path);
1960Sstevel@tonic-gate 		free(m->m_host);
1970Sstevel@tonic-gate 		free(m);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if (RMTAB_TOOMANY_DELETED())
2010Sstevel@tonic-gate 		rmtab_rewrite();
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	(void) rw_unlock(&rmtab_lock);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (iterator != NULL)
2060Sstevel@tonic-gate 		free(iterator);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Equivalent to xdr_mountlist from librpcsvc but for HASHSET
2110Sstevel@tonic-gate  * rather that for a linked list. It is used only to encode data
2120Sstevel@tonic-gate  * from HASHSET before sending it over the wire.
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static bool_t
xdr_mntlistencode(XDR * xdrs,HASHSET * mntlist)2160Sstevel@tonic-gate xdr_mntlistencode(XDR *xdrs, HASHSET *mntlist)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	HASHSET_ITERATOR iterator = h_iterator(*mntlist);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	for (;;) {
2210Sstevel@tonic-gate 		struct mntentry *m = (struct mntentry *)h_next(iterator);
2220Sstevel@tonic-gate 		bool_t more_data = (m != NULL);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &more_data)) {
2250Sstevel@tonic-gate 			if (iterator != NULL)
2260Sstevel@tonic-gate 				free(iterator);
2270Sstevel@tonic-gate 			return (FALSE);
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		if (!more_data)
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		if ((!xdr_name(xdrs, &m->m_host)) ||
234*11211SThomas.Haynes@Sun.COM 		    (!xdr_dirpath(xdrs, &m->m_path))) {
2350Sstevel@tonic-gate 			if (iterator != NULL)
2360Sstevel@tonic-gate 				free(iterator);
2370Sstevel@tonic-gate 			return (FALSE);
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (iterator != NULL)
2430Sstevel@tonic-gate 		free(iterator);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (TRUE);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate void
mntlist_send(SVCXPRT * transp)2490Sstevel@tonic-gate mntlist_send(SVCXPRT *transp)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	(void) rw_rdlock(&rmtab_lock);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	errno = 0;
2540Sstevel@tonic-gate 	if (!svc_sendreply(transp, xdr_mntlistencode, (char *)&mntlist))
2550Sstevel@tonic-gate 		log_cant_reply(transp);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	(void) rw_unlock(&rmtab_lock);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
261*11211SThomas.Haynes@Sun.COM  * Compute a 32 bit hash value for an mntlist entry.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
265*11211SThomas.Haynes@Sun.COM  * The string hashing algorithm is from the "Dragon Book" --
266*11211SThomas.Haynes@Sun.COM  * "Compilers: Principles, Tools & Techniques", by Aho, Sethi, Ullman
267*11211SThomas.Haynes@Sun.COM  *
268*11211SThomas.Haynes@Sun.COM  * And is modified for this application from usr/src/uts/common/os/modhash.c
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate 
271*11211SThomas.Haynes@Sun.COM static uint_t
mntentry_str_hash(char * s,uint_t hash)272*11211SThomas.Haynes@Sun.COM mntentry_str_hash(char *s, uint_t hash)
273*11211SThomas.Haynes@Sun.COM {
274*11211SThomas.Haynes@Sun.COM 	uint_t	g;
275*11211SThomas.Haynes@Sun.COM 
276*11211SThomas.Haynes@Sun.COM 	for (; *s != '\0'; s++) {
277*11211SThomas.Haynes@Sun.COM 		hash = (hash << 4) + *s;
278*11211SThomas.Haynes@Sun.COM 		if ((g = (hash & 0xf0000000)) != 0) {
279*11211SThomas.Haynes@Sun.COM 			hash ^= (g >> 24);
280*11211SThomas.Haynes@Sun.COM 			hash ^= g;
281*11211SThomas.Haynes@Sun.COM 		}
282*11211SThomas.Haynes@Sun.COM 	}
283*11211SThomas.Haynes@Sun.COM 
284*11211SThomas.Haynes@Sun.COM 	return (hash);
285*11211SThomas.Haynes@Sun.COM }
286*11211SThomas.Haynes@Sun.COM 
2870Sstevel@tonic-gate static uint32_t
mntentry_hash(const void * p)2880Sstevel@tonic-gate mntentry_hash(const void *p)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	struct mntentry *m = (struct mntentry *)p;
291*11211SThomas.Haynes@Sun.COM 	uint_t hash;
2920Sstevel@tonic-gate 
293*11211SThomas.Haynes@Sun.COM 	hash = mntentry_str_hash(m->m_host, 0);
294*11211SThomas.Haynes@Sun.COM 	hash = mntentry_str_hash(m->m_path, hash);
2950Sstevel@tonic-gate 
296*11211SThomas.Haynes@Sun.COM 	return (hash);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate  * Compare mntlist entries.
3010Sstevel@tonic-gate  * The comparison ignores a value of m_pos.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static int
mntentry_equal(const void * p1,const void * p2)3050Sstevel@tonic-gate mntentry_equal(const void *p1, const void *p2)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	struct mntentry *m1 = (struct mntentry *)p1;
3080Sstevel@tonic-gate 	struct mntentry *m2 = (struct mntentry *)p2;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	return ((strcasecmp(m1->m_host, m2->m_host) ||
311*11211SThomas.Haynes@Sun.COM 	    strcmp(m1->m_path, m2->m_path)) ? 0 : 1);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Rewrite /etc/rmtab with a current content of mntlist.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static void
rmtab_rewrite()3180Sstevel@tonic-gate rmtab_rewrite()
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	if (rmtabf)
3210Sstevel@tonic-gate 		(void) fclose(rmtabf);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/* Rewrite the file. */
3240Sstevel@tonic-gate 	if (rmtabf = fopen(RMTAB, "w+")) {
3250Sstevel@tonic-gate 		HASHSET_ITERATOR iterator;
3260Sstevel@tonic-gate 		struct mntentry *m;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		(void) fchmod(fileno(rmtabf),
3290Sstevel@tonic-gate 		    (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH));
3300Sstevel@tonic-gate 		rmtab_inuse = rmtab_deleted = 0;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		iterator = h_iterator(mntlist);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		while (m = (struct mntentry *)h_next(iterator))
3350Sstevel@tonic-gate 			m->m_pos = rmtab_insert(m->m_host, m->m_path);
3360Sstevel@tonic-gate 		if (iterator != NULL)
3370Sstevel@tonic-gate 			free(iterator);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * Parse the content of /etc/rmtab and insert the entries into mntlist.
3430Sstevel@tonic-gate  * The buffer s should be ended with a NUL char.
3440Sstevel@tonic-gate  */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate static void
rmtab_parse(char * s)3470Sstevel@tonic-gate rmtab_parse(char *s)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	char  *host;
3500Sstevel@tonic-gate 	char  *path;
3510Sstevel@tonic-gate 	char  *tmp;
3520Sstevel@tonic-gate 	struct in6_addr ipv6addr;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate host_part:
3550Sstevel@tonic-gate 	if (*s == '#')
3560Sstevel@tonic-gate 		goto skip_rest;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	host = s;
3590Sstevel@tonic-gate 	for (;;) {
3600Sstevel@tonic-gate 		switch (*s++) {
3610Sstevel@tonic-gate 		case '\0':
3620Sstevel@tonic-gate 			return;
3630Sstevel@tonic-gate 		case '\n':
3640Sstevel@tonic-gate 			goto host_part;
3650Sstevel@tonic-gate 		case ':':
3660Sstevel@tonic-gate 			s[-1] = '\0';
3670Sstevel@tonic-gate 			goto path_part;
3680Sstevel@tonic-gate 		case '[':
3690Sstevel@tonic-gate 			tmp = strchr(s, ']');
3700Sstevel@tonic-gate 			if (tmp) {
3710Sstevel@tonic-gate 				*tmp = '\0';
3720Sstevel@tonic-gate 				if (inet_pton(AF_INET6, s, &ipv6addr) > 0) {
3730Sstevel@tonic-gate 					host = s;
3740Sstevel@tonic-gate 					s = ++tmp;
3750Sstevel@tonic-gate 				} else
3760Sstevel@tonic-gate 					*tmp = ']';
3770Sstevel@tonic-gate 			}
3780Sstevel@tonic-gate 		default:
3790Sstevel@tonic-gate 			continue;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate path_part:
3840Sstevel@tonic-gate 	path = s;
3850Sstevel@tonic-gate 	for (;;) {
3860Sstevel@tonic-gate 		switch (*s++) {
3870Sstevel@tonic-gate 		case '\n':
3880Sstevel@tonic-gate 			s[-1] = '\0';
3890Sstevel@tonic-gate 			if (*host && *path)
3900Sstevel@tonic-gate 				mntlist_insert(host, path);
3910Sstevel@tonic-gate 			goto host_part;
3920Sstevel@tonic-gate 		case '\0':
3930Sstevel@tonic-gate 			if (*host && *path)
3940Sstevel@tonic-gate 				mntlist_insert(host, path);
3950Sstevel@tonic-gate 			return;
3960Sstevel@tonic-gate 		default:
3970Sstevel@tonic-gate 			continue;
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate skip_rest:
4020Sstevel@tonic-gate 	for (;;) {
4030Sstevel@tonic-gate 		switch (*++s) {
4040Sstevel@tonic-gate 		case '\n':
4050Sstevel@tonic-gate 			goto host_part;
4060Sstevel@tonic-gate 		case '\0':
4070Sstevel@tonic-gate 			return;
4080Sstevel@tonic-gate 		default:
4090Sstevel@tonic-gate 			continue;
4100Sstevel@tonic-gate 		}
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * Read in contents of rmtab.
4160Sstevel@tonic-gate  * Call rmtab_parse to parse the file and store entries in mntlist.
4170Sstevel@tonic-gate  * Rewrites the file to get rid of unused entries.
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate #define	RMTAB_LOADLEN	(16*2024)	/* Max bytes to read at a time */
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate void
rmtab_load()4230Sstevel@tonic-gate rmtab_load()
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	FILE *fp;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	(void) rwlock_init(&rmtab_lock, USYNC_THREAD, NULL);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Don't need to lock the list at this point
4310Sstevel@tonic-gate 	 * because there's only a single thread running.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	mntlist = h_create(mntentry_hash, mntentry_equal, 101, 0.75);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (fp = fopen(RMTAB, "r")) {
4360Sstevel@tonic-gate 		char buf[RMTAB_LOADLEN+1];
4370Sstevel@tonic-gate 		size_t len;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		/*
4400Sstevel@tonic-gate 		 * Read at most RMTAB_LOADLEN bytes from /etc/rmtab.
4410Sstevel@tonic-gate 		 * - if fread returns RMTAB_LOADLEN we can be in the middle
4420Sstevel@tonic-gate 		 *   of a line so change the last newline character into NUL
4430Sstevel@tonic-gate 		 *   and seek back to the next character after newline.
4440Sstevel@tonic-gate 		 * - otherwise set NUL behind the last character read.
4450Sstevel@tonic-gate 		 */
4460Sstevel@tonic-gate 		while ((len = fread(buf, 1, RMTAB_LOADLEN, fp)) > 0) {
4470Sstevel@tonic-gate 			if (len == RMTAB_LOADLEN) {
4480Sstevel@tonic-gate 				int i;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 				for (i = 1; i < len; i++) {
4510Sstevel@tonic-gate 					if (buf[len-i] == '\n') {
4520Sstevel@tonic-gate 						buf[len-i] = '\0';
453*11211SThomas.Haynes@Sun.COM 						(void) fseek(fp, -i + 1,
454*11211SThomas.Haynes@Sun.COM 						    SEEK_CUR);
4550Sstevel@tonic-gate 						goto parse;
4560Sstevel@tonic-gate 					}
4570Sstevel@tonic-gate 				}
4580Sstevel@tonic-gate 			}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 			/* Put a NUL character at the end of buffer */
4610Sstevel@tonic-gate 			buf[len] = '\0';
4620Sstevel@tonic-gate 	parse:
4630Sstevel@tonic-gate 			rmtab_parse(buf);
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 		(void) fclose(fp);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 	rmtab_rewrite();
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate  * Write an entry at the current location in rmtab
4720Sstevel@tonic-gate  * for the given client and path.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * Returns the starting position of the entry
4750Sstevel@tonic-gate  * or -1 if there was an error.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate long
rmtab_insert(char * host,char * path)4790Sstevel@tonic-gate rmtab_insert(char *host, char *path)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	long   pos;
4820Sstevel@tonic-gate 	struct in6_addr ipv6addr;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (rmtabf == NULL || fseek(rmtabf, 0L, 2) == -1) {
4850Sstevel@tonic-gate 		return (-1);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 	pos = ftell(rmtabf);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Check if host is an IPv6 literal
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (inet_pton(AF_INET6, host, &ipv6addr) > 0) {
4940Sstevel@tonic-gate 		if (fprintf(rmtabf, "[%s]:%s\n", host, path) == EOF) {
4950Sstevel@tonic-gate 			return (-1);
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 	} else {
4980Sstevel@tonic-gate 		if (fprintf(rmtabf, "%s:%s\n", host, path) == EOF) {
4990Sstevel@tonic-gate 			return (-1);
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	if (fflush(rmtabf) == EOF) {
5030Sstevel@tonic-gate 		return (-1);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 	rmtab_inuse++;
5060Sstevel@tonic-gate 	return (pos);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate  * Mark as unused the rmtab entry at the given offset in the file.
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate void
rmtab_delete(long pos)5140Sstevel@tonic-gate rmtab_delete(long pos)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	if (rmtabf != NULL && pos != -1 && fseek(rmtabf, pos, 0) == 0) {
5170Sstevel@tonic-gate 		(void) fprintf(rmtabf, "#");
5180Sstevel@tonic-gate 		(void) fflush(rmtabf);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		rmtab_inuse--;
5210Sstevel@tonic-gate 		rmtab_deleted++;
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate }
524