Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Jul 2005 15:25:12 GMT
From:      soc-bushman <soc-bushman@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 79562 for review
Message-ID:  <200507041525.j64FPCrD088400@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=79562

Change 79562 by soc-bushman@soc-bushman_stinger on 2005/07/04 15:24:11

	getservent sandbox test completed

Affected files ...

.. //depot/projects/soc2005/nsswitch_cached/tests/getservent/getservent.c#4 edit

Differences ...

==== //depot/projects/soc2005/nsswitch_cached/tests/getservent/getservent.c#4 (text+ko) ====

@@ -188,8 +188,10 @@
 
 enum constants
 {
-	SETSERVENT,
-	ENDSERVENT,
+	SETSERVENT = 1,
+	ENDSERVENT = 2,
+	SERVENT_STORAGE_INITIAL	= 1 << 10, /* 1 KByte */
+	SERVENT_STORAGE_MAX		= 1 << 20, /* 1 MByte */	
 };
 
 struct servent_mdata 
@@ -201,7 +203,7 @@
 static const ns_src defaultsrc[] = {
 	//{ NSSRC_COMPAT, NS_SUCCESS },
 	//{ NSSRC_FILES, NS_SUCCESS },
-	{ NSSRC_FILES, NS_SUCCESS },
+	{ NSSRC_COMPAT, NS_SUCCESS },
 	{ NULL, 0 }
 };
 
@@ -244,6 +246,22 @@
 static int compat_servent(void *, void *, va_list);
 static int compat_setservent(void *, void *, va_list);
 	
+struct servent_state {
+	struct servent serv;
+	char * buffer;
+	size_t bufsize;
+};
+static void servent_endstate(void *);
+NSS_TLS_HANDLING(servent);
+
+struct key {
+	char * proto;
+	union {
+		char * name;
+		int port;
+	};
+};
+
 /* nsswitch part - end */
 
 static int
@@ -335,16 +353,6 @@
 	TRACE_OUT(servent_init);
 }
 
-#define SERVENT_BUFFER_UNPACK(buffer, buffer_size, line, line_size, aliases) 	\
-	aliases=(char **)(_ALIGN((char **)buffer));										\
-	line=(char *)aliases+sizeof(char *)*_MAXALIASES;							\
-	if (line>buffer+buffer_size)												\
-		return (NS_UNAVAIL);													\
-	line_size=buffer+buffer_size-line;											\
-	if (line_size<_MAXLINELEN+1)												\
-		return (NS_TRYAGAIN);
-	/* TODO - must check the alignment here */
-
 #define SERVENT_BUFFER_SIZE sizeof(char*)*_MAXALIASES+_ALIGNBYTES+_MAXLINELEN
 
 /* files backend implementation - begin */
@@ -415,6 +423,7 @@
 	if (line_size<_MAXLINELEN+1)
 		return (NS_TRYAGAIN);*/
 
+	TRACE_IN(files_servent);
 	TRACE_POINT();
 	TRACE_INT(((struct servent_mdata *)mdata)->how);
 		
@@ -449,10 +458,15 @@
 	bufsize = va_arg(ap, size_t);
 	errnop = va_arg(ap,int *);
 	
-	TRACE_IN(files_servent);
+	TRACE_POINT();
+	
 	*errnop = files_getstate(&st);
