Commit f9dfa5c9 authored by Tom Lane's avatar Tom Lane

Improve postmaster's logging of listen socket creation.

When one of the kernel calls in the socket()/bind()/listen() sequence
fails, include the specific address we're trying to bind to in the log
message.  This greatly eases debugging of network misconfigurations.

Also, after successfully setting up a listen socket, report its address
in the log, to ease verification that the expected addresses were bound.
There was some debate about whether to print this message at LOG level or
only DEBUG1, but the majority of votes were for the former.

Discussion: https://postgr.es/m/9564.1489091245@sss.pgh.pa.us
parent de752816
...@@ -464,9 +464,9 @@ su - postgres -c "/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgs ...@@ -464,9 +464,9 @@ su - postgres -c "/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgs
<para> <para>
<screen> <screen>
LOG: could not bind IPv4 socket: Address already in use LOG: could not bind IPv4 address "127.0.0.1": Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
FATAL: could not create TCP/IP listen socket FATAL: could not create any TCP/IP sockets
</screen> </screen>
This usually means just what it suggests: you tried to start This usually means just what it suggests: you tried to start
another server on the same port where one is already running. another server on the same port where one is already running.
...@@ -476,9 +476,9 @@ FATAL: could not create TCP/IP listen socket ...@@ -476,9 +476,9 @@ FATAL: could not create TCP/IP listen socket
on a reserved port number might draw something like: on a reserved port number might draw something like:
<screen> <screen>
$ <userinput>postgres -p 666</userinput> $ <userinput>postgres -p 666</userinput>
LOG: could not bind IPv4 socket: Permission denied LOG: could not bind IPv4 address "127.0.0.1": Permission denied
HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry. HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry.
FATAL: could not create TCP/IP listen socket FATAL: could not create any TCP/IP sockets
</screen> </screen>
</para> </para>
......
...@@ -319,6 +319,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -319,6 +319,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
char portNumberStr[32]; char portNumberStr[32];
const char *familyDesc; const char *familyDesc;
char familyDescBuf[64]; char familyDescBuf[64];
const char *addrDesc;
char addrBuf[NI_MAXHOST];
char *service; char *service;
struct addrinfo *addrs = NULL, struct addrinfo *addrs = NULL,
*addr; *addr;
...@@ -407,7 +409,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -407,7 +409,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
break; break;
} }
/* set up family name for possible error messages */ /* set up address family name for log messages */
switch (addr->ai_family) switch (addr->ai_family)
{ {
case AF_INET: case AF_INET:
...@@ -431,13 +433,28 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -431,13 +433,28 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
break; break;
} }
/* set up text form of address for log messages */
#ifdef HAVE_UNIX_SOCKETS
if (addr->ai_family == AF_UNIX)
addrDesc = unixSocketPath;
else
#endif
{
pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
addr->ai_addrlen,
addrBuf, sizeof(addrBuf),
NULL, 0,
NI_NUMERICHOST);
addrDesc = addrBuf;
}
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET) if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
{ {
ereport(LOG, ereport(LOG,
(errcode_for_socket_access(), (errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */ /* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not create %s socket: %m", errmsg("could not create %s socket for address \"%s\": %m",
familyDesc))); familyDesc, addrDesc)));
continue; continue;
} }
...@@ -461,7 +478,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -461,7 +478,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{ {
ereport(LOG, ereport(LOG,
(errcode_for_socket_access(), (errcode_for_socket_access(),
errmsg("setsockopt(SO_REUSEADDR) failed: %m"))); /* translator: first %s is IPv4, IPv6, or Unix */
errmsg("setsockopt(SO_REUSEADDR) failed for %s address \"%s\": %m",
familyDesc, addrDesc)));
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -476,7 +495,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -476,7 +495,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{ {
ereport(LOG, ereport(LOG,
(errcode_for_socket_access(), (errcode_for_socket_access(),
errmsg("setsockopt(IPV6_V6ONLY) failed: %m"))); /* translator: first %s is IPv4, IPv6, or Unix */
errmsg("setsockopt(IPV6_V6ONLY) failed for %s address \"%s\": %m",
familyDesc, addrDesc)));
closesocket(fd); closesocket(fd);
continue; continue;
} }
...@@ -494,9 +515,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -494,9 +515,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{ {
ereport(LOG, ereport(LOG,
(errcode_for_socket_access(), (errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */ /* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not bind %s socket: %m", errmsg("could not bind %s address \"%s\": %m",
familyDesc), familyDesc, addrDesc),
(IS_AF_UNIX(addr->ai_family)) ? (IS_AF_UNIX(addr->ai_family)) ?
errhint("Is another postmaster already running on port %d?" errhint("Is another postmaster already running on port %d?"
" If not, remove socket file \"%s\" and retry.", " If not, remove socket file \"%s\" and retry.",
...@@ -533,12 +554,18 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber, ...@@ -533,12 +554,18 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{ {
ereport(LOG, ereport(LOG,
(errcode_for_socket_access(), (errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */ /* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not listen on %s socket: %m", errmsg("could not listen on %s address \"%s\": %m",
familyDesc))); familyDesc, addrDesc)));
closesocket(fd); closesocket(fd);
continue; continue;
} }
ereport(LOG,
/* translator: first %s is IPv4, IPv6, or Unix */
(errmsg("listening on %s address \"%s\"",
familyDesc, addrDesc)));
ListenSocket[listen_index] = fd; ListenSocket[listen_index] = fd;
added++; added++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment