From 7d0d04af0ae9a019adf0ee16248b5e9c8ea21922 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 8 Sep 2009 13:51:02 +0200 Subject: [PATCH] Refactor to avoid more static variables Move listener_socket to conf_server. It was static before and that would have given problems with multiple servers (multiple listeners). The WRBUF confdir is now part of conf_config and no longer static. Move conf_config to pazpar2_config.c because it is no longer needed by other modules. --- src/http.c | 12 ++-- src/http.h | 2 +- src/http_command.c | 2 +- src/logic.c | 53 +----------------- src/pazpar2.c | 6 +- src/pazpar2.h | 5 -- src/pazpar2_config.c | 148 ++++++++++++++++++++++++++++++++++++++------------ src/pazpar2_config.h | 23 ++++---- src/test_config.c | 2 +- src/test_record.c | 2 +- 10 files changed, 141 insertions(+), 114 deletions(-) diff --git a/src/http.c b/src/http.c index d40bd08..ee427fb 100644 --- a/src/http.c +++ b/src/http.c @@ -1129,8 +1129,6 @@ static void http_accept(IOCHAN i, int event) pazpar2_add_channel(c); } -static int listener_socket = 0; - /* Create a http-channel listener, syntax [host:]port */ int http_init(const char *addr, struct conf_server *server) { @@ -1193,7 +1191,7 @@ int http_init(const char *addr, struct conf_server *server) return 1; } - listener_socket = l; + server->listener_socket = l; c = iochan_create(l, http_accept, EVENT_INPUT | EVENT_EXCEPT); iochan_setdata(c, server); @@ -1201,15 +1199,15 @@ int http_init(const char *addr, struct conf_server *server) return 0; } -void http_close_server(void) +void http_close_server(struct conf_server *server) { /* break the event_loop (select) by closing down the HTTP listener sock */ - if (listener_socket) + if (server->listener_socket) { #ifdef WIN32 - closesocket(listener_socket); + closesocket(server->listener_socket); #else - close(listener_socket); + close(server->listener_socket); #endif } } diff --git a/src/http.h b/src/http.h index 90f40c8..dcaa3a8 100644 --- a/src/http.h +++ b/src/http.h @@ -102,7 +102,7 @@ struct http_response void http_set_proxyaddr(const char *url, struct conf_server *ser); int http_init(const char *addr, struct conf_server *ser); -void http_close_server(void); +void http_close_server(struct conf_server *ser); void http_addheader(struct http_response *r, const char *name, const char *value); struct http_header * http_header_append(struct http_channel *ch, diff --git a/src/http_command.c b/src/http_command.c index da9771e..73a8b1f 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -237,7 +237,7 @@ static int process_settings(struct session *se, struct http_request *rq, static void cmd_exit(struct http_channel *c) { yaz_log(YLOG_WARN, "exit"); - http_close_server(); + http_close_server(c->server); } static void cmd_init(struct http_channel *c) diff --git a/src/logic.c b/src/logic.c index 3ea153f..ee30f50 100644 --- a/src/logic.c +++ b/src/logic.c @@ -346,7 +346,8 @@ static int prepare_map(struct session *se, struct session_database *sdb) { (*m) = nmem_malloc(se->session_nmem, sizeof(**m)); (*m)->next = 0; - if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i]))) + if (!((*m)->stylesheet = conf_load_stylesheet(se->service->config, + stylesheets[i]))) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s", stylesheets[i]); @@ -867,56 +868,6 @@ void statistics(struct session *se, struct statistics *stat) stat->num_clients = count; } -int start_http_listener(struct conf_config *conf, - const char *listener_override, - const char *proxy_override) -{ - struct conf_server *ser; - for (ser = conf->servers; ser; ser = ser->next) - { - WRBUF w = wrbuf_alloc(); - int r; - if (listener_override) - { - wrbuf_puts(w, listener_override); - listener_override = 0; /* only first server is overriden */ - } - else - { - if (ser->host) - wrbuf_puts(w, ser->host); - if (ser->port) - { - if (wrbuf_len(w)) - wrbuf_puts(w, ":"); - wrbuf_printf(w, "%d", ser->port); - } - } - r = http_init(wrbuf_cstr(w), ser); - wrbuf_destroy(w); - if (r) - return -1; - - w = wrbuf_alloc(); - if (proxy_override) - wrbuf_puts(w, proxy_override); - else if (ser->proxy_host || ser->proxy_port) - { - if (ser->proxy_host) - wrbuf_puts(w, ser->proxy_host); - if (ser->proxy_port) - { - if (wrbuf_len(w)) - wrbuf_puts(w, ":"); - wrbuf_printf(w, "%d", ser->proxy_port); - } - } - if (wrbuf_len(w)) - http_set_proxyaddr(wrbuf_cstr(w), ser); - wrbuf_destroy(w); - } - return 0; -} // Master list of connections we're handling events to static IOCHAN channel_list = 0; diff --git a/src/pazpar2.c b/src/pazpar2.c index caafc4f..56c18a5 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include static char *path_override = 0; +static struct conf_config *sc_stop_config = 0; void child_handler(void *data) { @@ -118,6 +119,7 @@ static int sc_main( config = read_config(arg); if (!config) exit(1); + sc_stop_config = config; break; case 'h': listener_override = arg; @@ -184,7 +186,7 @@ static int sc_main( yaz_log(YLOG_FATAL, "Load config with -f"); return 1; } - ret = start_http_listener(config, listener_override, proxy_override); + ret = config_start_listeners(config, listener_override, proxy_override); if (ret) return ret; /* error starting http listener */ @@ -201,7 +203,7 @@ static int sc_main( static void sc_stop(yaz_sc_t s) { - http_close_server(); + config_stop_listeners(sc_stop_config); } int main(int argc, char **argv) diff --git a/src/pazpar2.h b/src/pazpar2.h index 7bb067f..8724a69 100644 --- a/src/pazpar2.h +++ b/src/pazpar2.h @@ -182,11 +182,6 @@ int session_active_clients(struct session *s); void session_apply_setting(struct session *se, char *dbname, char *setting, char *value); const char *session_setting_oneval(struct session_database *db, int offset); -int start_http_listener(struct conf_config *conf, - const char *listener_override, - const char *proxy_override); -void start_proxy(void); - void pazpar2_add_channel(IOCHAN c); void pazpar2_event_loop(void); diff --git a/src/pazpar2_config.c b/src/pazpar2_config.c index 668d907..f4539a0 100644 --- a/src/pazpar2_config.c +++ b/src/pazpar2_config.c @@ -36,10 +36,19 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "pazpar2_config.h" #include "settings.h" +#include "eventl.h" +#include "http.h" + +struct conf_config +{ + NMEM nmem; /* for conf_config and servers memory */ + struct conf_server *servers; + WRBUF confdir; +}; -static WRBUF confdir = 0; -static char *parse_settings(NMEM nmem, xmlNode *node); +static char *parse_settings(struct conf_config *config, + NMEM nmem, xmlNode *node); static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem, xmlNode *node); @@ -96,8 +105,9 @@ struct conf_sortkey * conf_sortkey_assign(NMEM nmem, } -struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, - const char *service_id) +struct conf_service * conf_service_create(struct conf_config *config, + int num_metadata, int num_sortkeys, + const char *service_id) { struct conf_service * service = 0; NMEM nmem = nmem_create(); @@ -110,7 +120,7 @@ struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, service->settings = 0; service->databases = 0; service->targetprofiles = 0; - + service->config = config; service->id = service_id ? nmem_strdup(nmem, service_id) : 0; service->num_metadata = num_metadata; @@ -213,7 +223,8 @@ int conf_service_sortkey_field_id(struct conf_service *service, /* Code to parse configuration file */ /* ==================================================== */ -static struct conf_service *parse_service(xmlNode *node, const char *service_id) +static struct conf_service *parse_service(struct conf_config *config, + xmlNode *node, const char *service_id) { xmlNode *n; int md_node = 0; @@ -235,7 +246,8 @@ static struct conf_service *parse_service(xmlNode *node, const char *service_id) xmlFree(sortkey); } - service = conf_service_create(num_metadata, num_sortkeys, service_id); + service = conf_service_create(config, + num_metadata, num_sortkeys, service_id); for (n = node->children; n; n = n->next) { @@ -248,7 +260,7 @@ static struct conf_service *parse_service(xmlNode *node, const char *service_id) yaz_log(YLOG_FATAL, "Can't repeat 'settings'"); return 0; } - service->settings = parse_settings(service->nmem, n); + service->settings = parse_settings(config, service->nmem, n); if (!service->settings) return 0; } @@ -441,7 +453,8 @@ service, sk_node, return service; } -static char *parse_settings(NMEM nmem, xmlNode *node) +static char *parse_settings(struct conf_config *config, + NMEM nmem, xmlNode *node) { xmlChar *src = xmlGetProp(node, (xmlChar *) "src"); char *r; @@ -453,8 +466,9 @@ static char *parse_settings(NMEM nmem, xmlNode *node) else { r = nmem_malloc(nmem, - wrbuf_len(confdir) + strlen((const char *) src) + 2); - sprintf(r, "%s/%s", wrbuf_cstr(confdir), src); + wrbuf_len(config->confdir) + + strlen((const char *) src) + 2); + sprintf(r, "%s/%s", wrbuf_cstr(config->confdir), src); } } else @@ -466,7 +480,8 @@ static char *parse_settings(NMEM nmem, xmlNode *node) return r; } -static struct conf_server *parse_server(NMEM nmem, xmlNode *node) +static struct conf_server *parse_server(struct conf_config *config, + NMEM nmem, xmlNode *node) { xmlNode *n; struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server)); @@ -521,7 +536,7 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) yaz_log(YLOG_FATAL, "Can't repeat 'settings'"); return 0; } - if (!(server->server_settings = parse_settings(nmem, n))) + if (!(server->server_settings = parse_settings(config, nmem, n))) return 0; } else if (!strcmp((const char *) n->name, "relevance")) @@ -565,7 +580,7 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) return 0; else { - struct conf_service *s = parse_service(n, service_id); + struct conf_service *s = parse_service(config, n, service_id); if (s) { s->relevance_pct = server->relevance_pct ? @@ -588,13 +603,15 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) return server; } -xsltStylesheet *conf_load_stylesheet(const char *fname) +xsltStylesheet *conf_load_stylesheet(struct conf_config *config, + const char *fname) { char path[256]; if (yaz_is_abspath(fname)) yaz_snprintf(path, sizeof(path), fname); - else - yaz_snprintf(path, sizeof(path), "%s/%s", wrbuf_cstr(confdir), fname); + else if (config) + yaz_snprintf(path, sizeof(path), "%s/%s", + wrbuf_cstr(config->confdir), fname); return xsltParseStylesheetFile((xmlChar *) path); } @@ -648,14 +665,9 @@ struct conf_service *locate_service(struct conf_server *server, } -static struct conf_config *parse_config(xmlNode *root) +static int parse_config(struct conf_config *config, xmlNode *root) { - NMEM nmem = nmem_create(); xmlNode *n; - struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config)); - - r->nmem = nmem; - r->servers = 0; for (n = root->children; n; n = n->next) { @@ -663,32 +675,34 @@ static struct conf_config *parse_config(xmlNode *root) continue; if (!strcmp((const char *) n->name, "server")) { - struct conf_server *tmp = parse_server(nmem, n); + struct conf_server *tmp = parse_server(config, config->nmem, n); if (!tmp) - return 0; - tmp->next = r->servers; - r->servers = tmp; + return -1; + tmp->next = config->servers; + config->servers = tmp; } else if (!strcmp((const char *) n->name, "targetprofiles")) { yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service"); - return 0; + return -1; } else { yaz_log(YLOG_FATAL, "Bad element: %s", n->name); - return 0; + return -1; } } - return r; + return 0; } struct conf_config *read_config(const char *fname) { xmlDoc *doc = xmlParseFile(fname); const char *p; - struct conf_config *config; + int r; + NMEM nmem = nmem_create(); + struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config)); xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; @@ -697,7 +711,11 @@ struct conf_config *read_config(const char *fname) yaz_log(YLOG_FATAL, "Failed to read %s", fname); exit(1); } - confdir = wrbuf_alloc(); + + config->nmem = nmem; + config->servers = 0; + + config->confdir = wrbuf_alloc(); if ((p = strrchr(fname, #ifdef WIN32 '\\' @@ -707,12 +725,14 @@ struct conf_config *read_config(const char *fname) ))) { int len = p - fname; - wrbuf_write(confdir, fname, len); + wrbuf_write(config->confdir, fname, len); } - wrbuf_puts(confdir, ""); - config = parse_config(xmlDocGetRootElement(doc)); + wrbuf_puts(config->confdir, ""); + r = parse_config(config, xmlDocGetRootElement(doc)); xmlFreeDoc(doc); + if (r) + return 0; return config; } @@ -734,6 +754,64 @@ void config_read_settings(struct conf_config *config, } } +void config_stop_listeners(struct conf_config *conf) +{ + struct conf_server *ser; + for (ser = conf->servers; ser; ser = ser->next) + http_close_server(ser); +} + +int config_start_listeners(struct conf_config *conf, + const char *listener_override, + const char *proxy_override) +{ + struct conf_server *ser; + for (ser = conf->servers; ser; ser = ser->next) + { + WRBUF w = wrbuf_alloc(); + int r; + if (listener_override) + { + wrbuf_puts(w, listener_override); + listener_override = 0; /* only first server is overriden */ + } + else + { + if (ser->host) + wrbuf_puts(w, ser->host); + if (ser->port) + { + if (wrbuf_len(w)) + wrbuf_puts(w, ":"); + wrbuf_printf(w, "%d", ser->port); + } + } + r = http_init(wrbuf_cstr(w), ser); + wrbuf_destroy(w); + if (r) + return -1; + + w = wrbuf_alloc(); + if (proxy_override) + wrbuf_puts(w, proxy_override); + else if (ser->proxy_host || ser->proxy_port) + { + if (ser->proxy_host) + wrbuf_puts(w, ser->proxy_host); + if (ser->proxy_port) + { + if (wrbuf_len(w)) + wrbuf_puts(w, ":"); + wrbuf_printf(w, "%d", ser->proxy_port); + } + } + if (wrbuf_len(w)) + http_set_proxyaddr(wrbuf_cstr(w), ser); + wrbuf_destroy(w); + } + return 0; +} + /* * Local variables: * c-basic-offset: 4 diff --git a/src/pazpar2_config.h b/src/pazpar2_config.h index d01be5e..ee62aad 100644 --- a/src/pazpar2_config.h +++ b/src/pazpar2_config.h @@ -113,10 +113,12 @@ struct conf_service struct database *databases; struct conf_targetprofiles *targetprofiles; + struct conf_config *config; }; -struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, - const char *service_id); +struct conf_service * conf_service_create(struct conf_config *config, + int num_metadata, int num_sortkeys, + const char *service_id); struct conf_metadata* conf_service_add_metadata(struct conf_service *service, int field_id, @@ -140,7 +142,6 @@ int conf_service_metadata_field_id(struct conf_service *service, const char * na int conf_service_sortkey_field_id(struct conf_service *service, const char * name); - struct conf_server { char *host; @@ -149,6 +150,7 @@ struct conf_server int proxy_port; char *myurl; struct sockaddr_in *proxy_addr; + int listener_socket; char *server_settings; @@ -167,14 +169,9 @@ struct conf_targetprofiles char *src; }; -struct conf_config -{ - NMEM nmem; /* for conf_config and servers memory */ - struct conf_server *servers; -}; - struct conf_config *read_config(const char *fname); -xsltStylesheet *conf_load_stylesheet(const char *fname); +xsltStylesheet *conf_load_stylesheet(struct conf_config *config, + const char *fname); void config_read_settings(struct conf_config *config, const char *path_override); @@ -183,6 +180,12 @@ struct conf_service *locate_service(struct conf_server *server, const char *service_id); +int config_start_listeners(struct conf_config *conf, + const char *listener_override, + const char *proxy_override); + +void config_stop_listeners(struct conf_config *conf); + #endif /* diff --git a/src/test_config.c b/src/test_config.c index 52d524a..21b3e5d 100644 --- a/src/test_config.c +++ b/src/test_config.c @@ -35,7 +35,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA void test_conf_service(int argc, char **argv) { struct conf_service *service = 0; - service = conf_service_create(4, 3, 0); + service = conf_service_create(0, 4, 3, 0); YAZ_CHECK(service); diff --git a/src/test_record.c b/src/test_record.c index 0b62f0f..f9014f7 100644 --- a/src/test_record.c +++ b/src/test_record.c @@ -51,7 +51,7 @@ void test_record(int argc, char **argv) data_num.number.max = 5; - service = conf_service_create(4, 3, 0); + service = conf_service_create(0, 4, 3, 0); YAZ_CHECK(service); YAZ_CHECK(conf_service_add_metadata( -- 1.7.10.4