Add resource management as part of the kore shutdown process.

This commit is contained in:
Stig Telfer 2015-12-29 19:39:39 +00:00
parent 8368f6d471
commit 0c51d9da53
10 changed files with 115 additions and 7 deletions

View File

@ -215,6 +215,7 @@ extern u_int16_t http_keepalive_time;
extern u_int32_t http_request_limit;
void http_init(void);
void http_fini(void);
void http_process(void);
const char *http_status_text(int);
time_t http_date_to_time(char *);

View File

@ -420,6 +420,7 @@ struct kore_worker *kore_worker_data(u_int8_t);
void kore_platform_init(void);
void kore_platform_event_init(void);
void kore_platform_event_fini(void);
void kore_platform_proctitle(char *);
void kore_platform_disable_read(int);
void kore_platform_enable_accept(void);
@ -453,6 +454,7 @@ int kore_server_bind(const char *, const char *, const char *);
void kore_tls_info_callback(const SSL *, int, int);
void kore_connection_init(void);
void kore_connection_fini(void);
void kore_connection_prune(int);
struct connection *kore_connection_new(void *);
void kore_connection_check_timeout(void);
@ -481,6 +483,7 @@ void *kore_pool_get(struct kore_pool *);
void kore_pool_put(struct kore_pool *, void *);
void kore_pool_init(struct kore_pool *, const char *,
u_int32_t, u_int32_t);
void kore_pool_fini(struct kore_pool *);
time_t kore_date_to_time(char *);
char *kore_time_to_date(time_t);
@ -554,6 +557,7 @@ void net_write32(u_int8_t *, u_int32_t);
void net_write64(u_int8_t *, u_int64_t);
void net_init(void);
void net_fini(void);
int net_send(struct connection *);
int net_send_flush(struct connection *);
int net_recv_flush(struct connection *);

View File

@ -38,6 +38,16 @@ kore_connection_init(void)
sizeof(struct connection), worker_max_connections);
}
void
kore_connection_fini(void)
{
kore_debug("connection_fini()");
/* Drop all connections */
kore_connection_prune(KORE_CONNECTION_PRUNE_ALL);
kore_pool_fini(&connection_pool);
}
struct connection *
kore_connection_new(void *owner)
{

View File

@ -86,6 +86,20 @@ http_init(void)
"http_path_pool", HTTP_URI_LEN, prealloc);
}
void
http_fini(void)
{
if (header_buf != NULL) {
kore_buf_free(header_buf);
header_buf = NULL;
}
kore_pool_fini(&http_request_pool);
kore_pool_fini(&http_header_pool);
kore_pool_fini(&http_host_pool);
kore_pool_fini(&http_path_pool);
}
int
http_request_new(struct connection *c, const char *host,
const char *method, const char *path, const char *version,

View File

@ -402,6 +402,10 @@ kore_server_start(void)
kore_platform_event_wait(100);
kore_connection_prune(KORE_CONNECTION_PRUNE_DISCONNECT);
}
kore_platform_event_fini();
kore_connection_fini();
net_fini();
}
static void

View File

@ -71,6 +71,20 @@ kore_platform_event_init(void)
events = kore_calloc(event_count, sizeof(struct epoll_event));
}
void
kore_platform_event_fini(void)
{
if (efd >= 0) {
close(efd);
efd = -1;
}
if (events != NULL) {
kore_mem_free(events);
events = NULL;
}
}
int
kore_platform_event_wait(u_int64_t timer)
{

View File

@ -50,7 +50,7 @@ kore_malloc(size_t len)
mlen = sizeof(u_int32_t) + len + sizeof(struct meminfo);
if ((ptr = calloc(1, mlen)) == NULL)
fatal("kore_malloc(%d): %d", len, errno);
fatal("kore_malloc(%zd): %d", len, errno);
plen = (u_int32_t *)ptr;
*plen = len;
@ -89,12 +89,19 @@ kore_realloc(void *ptr, size_t len)
void *
kore_calloc(size_t memb, size_t len)
{
if (memb == 0 || len == 0)
fatal("kore_calloc(): zero size");
if (SIZE_MAX / memb < len)
fatal("kore_calloc: memb * len > SIZE_MAX");
void *result;
const size_t nbytes = memb * len;
return (kore_malloc(memb * len));
if (nbytes == 0)
fatal("kore_calloc(): zero size");
if (nbytes > SIZE_MAX)
fatal("kore_calloc: memb * len > SIZE_MAX");
result = kore_malloc(nbytes);
if (result == NULL)
fatal("kore_calloc: alloc failed for %zd bytes", nbytes);
return (result);
}
void

View File

@ -36,6 +36,13 @@ net_init(void)
kore_pool_init(&nb_pool, "nb_pool", sizeof(struct netbuf), 1000);
}
void
net_fini(void)
{
kore_debug("net_fini()");
kore_pool_fini(&nb_pool);
}
void
net_send_queue(struct connection *c, const void *data, u_int32_t len)
{

View File

@ -22,6 +22,7 @@
#define POOL_ELEMENT_FREE 1
static void pool_region_create(struct kore_pool *, u_int32_t);
static void pool_region_destroy(struct kore_pool *);
void
kore_pool_init(struct kore_pool *pool, const char *name,
@ -41,6 +42,27 @@ kore_pool_init(struct kore_pool *pool, const char *name,
pool_region_create(pool, elm);
}
void
kore_pool_fini(struct kore_pool *pool)
{
if (pool->inuse)
kore_debug("Pool %s: destroyed with %u allocs in use",
pool->name ? pool->name : "<unknown>",
pool->inuse );
pool->elms = 0;
pool->inuse = 0;
pool->elen = 0;
pool->slen = 0;
if (pool->name != NULL) {
kore_mem_free(pool->name);
pool->name = NULL;
}
pool_region_destroy(pool);
}
void *
kore_pool_get(struct kore_pool *pool)
{
@ -111,3 +133,23 @@ pool_region_create(struct kore_pool *pool, u_int32_t elms)
pool->elms += elms;
}
static void
pool_region_destroy(struct kore_pool *pool)
{
struct kore_pool_region *reg;
kore_debug("pool_region_destroy(%p)", pool);
/* Take care iterating when modifying list contents */
while (!LIST_EMPTY(&pool->regions)) {
reg = LIST_FIRST(&pool->regions);
LIST_REMOVE(reg, list);
kore_mem_free(reg->start);
kore_mem_free(reg);
}
/* Freelist references into the regions memory allocations */
LIST_INIT(&pool->freelist);
pool->elms = 0;
}

View File

@ -355,7 +355,12 @@ kore_worker_entry(struct kore_worker *kw)
#endif
}
kore_connection_prune(KORE_CONNECTION_PRUNE_ALL);
kore_platform_event_fini();
kore_connection_fini();
#if !defined(KORE_NO_HTTP)
http_fini();
#endif
net_fini();
kore_debug("worker %d shutting down", kw->id);
exit(0);
}