state-threads
[Top] [All Lists]

Re: strange code

To: Lev Walkin <vlm@xxxxxxxxx>
Subject: Re: strange code
From: Gene Shekhtman <gsh@xxxxxxxxxx>
Date: Thu, 25 Oct 2001 16:19:14 -0700
Cc: state-threads@xxxxxxxxxxx
Organization: Abeona Networks, Inc.
References: <200110251417.f9PEHo308203@xxxxxxxxxxxxxxxx>
Sender: owner-state-threads@xxxxxxxxxxx
Lev Walkin wrote:

> Hi there,
>
> The sources (io.c) has the following function:
>
> ===
>
> int st_connect(st_netfd_t *fd, struct sockaddr *addr, int addrlen,
>                st_utime_t timeout)
> {
>   int n, err = 0;
>
>   while (connect(fd->osfd, addr, addrlen) < 0) {
>     if (errno != EINTR) {
>       if (errno != EINPROGRESS && (errno != EADDRINUSE || err == 0))
>         return -1;
>       /* Wait until the socket becomes writable */
>       if (st_netfd_poll(fd, POLLOUT, timeout) < 0)
>         return -1;
>       /* Try to find out whether the connection setup succeeded or failed */
>       n = sizeof(int);
>       if (getsockopt(fd->osfd, SOL_SOCKET, SO_ERROR, (char *)&err,
>                      (socklen_t *)&n) < 0)
>         return -1;
>       if (err) {
>         errno = err;
>         return -1;
>       }
>       break;
>     }
>     err = 1;
>   }
>
>   return 0;
> }
>
> ===
>
> Can anybody explain the hidden meaning of
>
>         err = 1
>
> string?
>

Some platforms (e.g., IRIX 6.2, fixed in 6.5) have "peculiar"
implementation of connect(2).  On those platforms if connect(2) is
interrupted (errno == EINTR) after socket was bound by the kernel,
the second connect(2) attempt will fail with errno == EADDRINUSE.

The code above ignores EADDRINUSE iff connect(2) was previously
interrupted (the err flag was set).

--Gene



<Prev in Thread] Current Thread [Next in Thread>