From Peter.Mutsaers@mail.ing.nl  Mon Nov  9 07:45:26 1998
Received: from gatekeeper.ing.nl (gatekeeper.ing.nl [194.178.239.2])
          by hub.freebsd.org (8.8.8/8.8.8) with SMTP id HAA17471
          for <freebsd-gnats-submit@freebsd.org>; Mon, 9 Nov 1998 07:45:24 -0800 (PST)
          (envelope-from Peter.Mutsaers@mail.ing.nl)
Received: by ING-mailhub; id AA18562; Mon, 9 Nov 1998 16:43:57 +0100
Message-Id: <000AAC47.C21023@mail.ing.nl>
Date: Mon, 9 Nov 1998 15:58:23 +0000
From: Peter.Mutsaers@mail.ing.nl
To: freebsd-gnats-submit@freebsd.org
Subject: accept (2) errno bug

>Number:         8629
>Category:       kern
>Synopsis:       accept(2) errno uses incorrect code
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    nectar
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Nov  9 07:50:00 PST 1998
>Closed-Date:    Sun Nov 15 19:49:57 PST 1998
>Last-Modified:  Wed Nov 18 15:30:00 PST 1998
>Originator:     Peter Mutsaers
>Release:        FreeBSD 3.0-BETA i386
>Organization:
none
>Environment:

nothing special

>Description:

According to the man page (and on other UNIX systems) accept on a
socket which is not of type SOCK_STREAM (such as SOCK_DGRAM) should
yield an errno code of EOPNOTSUPP. Instead, -current sets errno to
EINVAL.

>How-To-Repeat:

call accept(2) on a socket of type SOCK_DGRAM

>Fix:
        
????

>Release-Note:
>Audit-Trail:

From: Jacques Vidrine <n@nectar.com>
To: freebsd-gnats-submit@freebsd.org, Peter.Mutsaers@mail.ing.nl
Cc:  Subject: Re: kern/8629: accept(2) errno uses incorrect code
Date: Fri, 13 Nov 1998 12:41:23 -0600

 [WS1995] Wright, Gary R. and Richard W. Stevens. TCP/IP Illustrated,
          Volume 2. Addison-Wesley Publishing Company, Inc., 1995.
 
 The man pages for several UNIX operating systems (at least BSD 4.4 Lite,
 FreeBSD 3.0, and SunOS 5.5.1]) indicate that if accept is called with
 a socket of type SOCK_DGRAM, that the error returned will be EOPNOTSUPP.
 
 However, studying the implementation of the accept system call in
 BSD 4.4 Lite and FreeBSD 3.0, and the implementation discussed in 
 [WS1995], accept returns EINVAL when called with a socket of type
 SOCK_DGRAM.  This is because accept returns EINVAL for any socket
 that has not had listen() called on it, as these code fragments 
 show.
 
 	#define SO_ACCEPTCONN   0x0002 /* socket has had listen() */
 
 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
 		splx(s);
 		return (EINVAL);
 	}
 
 In fact, listen returns EOPNOTSUPP when called with a socket of type
 SOCK_DGRAM.
 
 The accept(2) man page should be updated to reflect reality.
 
 --- accept.2    1998/01/11 17:07:20     1.5
 +++ accept.2    1998/11/13 18:37:09
 @@ -171,9 +171,9 @@
  The system file table is full.
  .It Bq Er ENOTSOCK
  The descriptor references a file, not a socket.
 -.It Bq Er EOPNOTSUPP
 -The referenced socket is not of type
 -.Dv SOCK_STREAM . 
 +.It Bq Er EINVAL
 +.Xr listen 2 
 +has not been called on the socket descriptor.
  .It Bq Er EFAULT
  The
  .Fa addr
 
 -- 
 Jacques Vidrine / n@nectar.com / nectar@FreeBSD.org
State-Changed-From-To: open->feedback 
State-Changed-By: nectar 
State-Changed-When: Fri Nov 13 10:55:23 PST 1998 
State-Changed-Why:  
A fix has been suggested. 


Responsible-Changed-From-To: freebsd-bugs->nectar 
Responsible-Changed-By: nectar 
Responsible-Changed-When: Fri Nov 13 10:55:23 PST 1998 
Responsible-Changed-Why:  
I provided analysis and fix. 
State-Changed-From-To: feedback->closed 
State-Changed-By: nectar 
State-Changed-When: Sun Nov 15 19:49:57 PST 1998 
State-Changed-Why:  
man page updated and committed. 

