From f497ab709dd9d452cb889da09cd47d344e2b497f Mon Sep 17 00:00:00 2001 From: Michael Graff Date: Thu, 20 Jan 2000 00:07:51 +0000 Subject: [PATCH] checkpoint --- bin/lwresd/client.c | 19 ++++++++ bin/lwresd/main.c | 112 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 5 deletions(-) diff --git a/bin/lwresd/client.c b/bin/lwresd/client.c index 5221b25f4d..20cd24c35a 100644 --- a/bin/lwresd/client.c +++ b/bin/lwresd/client.c @@ -57,10 +57,22 @@ client_recv(isc_task_t *task, isc_event_t *ev) isc_event_free(&ev); dev = NULL; + /* + * Go idle. + */ + ISC_LIST_UNLINK(cm->running, client, link); + ISC_LIST_APPEND(cm->idle, client, link); + clientmgr_can_die(cm); return; } + /* + * XXXMLG Nothing to do right now, just go idle. + */ + ISC_LIST_UNLINK(cm->running, client, link); + ISC_LIST_APPEND(cm->idle, client, link); + client_start_recv(cm); isc_event_free(&ev); @@ -101,6 +113,13 @@ client_start_recv(clientmgr_t *cm) */ cm->flags |= CLIENTMGR_FLAG_RECVPENDING; + /* + * Remove the client from the idle list, and put it on the running + * list. + */ + ISC_LIST_UNLINK(cm->idle, client, link); + ISC_LIST_APPEND(cm->running, client, link); + return (ISC_R_SUCCESS); } diff --git a/bin/lwresd/main.c b/bin/lwresd/main.c index 91f2a2747b..f7e4a55f64 100644 --- a/bin/lwresd/main.c +++ b/bin/lwresd/main.c @@ -17,14 +17,20 @@ #include -#include +#include +#include #include +#include #include #include +#include +#include #include #include +#include + #include "client.h" /* @@ -46,6 +52,10 @@ main(int argc, char **argv) { isc_mem_t *mem; isc_taskmgr_t *taskmgr; + isc_socketmgr_t *sockmgr; + isc_socket_t *sock; + isc_sockaddr_t localhost; + struct in_addr lh_addr; isc_result_t result; unsigned int i, j; client_t *client; @@ -53,22 +63,56 @@ main(int argc, char **argv) UNUSED(argc); UNUSED(argv); + isc_app_start(); + mem = NULL; result = isc_mem_create(0, 0, &mem); INSIST(result == ISC_R_SUCCESS); - cmgr = isc_mem_get(mem, sizeof(clientmgr_t) * NTHREADS); + cmgr = isc_mem_get(mem, sizeof(clientmgr_t) * NTASKS); INSIST(cmgr != NULL); + /* + * Create a task manager. + */ taskmgr = NULL; result = isc_taskmgr_create(mem, NTHREADS, 0, &taskmgr); INSIST(result == ISC_R_SUCCESS); + /* + * Create a socket manager. + */ + sockmgr = NULL; + result = isc_socketmgr_create(mem, &sockmgr); + INSIST(result == ISC_R_SUCCESS); + + /* + * We'll need a socket. It will be a UDP socket, and bound to + * 127.0.0.1 port LWRES_UDP_PORT. + */ + sock = NULL; + result = isc_socket_create(sockmgr, AF_INET, isc_sockettype_udp, + &sock); + INSIST(result == ISC_R_SUCCESS); + + lh_addr.s_addr = htonl(INADDR_LOOPBACK); + isc_sockaddr_fromin(&localhost, &lh_addr, LWRES_UDP_PORT); + + result = isc_socket_bind(sock, &localhost); + INSIST(result == ISC_R_SUCCESS); + /* * Create one task for each client manager. */ for (i = 0 ; i < NTASKS ; i++) { cmgr[i].task = NULL; + cmgr[i].sock = sock; + cmgr[i].flags = 0; + ISC_EVENT_INIT(&cmgr[i].sdev, sizeof(isc_event_t), + ISC_EVENTATTR_NOPURGE, + 0, LWRD_SHUTDOWN, + client_shutdown, &cmgr[i], main, + NULL, NULL); ISC_LIST_INIT(cmgr[i].idle); ISC_LIST_INIT(cmgr[i].running); result = isc_task_create(taskmgr, mem, 0, &cmgr[i].task); @@ -88,7 +132,7 @@ main(int argc, char **argv) if (client == NULL) break; for (j = 0 ; j < ntasks ; j++) { - client[j].socket = NULL; + client[j].clientmgr = &cmgr[j]; ISC_LINK_INIT(&client[j], link); ISC_LIST_APPEND(cmgr[j].idle, &client[j], link); } @@ -96,13 +140,71 @@ main(int argc, char **argv) INSIST(i > 0); /* - * Now, create a socket. Issue one read request for each task - * we have. + * Issue one read request for each task we have. */ + for (j = 0 ; j < ntasks ; j++) { + result = client_start_recv(&cmgr[j]); + INSIST(result == ISC_R_SUCCESS); + } /* * Wait for ^c or kill. */ + isc_mem_stats(mem, stdout); + isc_app_run(); + isc_mem_stats(mem, stdout); + + /* + * Send a shutdown event to every task. + */ + for (j = 0 ; j < ntasks ; j++) { + isc_event_t *ev; + + ev = &cmgr[j].sdev; + isc_task_send(cmgr[j].task, &ev); + printf("Sending shutdown events to task %p\n", cmgr[j].task); + } + + /* + * Wait for the tasks to all die. + */ + printf("Waiting for task manager to die...\n"); + isc_taskmgr_destroy(&taskmgr); + + /* + * Wait for everything to die off by waiting for the sockets + * to be detached. + */ + printf("Waiting for socket manager to die...\n"); + isc_socket_detach(&sock); + isc_socketmgr_destroy(&sockmgr); + + /* + * Free up memory allocated. This is somewhat magical. We allocated + * the client_t's in blocks, but the first task always has the + * first pointer. Just loop here, freeing them. + */ + client = ISC_LIST_HEAD(cmgr[0].idle); + while (client != NULL) { + ISC_LIST_UNLINK(cmgr[0].idle, client, link); + isc_mem_put(mem, client, sizeof(client_t) * ntasks); + client = ISC_LIST_HEAD(cmgr[0].idle); + } + INSIST(ISC_LIST_EMPTY(cmgr[0].running)); + + /* + * Now, kill off the client manager structures. + */ + isc_mem_put(mem, cmgr, sizeof(clientmgr_t) * NTASKS); + cmgr = NULL; + + /* + * Kill the memory system. + */ + isc_mem_stats(mem, stdout); + isc_mem_destroy(&mem); + + isc_app_finish(); return (0); }