From 9a3dd78926f2782a666d3fa0ae967e13f2d3e995 Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Wed, 17 Jul 2013 16:06:08 +0200 Subject: [PATCH] Addition of reload commands in the debug CLI for users and configuration Fixed bug in reload configuration --- core/dbusers.c | 69 +++++++++++++++++++++++++++++++------- core/service.c | 2 +- include/dbusers.h | 1 + modules/routing/debugcmd.c | 43 ++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/core/dbusers.c b/core/dbusers.c index a7d911cda..8530914a2 100644 --- a/core/dbusers.c +++ b/core/dbusers.c @@ -34,6 +34,10 @@ #include #include #include +#include +#include + +static int getUsers(SERVICE *service, struct users *users); /** * Load the user/passwd form mysql.user table into the service users' hashtable @@ -44,7 +48,46 @@ */ int load_mysql_users(SERVICE *service) -{ +{ + return getUsers(service, service->users); +} + +/** + * Reload the user/passwd form mysql.user table into the service users' hashtable + * environment. + * + * @param service The current service + * @return -1 on any error or the number of users inserted (0 means no users at all) + */ +int +reload_mysql_users(SERVICE *service) +{ +int i; +struct users *newusers, *oldusers; + + if ((newusers = users_alloc()) == NULL) + return 0; + i = getUsers(service, newusers); + spinlock_acquire(&service->spin); + oldusers = service->users; + service->users = newusers; + spinlock_release(&service->spin); + users_free(oldusers); + + return i; +} + +/** + * Load the user/passwd form mysql.user table into the service users' hashtable + * environment. + * + * @param service The current service + * @param users The users table into which to load the users + * @return -1 on any error or the number of users inserted (0 means no users at all) + */ +static int +getUsers(SERVICE *service, struct users *users) +{ MYSQL *con = NULL; MYSQL_ROW row; MYSQL_RES *result = NULL; @@ -52,14 +95,14 @@ load_mysql_users(SERVICE *service) char *service_user = NULL; char *service_passwd = NULL; int total_users = 0; - SERVER *server; + SERVER *server; serviceGetUser(service, &service_user, &service_passwd); - /** multi-thread environment requires that thread init succeeds. */ - if (mysql_thread_init()) { - skygw_log_write_flush(NULL, "ERROR : mysql_thread_init failed.\n"); - return -1; - } + /** multi-thread environment requires that thread init succeeds. */ + if (mysql_thread_init()) { + skygw_log_write_flush(NULL, LOGFILE_ERROR, "ERROR : mysql_thread_init failed.\n"); + return -1; + } con = mysql_init(NULL); @@ -68,11 +111,11 @@ load_mysql_users(SERVICE *service) return -1; } - if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) { - skygw_log_write_flush(NULL, "Fatal : failed to set external connection. " + if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) { + skygw_log_write_flush(NULL, LOGFILE_ERROR, "Fatal : failed to set external connection. " "It is needed for backend server connections. Exiting.\n"); - return -1; - } + return -1; + } /* * Attempt to connect to each database in the service in turn until * we find one that we can connect to or until we run out of databases @@ -118,13 +161,13 @@ load_mysql_users(SERVICE *service) //printf("User %s , Passwd %s\n", row[0], row[1]); // now adding to the hastable user and passwd+1 (escaping the first byte that is '*') - users_add(service->users, row[0], row[1]+1); + users_add(users, row[0], row[1]+1); total_users++; } mysql_free_result(result); mysql_close(con); - mysql_thread_end(); + mysql_thread_end(); return total_users; } diff --git a/core/service.c b/core/service.c index 286791a35..530992cd4 100644 --- a/core/service.c +++ b/core/service.c @@ -617,7 +617,7 @@ void *router_obj; service->router = router_obj; } } - if (strcmp(service->credentials.name, user) == 0 || strcmp(service->credentials.authdata, auth) == 0) + if (user && (strcmp(service->credentials.name, user) != 0 || strcmp(service->credentials.authdata, auth) != 0)) { skygw_log_write(NULL, LOGFILE_MESSAGE, "Update credentials for service %s", service->name); serviceSetUser(service, user, auth); diff --git a/include/dbusers.h b/include/dbusers.h index 4d3f2d87c..3b5e55f9d 100644 --- a/include/dbusers.h +++ b/include/dbusers.h @@ -32,4 +32,5 @@ */ extern int load_mysql_users(SERVICE *service); +extern int reload_mysql_users(SERVICE *service); #endif diff --git a/modules/routing/debugcmd.c b/modules/routing/debugcmd.c index bf176096f..8ef43661c 100644 --- a/modules/routing/debugcmd.c +++ b/modules/routing/debugcmd.c @@ -35,6 +35,7 @@ * * Date Who Description * 20/06/13 Mark Riddoch Initial implementation + * 17/07/13 Mark Riddoch Additional commands * * @endverbatim */ @@ -51,6 +52,8 @@ #include #include #include +#include +#include #include #define MAXARGS 5 @@ -148,6 +151,21 @@ struct subcommand clearoptions[] = { {0, 0, 0} } }; +static void reload_users(DCB *dcb, SERVICE *service); +static void reload_config(DCB *dcb); + +/** + * The subcommands of the reload command + */ +struct subcommand reloadoptions[] = { + { "users", 1, reload_users, "Reload the user data for a service. E.g. reload users 0x849420", + {ARG_TYPE_ADDRESS, 0, 0} }, + { "config", 0, reload_config, "Reload the configuration data for hte gateway.", + {ARG_TYPE_ADDRESS, 0, 0} }, + { NULL, 0, NULL, NULL, + {0, 0, 0} } +}; + /** * The debug command table */ @@ -160,6 +178,7 @@ static struct { { "restart", restartoptions }, { "set", setoptions }, { "clear", clearoptions }, + { "reload", reloadoptions }, { NULL, NULL } }; @@ -403,3 +422,27 @@ unsigned int bitvalue; else dcb_printf(dcb, "Unknown status bit %s\n", bit); } + +/** + * Reload the authenticaton data from the backend database of a service. + * + * @param dcb DCB to send output + * @param service The service to update + */ +static void +reload_users(DCB *dcb, SERVICE *service) +{ + dcb_printf(dcb, "Loaded %d users.\n", reload_mysql_users(service)); +} + +/** + * Relaod the configuration data from the config file + * + * @param dcb DCB to use to send output + */ +static void +reload_config(DCB *dcb) +{ + dcb_printf(dcb, "Reloading configuration from file.\n"); + config_reload(); +}