From: Peter.Mutsaers@mail.ing.nl
To: Jacques Vidrine <n@nectar.com>, freebsd-gnats-submit@freebsd.org
Cc:  Subject: RE: kern/8629: accept (2) errno uses incorrect code
Date: Mon, 16 Nov 1998 09:28:47 +0000

 On Friday, November 13, 1998 12:41, Jacques Vidrine <n@nectar.com>  wrote:
 > The man pages for several UNIX operating systems (at least BSD 4.4 Lite,
 > FreeBSD 3.0, and SunOS 5.5.1]) indicate that if accept is called with
 > a socket of type SOCK_DGRAM, that the error returned will be EOPNOTSUPP.
 > 
 > However, studying the implementation of the accept system call in
 > BSD 4.4 Lite and FreeBSD 3.0, and the implementation discussed in 
 > [WS1995], accept returns EINVAL when called with a socket of type
 > SOCK_DGRAM.  This is because accept returns EINVAL for any socket
 > that has not had listen() called on it, as these code fragments 
 > show.
 > 
 >         #define SO_ACCEPTCONN   0x0002 /* socket has had listen() */
 > 
 >         if ((so->so_options & SO_ACCEPTCONN) == 0) {
 >                 splx(s);
 >                 return (EINVAL);
 >         }
 > 
 > In fact, listen returns EOPNOTSUPP when called with a socket of type
 > SOCK_DGRAM.
 > 
 > The accept(2) man page should be updated to reflect reality.
 > 
 
 Hmm, would it not be better to update the kernel to match what is normal in
 other Unices? I had a program which used UDP and TCP sockets, and used the
 EOPNOTSUPP retval to keep them apart later. It worked on many different types of
 UNIX, but not on FreeBSD.

From: Jacques Vidrine <n@nectar.com>
To: Peter.Mutsaers@mail.ing.nl
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: kern/8629: accept (2) errno uses incorrect code 
Date: Mon, 16 Nov 1998 11:50:13 -0600

 -----BEGIN PGP SIGNED MESSAGE-----
 
 Supply me examples of other Unix systems that actually return EOPNOTSUPP
 in accept when passed a SOCK_DGRAM socket descriptor.  I can find none,
 and all documented implementations of BSD sockets I can find return EINVAL,
 not EOPNOTSUPP.
 
 Jacques Vidrine / n@nectar.com / nectar@FreeBSD.org
 
 On 16 November 1998 at 9:28, Peter.Mutsaers@mail.ing.nl wrote:
 > Hmm, would it not be better to update the kernel to match what is normal in
 > other Unices? I had a program which used UDP and TCP sockets, and used the
 > EOPNOTSUPP retval to keep them apart later. It worked on many different types
  of
 > UNIX, but not on FreeBSD.
 > 
 
 
 
 -----BEGIN PGP SIGNATURE-----
 Version: 2.6.2
 
 iQCVAwUBNlBl1TeRhT8JRySpAQGingP/TkRGjy1TDE7f2E1v9wure5Y/IuyoxCeq
 8Ly7bcyHnUUWjkl7pxchDsq0rmjK28/iZ2u2J7F3gVAiKif7xhjqPNf88Z+i6J+o
 lhYlj82PyA0aq/zKLp6o9CuLLVTmD4mL8/QM2QgdUDrOgTHQUSljA0VF/oAKIcUC
 tq8lhu/z7Ko=
 =rkg1
 -----END PGP SIGNATURE-----

From: Peter.Mutsaers@mail.ing.nl
To: Jacques Vidrine <n@nectar.com>
Cc: freebsd-gnats-submit@freebsd.org
Subject: RE: kern/8629: accept (2) errno uses incorrect code
Date: Tue, 17 Nov 1998 11:03:45 +0000

 On Monday, November 16, 1998 11:50, Jacques Vidrine <n@nectar.com>  wrote:
 > -----BEGIN PGP SIGNED MESSAGE-----
 > 
 > Supply me examples of other Unix systems that actually return EOPNOTSUPP
 > in accept when passed a SOCK_DGRAM socket descriptor.  I can find none,
 > and all documented implementations of BSD sockets I can find return EINVAL,
 > not EOPNOTSUPP.
 > 
 > Jacques Vidrine / n@nectar.com / nectar@FreeBSD.org
 
 Solaris 2.6 and Digital UNIX 4.0 do. I tested my program on them
 
 Output from a small program doing an accept on socket 69/udp (tftp), using
 perror to show the errno value:
 
 On FreeBSD:
 ~> a.out         
 69/udp
 accept: Invalid argument
 
 
 On Solaris 2.6:
 ~> ./a.out 
 69/udp
 accept: Operation not supported on transport endpoint
 
 
 On Digital UNIX 4.0:
 ~> a.out
 69/udp
 accept: Operation not supported on socket
 
 
 AIX 4.1 (does it wrong, like FreeBSD):
 ~> a.out
 69/udp
 accept: Invalid argument
 
 
 Fixed in AIX 4.2!!!:
 ~> a.out              
 69/udp
 accept: Operation not supported on socket
 
 
 Actually I should look it up in the POSIX spec but I don't have one at hand.
 However the number of Unices doing it like the original manpage, and the fact
 that AIX 4.1 --> 4.2 
 modified it to comply, convinces me that really it should return EOPNOTSUPP.
 
 
 Regards,
 
 Peter Mutsaers

