Reindented server/core/gw_utils.c
This commit is contained in:
@ -56,11 +56,12 @@ SPINLOCK tmplock = SPINLOCK_INIT;
|
||||
* @return 1 on success, 0 on failure
|
||||
*/
|
||||
int
|
||||
setipaddress(struct in_addr *a, char *p) {
|
||||
setipaddress(struct in_addr *a, char *p)
|
||||
{
|
||||
#ifdef __USE_POSIX
|
||||
struct addrinfo *ai = NULL, hint;
|
||||
int rc;
|
||||
struct sockaddr_in * res_addr;
|
||||
struct sockaddr_in *res_addr;
|
||||
memset(&hint, 0, sizeof (hint));
|
||||
|
||||
hint.ai_socktype = SOCK_STREAM;
|
||||
@ -70,21 +71,26 @@ setipaddress(struct in_addr *a, char *p) {
|
||||
* 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_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",
|
||||
p,
|
||||
gai_strerror(rc));
|
||||
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
hint.ai_flags = AI_CANONNAME;
|
||||
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",
|
||||
p,
|
||||
gai_strerror(rc));
|
||||
@ -94,7 +100,8 @@ setipaddress(struct in_addr *a, char *p) {
|
||||
}
|
||||
|
||||
/* take the first one */
|
||||
if (ai != NULL) {
|
||||
if (ai != NULL)
|
||||
{
|
||||
res_addr = (struct sockaddr_in *)(ai->ai_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);
|
||||
spinlock_release(&tmplock);
|
||||
|
||||
if (h == NULL) {
|
||||
if ((a->s_addr = inet_addr(p)) == -1) {
|
||||
if (h == NULL)
|
||||
{
|
||||
if ((a->s_addr = inet_addr(p)) == -1)
|
||||
{
|
||||
MXS_ERROR("gethostbyname failed for [%s]", p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* take the first one */
|
||||
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
|
||||
* background.
|
||||
*/
|
||||
bool gw_daemonize(void) {
|
||||
bool gw_daemonize(void)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
if (pid < 0)
|
||||
{
|
||||
char errbuf[STRERROR_BUFLEN];
|
||||
fprintf(stderr, "fork() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (pid != 0) {
|
||||
if (pid != 0)
|
||||
{
|
||||
/* exit from main */
|
||||
return true;
|
||||
}
|
||||
|
||||
if (setsid() < 0) {
|
||||
if (setsid() < 0)
|
||||
{
|
||||
char errbuf[STRERROR_BUFLEN];
|
||||
fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||
exit(1);
|
||||
@ -169,10 +184,9 @@ bool gw_daemonize(void) {
|
||||
int
|
||||
parse_bindconfig(char *config, unsigned short def_port, struct sockaddr_in *addr)
|
||||
{
|
||||
char *port, buf[1024 + 1];
|
||||
short pnum;
|
||||
struct hostent *hp;
|
||||
|
||||
char *port, buf[1024 + 1];
|
||||
short pnum;
|
||||
struct hostent *hp;
|
||||
|
||||
strncpy(buf, config, 1024);
|
||||
port = strrchr(buf, ':');
|
||||
|
Reference in New Issue
Block a user