diff --git a/sources/config_reader.c b/sources/config_reader.c index 96d3a66c..ca85fa56 100644 --- a/sources/config_reader.c +++ b/sources/config_reader.c @@ -767,13 +767,18 @@ od_config_reader_route(od_config_reader_t *reader, /* quantiles */ case OD_LQUANTILES: { char *quantiles_str = NULL; - if (!od_config_reader_string(reader, &quantiles_str)) - return -1; + if (!od_config_reader_string(reader, &quantiles_str)) { + free(quantiles_str); + return NOT_OK_RESPONSE; + } if (!od_config_reader_quantiles(reader, quantiles_str, &route->quantiles, - &route->quantiles_count)) - return -1; + &route->quantiles_count)) { + free(quantiles_str); + return NOT_OK_RESPONSE; + } + free(quantiles_str); } break; /* application_name_add_host */ case OD_LAPPLICATION_NAME_ADD_HOST: diff --git a/sources/counter.c b/sources/counter.c index 975e37e3..4557a0d6 100644 --- a/sources/counter.c +++ b/sources/counter.c @@ -4,25 +4,29 @@ inline od_counter_llist_t * od_counter_llist_create(void) { - od_counter_llist_t *l = malloc(sizeof(od_counter_llist_t)); + od_counter_llist_t *llist = malloc(sizeof(od_counter_llist_t)); + if (llist == NULL) + return NULL; - l->list = NULL; - l->count = 0; + llist->list = NULL; + llist->count = 0; - return l; + return llist; } inline void -od_counter_llist_add(od_counter_llist_t *l, const od_counter_item_t *it) +od_counter_llist_add(od_counter_llist_t *llist, const od_counter_item_t *it) { od_counter_litem_t *litem = malloc(sizeof(od_counter_litem_t)); - litem->value = *it; - litem->cnt = 1; + if (litem == NULL) + return; + litem->value = *it; + litem->cnt = 1; - litem->next = l->list; - l->list = litem; + litem->next = llist->list; + llist->list = litem; - ++l->count; + ++llist->count; } inline od_retcode_t @@ -42,13 +46,27 @@ od_counter_t * od_counter_create(size_t sz) { od_counter_t *t = malloc(sizeof(od_counter_t)); - t->buckets = malloc(sizeof(od_counter_llist_t) * sz); + if (t == NULL) { + return NULL; + } + t->buckets = malloc(sizeof(od_counter_llist_t) * sz); + if (t->buckets == NULL) { + return NULL; + } t->bucket_mutex = malloc(sizeof(pthread_mutex_t) * sz); - t->size = sz; + if (t->bucket_mutex == NULL) { + return NULL; + } + t->size = sz; for (size_t i = 0; i < t->size; ++i) { t->buckets[i] = od_counter_llist_create(); - pthread_mutex_init(&t->bucket_mutex[i], NULL); + if (t->buckets[i] == NULL) + return NULL; + const int res = pthread_mutex_init(&t->bucket_mutex[i], NULL); + if (res) { + return NULL; + } } return t; diff --git a/sources/counter.h b/sources/counter.h index c5398873..36a39234 100644 --- a/sources/counter.h +++ b/sources/counter.h @@ -8,7 +8,6 @@ * Scalable PostgreSQL connection pooler. */ -#include #include #include "macro.h" @@ -40,7 +39,7 @@ extern od_counter_llist_t * od_counter_llist_create(void); extern void -od_counter_llist_add(od_counter_llist_t *l, const od_counter_item_t *it); +od_counter_llist_add(od_counter_llist_t *llist, const od_counter_item_t *it); extern od_retcode_t od_counter_llist_free(od_counter_llist_t *l); diff --git a/sources/err_logger.c b/sources/err_logger.c index afae62fb..8c8308dd 100644 --- a/sources/err_logger.c +++ b/sources/err_logger.c @@ -4,33 +4,39 @@ od_error_logger_t * od_err_logger_create(size_t intervals_count) { - od_error_logger_t *l = malloc(sizeof(od_error_logger_t)); - - l->intercals_cnt = intervals_count; - l->current_interval_num = 0; - - l->interval_counters = malloc(sizeof(od_counter_t) * intervals_count); - - for (size_t i = 0; i < intervals_count; ++i) { - l->interval_counters[i] = od_counter_create_default(); + od_error_logger_t *err_logger = malloc(sizeof(od_error_logger_t)); + if (err_logger == NULL) { + return NULL; } - pthread_mutex_init(&l->lock, NULL); + err_logger->intercals_cnt = intervals_count; + err_logger->current_interval_num = 0; - return l; + err_logger->interval_counters = + malloc(sizeof(od_counter_t) * intervals_count); + + for (size_t i = 0; i < intervals_count; ++i) { + err_logger->interval_counters[i] = od_counter_create_default(); + if (err_logger->interval_counters[i] == NULL) + return NULL; + } + + pthread_mutex_init(&err_logger->lock, NULL); + + return err_logger; } od_retcode_t -od_err_logger_free(od_error_logger_t *l) +od_err_logger_free(od_error_logger_t *err_logger) { - for (size_t i = 0; i < l->intercals_cnt; ++i) { - int rc = od_counter_free(l->interval_counters[i]); + for (size_t i = 0; i < err_logger->intercals_cnt; ++i) { + int rc = od_counter_free(err_logger->interval_counters[i]); if (rc != OK_RESPONSE) return rc; } - pthread_mutex_destroy(&l->lock); - free(l); + pthread_mutex_destroy(&err_logger->lock); + free(err_logger); return OK_RESPONSE; } diff --git a/sources/err_logger.h b/sources/err_logger.h index 17e0c826..6e39f34d 100644 --- a/sources/err_logger.h +++ b/sources/err_logger.h @@ -37,7 +37,7 @@ od_err_logger_create_default() } extern od_retcode_t -od_err_logger_free(od_error_logger_t *l); +od_err_logger_free(od_error_logger_t *err_logger); static inline od_retcode_t od_err_logger_inc_interval(od_error_logger_t *l) diff --git a/sources/frontend.c b/sources/frontend.c index 5a050342..1f9e7ed4 100644 --- a/sources/frontend.c +++ b/sources/frontend.c @@ -785,9 +785,9 @@ od_frontend_remote(od_client_t *client) status = od_relay_step(&server->relay); if (status == OD_DETACH) { /* write any pending data to server first */ - od_frontend_status_t status; - status = od_relay_flush(&server->relay); - if (status != OD_OK) + od_frontend_status_t flush_status; + flush_status = od_relay_flush(&server->relay); + if (flush_status != OD_OK) break; od_relay_detach(&client->relay); od_relay_stop(&server->relay); @@ -795,7 +795,7 @@ od_frontend_remote(od_client_t *client) /* cleanup server */ rc = od_reset(server); if (rc == -1) { - status = OD_ESERVER_WRITE; + flush_status = OD_ESERVER_WRITE; break; } @@ -810,11 +810,11 @@ od_frontend_remote(od_client_t *client) } if (client->server) { - od_server_t *server = client->server; + od_server_t *curr_server = client->server; od_frontend_status_t flush_status; - flush_status = od_relay_flush(&server->relay); - od_relay_stop(&server->relay); + flush_status = od_relay_flush(&curr_server->relay); + od_relay_stop(&curr_server->relay); if (flush_status != OD_OK) { return flush_status; } diff --git a/sources/instance.c b/sources/instance.c index 2541baad..590075db 100644 --- a/sources/instance.c +++ b/sources/instance.c @@ -90,10 +90,11 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) /* validate command line options */ if (argc != 2) { od_usage(instance, argv[0]); - return -1; + goto error; } if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { od_usage(instance, argv[0]); + od_router_free(&router); return 0; } instance->config_file = argv[1]; @@ -109,18 +110,20 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) instance->config_file); if (rc == -1) { od_error(&instance->logger, "config", NULL, NULL, "%s", error.error); - return -1; + goto error; } /* validate configuration */ rc = od_config_validate(&instance->config, &instance->logger); - if (rc == -1) - return -1; + if (rc == -1) { + goto error; + } /* validate rules */ rc = od_rules_validate(&router.rules, &instance->config, &instance->logger); - if (rc == -1) - return -1; + if (rc == -1) { + goto error; + } /* configure logger */ od_logger_set_format(&instance->logger, instance->config.log_format); @@ -130,8 +133,9 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) /* run as daemon */ if (instance->config.daemonize) { rc = od_daemonize(); - if (rc == -1) - return -1; + if (rc == -1) { + goto error; + } /* update pid */ od_pid_init(&instance->pid); } @@ -146,7 +150,7 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) NULL, "failed to open log file '%s'", instance->config.log_file); - return -1; + goto error; } } @@ -202,7 +206,7 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) if (rc == -1) { od_error( &instance->logger, "init", NULL, NULL, "failed to init machinarium"); - return -1; + goto error; } /* create pid file */ @@ -211,8 +215,13 @@ od_instance_main(od_instance_t *instance, int argc, char **argv) /* start system machine thread */ rc = od_system_start(&system, &global); - if (rc == -1) - return -1; + if (rc == -1) { + goto error; + } return machine_wait(system.machine); + +error: + od_router_free(&router); + return NOT_OK_RESPONSE; } diff --git a/sources/route.h b/sources/route.h index e8fa6c4a..5645e3fd 100644 --- a/sources/route.h +++ b/sources/route.h @@ -42,7 +42,7 @@ od_route_init(od_route_t *route) /* stat init */ route->stats_mark_db = false; - /* error logging*/; + /* error logging */; route->frontend_err_logger = od_err_logger_create_default(); od_stat_init(&route->stats); diff --git a/sources/router.c b/sources/router.c index dcce6053..a8015c78 100644 --- a/sources/router.c +++ b/sources/router.c @@ -39,6 +39,7 @@ od_router_free(od_router_t *router) od_route_pool_free(&router->route_pool); od_rules_free(&router->rules); pthread_mutex_destroy(&router->lock); + od_err_logger_free(router->router_err_logger); } inline int