From: Jacques Vidrine <n@nectar.com>
To: Cc:  Subject: Re: kern/8629: accept (2) errno uses incorrect code 
Date: Wed, 18 Nov 1998 17:25:33 -0600

 ------- Blind-Carbon-Copy
 
 X-Mailer: exmh version 2.0.2 2/24/98
 X-Exmh-Isig-CompType: repl
 X-Exmh-Isig-Folder: lists/freebsd
 X-PGP-RSAfprint: 00 F9 E6 A2 C5 4D 0A 76  26 8B 8B 57 73 D0 DE EE
 X-PGP-RSAkey: http://www.nectar.com/nectar-pgp262.txt
 From: Jacques Vidrine <n@nectar.com>
 In-reply-to: <000BCF69.C21023@mail.ing.nl> 
 References: <000BCF69.C21023@mail.ing.nl>
 Subject: Re: kern/8629: accept (2) errno uses incorrect code 
 To: Peter.Mutsaers@mail.ing.nl
 cc: hackers@freebsd.org
 Date: Wed, 18 Nov 1998 17:25:33 -0600
 Sender: nectar@spawn.nectar.com
 
 - -----BEGIN PGP SIGNED MESSAGE-----
 
 [sending to -hackers to solicit comments regarding PR 8629]
 
 On 17 November 1998 at 11:03, Peter.Mutsaers@mail.ing.nl wrote:
 
 [regarding operating systems which return EOPNOTSUPP from accept
  when called with a socket _not_ of type SOCK_STREAM]
 
 > Solaris 2.6 and Digital UNIX 4.0 do. I tested my program on them
 > 
 > Output from a small program doing an accept on socket 69/udp (tftp), using
 > perror to show the errno value:
 [summary from Peter's experience and mine:
 
 	Returns EINVAL			Returns EOPNOTSUPP
 	4.4 BSD Lite2
 	FreeBSD
 	NetBSD
 	OpenBSD
 	AIX 3.2.5
 	AIX 4.1
 	HP-UX 10.20
 	HP-UX 10.30
 	Linux 2.1.128
 					AIX 4.2
 					Solaris 2.6
 					Digital UNIX 4.0
 ]
 	
 > Actually I should look it up in the POSIX spec but I don't have one at hand.
 
 Yes, if POSIX has something to say about it, then that has to be
 definitive.
 
 > However the number of Unices doing it like the original manpage, and the fact
 > that AIX 4.1 --> 4.2 
 > modified it to comply, convinces me that really it should return EOPNOTSUPP.
 
 I'm far from convinced.  See above.  Additionally, the BSD and
 Linux accept() code is ``correct'' in that checking the type
 of the socket (and therefore returning EOPNOTSUPP) would be an
 additional, redundant check.   Checking whether or not the 
 socket is in a ``listening state'' is sufficient.
 
 So far it seems as if BSD implementations return EINVAL, while System
 V implementations return EOPNOTSUPP. The Linux implementation is
 homegrown, but returns EINVAL in this situation for the same reasons
 the BSD code does.  Since the sockets interface is defined by BSD, I
 expect that BSD is correct, and that the System V vendors got it
 wrong.  I can't blame them, though, since BSD has had the manual page
 wrong forever.
 
 Jacques Vidrine / n@nectar.com / nectar@FreeBSD.org
 
 - -----BEGIN PGP SIGNATURE-----
 Version: 2.6.2
 
 iQCVAwUBNlNXbTeRhT8JRySpAQGwrAQAsqrOv4AY6FcphYZIA92g4GjnZtUzHoH9
 DEdsWjc2hiN6vj9GvPmlbpfUObk/htLeS7ncBGSZouGkpjx/2Rpv0JjjTh35u1bI
 VtrEpkQVfcOgc0TWUu52WKeOcb8WExwZ+KPsoYzEIdU6Wz6FEP0c+iT6ri5v+d+w
 VWCoYVyqdig=
 =kcSM
 - -----END PGP SIGNATURE-----
 
 ------- End of Blind-Carbon-Copy
>Unformatted:
