libcutils/mq.c
static void masterProxyAcceptConnection(PeerProxy* masterProxy) {
    struct msghdr msg;
    struct iovec iov[1];
    ssize_t size;
    char ignored;
    int incomingFd;
    
    // TODO: Reuse code which writes the connection. Who the heck designed
    // this API anyway?
    union {
        struct cmsghdr cm;
        char control[CMSG_SPACE(sizeof(int))];
    } control_un;
    struct cmsghdr *cmptr;
    msg.msg_control = control_un.control;
    msg.msg_controllen = sizeof(control_un.control);

    msg.msg_name = NULL;
    msg.msg_namelen = 0;

    // We sent 1 byte of data so we can detect EOF.
    iov[0].iov_base = &ignored;
    iov[0].iov_len = 1;
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;

    size = recvmsg(masterProxy->fd->fd, &msg, 0);
    if (size < 0) {
        if (errno == EINTR) {
            // Log interruptions but otherwise ignore them.
            LOGW("recvmsg() interrupted.");
            return;
        } else if (errno == EAGAIN) {
            // Keep waiting for the connection.
            return;
        } else {
            LOG_ALWAYS_FATAL("Error reading connection from master: %s",
                    strerror(errno));
        }
    } else if (size == 0) {
        // EOF.
        LOG_ALWAYS_FATAL("Received EOF from master.");
    }

    // Extract fd from message.
    if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL 
            && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
        if (cmptr->cmsg_level != SOL_SOCKET) {
            LOG_ALWAYS_FATAL("Expected SOL_SOCKET.");
        }
        if (cmptr->cmsg_type != SCM_RIGHTS) {
            LOG_ALWAYS_FATAL("Expected SCM_RIGHTS.");
        }
        incomingFd = *((int*) CMSG_DATA(cmptr));
    } else {
        LOG_ALWAYS_FATAL("Expected fd.");
    }
    
    // The peer proxy this connection is for.
    PeerProxy* peerProxy = masterProxy->connecting;
    if (peerProxy == NULL) {
        LOGW("Received connection for unknown peer.");
        closeWithWarning(incomingFd);
    } else {
        Peer* peer = masterProxy->peer;
        
        SelectableFd* selectableFd = selectorAdd(peer->selector, incomingFd);
        if (selectableFd == NULL) {
            LOGW("Error adding fd to selector for %d.",
                    peerProxy->credentials.pid);
            closeWithWarning(incomingFd);
            peerProxyKill(peerProxy, false);
        }

        peerProxySetFd(peerProxy, selectableFd);
    }
    
    peerProxyExpectHeader(masterProxy);
}
/**
 * Accepts a connection sent by the master proxy.
 */
Accepts a connection sent by the master proxy.
Accepts a connection sent by the master proxy .
static void masterProxyAcceptConnection(PeerProxy* masterProxy) {
    struct msghdr msg;
    struct iovec iov[1];
    ssize_t size;
    char ignored;
    int incomingFd;
    union {
        struct cmsghdr cm;
        char control[CMSG_SPACE(sizeof(int))];
    } control_un;
    struct cmsghdr *cmptr;
    msg.msg_control = control_un.control;
    msg.msg_controllen = sizeof(control_un.control);
    msg.msg_name = NULL;
    msg.msg_namelen = 0;
    iov[0].iov_base = &ignored;
    iov[0].iov_len = 1;
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    size = recvmsg(masterProxy->fd->fd, &msg, 0);
    if (size < 0) {
        if (errno == EINTR) {
            LOGW("recvmsg() interrupted.");
            return;
        } else if (errno == EAGAIN) {
            return;
        } else {
            LOG_ALWAYS_FATAL("Error reading connection from master: %s",
                    strerror(errno));
        }
    } else if (size == 0) {
        LOG_ALWAYS_FATAL("Received EOF from master.");
    }
    if ((cmptr = CMSG_FIRSTHDR(&msg)) != NULL 
            && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
        if (cmptr->cmsg_level != SOL_SOCKET) {
            LOG_ALWAYS_FATAL("Expected SOL_SOCKET.");
        }
        if (cmptr->cmsg_type != SCM_RIGHTS) {
            LOG_ALWAYS_FATAL("Expected SCM_RIGHTS.");
        }
        incomingFd = *((int*) CMSG_DATA(cmptr));
    } else {
        LOG_ALWAYS_FATAL("Expected fd.");
    }
    PeerProxy* peerProxy = masterProxy->connecting;
    if (peerProxy == NULL) {
        LOGW("Received connection for unknown peer.");
        closeWithWarning(incomingFd);
    } else {
        Peer* peer = masterProxy->peer;
        SelectableFd* selectableFd = selectorAdd(peer->selector, incomingFd);
        if (selectableFd == NULL) {
            LOGW("Error adding fd to selector for %d.",
                    peerProxy->credentials.pid);
            closeWithWarning(incomingFd);
            peerProxyKill(peerProxy, false);
        }
        peerProxySetFd(peerProxy, selectableFd);
    }
    peerProxyExpectHeader(masterProxy);
}
static void masterProxyAcceptConnection ( PeerProxy * masterProxy ) { struct msghdr msg ; struct iovec iov [ 1 ] ; ssize_t size ; char ignored ; int incomingFd ; union { struct cmsghdr cm ; char control [ CMSG_SPACE ( sizeof ( int ) ) ] ; } control_un ; struct cmsghdr * cmptr ; msg . msg_control = control_un . control ; msg . msg_controllen = sizeof ( control_un . control ) ; msg . msg_name = NULL ; msg . msg_namelen = 0 ; iov [ 0 ] . iov_base = & ignored ; iov [ 0 ] . iov_len = 1 ; msg . msg_iov = iov ; msg . msg_iovlen = 1 ; size = recvmsg ( masterProxy -> fd -> fd , & msg , 0 ) ; if ( size < 0 ) { if ( errno == EINTR ) { LOGW ( " " ) ; return ; } else if ( errno == EAGAIN ) { return ; } else { LOG_ALWAYS_FATAL ( " " , strerror ( errno ) ) ; } } else if ( size == 0 ) { LOG_ALWAYS_FATAL ( " " ) ; } if ( ( cmptr = CMSG_FIRSTHDR ( & msg ) ) != NULL && cmptr -> cmsg_len == CMSG_LEN ( sizeof ( int ) ) ) { if ( cmptr -> cmsg_level != SOL_SOCKET ) { LOG_ALWAYS_FATAL ( " " ) ; } if ( cmptr -> cmsg_type != SCM_RIGHTS ) { LOG_ALWAYS_FATAL ( " " ) ; } incomingFd = * ( ( int * ) CMSG_DATA ( cmptr ) ) ; } else { LOG_ALWAYS_FATAL ( " " ) ; } PeerProxy * peerProxy = masterProxy -> connecting ; if ( peerProxy == NULL ) { LOGW ( " " ) ; closeWithWarning ( incomingFd ) ; } else { Peer * peer = masterProxy -> peer ; SelectableFd * selectableFd = selectorAdd ( peer -> selector , incomingFd ) ; if ( selectableFd == NULL ) { LOGW ( " " , peerProxy -> credentials . pid ) ; closeWithWarning ( incomingFd ) ; peerProxyKill ( peerProxy , false ) ; } peerProxySetFd ( peerProxy , selectableFd ) ; } peerProxyExpectHeader ( masterProxy ) ; }
Accepts a connection sent by the master proxy.
Accepts a connection sent by the master proxy .
// TODO: Reuse code which writes the connection. Who the heck designed // this API anyway? // We sent 1 byte of data so we can detect EOF. // Log interruptions but otherwise ignore them. // Keep waiting for the connection. // EOF. // Extract fd from message. // The peer proxy this connection is for.