fd passing between Independent processes using unix domain sockets

Hi,
I am having some error handling issues with and fd passed between Independent processes using unix domain sockets (On HPUX).

Here is the scnerio

Step 1: TPC/Client (connect()) ---Connects to ------TCP/Server(Gateway) (server gets fd)

Step 2: TPC/Server(Gateway) passes the fd to another process (Logic Server) to handle the request unsing unix domain sockets (sendmsg).

Step 3: Now the Logic Server (which got the fd) directly communicated with the clinet in TPC and does send()/recv() of the fd

� The TCP/server behaves as a gateway server and accepts the connection from the client.
� The fd is passed to the Logic Server process using UNIX domain sockets.
� The Logic Server process using this fd does send() and recv() and communicates with the client.
� The client now communicates directly with the server using TCP and this handover of the live connection is transparent to the client.
� The client send() request to the server and makes a blocking recv() for the response.

All this works fine.

Error Description

But, if due to some reason if the Logic Server dumps without sending any response to the client the client keeps hanging on the blocking recv().

But, as the connection between the server and client is using TPC, on the exit of the server the recv() in the client should return with error. This is not happening.

Instead if the TCP/server (which the client connected initially) is killed the recv() in the client side return with the error.

So, although the fd is passed to the server, the �association� of the clinet remains with the TCP/Server.

Code Fragment

Following code is used to pass the fd from the TCP/SERVER to the Server:

int snd_fd(int sockfd, void *ptr, size_t nbytes, int sendfd)
{
struct iovec iov[1];
struct msghdr msg;
int retval = 0;

    \#ifdef DEBUG
            printf \(" snd_fd :: cmd = %d \\n",*\(\(int *\)ptr\)\);
            printf \(" snd_fd :: sockfd =%d \\n",sockfd\);
            printf \(" snd_fd :: nbytes =%d \\n",nbytes\);
            printf \(" snd_fd :: sendfd =%d \\n",sendfd\);
    \#endif

    iov[0].iov_base = ptr;
    iov[0].iov_len = nbytes;
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    msg.msg_name = \(caddr_t\) 0;
    msg.msg_namelen = 0;
    if\(*\(\(int *\)ptr\) == GETTOWORK_CMD\)
    \{
            msg.msg_accrights = \(caddr_t\) &sendfd;
            msg.msg_accrightslen = sizeof\(int\);
    \}
    \#ifdef DEBUG
            printf\(" snd_fd :: Sending Msg\\n"\);
    \#endif

    retval = sendmsg \(sockfd, &msg, 0\);
    \#ifdef DEBUG
    if\(retval >= 0\)
            printf\("inside snd_fd, sendmsg success"\);
    else
            perror\("sendmsg failed:"\);
    \#endif

    return \(retval\);

}

After your TCP/Server passes the fd to the Logic Server, the TCP/Server must issue a close() on the fd. This will not perform a shutdown on the socket. Later when the Logic Server dies, the exit code will also close() the socket. This will be the final process with a pointer to the socket's file table entry. So this close() will trigger a shutdown on the socket.

Thanks a lot for the reply. It solved the problem.

Thanks.