VirtualBox

Changeset 27288 in vbox


Ignore:
Timestamp:
Mar 11, 2010 4:37:17 PM (15 years ago)
Author:
vboxsync
Message:

r3/tcp.cpp: Mark all sockets as no-inherit. (TODO: windows)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/tcp.cpp

    r26915 r27288  
    4848# include <netdb.h>
    4949# include <unistd.h>
     50# include <fcntl.h>
    5051#endif /* !RT_OS_WINDOWS */
    5152#include <limits.h>
     
    158159static DECLCALLBACK(int)  rtTcpServerThread(RTTHREAD ThreadSelf, void *pvServer);
    159160static int  rtTcpServerListen(PRTTCPSERVER pServer);
    160 static int  rcTcpServerListenCleanup(PRTTCPSERVER pServer);
     161static int  rtTcpServerListenCleanup(PRTTCPSERVER pServer);
    161162static int  rtTcpServerDestroySocket(RTSOCKET volatile *pSockClient, const char *pszMsg);
    162163static int  rtTcpClose(RTSOCKET Sock, const char *pszMsg, bool fTryGracefulShutdown);
     
    217218            return VERR_UNRESOLVED_ERROR;
    218219    }
     220#endif
     221}
     222
     223
     224/**
     225 * Helper that ensures the correct inheritability of a socket.
     226 *
     227 * We're currently ignoring failures.
     228 *
     229 * @param   Socket          The socket handle.
     230 * @param   fInheritable    The desired inheritability state.
     231 */
     232static void rtTcpSetInheritance(RTSOCKET Socket, bool fInheritable)
     233{
     234#ifdef RT_OS_WINDOWS
     235    /** @todo Find some magic way to change this...  Worst case we have to use
     236     *        DuplicateHandle w/bInheritable=FALSE and replace the original
     237     *        handles... */
     238#else
     239    fcntl(Socket, F_SETFD, fInheritable ? 0 : FD_CLOEXEC);
    219240#endif
    220241}
     
    357378        rc = rtTcpServerListen(pServer);
    358379    else
    359         rc = rcTcpServerListenCleanup(pServer);
     380        rc = rtTcpServerListenCleanup(pServer);
    360381    RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
    361382    NOREF(ThreadSelf);
     
    425446    if (WaitSock != -1)
    426447    {
     448        rtTcpSetInheritance(WaitSock, false /*fInheritable*/);
     449
    427450        /*
    428451         * Set socket options.
     
    573596        if (    enmState != RTTCPSERVERSTATE_ACCEPTING
    574597            &&  enmState != RTTCPSERVERSTATE_SERVING)
    575             return rcTcpServerListenCleanup(pServer);
     598            return rtTcpServerListenCleanup(pServer);
    576599        if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, enmState))
    577600            continue;
     
    589612            /* These are typical for what can happen during destruction. */
    590613            if (errno == EBADF || errno == EINVAL || errno == ENOTSOCK)
    591                 return rcTcpServerListenCleanup(pServer);
     614                return rtTcpServerListenCleanup(pServer);
    592615#endif
    593616            continue;
    594617        }
     618        rtTcpSetInheritance(Socket, false /*fInheritable*/);
    595619
    596620        /*
     
    600624        {
    601625            rtTcpClose(Socket, "rtTcpServerListen", true /*fTryGracefulShutdown*/);
    602             return rcTcpServerListenCleanup(pServer);
     626            return rtTcpServerListenCleanup(pServer);
    603627        }
    604628        rtTcpAtomicXchgSock(&pServer->SockClient, Socket);
     
    622646            }
    623647            else
    624                 rcTcpServerListenCleanup(pServer); /* ignore rc */
     648                rtTcpServerListenCleanup(pServer); /* ignore rc */
    625649            return rc;
    626650        }
     
    632656 * Clean up after listener.
    633657 */
    634 static int rcTcpServerListenCleanup(PRTTCPSERVER pServer)
     658static int rtTcpServerListenCleanup(PRTTCPSERVER pServer)
    635659{
    636660    /*
     
    706730            &&  enmState != RTTCPSERVERSTATE_CREATED)
    707731        {
    708             rc = rcTcpServerListenCleanup(pServer);
     732            rc = rtTcpServerListenCleanup(pServer);
    709733            break;
    710734        }
     
    729753            rc = rtTcpError();
    730754            if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_CREATED, RTTCPSERVERSTATE_ACCEPTING))
    731                 rc = rcTcpServerListenCleanup(pServer);
     755                rc = rtTcpServerListenCleanup(pServer);
    732756            if (RT_FAILURE(rc))
    733757                break;
    734758            continue;
    735759        }
     760        rtTcpSetInheritance(Socket, false /*fInheritable*/);
    736761
    737762        /*
     
    740765        if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_SERVING, RTTCPSERVERSTATE_ACCEPTING))
    741766        {
     767            RTSOCKET OldSocket = rtTcpAtomicXchgSock(&pServer->SockClient, Socket);
     768            Assert(OldSocket == NIL_RTSOCKET); NOREF(OldSocket);
    742769            *pSockClient = Socket;
    743770            rc = VINF_SUCCESS;
     
    746773        {
    747774            rtTcpClose(Socket, "RTTcpServerListen2", true /*fTryGracefulShutdown*/);
    748             rc = rcTcpServerListenCleanup(pServer);
     775            rc = rtTcpServerListenCleanup(pServer);
    749776        }
    750777        break;
     
    11511178    if (Sock != -1)
    11521179    {
     1180        rtTcpSetInheritance(Sock, false /*fInheritable*/);
     1181
    11531182        struct sockaddr_in InAddr;
    11541183        RT_ZERO(InAddr);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette