summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwpaul <wpaul@FreeBSD.org>1996-06-01 04:40:42 +0000
committerwpaul <wpaul@FreeBSD.org>1996-06-01 04:40:42 +0000
commitf96a6bec7fc3dcdc42d9d842779178e93798aac2 (patch)
tree88a15fae0931f2ee7da2910b94f61e8f0d043458
parenta90d30f14691d10790925c99a7bcc9921319d21e (diff)
downloadFreeBSD-src-f96a6bec7fc3dcdc42d9d842779178e93798aac2.zip
FreeBSD-src-f96a6bec7fc3dcdc42d9d842779178e93798aac2.tar.gz
Improve NIS performace of getservbyname() and getservbyport(). Both these
functions are implimented as wrappers around getservent(), which means it's up to getservent() to do all the work. The NIS support in getservent() only allows it to scan through the services.byname map one entry at a time until it finds the requested service name/port. This can be painfully slow due to the overhead involved (lots and lots of successive RPCs). To fix this, we allow getservbyname() and getservbyport() to signal getservent() that if NIS is turned on (there's a '+' in /etc/services), the usual yp_first()/yp_next() linear search should be abandoned and yp_match() used instead. This causes getservent() to immediately locate the requested entry instead of wasting time groping through the whole map. The downside is that this trick is accomplished by exporting a couple of pointers from getservent.c which getservbyname.c and getservbyport.c can preset in order to tell getservent() what to do. If all three functions were in the same source module, then the extra cruft could be delcared static to avoid poluting the global symbol space. Maybe they should be combined anyway. For now I've settled on prepending lots of underscores.
-rw-r--r--lib/libc/net/getservbyname.c14
-rw-r--r--lib/libc/net/getservbyport.c14
-rw-r--r--lib/libc/net/getservent.c102
3 files changed, 123 insertions, 7 deletions
diff --git a/lib/libc/net/getservbyname.c b/lib/libc/net/getservbyname.c
index 77bf813..fcff7cf 100644
--- a/lib/libc/net/getservbyname.c
+++ b/lib/libc/net/getservbyname.c
@@ -47,6 +47,14 @@ getservbyname(name, proto)
register struct servent *p;
register char **cp;
+#ifdef YP
+ extern char *___getservbyname_yp;
+ extern char *___getservbyproto_yp;
+
+ ___getservbyname_yp = (char *)name;
+ ___getservbyproto_yp = (char *)proto;
+#endif
+
setservent(_serv_stayopen);
while (p = getservent()) {
if (strcmp(name, p->s_name) == 0)
@@ -61,5 +69,11 @@ gotname:
}
if (!_serv_stayopen)
endservent();
+
+#ifdef YP
+ ___getservbyname_yp = NULL;
+ ___getservbyproto_yp = NULL;
+#endif
+
return (p);
}
diff --git a/lib/libc/net/getservbyport.c b/lib/libc/net/getservbyport.c
index 0acb31b..a24f8df 100644
--- a/lib/libc/net/getservbyport.c
+++ b/lib/libc/net/getservbyport.c
@@ -47,6 +47,14 @@ getservbyport(port, proto)
{
register struct servent *p;
+#ifdef YP
+ extern int ___getservbyport_yp;
+ extern char *___getservbyproto_yp;
+
+ ___getservbyport_yp = port;
+ ___getservbyproto_yp = (char *)proto;
+#endif
+
setservent(_serv_stayopen);
while (p = getservent()) {
if (p->s_port != port)
@@ -56,5 +64,11 @@ getservbyport(port, proto)
}
if (!_serv_stayopen)
endservent();
+
+#ifdef YP
+ ___getservbyport_yp = 0;
+ ___getservbyproto_yp = NULL;
+#endif
+
return (p);
}
diff --git a/lib/libc/net/getservent.c b/lib/libc/net/getservent.c
index 35e535a..6797b5a 100644
--- a/lib/libc/net/getservent.c
+++ b/lib/libc/net/getservent.c
@@ -57,13 +57,94 @@ static char *serv_aliases[MAXALIASES];
int _serv_stayopen;
#ifdef YP
+char *___getservbyname_yp = NULL;
+char *___getservbyproto_yp = NULL;
+int ___getservbyport_yp = 0;
+static char *yp_domain = NULL;
+
static int
-_getypservent(line)
+_getservbyport_yp(line)
+ char *line;
+{
+ char *result;
+ int resultlen;
+ char buf[YPMAXRECORD];
+ int rv;
+
+ snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
+ ___getservbyproto_yp);
+
+ ___getservbyport_yp = 0;
+ ___getservbyproto_yp = NULL;
+
+ if(!yp_domain) {
+ if(yp_get_default_domain(&yp_domain))
+ return (0);
+ }
+
+ /*
+ * We have to be a little flexible here. Ideally you're supposed
+ * to have both a services.byname and a services.byport map, but
+ * some systems have only services.byname. FreeBSD cheats a little
+ * by putting the services.byport information in the same map as
+ * services.byname so that either case will work. We allow for both
+ * possibilities here: if there is no services.byport map, we try
+ * services.byname instead.
+ */
+ if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
+ &result, &resultlen))) {
+ if (rv == YPERR_MAP) {
+ if (yp_match(yp_domain, "services.byname", buf,
+ strlen(buf), &result, &resultlen))
+ return(0);
+ } else
+ return(0);
+ }
+
+ /* getservent() expects lines terminated with \n -- make it happy */
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
+
+ free(result);
+ return(1);
+}
+
+static int
+_getservbyname_yp(line)
+ char *line;
+{
+ char *result;
+ int resultlen;
+ char buf[YPMAXRECORD];
+
+ if(!yp_domain) {
+ if(yp_get_default_domain(&yp_domain))
+ return (0);
+ }
+
+ snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
+ ___getservbyproto_yp);
+
+ ___getservbyname_yp = 0;
+ ___getservbyproto_yp = NULL;
+
+ if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
+ &result, &resultlen)) {
+ return(0);
+ }
+
+ /* getservent() expects lines terminated with \n -- make it happy */
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
+
+ free(result);
+ return(1);
+}
+
+static int
+_getservent_yp(line)
char *line;
{
static char *key = NULL;
static int keylen;
- static char *yp_domain = NULL;
char *lastkey, *result;
int resultlen;
int rv;
@@ -93,9 +174,8 @@ _getypservent(line)
}
}
- strncpy(line, result, BUFSIZ - 1);
/* getservent() expects lines terminated with \n -- make it happy */
- strcat(line, "\n");
+ snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
free(result);
@@ -131,7 +211,7 @@ getservent()
register char *cp, **q;
#ifdef YP
- if (serv_stepping_yp && _getypservent(line)) {
+ if (serv_stepping_yp && _getservent_yp(line)) {
p = (char *)&line;
goto unpack;
}
@@ -143,8 +223,16 @@ again:
if ((p = fgets(line, BUFSIZ, servf)) == NULL)
return (NULL);
#ifdef YP
- if (*p == '+') {
- if (!_getypservent(&line))
+ if (*p == '+' && _yp_check(NULL)) {
+ if (___getservbyname_yp != NULL) {
+ if (!_getservbyname_yp(&line))
+ goto tryagain;
+ }
+ else if (___getservbyport_yp != 0) {
+ if (!_getservbyport_yp(&line))
+ goto tryagain;
+ }
+ else if (!_getservent_yp(&line))
goto tryagain;
}
unpack:
OpenPOWER on IntegriCloud