From d5cf74bd24ed000df6aab90af44d9e432aee8e63 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Sat, 15 Oct 2016 11:14:11 +0300 Subject: [PATCH] Cleanup gw.h, part 1. Gw.h contained a fair amount of obsolete function declarations, duplicate declarations of functions declared in utils.h and declarations of functions that conceptually are similar to those in utils.h. The obsolete and duplicate ones were removed and all but one of the remaining moved to utils.h. Correspondingly the implementation was moved from gw_utils.c to utils.c. The one remaining function - gw_daemonize() - is not really worthy of a file of its own, so that is to be moved to gateway.c after which gw_utils.c can be removed. Gw.h still contains defines that are duplicated in maxscale/protocol/mysql.h. The ones in gw.h are to be removed. It appears that the entire gw.h will disappear. --- include/maxscale/cdefs.h | 1 + include/maxscale/gw.h | 16 ---- include/maxscale/utils.h | 11 +++ server/core/dcb.c | 1 + server/core/gw_utils.c | 167 ----------------------------------- server/core/gwdirs.c | 2 +- server/core/poll.c | 1 + server/core/utils.c | 185 +++++++++++++++++++++++++++++++++++++-- 8 files changed, 193 insertions(+), 191 deletions(-) diff --git a/include/maxscale/cdefs.h b/include/maxscale/cdefs.h index 62c28b328..a26342939 100644 --- a/include/maxscale/cdefs.h +++ b/include/maxscale/cdefs.h @@ -72,5 +72,6 @@ */ #include #include +#include #endif diff --git a/include/maxscale/gw.h b/include/maxscale/gw.h index 085c73e58..5e81ba2fe 100644 --- a/include/maxscale/gw.h +++ b/include/maxscale/gw.h @@ -69,23 +69,7 @@ MXS_BEGIN_DECLS #define MYSQL_CONN_DEBUG #undef MYSQL_CONN_DEBUG -#include "dcb.h" - bool gw_daemonize(void); -int do_read_dcb(DCB *dcb); -void MySQLListener(int epfd, char *config_bind); -int MySQLAccept(DCB *listener); -int do_read_dcb(DCB *dcb); -int do_read_10(DCB *dcb, uint8_t *buffer); -int MySQLWrite(DCB *dcb, GWBUF *queue); -int setnonblocking(int fd); -int gw_getsockerrno(int fd); -int parse_bindconfig(const char *, struct sockaddr_in *); -int setipaddress(struct in_addr *, char *); -char* get_libdir(); -long get_processor_count(); -void clean_up_pathname(char *path); -bool mxs_mkdir_all(const char *path, int mask); MXS_END_DECLS diff --git a/include/maxscale/utils.h b/include/maxscale/utils.h index 1f608296d..55ce9a34e 100644 --- a/include/maxscale/utils.h +++ b/include/maxscale/utils.h @@ -27,8 +27,10 @@ */ #include +#include #include #include +#include MXS_BEGIN_DECLS @@ -41,6 +43,9 @@ bool utils_init(); /*< Call this first before using any other function */ void utils_end(); int setnonblocking(int fd); +int parse_bindconfig(const char *, struct sockaddr_in *); +int setipaddress(struct in_addr *, char *); + char *gw_strend(register const char *s); static char gw_randomchar(); int gw_generate_random_str(char *output, int len); @@ -67,6 +72,12 @@ char* replace_literal(char* haystack, const char* replacement); char* replace_quoted(const char** src, const size_t* srcsize, char** dest, size_t* destsize); +void clean_up_pathname(char *path); + +bool mxs_mkdir_all(const char *path, int mask); + +long get_processor_count(); + MXS_END_DECLS #endif diff --git a/server/core/dcb.c b/server/core/dcb.c index 065213cec..675078798 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -90,6 +90,7 @@ #include #include #include +#include #if defined(FAKE_CODE) unsigned char dcb_fake_write_errno[10240]; diff --git a/server/core/gw_utils.c b/server/core/gw_utils.c index 2ef05638d..60cc9bed5 100644 --- a/server/core/gw_utils.c +++ b/server/core/gw_utils.c @@ -39,96 +39,6 @@ #include #include -SPINLOCK tmplock = SPINLOCK_INIT; - -/* - * Set IP address in socket structure in_addr - * - * @param a Pointer to a struct in_addr into which the address is written - * @param p The hostname to lookup - * @return 1 on success, 0 on failure - */ -int -setipaddress(struct in_addr *a, char *p) -{ -#ifdef __USE_POSIX - struct addrinfo *ai = NULL, hint; - int rc; - struct sockaddr_in *res_addr; - memset(&hint, 0, sizeof (hint)); - - hint.ai_socktype = SOCK_STREAM; - - /* - * This is for the listening socket, matching INADDR_ANY only for now. - * For future specific addresses bind, a dedicated routine woulbd be better - */ - - if (strcmp(p, "0.0.0.0") == 0) - { - hint.ai_flags = AI_PASSIVE; - hint.ai_family = AF_UNSPEC; - if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) - { - MXS_ERROR("Failed to obtain address for host %s, %s", - p, - gai_strerror(rc)); - - return 0; - } - } - else - { - hint.ai_flags = AI_CANONNAME; - hint.ai_family = AF_INET; - - if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) - { - MXS_ERROR("Failed to obtain address for host %s, %s", - p, - gai_strerror(rc)); - - return 0; - } - } - - /* take the first one */ - if (ai != NULL) - { - res_addr = (struct sockaddr_in *)(ai->ai_addr); - memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr)); - - freeaddrinfo(ai); - - return 1; - } -#else - struct hostent *h; - - spinlock_acquire(&tmplock); - h = gethostbyname(p); - spinlock_release(&tmplock); - - if (h == NULL) - { - if ((a->s_addr = inet_addr(p)) == -1) - { - MXS_ERROR("gethostbyname failed for [%s]", p); - - return 0; - } - } - else - { - /* take the first one */ - memcpy(a, h->h_addr, h->h_length); - - return 1; - } -#endif - return 0; -} - /** * Daemonize the process by forking and putting the process into the * background. @@ -160,80 +70,3 @@ bool gw_daemonize(void) } return false; } - -/** - * Parse the bind config data. This is passed in a string as address:port. - * - * The address may be either a . separated IP address or a hostname to - * lookup. The address 0.0.0.0 is the wildcard address for SOCKADR_ANY. - * The ':' and port are required. - * - * @param config The bind address and port separated by a ':' - * @param addr The sockaddr_in in which the data is written - * @return 0 on failure - */ -int -parse_bindconfig(const char *config, struct sockaddr_in *addr) -{ - char buf[strlen(config) + 1]; - strcpy(buf, config); - - char *port = strrchr(buf, ':'); - short pnum; - if (port) - { - *port = 0; - port++; - pnum = atoi(port); - } - else - { - return 0; - } - - if (!strcmp(buf, "0.0.0.0")) - { - addr->sin_addr.s_addr = htonl(INADDR_ANY); - } - else - { - if (!inet_aton(buf, &addr->sin_addr)) - { - struct hostent *hp = gethostbyname(buf); - - if (hp) - { - bcopy(hp->h_addr, &(addr->sin_addr.s_addr), hp->h_length); - } - else - { - MXS_ERROR("Failed to lookup host '%s'.", buf); - return 0; - } - } - } - - addr->sin_family = AF_INET; - addr->sin_port = htons(pnum); - return 1; -} - -/** - * Return the number of processors available. - * @return Number of processors or 1 if the required definition of _SC_NPROCESSORS_CONF - * is not found - */ -long get_processor_count() -{ - long processors = 1; -#ifdef _SC_NPROCESSORS_ONLN - if ((processors = sysconf(_SC_NPROCESSORS_ONLN)) <= 0) - { - MXS_WARNING("Unable to establish the number of available cores. Defaulting to 1."); - processors = 1; - } -#else -#error _SC_NPROCESSORS_ONLN not available. -#endif - return processors; -} diff --git a/server/core/gwdirs.c b/server/core/gwdirs.c index d7c54be0f..eb8ca6394 100644 --- a/server/core/gwdirs.c +++ b/server/core/gwdirs.c @@ -13,7 +13,7 @@ #include #include -#include +#include /** * Set the configuration file directory diff --git a/server/core/poll.c b/server/core/poll.c index 149b67fdb..c32661f68 100644 --- a/server/core/poll.c +++ b/server/core/poll.c @@ -33,6 +33,7 @@ #include #include #include +#include #define PROFILE_POLL 0 diff --git a/server/core/utils.c b/server/core/utils.c index ead727b8e..0688da6c8 100644 --- a/server/core/utils.c +++ b/server/core/utils.c @@ -28,18 +28,21 @@ * @endverbatim */ - +#include +#include +#include #include -#include -#include -#include +#include +#include #include #include -#include +#include #include -#include -#include #include +#include +#include +#include +#include #if !defined(PATH_MAX) # if defined(__USE_POSIX) @@ -880,3 +883,171 @@ void utils_end() pcre2_code_free(replace_values_re); replace_values_re = NULL; } + +SPINLOCK tmplock = SPINLOCK_INIT; + +/* + * Set IP address in socket structure in_addr + * + * @param a Pointer to a struct in_addr into which the address is written + * @param p The hostname to lookup + * @return 1 on success, 0 on failure + */ +int +setipaddress(struct in_addr *a, char *p) +{ +#ifdef __USE_POSIX + struct addrinfo *ai = NULL, hint; + int rc; + struct sockaddr_in *res_addr; + memset(&hint, 0, sizeof (hint)); + + hint.ai_socktype = SOCK_STREAM; + + /* + * This is for the listening socket, matching INADDR_ANY only for now. + * For future specific addresses bind, a dedicated routine woulbd be better + */ + + if (strcmp(p, "0.0.0.0") == 0) + { + hint.ai_flags = AI_PASSIVE; + hint.ai_family = AF_UNSPEC; + if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) + { + MXS_ERROR("Failed to obtain address for host %s, %s", + p, + gai_strerror(rc)); + + return 0; + } + } + else + { + hint.ai_flags = AI_CANONNAME; + hint.ai_family = AF_INET; + + if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) + { + MXS_ERROR("Failed to obtain address for host %s, %s", + p, + gai_strerror(rc)); + + return 0; + } + } + + /* take the first one */ + if (ai != NULL) + { + res_addr = (struct sockaddr_in *)(ai->ai_addr); + memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr)); + + freeaddrinfo(ai); + + return 1; + } +#else + struct hostent *h; + + spinlock_acquire(&tmplock); + h = gethostbyname(p); + spinlock_release(&tmplock); + + if (h == NULL) + { + if ((a->s_addr = inet_addr(p)) == -1) + { + MXS_ERROR("gethostbyname failed for [%s]", p); + + return 0; + } + } + else + { + /* take the first one */ + memcpy(a, h->h_addr, h->h_length); + + return 1; + } +#endif + return 0; +} + + +/** + * Parse the bind config data. This is passed in a string as address:port. + * + * The address may be either a . separated IP address or a hostname to + * lookup. The address 0.0.0.0 is the wildcard address for SOCKADR_ANY. + * The ':' and port are required. + * + * @param config The bind address and port separated by a ':' + * @param addr The sockaddr_in in which the data is written + * @return 0 on failure + */ +int +parse_bindconfig(const char *config, struct sockaddr_in *addr) +{ + char buf[strlen(config) + 1]; + strcpy(buf, config); + + char *port = strrchr(buf, ':'); + short pnum; + if (port) + { + *port = 0; + port++; + pnum = atoi(port); + } + else + { + return 0; + } + + if (!strcmp(buf, "0.0.0.0")) + { + addr->sin_addr.s_addr = htonl(INADDR_ANY); + } + else + { + if (!inet_aton(buf, &addr->sin_addr)) + { + struct hostent *hp = gethostbyname(buf); + + if (hp) + { + bcopy(hp->h_addr, &(addr->sin_addr.s_addr), hp->h_length); + } + else + { + MXS_ERROR("Failed to lookup host '%s'.", buf); + return 0; + } + } + } + + addr->sin_family = AF_INET; + addr->sin_port = htons(pnum); + return 1; +} + +/** + * Return the number of processors available. + * @return Number of processors or 1 if the required definition of _SC_NPROCESSORS_CONF + * is not found + */ +long get_processor_count() +{ + long processors = 1; +#ifdef _SC_NPROCESSORS_ONLN + if ((processors = sysconf(_SC_NPROCESSORS_ONLN)) <= 0) + { + MXS_WARNING("Unable to establish the number of available cores. Defaulting to 1."); + processors = 1; + } +#else +#error _SC_NPROCESSORS_ONLN not available. +#endif + return processors; +}