LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   shutdown(2) after fork(2) behaviour changed or undefined? (http://www.linuxquestions.org/questions/linux-networking-3/shutdown-2-after-fork-2-behaviour-changed-or-undefined-342747/)

MScheffer 07-13-2005 05:46 AM

shutdown(2) after fork(2) behaviour changed or undefined?
 
Hi,

Is there a definition of how a Socket shutdown(2) operation should behave if there is still a child process that has the socket open as well?
Although not that pretty, code like the one below has been working as intended for years (AIX3, 4, Suse7). Under Fedora Core 3 (2.6.11-1.27 kernel), however, I noticed that the client process does no longer receive requests.

Any help is appreciated,

Mark.

Code:

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

main()
{
        int sok = do_listen (12345);
        if (fork())
                shutdown (sok, SHUT_RD);
        else
                sleep (120);  // Try connecting with e.g. telnet localhost 12345
}

int do_listen (int port)
{
        int    sok;
        struct  sockaddr_in    s;

        sok = socket (AF_INET, SOCK_STREAM, 0);

        s.sin_family = AF_INET;
        s.sin_port = htons (port);
        s.sin_addr.s_addr = INADDR_ANY;

        bind (sok, (struct sockaddr *)&s, sizeof(s));
        listen (sok, 5);

        return sok;
}


Matir 07-14-2005 04:44 PM

My guess would be that (since it is only a single socket) that SHOULD actually shut it down. I guess one would have to look at the POSIX spec to see how it's intended to work.

Is there a reason your code needs to do that? (Obviously the code above is just a sample)

MScheffer 07-15-2005 01:20 AM

Quote:

My guess would be that (since it is only a single socket) that SHOULD actually shut it down. I guess one would have to look at the POSIX spec to see how it's intended to work.
http://www.opengroup.org/onlinepubs/...html#tag_02_10 does not even mention the shutdown() operation. The only information on shutdown is available through http://www.opengroup.org/onlinepubs/...shutdown.html.

Quote:

Is there a reason your code needs to do that? (Obviously the code above is just a sample)
I can't find a reason. Either you manage the socket from the parent only or from the child process. The problem is that in the actual case the problem was hidden in libraries with an atexit() to clean up the sockets.... Took some effort to see what was going on (a fork() does not simplify debugging)
Fixing the problem took only a few seconds.

Matir 07-15-2005 09:11 AM

Well, I'm glad that you were able to fix it. I'm sorry if my post added to the confusion rather than helped to clear it up.


All times are GMT -5. The time now is 12:53 AM.