Browse Source

api: auto bind to first available port

when default port is used, else keep trying for one minute

This allow to auto-detect processes iterating 4068,69,70...
master
Tanguy Pruvot 10 years ago
parent
commit
c418eff4e9
  1. 24
      api.cpp

24
api.cpp

@ -794,14 +794,29 @@ static void api()
binderror = strerror(errno); binderror = strerror(errno);
if ((time(NULL) - bindstart) > 61) if ((time(NULL) - bindstart) > 61)
break; break;
else { else if (opt_api_listen == 4068) {
/* when port is default one, use first available */
if (opt_debug)
applog(LOG_DEBUG, "API bind to port %d failed, trying port %u",
port, (uint32_t) port+1);
port++;
serv.sin_port = htons(port);
sleep(1);
} else {
if (!opt_quiet || opt_debug) if (!opt_quiet || opt_debug)
applog(LOG_WARNING, "API bind to port %d failed - trying again in 20sec", port); applog(LOG_WARNING, "API bind to port %u failed - trying again in 20sec",
(uint32_t) port);
sleep(20); sleep(20);
} }
} }
else else {
bound = 1; bound = 1;
if (opt_api_listen != port) {
applog(LOG_WARNING, "API bind to port %d failed - using port %u",
opt_api_listen, (uint32_t) port);
opt_api_listen = port;
}
}
} }
if (bound == 0) { if (bound == 0) {
@ -824,7 +839,8 @@ static void api()
counter++; counter++;
clisiz = sizeof(cli); clisiz = sizeof(cli);
if (SOCKETFAIL(c = accept(*apisock, (struct sockaddr *)(&cli), &clisiz))) { c = accept(*apisock, (struct sockaddr*) (&cli), &clisiz);
if (SOCKETFAIL(c)) {
applog(LOG_ERR, "API failed (%s)%s", strerror(errno), UNAVAILABLE); applog(LOG_ERR, "API failed (%s)%s", strerror(errno), UNAVAILABLE);
CLOSESOCKET(*apisock); CLOSESOCKET(*apisock);
free(apisock); free(apisock);

Loading…
Cancel
Save