Skip to content

Commit

Permalink
Fix error message on conflicting ports while using '-l'
Browse files Browse the repository at this point in the history
Memcached can be started with configurable addresses to listen on,
using the "-l" option. Eg:
    $> ./memcached -l 127.0.2.1:22122

If the port 22122 is available, things run smoothly. However, if
the port is unavailable, we see the following error message:
    "failed to listen on TCP port 11211"

Note that the port number is incorrect and is the default port
number 11211 instead of the configured 22122.

This is because the server_sockets() function treats settings.port
and settings.inter differently. If "-l" is provided, settings.inter
contains the correct port number, whereas settings.port the default
port, and settings.port is used to print the error info, hence
printing the incorrect port number.

Since multiple interfaces can be provided with '-l', this patch prints
all of them on a bind error, to maintain code simplicity. The
server_sockets() function is also adjusted to remember the first bind
error's 'errno' that occurs, as the caller of server_sockets() relies
on the errno to print the error message.
  • Loading branch information
smukil authored and dormando committed Jan 25, 2022
1 parent 9c2a4bc commit 5ae69e9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,8 @@ static int server_sockets(int port, enum network_transport transport,
fprintf(stderr, "Failed to allocate memory for parsing server interface string\n");
return 1;
}
// If we encounter any failure, preserve the first errno for the caller.
int errno_save = 0;
for (char *p = strtok_r(list, ";,", &b);
p != NULL;
p = strtok_r(NULL, ";,", &b)) {
Expand Down Expand Up @@ -3700,8 +3702,10 @@ static int server_sockets(int port, enum network_transport transport,
p = NULL;
}
ret |= server_socket(p, the_port, transport, portnumber_file, ssl_enabled);
if (ret != 0 && errno_save == 0) errno_save = errno;
}
free(list);
errno = errno_save;
return ret;
}
}
Expand Down Expand Up @@ -6029,7 +6033,11 @@ int main (int argc, char **argv) {
errno = 0;
if (settings.port && server_sockets(settings.port, tcp_transport,
portnumber_file)) {
vperror("failed to listen on TCP port %d", settings.port);
if (settings.inter == NULL) {
vperror("failed to listen on TCP port %d", settings.port);
} else {
vperror("failed to listen on one of interface(s) %s", settings.inter);
}
exit(EX_OSERR);
}

Expand All @@ -6044,7 +6052,11 @@ int main (int argc, char **argv) {
errno = 0;
if (settings.udpport && server_sockets(settings.udpport, udp_transport,
portnumber_file)) {
vperror("failed to listen on UDP port %d", settings.udpport);
if (settings.inter == NULL) {
vperror("failed to listen on UDP port %d", settings.udpport);
} else {
vperror("failed to listen on one of interface(s) %s", settings.inter);
}
exit(EX_OSERR);
}

Expand Down

0 comments on commit 5ae69e9

Please sign in to comment.