[MLton] bug in MLton and bug in basis

Florian Weimer fw@deneb.enyo.de
Thu, 18 Aug 2005 16:51:27 +0200


* Wesley W. Terpstra:

>> Is this some ML-specific feature?  Usually, successful calls do not
>> set the errno variable.
>
> Maybe you misunderstood me..?
>
>     int status, n;
>     n = sizeof(status);
>     getsockopt(socket, SOL_SOCKET, SO_ERROR, (char*)&status,  
> (socklen_t*)&n);
>
> The result stored in 'status' is the value of 'errno' after the last
> call on the provided socket. For example, if you just saw a socket
> become writeable (after non-blocking connect), then 'status' will
> contain what 'errno' would have been if you had run a blocking
> connect.

Ah, okay.

Solaris returns the error code in errno, not in the passed integer, so
you need something like this:

int
half_duplex_handler::get_error()
{
  int error = 0;
  socklen_t len = sizeof(error);

  errno = 0;
  if (getsockopt(fd(), SOL_SOCKET, SO_ERROR, &error, &len) >= 0) {
    // Non-Solaris case: error is overwritten with the error code,
    // Nothing to do.
  } else {
    // Solaris case: errno has the error code.
    error = errno;
  }

  return error;
}