Thread: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
Kenji Sugita
Date:
It seems that a value of addr->ai_socktype returned by getaddrinfo in
pg_stat.c is not SOCK_DGRAM.


Kenji Sugita                                      



Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
Kurt Roeckx
Date:
On Thu, Jul 03, 2003 at 10:44:31AM +0900, Kenji Sugita wrote:
> 
> From: Kurt Roeckx <Q@ping.be>
> To: Kenji Sugita <sugita@srapc1327.sra.co.jp>
> Cc: pgsql-hackers@postgresql.org
> Date: Wed, 2 Jul 2003 19:20:11 +0200
> 
> ;;; What system are you running on, and does it have a getaddrinfo()
> ;;; or not?
> 
> Red Hat Linux 6.2 and Mac OS X 10.2.6.
[...]
> For Mac OS X it produces LOG:  PGSTAT: socket() failed: Protocol not supported.

This looks a little broken behaviour to me.  My guess is that it
returns an AF_INET6 socket but doesn't support it in the kernel.
In that case with the AI_ADDRCONFIG option it shouldn't have
returned that address.  The question is wether your getaddrinfo()
supports that option, and wether it's working or not.

We can fix this by going over all the returned addresses until
one of the socket() calls works.

We probably should also skip AF_UNIX sockets, since that might
not be want we want.

> $ postmaster (on Red Hat)
> ...
> 2003-07-03 10:19:38 [29761] LOG:  XX000: PGSTAT: getaddrinfo2() failed: Name or service not known

That is just evil.  It can't even resolv localhost?  Do you have
a localhost entry in /etc/hosts?

If that's not the problem I see no other way but to use
INADDR_LOOPBACK and in6addr_loopback directly instead, which I
really hate.

> What value shuld be passed to a following socket call with
> addr->ai_socktype?
> 
> ====  pgstats.c  ====
>         if ((pgStatSock = socket(addr->ai_family,
>                 addr->ai_socktype, addr->ai_protocol)) < 0)
>         {
>                 elog(LOG, "PGSTAT: socket() failed: %m");
>                 goto startup_failed;
>         }

The ai_family should be either AF_INET or AF_INET6, ai_socktype
should be SOCK_DGRAM, and ai_protocol 0 or IPPROTO_UDP.



Kurt



Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
qhwt@myrealbox.com
Date:
Hi,

On Wed, Jul 02, 2003 at 03:49:19PM +0900, Kenji Sugita wrote:
> It seems that a value of addr->ai_socktype returned by getaddrinfo in
> pg_stat.c is not SOCK_DGRAM.

Please try the following untested patch:

--- pgstat.c.orig    Thu Jun 12 16:36:51 2003
+++ pgstat.c        Mon Jul  7 00:34:50 2003
@@ -194,10 +194,12 @@            gai_strerror(ret));        goto startup_failed;    }
-    
-    if ((pgStatSock = socket(addr->ai_family,
-        addr->ai_socktype, addr->ai_protocol)) < 0)
-    {
+
+    for (; addr != NULL; addr = addr->ai_next)
+        if ((pgStatSock = socket(addr->ai_family,
+            addr->ai_socktype, addr->ai_protocol)) >= 0)
+            break;
+    if (pgStatSock < 0) {        elog(LOG, "PGSTAT: socket() failed: %m");        goto startup_failed;    }




Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
qhwt@myrealbox.com
Date:
On Mon, Jul 07, 2003 at 12:38:57AM +0900, qhwt@myrealbox.com wrote:
> Hi,
> 
> On Wed, Jul 02, 2003 at 03:49:19PM +0900, Kenji Sugita wrote:
> > It seems that a value of addr->ai_socktype returned by getaddrinfo in
> > pg_stat.c is not SOCK_DGRAM.
> 
> Please try the following untested patch:

No, please disregard the previous mail and try this one.

When hints.ai_family is PF_UNSPEC, getaddrinfo() returns two entries,
first one being IPv6 one, and the second one is IPv4 one, even if
IPv6 support is not compiled in the kernel(this is true at least on
my FreeBSD box).

--- pgstat.c    Thu Jun 12 16:36:51 2003
+++ pgstat.c    Mon Jul  7 00:49:07 2003
@@ -145,7 +145,7 @@pgstat_init(void){    ACCEPT_TYPE_ARG3    alen;
-    struct    addrinfo    *addr, hints;
+    struct    addrinfo    *addr0, addr, hints;    int            ret;    /*
@@ -187,17 +187,19 @@    hints.ai_addr = NULL;    hints.ai_canonname = NULL;    hints.ai_next = NULL;
-    ret = getaddrinfo2("localhost", NULL, &hints, &addr);
-    if (ret || !addr)
+    ret = getaddrinfo2("localhost", NULL, &hints, &addr0);
+    if (ret || !addr0)    {        elog(LOG, "PGSTAT: getaddrinfo2() failed: %s",            gai_strerror(ret));
goto startup_failed;    }
 
-    
-    if ((pgStatSock = socket(addr->ai_family,
-        addr->ai_socktype, addr->ai_protocol)) < 0)
-    {
+
+    for (addr = addr0; addr != NULL; addr = addr->ai_next)
+        if ((pgStatSock = socket(addr->ai_family,
+            addr->ai_socktype, addr->ai_protocol)) >= 0)
+            break;
+    if (pgStatSock < 0) {        elog(LOG, "PGSTAT: socket() failed: %m");        goto startup_failed;    }
@@ -211,8 +213,8 @@        elog(LOG, "PGSTAT: bind() failed: %m");        goto startup_failed;    }
-    freeaddrinfo2(hints.ai_family, addr);
-    addr = NULL;
+    freeaddrinfo2(hints.ai_family, addr0);
+    addr0 = addr = NULL;    alen = sizeof(pgStatAddr);    if (getsockname(pgStatSock, (struct sockaddr *)&pgStatAddr,
&alen)< 0)
 




Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
Kurt Roeckx
Date:
On Mon, Jul 07, 2003 at 12:38:57AM +0900, qhwt@myrealbox.com wrote:
>
> On Wed, Jul 02, 2003 at 03:49:19PM +0900, Kenji Sugita wrote:
> > It seems that a value of addr->ai_socktype returned by getaddrinfo in
> > pg_stat.c is not SOCK_DGRAM.
>
> Please try the following untested patch:
[...]

> +    for (; addr != NULL; addr = addr->ai_next)
> +        if ((pgStatSock = socket(addr->ai_family,
> +            addr->ai_socktype, addr->ai_protocol)) >= 0)
> +            break;

This will break.  You should use a pointer to the addr, and go
over the list using that.

freeaddrinfo() needs to have the original addr back.

He seems to have 2 problems:
- On Mac OS X getaddrinfo() returns something it shouldn't.
  A patch like that could fix it.
- On Linux Redhat 6.2 (and 6.0) getaddrinfo() seems to have a
  problem with AI_ADDRCONFIG.  Which is also used in
  StreamServerPort().  So he won't be able to listen to any
  socket there either.

Not being able to use AI_ADDRCONFIG is rather annoying, but I
guess if we just try all the returned addresses until 1 works
will have to do.

Try the attached patch instead.


Kurt


Attachment

Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
Kurt Roeckx
Date:
On Mon, Jul 07, 2003 at 12:55:06AM +0900, qhwt@myrealbox.com wrote:
> 
> When hints.ai_family is PF_UNSPEC, getaddrinfo() returns two entries,
> first one being IPv6 one, and the second one is IPv4 one, even if
> IPv6 support is not compiled in the kernel(this is true at least on
> my FreeBSD box).

And that is why you have AI_ADDRCONFIG, which seems to be broken
for him.


Kurt



Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
Kurt Roeckx
Date:
On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
> Try the attached patch instead.

Oops, that one was a little broken. I change it.

Try the attached one instead.


Kurt


Attachment

Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Kenji Sugita
Date:
From: Kurt Roeckx <Q@ping.be>
Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument
Date: Sun, 6 Jul 2003 18:44:35 +0200

;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
;;; > Try the attached patch instead.
;;; 
;;; Oops, that one was a little broken. I change it.
;;; 
;;; Try the attached one instead.

I tried the newest one.
   ====  Red Hat 6.2   $ postmaster   # Waiting forever
   $ top    11:24am  up 276 days, 14:12,  2 users,  load average: 0.90, 0.66, 0.35   46 processes: 44 sleeping, 2
running,0 zombie, 0 stopped   CPU states: 49.6% user,  0.8% system,  0.0% nice, 49.5% idle   Mem:  2009220K av,
1544312Kused,  464908K free,   37792K shrd, 1402556K buff   Swap:  265064K av,    4024K used,  261040K free
     78444K cached
 
     PID USER     PRI  NI  SIZE  RSS SHARE STAT  LIB %CPU %MEM   TIME COMMAND   12074 sugita    10   0  1668 1668  1328
R      0 99.2  0.0   1:00 postmaster   ..
 
   ====  Mac OS X 10.2.6   $ postmaster   2003-07-08 11:48:32 [1512] LOG:  PGSTAT: socket() failed: Protocol not
supported


Kenji Sugita                                      


Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Kurt Roeckx
Date:
On Tue, Jul 08, 2003 at 12:34:19PM +0900, Kenji Sugita wrote:
> From: Kurt Roeckx <Q@ping.be>
> Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument
> Date: Sun, 6 Jul 2003 18:44:35 +0200
>
> ;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
> ;;; > Try the attached patch instead.
> ;;;
> ;;; Oops, that one was a little broken. I change it.
> ;;;
> ;;; Try the attached one instead.
>
> I tried the newest one.

I did some investigation on a redhat 6.0.  It seems getaddrinfo
is really broken.

It returns an AF_INET socket, but it doesn't say it should be of
socket type SOCK_DGRAM.  It also returns an AF_UNIX socket and
for that it sets the socktype correct, but we don't want to use
the AF_UNIX one.

I'm going to assume that Mac Os X is broken in the same way.

Here is a new patch.


Kurt


Attachment

Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Kenji Sugita
Date:
From: Kurt Roeckx <Q@ping.be>
Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:
Date: Tue, 8 Jul 2003 22:25:44 +0200

;;; > ;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
;;; > ;;; > Try the attached patch instead.
;;; > ;;; 
;;; > ;;; Oops, that one was a little broken. I change it.
;;; > ;;; 
;;; > ;;; Try the attached one instead.
;;; > 
;;; > I tried the newest one.
;;; 
;;; I did some investigation on a redhat 6.0.  It seems getaddrinfo
;;; is really broken.

----------------------------------------------------------------

;;; It returns an AF_INET socket, but it doesn't say it should be of
;;; socket type SOCK_DGRAM.  

I pointed it in the first post. Rturned type is not SOCK_DGRAM.

;;;                  It also returns an AF_UNIX socket and
;;; for that it sets the socktype correct, but we don't want to use
;;; the AF_UNIX one.
;;; 
;;; I'm going to assume that Mac Os X is broken in the same way.
;;; 
;;; Here is a new patch.

It works on both platforms.

Thanks.


Kenji Sugita                                      



Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument

From
qhwt@myrealbox.com
Date:
Hello,

> On Mon, Jul 07, 2003 at 12:55:06AM +0900, qhwt@myrealbox.com wrote:
> > 
> > When hints.ai_family is PF_UNSPEC, getaddrinfo() returns two entries,
> > first one being IPv6 one, and the second one is IPv4 one, even if
> > IPv6 support is not compiled in the kernel(this is true at least on
> > my FreeBSD box).

Sorry, this turned out to be bogus; AI_ADDRCONFIG flag was already present as
early as FreeBSD 4.3-RELEASE, but I forgot adding the flag in my test code.
The flag is not documented in the getaddrinfo(3) man page.

On Sun, Jul 06, 2003 at 06:35:52PM +0200, Kurt Roeckx wrote:
> And that is why you have AI_ADDRCONFIG, which seems to be broken
> for him.

I doubt RedHat 6 or MacOS X really support AI_ADDRCONFIG. Is it actually
defined somewhere in the system headers? Anyway it seems like its absence
is hidden by the following lines:

src/include/libpq/pqcomm.h:
85-/* Some systems don't have it, so default it to 0 so it doesn't
86- * have any effect on those systems. */
87:#ifndef      AI_ADDRCONFIG
88:#define      AI_ADDRCONFIG 0
89-#endif


Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Kenji Sugita
Date:
From: Kurt Roeckx <Q@ping.be>
Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument
Date: Sun, 6 Jul 2003 18:44:35 +0200

;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
;;; > Try the attached patch instead.
;;; 
;;; Oops, that one was a little broken. I change it.
;;; 
;;; Try the attached one instead.

I tried the newest one.
   ====  Red Hat 6.2   $ postmaster   # Waiting forever
   $ top    11:24am  up 276 days, 14:12,  2 users,  load average: 0.90, 0.66, 0.35   46 processes: 44 sleeping, 2
running,0 zombie, 0 stopped   CPU states: 49.6% user,  0.8% system,  0.0% nice, 49.5% idle   Mem:  2009220K av,
1544312Kused,  464908K free,   37792K shrd, 1402556K buff   Swap:  265064K av,    4024K used,  261040K free
     78444K cached
 
     PID USER     PRI  NI  SIZE  RSS SHARE STAT  LIB %CPU %MEM   TIME COMMAND   12074 sugita    10   0  1668 1668  1328
R      0 99.2  0.0   1:00 postmaster   ..
 
   ====  Mac OS X 10.2.6   $ postmaster   2003-07-08 11:48:32 [1512] LOG:  PGSTAT: socket() failed: Protocol not
supported


Kenji Sugita                                      


Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Bruce Momjian
Date:
Your patch has been added to the PostgreSQL unapplied patches list at:
http://momjian.postgresql.org/cgi-bin/pgpatches

I will try to apply it within the next 48 hours.

---------------------------------------------------------------------------


Kurt Roeckx wrote:
> On Tue, Jul 08, 2003 at 12:34:19PM +0900, Kenji Sugita wrote:
> > From: Kurt Roeckx <Q@ping.be>
> > Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument
> > Date: Sun, 6 Jul 2003 18:44:35 +0200
> > 
> > ;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
> > ;;; > Try the attached patch instead.
> > ;;; 
> > ;;; Oops, that one was a little broken. I change it.
> > ;;; 
> > ;;; Try the attached one instead.
> > 
> > I tried the newest one.
> 
> I did some investigation on a redhat 6.0.  It seems getaddrinfo
> is really broken.
> 
> It returns an AF_INET socket, but it doesn't say it should be of
> socket type SOCK_DGRAM.  It also returns an AF_UNIX socket and
> for that it sets the socktype correct, but we don't want to use
> the AF_UNIX one.
> 
> I'm going to assume that Mac Os X is broken in the same way.
> 
> Here is a new patch.
> 
> 
> Kurt
> 

[ Attachment, skipping... ]

> 
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073
 


Re: PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed:

From
Bruce Momjian
Date:
Already applied by Tom.  Thanks.

---------------------------------------------------------------------------


Kurt Roeckx wrote:
> On Tue, Jul 08, 2003 at 12:34:19PM +0900, Kenji Sugita wrote:
> > From: Kurt Roeckx <Q@ping.be>
> > Subject: Re: [HACKERS] PostgreSQL 7.4devel - LOG: PGSTAT: socket() failed: Invalid argument
> > Date: Sun, 6 Jul 2003 18:44:35 +0200
> > 
> > ;;; On Sun, Jul 06, 2003 at 06:30:04PM +0200, Kurt Roeckx wrote:
> > ;;; > Try the attached patch instead.
> > ;;; 
> > ;;; Oops, that one was a little broken. I change it.
> > ;;; 
> > ;;; Try the attached one instead.
> > 
> > I tried the newest one.
> 
> I did some investigation on a redhat 6.0.  It seems getaddrinfo
> is really broken.
> 
> It returns an AF_INET socket, but it doesn't say it should be of
> socket type SOCK_DGRAM.  It also returns an AF_UNIX socket and
> for that it sets the socktype correct, but we don't want to use
> the AF_UNIX one.
> 
> I'm going to assume that Mac Os X is broken in the same way.
> 
> Here is a new patch.
> 
> 
> Kurt
> 

[ Attachment, skipping... ]

> 
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
359-1001+  If your life is a hard drive,     |  13 Roberts Road +  Christ can be your backup.        |  Newtown Square,
Pennsylvania19073