Reindented server/core/gw_utils.c

This commit is contained in:
Johan Wikman
2015-11-30 13:22:49 +02:00
parent d2aebe2bf0
commit 26d1cf0c1f
2 changed files with 144 additions and 130 deletions

View File

@ -56,7 +56,8 @@ SPINLOCK tmplock = SPINLOCK_INIT;
* @return 1 on success, 0 on failure * @return 1 on success, 0 on failure
*/ */
int int
setipaddress(struct in_addr *a, char *p) { setipaddress(struct in_addr *a, char *p)
{
#ifdef __USE_POSIX #ifdef __USE_POSIX
struct addrinfo *ai = NULL, hint; struct addrinfo *ai = NULL, hint;
int rc; int rc;
@ -70,21 +71,26 @@ setipaddress(struct in_addr *a, char *p) {
* For future specific addresses bind, a dedicated routine woulbd be better * For future specific addresses bind, a dedicated routine woulbd be better
*/ */
if (strcmp(p, "0.0.0.0") == 0) { if (strcmp(p, "0.0.0.0") == 0)
{
hint.ai_flags = AI_PASSIVE; hint.ai_flags = AI_PASSIVE;
hint.ai_family = AF_UNSPEC; hint.ai_family = AF_UNSPEC;
if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) { if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0)
{
MXS_ERROR("Failed to obtain address for host %s, %s", MXS_ERROR("Failed to obtain address for host %s, %s",
p, p,
gai_strerror(rc)); gai_strerror(rc));
return 0; return 0;
} }
} else { }
else
{
hint.ai_flags = AI_CANONNAME; hint.ai_flags = AI_CANONNAME;
hint.ai_family = AF_INET; hint.ai_family = AF_INET;
if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0) { if ((rc = getaddrinfo(p, NULL, &hint, &ai)) != 0)
{
MXS_ERROR("Failed to obtain address for host %s, %s", MXS_ERROR("Failed to obtain address for host %s, %s",
p, p,
gai_strerror(rc)); gai_strerror(rc));
@ -94,7 +100,8 @@ setipaddress(struct in_addr *a, char *p) {
} }
/* take the first one */ /* take the first one */
if (ai != NULL) { if (ai != NULL)
{
res_addr = (struct sockaddr_in *)(ai->ai_addr); res_addr = (struct sockaddr_in *)(ai->ai_addr);
memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr)); memcpy(a, &res_addr->sin_addr, sizeof(struct in_addr));
@ -109,13 +116,17 @@ setipaddress(struct in_addr *a, char *p) {
h = gethostbyname(p); h = gethostbyname(p);
spinlock_release(&tmplock); spinlock_release(&tmplock);
if (h == NULL) { if (h == NULL)
if ((a->s_addr = inet_addr(p)) == -1) { {
if ((a->s_addr = inet_addr(p)) == -1)
{
MXS_ERROR("gethostbyname failed for [%s]", p); MXS_ERROR("gethostbyname failed for [%s]", p);
return 0; return 0;
} }
} else { }
else
{
/* take the first one */ /* take the first one */
memcpy(a, h->h_addr, h->h_length); memcpy(a, h->h_addr, h->h_length);
@ -129,23 +140,27 @@ setipaddress(struct in_addr *a, char *p) {
* Daemonize the process by forking and putting the process into the * Daemonize the process by forking and putting the process into the
* background. * background.
*/ */
bool gw_daemonize(void) { bool gw_daemonize(void)
{
pid_t pid; pid_t pid;
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0)
{
char errbuf[STRERROR_BUFLEN]; char errbuf[STRERROR_BUFLEN];
fprintf(stderr, "fork() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); fprintf(stderr, "fork() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf)));
exit(1); exit(1);
} }
if (pid != 0) { if (pid != 0)
{
/* exit from main */ /* exit from main */
return true; return true;
} }
if (setsid() < 0) { if (setsid() < 0)
{
char errbuf[STRERROR_BUFLEN]; char errbuf[STRERROR_BUFLEN];
fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf)));
exit(1); exit(1);
@ -173,7 +188,6 @@ char *port, buf[1024 + 1];
short pnum; short pnum;
struct hostent *hp; struct hostent *hp;
strncpy(buf, config, 1024); strncpy(buf, config, 1024);
port = strrchr(buf, ':'); port = strrchr(buf, ':');
if (port) if (port)