-	if (*errnop != 0)
+	TRACE_POINT();
+	if (*errnop != 0) {
+		TRACE_INT(*errnop);
 		return (NS_UNAVAIL);	
+	}
+	TRACE_POINT();
 	
 /*	SERVENT_BUFFER_UNPACK(buffer, buffer_size,line,line_size,aliases)
 	TRACE_PTR(buffer);
@@ -492,6 +506,7 @@
 				break;
 			}
 
+			TRACE_POINT();
 			if (*line=='+')
 				st->compat_mode_active = 1;
 			else {			
@@ -509,11 +524,11 @@
 				}
 				
 				memcpy(buffer,line,linesize);
-				line[linesize]='\0';										
+				buffer[linesize]='\0';										
 			}			
 		}
 		
-		
+		TRACE_POINT();
 /*		TRACE_POINT();
 		if (!st->compat_mode_active && *p=='+') {
 			st->compat_mode_active=1;
@@ -556,8 +571,10 @@
 
 		rv = servent_unpack(line,serv,aliases,aliases_size,errnop);
 		if (rv !=0 ) {
-			if (*errnop == 0)
+			if (*errnop == 0) {
+				rv = NS_NOTFOUND;
 				continue;
+			}
 			else {
 				rv = NS_RETURN;
 				break;
@@ -860,19 +877,22 @@
 	bufsize = va_arg(ap, size_t);
 	errnop = va_arg(ap,int *);
 	
+	TRACE_POINT();
 	*errnop = nis_getstate(&st);
-	if (errnop != 0) {
+	if (*errnop != 0) {
 		TRACE_OUT(nis_servent);
 		return (NS_UNAVAIL);
 	}	
 	
-	if (!st->yp_domain[0]) {
+	TRACE_POINT();
+	if (st->yp_domain[0] == '\0') {
 		if (getdomainname(st->yp_domain,sizeof st->yp_domain)) {
 			*errnop=errno;
 			TRACE_OUT(nis_servent);
 			return (NS_UNAVAIL);
 		}
 	}
+	TRACE_POINT();
 
 	TRACE_POINT();
 	do {
@@ -971,7 +991,8 @@
 			else
 				rv = NS_RETURN;
 		}
-		free(resultbuf);								
+		free(resultbuf);
+		rv = NS_SUCCESS;
 
 		TRACE_INT(rv);
 		TRACE_INT(!(rv & NS_TERMINATE));
@@ -1415,6 +1436,115 @@
 	(void) nsdispatch(NULL, dtab, NSDB_SERVICES, "endservent", defaultsrc);
 }
 
+static 	void
+servent_endstate(void *p)
+{
+	TRACE_IN(servent_endstate);
+	
+	if (p == NULL)
+		return;
+
+	free(((struct servent_state *)p)->buffer);	
+	free(p);
+
+	TRACE_OUT(files_endstate);
+}
+
+
+static int
+wrap_getservbyname_r(struct key key, struct servent *serv, char *buffer, size_t bufsize,
+    struct servent **res)
+{
+	return (my_getservbyname_r(key.name, key.proto, serv, buffer, bufsize, res));
+}
+
+static int
+wrap_getservbyport_r(struct key key, struct servent *serv, char *buffer, size_t bufsize,
+    struct servent **res)
+{
+	return (my_getservbyport_r(key.port, key.proto, serv, buffer, bufsize, res));
+}
+
+static int
+wrap_getservent_r(struct key key, struct servent *serv, char *buffer, size_t bufsize,
+    struct servent **res)
+{
+	return (my_getservent_r(serv, buffer, bufsize, res));
+}
+
+static struct servent *
+getserv(int (*fn)(struct key, struct servent *, char *, size_t, struct servent **),
+    struct key key)
+{
+	int		 rv;
+	struct servent	*res;
+	struct servent_state * st;
+		
+	rv=servent_getstate(&st);
+	if (rv != 0) {
+		errno = rv;
+		return NULL;
+	}
+
+	if (st->buffer == NULL) {
+		st->buffer = malloc(SERVENT_STORAGE_INITIAL);
+		if (st->buffer == NULL)
+			return (NULL);
+		st->bufsize = SERVENT_STORAGE_INITIAL;
+	}
+	do {
+		rv = fn(key, &st->serv, st->buffer, st->bufsize, &res);
+		if (res == NULL && rv == ERANGE) {
+			free(st->buffer);
+			if ((st->bufsize << 1) > SERVENT_STORAGE_MAX) {
+				st->buffer = NULL;
+				errno = ERANGE;
+				return (NULL);
+			}
+			st->bufsize <<= 1;
+			st->buffer = malloc(st->bufsize);
+			if (st->buffer == NULL)
+				return (NULL);
+		}
+	} while (res == NULL && rv == ERANGE);
+	if (rv != 0)
+		errno = rv;
+	return (res);
+}
+
+struct servent *
+my_getservbyname(char * name, char * proto)
+{
+	struct key key;
+
+	key.name=name;
+	key.proto=proto;
+	
+	return (getserv(wrap_getservbyname_r,key));
+}
+
+struct servent *
+my_getservbyport(int port, char * proto)
+{
+	struct key key;
+		
+	key.port=port;
+	key.proto=proto;
+	
+	return (getserv(wrap_getservbyport_r,key));
+}
+
+struct servent *
+my_getservent()
+{
+	struct key key;
+	
+	key.proto=NULL;
+	key.port=0;
+	
+	return (getserv(wrap_getservent_r,key));
+}
+
 /*
 static struct servdata servdata;
 static thread_key_t servdata_key;
@@ -1897,13 +2027,66 @@
 		printf("\nFAILURE\n");
 }
 
+static void
+ent_test2()
+{
+	struct servent * result;
+		
+	int rv;
+	
+	printf("\ntesting getservent_r function - simple iteration\n");	
+	my_setservent(1);
+	do
+	{
+		//TRACE_OFF();
+		result=my_getservent();
+		result_info(errno,result);
+		//TRACE_ON();		
+	} while (result!=NULL );
+
+	if (errno == 0)
+		printf("\nSUCCESS\n");
+	else
+		printf("\nFAILURE\n");
+}
+
 int main()
 {
+	int i;
 	printf("getserv* functions sandbox test (02.07.05)\n");
 	
-//	byname_test1_r();
-//	byport_test1_r();
+	for (;i<10;++i) {
+		byname_test1_r();
+		byport_test1_r();
+		ent_test2();
+		ent_test2_r();
+		byname_test1_r();
+		ent_test1_r();
+		byport_test1_r();
+		ent_test2_r();
+		byname_test1_r();
+		ent_test2();
+		ent_test2();
+		ent_test1_r();
+		byname_test1_r();
+		byport_test1_r();
+		ent_test1_r();
+		ent_test2_r();
+		ent_test2();
+	}
+	/*
+	byname_test1_r();
+	byport_test1_r();
+	ent_test2_r();
+	byname_test1_r();
+	ent_test1_r();
+	byport_test1_r();
 	ent_test2_r();
-//	ent_test1_r();
+	byname_test1_r();
+	ent_test1_r();
+	byname_test1_r();
+	byport_test1_r();
+	ent_test1_r();
+	ent_test2_r();*/
 	return 0;
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507041525.j64FPCrD088400>