Subject: [Beta-20.1] gethostbyname fails after fork.
From: [email-masked] (Andrew M. Bishop)

20 Dec 1998 15:25:29 -0800
Message-ID: <[email-masked]>
To: [email-masked]

A program of mine that works fine under UNIX (most types) does not
work using cygwin and Windows 95.

The failure comes when a name lookup is performed using
gethostbyname() after doing a fork() under a certain set of conditions
(other sockets open, inherited from before the fork).

The program that I am using as a test case is listed below:

-------------------- nslookup.c --------------------
#include <stdio.h>
#include <string.h>

#include <errno.h>
#include <unistd.h>

#include <netdb.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define HOST "gedanken.demon.co.uk"

int main()
{
 int serve,client,pid;
 int retval;
 struct sockaddr_in server;
 struct hostent* hp;
 int reuse_addr=1;

 serve=socket(PF_INET,SOCK_STREAM,0);
 if(serve==-1)
   {fprintf(stderr,"Cannot create server socket [%d].\n",errno);return(1);}

 setsockopt(serve,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(reuse_addr));

 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=htons(8001);

 retval=bind(serve,(struct sockaddr*)&server,sizeof(server));
 if(retval==-1)
   {fprintf(stderr,"Failed to bind server socket [%d].\n",errno);return(1);}

 listen(serve,4);

 hp=gethostbyname(HOST);
 if(!hp)
   {fprintf(stderr,"Unknown host (1) '%s' [%d].\n",HOST,h_errno);return(1);}
 else
   {
    memcpy((char*)&server.sin_addr,(char*)hp->h_addr,sizeof(server.sin_addr));
    printf("Host (1) '%s' is '%s'\n",HOST,inet_ntoa(server.sin_addr));
   }

 client=accept(serve,(struct sockaddr*)0,(int*)0);

 if(client==-1)
   {fprintf(stderr,"Accept on server socket failed [%d].\n",errno);return(1);}

 pid=fork();

 if(pid==0)
   {
    close(serve);

    hp=gethostbyname(HOST);
    if(!hp)
      {fprintf(stderr,"Unknown host (2) '%s' [%d].\n",HOST,h_errno);return(1);}
    else
      {
       memcpy((char*)&server.sin_addr,(char*)hp->h_addr,sizeof(server.sin_addr));
       printf("Host (2) '%s' is '%s'\n",HOST,inet_ntoa(server.sin_addr));
      }

    close(client);
   }
 else if(pid>0)
   {
    close(client);
    sleep(30);
    close(serve);
   }

 return(0);
}
-------------------- nslookup.c --------------------

I run this from bash under beta-20.1 (beta-20 + new cygwin1.dll).

nslookup > nslookup.log 2>&1

The first name lookup proceeds correctly:

-------------------- nslookup.log part 1 --------------------
[main] NSLOOKUP 1136 (0) cygwin_socket: 3 = socket (2, 1, 0)
[main] NSLOOKUP 1136 (0) cygwin_setsockopt: setsockopt optval=1
[main] NSLOOKUP 1136 (0) cygwin_setsockopt: 0 = setsockopt (3, 65535, 4 (SO_REUSEADDR), 253FD90, 4)
[main] NSLOOKUP 1136 (0) cygwin_bind: 0 = bind (3, 253FD98, 16)
[main] NSLOOKUP 1136 (0) cygwin_listen: 0 = listen (3, 4)
[main] NSLOOKUP 1136 (1) cygwin_gethostbyname: h_name gedanken.demon.co.uk
[main] NSLOOKUP 1136 (0) fhandler_disk_file::fstat: 1 = GetFileInformationByHandle (nslookup.log, 26)
[main] NSLOOKUP 1136 (0) fhandler_disk_file::fstat: 0 = fstat (, 0x253FA5C) st_atime=367C3E00 st_size=3767, st_mode=0x81A4, st_ino=629473249, sizeof=64
[main] NSLOOKUP 1136 (0) _fstat: 0 = fstat (1, 253FA5C)
[main] NSLOOKUP 1136 (0) _write: write (1, 0x25508F0, 52)
Host (1) 'gedanken.demon.co.uk' is '158.152.211.20'
-------------------- nslookup.log part 1 --------------------

The second name lookup does not even occur:

-------------------- nslookup.log part 2 --------------------
[main] NSLOOKUP 1138 (0) fork: 0 = fork()
[main] NSLOOKUP 1138 (0) _close: close (3)
[main] NSLOOKUP 1138 (0) __block_sig_dispatch: waiting for signal_mutex (0xE)
[main] NSLOOKUP 1138 (0) __release_signal_mutex: released for /home/noer/src/b20/comp-tools/devo/winsup/winsup.h:720
[main] NSLOOKUP 1138 (0) _close: 0 = close (3)
[main] NSLOOKUP 1138 (0) set_winsock_errno: unknown error 11004
[main] NSLOOKUP 1138 (0) _write: write (2, 0x253F644, 45)
Unknown host (2) 'gedanken.demon.co.uk' [4].
-------------------- nslookup.log part 2 --------------------

The whole log is available if required.

-- 
Andrew.
----------------------------------------------------------------------
Andrew M. Bishop                             [email-masked]
                                      http://www.gedanken.demon.co.uk/
-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".