Ted Unangst <t...@tedunangst.com> writes:

> On Tue, Apr 30, 2013 at 22:11, Jérémie Courrèges-Anglas wrote:
>> Mark Kettenis <mark.kette...@xs4all.nl> writes:
>> 
>>>> From: j...@wxcvbn.org (=?utf-8?Q?J=C3=A9r=C3=A9mie_Courr=C3=A8ges-Anglas?=)
>>>> Date: Tue, 30 Apr 2013 20:53:36 +0200
>>>>
>>>>
>>>> New diff with .Pp suggested by jmc (which was ok otherwise - thanks).
>>>
>>> Sorry, but I don't think this is an improvement.
>> 
>> I guess you're not talking about the .Pp... if so, may I ask why?
>> 
>
> I think a much better improvement would be an example that shows
> correct handling. A man page that says "do this, only better" isn't
> very useful if you don't know what better is. Pick a sample from the
> src tree and use that.

I see, that makes sense.
- no addition to CAVEATS
In the example:
- include err.h
- ignore ECONNABORTED
- handle EINTR, EWOULDBLOCK, EMFILE and ENFILE
- add portability note about EAGAIN

I hope this sounds reasonable.

-- 
Jérémie Courrèges-Anglas
PGP Key fingerprint: 61DB D9A0 00A4 67CF 2A90  8961 6191 8FBF 06A1 1494

Index: accept.2
===================================================================
RCS file: /cvs/src/lib/libc/sys/accept.2,v
retrieving revision 1.25
diff -u -p -r1.25 accept.2
--- accept.2    21 Apr 2013 07:44:59 -0000      1.25
+++ accept.2    30 Apr 2013 23:19:12 -0000
@@ -134,20 +134,46 @@ The call returns \-1 on error.
 If it succeeds, it returns a non-negative integer that is a descriptor
 for the accepted socket.
 .Sh EXAMPLES
-The following code uses struct
+The following code checks errno and uses struct
 .Li sockaddr_storage
 to allocate enough space for the returned address:
 .Bd -literal -offset indent
 #include <sys/types.h>
 #include <sys/socket.h>
 
+#include <err.h>
+#include <errno.h>
+
 struct sockaddr_storage addr;
 socklen_t len = sizeof(addr);
 int retcode;
 
+retry:
 retcode = accept(s, (struct sockaddr *)&addr, &len);
-if (retcode == -1)
-       err(1, "accept");
+if (retcode == -1) {
+       switch (errno) {
+       case ECONNABORTED:
+               /* Ignore this error. */
+               goto retry;
+               /* NOTREACHED */
+       case EINTR:
+               /* Possibly check for received signals here. */
+               break;
+       case EMFILE:
+       case ENFILE:
+               /* You may gracefully handle fd starvation. */
+               break;
+       case EWOULDBLOCK:
+       case EAGAIN:
+               /*
+                * Portable code should check for both EAGAIN
+                * and EWOULDBLOCK
+                */
+               break;
+       default:
+               err(1, "accept");
+       }
+}
 .Ed
 .Sh ERRORS
 .Fn accept

Reply via email to