(U) Failing sockets calls, bad errno with threads

(U) Failing sockets calls, bad errno with threads

Post by Neil Kolb » Sun, 10 Oct 1993 03:52:41



Folks,

I have recently installed Solaris 2.2 and find that if I compile and run the
following program, the errno returned from the bind() call is ALWAYS 0 even
when it returns -1 to signify an error.

This only happens when I link with the threads library.

Has anyone seen this before?

Cheers ....
Neil Kolban


Compile with the following:
cc -D_REENTRANT main.c -lnsl -lsocket -lthread

Run with

./a.out 23      /* Fails with errno = 0 */

--- program ---
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

main(int argc, char *argv]()
{
    struct sockaddr_in addr;
    int rc;
    int s;

    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv]1());
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    s = socket(AF_INET, SOCK_STREAM, 0);
    rc = bind(s, (struct sockaddr *)&addr, sizeof(addr));
    printf("rc=%d errno=%d\n", rc, errno);
    perror("bind()");

Quote:}

--- end program ---
 
 
 

(U) Failing sockets calls, bad errno with threads

Post by Casper H.S. D » Sun, 10 Oct 1993 22:34:09



>Folks,
>I have recently installed Solaris 2.2 and find that if I compile and run the
>following program, the errno returned from the bind() call is ALWAYS 0 even
>when it returns -1 to signify an error.
>This only happens when I link with the threads library.
>Has anyone seen this before?

No and in fact this shouldn't happen. It is a bug in the socket library
which has little or nothing to do with multi-threading.

If you link with -lthread, you'll pick up another version of
sigprocmask, the one from libthread. Its behaviour is slightly different
from the one in libc in that it may clear errno.

The code in bind that has the bug looks like this:

        if (ioctl( ......)) { /* ioctl that does the bind */
            sigprocmask(...)
            return -1;
        }

Of course, this code has a bug: it should save errno vefore calling
sigprocmask and restore it afterwards. (Unless sigprocmask is guaranteed
not to much with errno, in that case libthread's sigprocmask must
be fixed)

Casper

 
 
 

1. failed socket() call -- "Bad file number"

I am writing a webcrawler, under Linux 2.1.13.  After running for a while,
the webcrawler fails on a call to socket() with the error message "Bad file
number" (errno == 9).  At first I thought perhaps I wasn't closing the
sockets correctly when I was done with them, but doing a netstat -a shows
everything looks normal.  I tried looping with a sleep() if it failed,
but it never comes back.

My call to socket looks simply like:
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
I'm using connected sockets.  Maybe I could work around this by using
non-connected sockets, but I'd rather understand the problem and fix it.
Oh, and on my test case this even happens if I'm running a single thread.

Any ideas what's going wrong?

Please email or cc me directly.

Thanks.

- Charles

2. "YPBINDPROC_DOMAIN: Domain not bound" error

3. Unexpected errno from socket call on SPARC 1000 server

4. route, pppo3, weirdness

5. Sockets and threads, sockets and threads

6. How to learn XWindows programming?

7. system() call failing with rc=-1, errno=ECHILD

8. Help : PCMCIA modem setup

9. hdcd open call failed errno 6 port=0 in solaris 8

10. Server Socket Threads - Bad file number

11. <alarm call> systemmessage with threads and socket

12. threads and socket related system calls...

13. glibc mallinfo() call from threaded application fails