Make error hint from bind() failure more accurate
authorPeter Eisentraut
Wed, 25 Nov 2020 07:14:23 +0000 (08:14 +0100)
committerPeter Eisentraut
Wed, 25 Nov 2020 07:33:57 +0000 (08:33 +0100)
The hint "Is another postmaster already running ..." should only be
printed for errors that are really about something else already using
the address.  In other cases it is misleading.  So only show that hint
if errno == EADDRINUSE.

Also, since Unix-domain sockets in the file-system namespace never
report EADDRINUSE for an existing file (they would just overwrite it),
the part of the hint saying "If not, remove socket file \"%s\" and
retry." can never happen, so remove it.  Unix-domain sockets in the
abstract namespace can report EADDRINUSE, but in that case there is no
file to remove, so the hint doesn't work there either.

Reviewed-by: Michael Paquier
Discussion: https://www.postgresql.org/message-id/flat/6dee8574-b0ad-fc49-9c8c-2edc796f0033@2ndquadrant.com

src/backend/libpq/pqcomm.c

index a9a52d48f9127d70a528981f8942bd2a0bcbaa2a..64a351cedcb1920c08ddad9e6ca86eb2ae15f1ef 100644 (file)
@@ -530,18 +530,20 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
        err = bind(fd, addr->ai_addr, addr->ai_addrlen);
        if (err < 0)
        {
+           int         saved_errno = errno;
+
            ereport(LOG,
                    (errcode_for_socket_access(),
            /* translator: first %s is IPv4, IPv6, or Unix */
                     errmsg("could not bind %s address \"%s\": %m",
                            familyDesc, addrDesc),
-                    (IS_AF_UNIX(addr->ai_family)) ?
-                    errhint("Is another postmaster already running on port %d?"
-                            " If not, remove socket file \"%s\" and retry.",
-                            (int) portNumber, service) :
-                    errhint("Is another postmaster already running on port %d?"
-                            " If not, wait a few seconds and retry.",
-                            (int) portNumber)));
+                    saved_errno == EADDRINUSE ?
+                    (IS_AF_UNIX(addr->ai_family) ?
+                     errhint("Is another postmaster already running on port %d?",
+                             (int) portNumber) :
+                     errhint("Is another postmaster already running on port %d?"
+                             " If not, wait a few seconds and retry.",
+                             (int) portNumber)) : 0));
            closesocket(fd);
            continue;
        }