summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ypserv/yp_server.c
diff options
context:
space:
mode:
authorwpaul <wpaul@FreeBSD.org>1996-12-22 22:30:58 +0000
committerwpaul <wpaul@FreeBSD.org>1996-12-22 22:30:58 +0000
commitc1aa3ecaa1541240fd02352141c42ab719ee6e88 (patch)
tree8fe710785aad94fb6f6a3c1a55e03c034c4b1748 /usr.sbin/ypserv/yp_server.c
parentd389236bb21608788d7843fe717422b5eeb5af45 (diff)
downloadFreeBSD-src-c1aa3ecaa1541240fd02352141c42ab719ee6e88.zip
FreeBSD-src-c1aa3ecaa1541240fd02352141c42ab719ee6e88.tar.gz
Big round o changes:
- yp_dblookup.c: Create non-DB specific database access functions. Using these allows access to the underlying database functions without needing explicit knowledge of Berkeley DB. (These are used only when DB_CACHE is #defined. Other programs that use the non-caching functions (yp_mkdb, ypxfr, yppush, rpc.yppasswdd) shouldn't notice the difference.) - yp_dnslookup: Implement async DNS lookups. We send our own DNS requests using UDP and put the request in a queue. When the response arrives, we use the ID in the header to find the corresponsing queue entry and then send the response to the client. We can go about our business and handle other YP requests in the meantime. This way, we can deal with time consuming DNS requests without blocking and without forking. - yp_server.c: Convert to using new non-DB-specific database access functions. This simplifies the code a bit and removes the need for this module to know anything about Berkeley DB. Also convert the ypproc_match_2_svc() function to use the async DNS lookup routines. - yp_main.c: tweak yp_svc_run() to add the resolver socket to the set of descriptors monitored in the select() loop. Also add a timeout to select(); we may get stale DNS requests stuck in the queue which we want to invalidate after a while. If the timeout hits, we decrement the ttl on all pending DNS requests and nuke those requests that aren't handled before ttl hits zero. - yp_extern.h: Add prototypes for new stuff. - yp_svc_udp.c (new file): The async resolver code needs to be able to rummage around inside the RPC UDP transport handle in order to work correcty. There's basically one transport handle, and each time a request comes in, the transaction ID in the handle is changed. This means that if we queue a DNS request, then we handle some other unrelated requests, we will be unable to send the DNS response because the transaction ID and remote address of the client that made the DNS request will have been lost. What we need to do is save the client address and transaction ID in the queue entry for the DNS request, then put the transaction ID and address back in the transport handle when we're ready to reply. (And then we have to undo the change so as not to confuse any other part of the server.) The trouble is that the transaction ID is hidden in an opaque part of the transport handle, and only the code in the svc_udp module in the RPC library knows how to handle it. This file contains a couple of functions that let us read and set the transaction ID in spite of this. This is really a dirty trick and I should be taken out and shot for even thinking about it, but there's no other way to get this stuff to work. - Makefile: add yp_svc_udp.c to SRCS.
Diffstat (limited to 'usr.sbin/ypserv/yp_server.c')
-rw-r--r--usr.sbin/ypserv/yp_server.c162
1 files changed, 50 insertions, 112 deletions
diff --git a/usr.sbin/ypserv/yp_server.c b/usr.sbin/ypserv/yp_server.c
index 77cc6f7..32a8a2c 100644
--- a/usr.sbin/ypserv/yp_server.c
+++ b/usr.sbin/ypserv/yp_server.c
@@ -31,8 +31,8 @@
*
*/
-#include "yp_extern.h"
#include "yp.h"
+#include "yp_extern.h"
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
@@ -45,12 +45,11 @@
#include <rpc/rpc.h>
#ifndef lint
-static const char rcsid[] = "$Id: yp_server.c,v 1.12 1996/10/24 18:58:26 wpaul Exp $";
+static const char rcsid[] = "$Id: yp_server.c,v 1.2 1996/12/22 06:57:55 wpaul Exp $";
#endif /* not lint */
int forked = 0;
int children = 0;
-static DB *spec_dbp = NULL; /* Special global DB handle for ypproc_all. */
static char *master_string = "YP_MASTER_NAME";
static char *order_string = "YP_LAST_MODIFIED";
static int master_sz = sizeof("YP_MASTER_NAME") - 1;
@@ -124,7 +123,6 @@ ypresp_val *
ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
{
static ypresp_val result;
- DBT key, data;
result.val.valdat_val = "";
result.val.valdat_len = 0;
@@ -143,15 +141,13 @@ ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
return (&result);
}
- key.size = argp->key.keydat_len;
- key.data = argp->key.keydat_val;
-
- if ((result.stat = yp_get_record(argp->domain, argp->map,
- &key, &data, 1)) == YP_TRUE) {
- result.val.valdat_len = data.size;
- result.val.valdat_val = data.data;
+ if (yp_select_map(argp->map, argp->domain, &argp->key, 1) != YP_TRUE) {
+ result.stat = yp_errno;
+ return(&result);
}
+ result.stat = yp_getbykey(&argp->key, &result.val);
+
/*
* Do DNS lookups for hosts maps if database lookup failed.
*/
@@ -163,50 +159,24 @@ ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
#else
if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) {
#endif
- char *rval = NULL;
-
- /* DNS lookups can take time -- do them in a subprocess */
-
- if (!debug && children < MAX_CHILDREN && fork()) {
- children++;
- forked = 0;
- /*
- * Returning NULL here prevents svc_sendreply()
- * from being called by the parent. This is vital
- * since having both the parent and the child process
- * call it would confuse the client.
- */
- return (NULL);
- } else {
- forked++;
- }
+
+ /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
+ argp->key.keydat_val[argp->key.keydat_len] = '\0';
if (debug)
yp_error("Doing DNS lookup of %.*s",
argp->key.keydat_len,
argp->key.keydat_val);
- /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */
- argp->key.keydat_val[argp->key.keydat_len] = '\0';
-
if (!strcmp(argp->map, "hosts.byname"))
- rval = yp_dnsname((char *)argp->key.keydat_val);
+ result.stat = yp_async_lookup_name(rqstp->rq_xprt,
+ (char *)argp->key.keydat_val);
else if (!strcmp(argp->map, "hosts.byaddr"))
- rval = yp_dnsaddr((const char *)argp->key.keydat_val);
-
+ result.stat = yp_async_lookup_addr(rqstp->rq_xprt,
+ (char *)argp->key.keydat_val);
- if (rval) {
- if (debug)
- yp_error("DNS lookup successful. Result: %s",
- rval);
- result.val.valdat_len = strlen(rval);
- result.val.valdat_val = rval;
- result.stat = YP_TRUE;
- } else {
- if (debug)
- yp_error("DNS lookup failed.");
- result.stat = YP_NOKEY;
- }
+ if (result.stat == YP_TRUE)
+ return(NULL);
}
return (&result);
@@ -216,8 +186,6 @@ ypresp_key_val *
ypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_key_val result;
- DBT key, data;
- DB *dbp;
result.val.valdat_val = result.key.keydat_val = "";
result.val.valdat_len = result.key.keydat_len = 0;
@@ -236,27 +204,13 @@ ypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
return (&result);
}
-#ifdef DB_CACHE
- if ((dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) {
-#else
- if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
-#endif
+ if (yp_select_map(argp->map, argp->domain, &result.key, 0) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
- key.data = NULL;
- key.size = 0;
+ result.stat = yp_firstbykey(&result.key, &result.val);
- if ((result.stat = yp_first_record(dbp, &key, &data, 0)) == YP_TRUE) {
- result.key.keydat_len = key.size;
- result.key.keydat_val = key.data;
- result.val.valdat_len = data.size;
- result.val.valdat_val = data.data;
- }
-#ifndef DB_CACHE
- (void)(dbp->close)(dbp);
-#endif
return (&result);
}
@@ -264,8 +218,6 @@ ypresp_key_val *
ypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
{
static ypresp_key_val result;
- DBT key, data;
- DB *dbp;
result.val.valdat_val = result.key.keydat_val = "";
result.val.valdat_len = result.key.keydat_len = 0;
@@ -284,29 +236,16 @@ ypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
return (&result);
}
-#ifdef DB_CACHE
- if ((dbp = yp_open_db_cache(argp->domain, argp->map,
- argp->key.keydat_val,
- argp->key.keydat_len)) == NULL) {
-#else
- if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
-#endif
+ if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
- key.size = argp->key.keydat_len;
- key.data = argp->key.keydat_val;
+ result.key.keydat_len = argp->key.keydat_len;
+ result.key.keydat_val = argp->key.keydat_val;
+
+ result.stat = yp_nextbykey(&result.key, &result.val);
- if ((result.stat = yp_next_record(dbp, &key, &data,0,0)) == YP_TRUE) {
- result.key.keydat_len = key.size;
- result.key.keydat_val = key.data;
- result.val.valdat_len = data.size;
- result.val.valdat_val = data.data;
- }
-#ifndef DB_CACHE
- (void)(dbp->close)(dbp);
-#endif
return (&result);
}
@@ -480,16 +419,11 @@ ypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
static bool_t
xdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
{
- DBT key = { NULL, 0 } , data = { NULL, 0 };
-
while (1) {
/* Get a record. */
if ((objp->ypresp_all_u.val.stat =
- yp_next_record(spec_dbp,&key,&data,1,0)) == YP_TRUE) {
- objp->ypresp_all_u.val.val.valdat_len = data.size;
- objp->ypresp_all_u.val.val.valdat_val = data.data;
- objp->ypresp_all_u.val.key.keydat_len = key.size;
- objp->ypresp_all_u.val.key.keydat_val = key.data;
+ yp_nextbykey(&objp->ypresp_all_u.val.key,
+ &objp->ypresp_all_u.val.val)) == YP_TRUE) {
objp->more = TRUE;
} else {
objp->more = FALSE;
@@ -545,24 +479,15 @@ ypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
forked++;
}
-#ifndef DB_CACHE
- if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) {
- result.ypresp_all_u.val.stat = yp_errno;
- return(&result);
- }
-#else
- if ((spec_dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) {
+ if (yp_select_map(argp->map, argp->domain,
+ &result.ypresp_all_u.val.key, 0) != YP_TRUE) {
result.ypresp_all_u.val.stat = yp_errno;
return(&result);
}
-#endif
/* Kick off the actual data transfer. */
svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result);
-#ifndef DB_CACHE
- (void)(spec_dbp->close)(spec_dbp);
-#endif
/*
* Returning NULL prevents the dispatcher from calling
* svc_sendreply() since we already did it.
@@ -575,7 +500,8 @@ ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_master result;
static char ypvalbuf[YPMAXRECORD];
- DBT key = { master_string, master_sz }, data;
+ keydat key = { master_sz, master_string };
+ valdat val;
result.peer = "";
@@ -593,6 +519,11 @@ ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
return (&result);
}
+ if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
+ result.stat = yp_errno;
+ return(&result);
+ }
+
/*
* Note that we copy the data retrieved from the database to
* a private buffer and NUL terminate the buffer rather than
@@ -601,10 +532,11 @@ ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
* allocated by the DB package. This is a bad thing now that we
* cache DB handles rather than closing the database immediately.
*/
- if ((result.stat = yp_get_record(argp->domain, argp->map,
- &key, &data, 1)) == YP_TRUE) {
- bcopy((char *)data.data, (char *)&ypvalbuf, data.size);
- ypvalbuf[data.size] = '\0';
+ result.stat = yp_getbykey(&key, &val);
+ if (result.stat == YP_TRUE) {
+ bcopy((char *)val.valdat_val, (char *)&ypvalbuf,
+ val.valdat_len);
+ ypvalbuf[val.valdat_len] = '\0';
result.peer = (char *)&ypvalbuf;
} else
result.peer = "";
@@ -616,7 +548,8 @@ ypresp_order *
ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_order result;
- DBT key = { order_string, order_sz }, data;
+ keydat key = { order_sz, order_string };
+ valdat val;
result.ordernum = 0;
@@ -641,13 +574,18 @@ ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
* updated.
*/
- if ((result.stat = yp_get_record(argp->domain, argp->map,
- &key, &data, 1)) == YP_TRUE)
- result.ordernum = atoi((char *)data.data);
+ if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
+ result.stat = yp_errno;
+ return(&result);
+ }
+
+ result.stat = yp_getbykey(&key, &val);
+
+ if (result.stat == YP_TRUE)
+ result.ordernum = atoi((char *)val.valdat_val);
else
result.ordernum = 0;
-
return (&result);
}
OpenPOWER on IntegriCloud