libcutils/mq.c
static void masterAcceptConnection(SelectableFd* listenerFd) {
    // Accept connection.
    int socket = accept(listenerFd->fd, NULL, NULL);
    if (socket == -1) {
        LOGW("accept() error: %s", strerror(errno));
        return;
    }
    
    LOGD("Accepted connection as fd %d.", socket);
    
    // Get credentials.
    Credentials credentials;
    struct ucred ucredentials;
    socklen_t credentialsSize = sizeof(struct ucred);
    int result = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, 
                &ucredentials, &credentialsSize);
    // We might want to verify credentialsSize.
    if (result == -1) {
        LOGW("getsockopt() error: %s", strerror(errno));
        closeWithWarning(socket);
        return;
    }

    // Copy values into our own structure so we know we have the types right.
    credentials.pid = ucredentials.pid;
    credentials.uid = ucredentials.uid;
    credentials.gid = ucredentials.gid;
    
    LOGI("Accepted connection from process %d.", credentials.pid);
   
    Peer* masterPeer = (Peer*) listenerFd->data;
    
    peerLock(masterPeer);
    
    // Make sure we don't already have a connection from that process.
    PeerProxy* peerProxy 
        = hashmapGet(masterPeer->peerProxies, &credentials.pid);
    if (peerProxy != NULL) {
        peerUnlock(masterPeer);
        LOGW("Alread connected to process %d.", credentials.pid);
        closeWithWarning(socket);
        return;
    }
   
    // Add connection to the selector.
    SelectableFd* socketFd = selectorAdd(masterPeer->selector, socket);
    if (socketFd == NULL) {
        peerUnlock(masterPeer);
        LOGW("malloc() failed.");
        closeWithWarning(socket);
        return;
    }

    // Create a peer proxy.
    peerProxy = peerProxyCreate(masterPeer, credentials);
    peerUnlock(masterPeer);
    if (peerProxy == NULL) {
        LOGW("malloc() failed.");
        socketFd->remove = true;
        closeWithWarning(socket);
    }
    peerProxy->connections = hashmapCreate(10, &pidHash, &pidEquals);
    peerProxySetFd(peerProxy, socketFd);
}
/** Accepts a connection to the master peer. */
Accepts a connection to the master peer.
Accepts a connection to the master peer .
static void masterAcceptConnection(SelectableFd* listenerFd) {
    int socket = accept(listenerFd->fd, NULL, NULL);
    if (socket == -1) {
        LOGW("accept() error: %s", strerror(errno));
        return;
    }
    LOGD("Accepted connection as fd %d.", socket);
    Credentials credentials;
    struct ucred ucredentials;
    socklen_t credentialsSize = sizeof(struct ucred);
    int result = getsockopt(socket, SOL_SOCKET, SO_PEERCRED, 
                &ucredentials, &credentialsSize);
    if (result == -1) {
        LOGW("getsockopt() error: %s", strerror(errno));
        closeWithWarning(socket);
        return;
    }
    credentials.pid = ucredentials.pid;
    credentials.uid = ucredentials.uid;
    credentials.gid = ucredentials.gid;
    LOGI("Accepted connection from process %d.", credentials.pid);
    Peer* masterPeer = (Peer*) listenerFd->data;
    peerLock(masterPeer);
    PeerProxy* peerProxy 
        = hashmapGet(masterPeer->peerProxies, &credentials.pid);
    if (peerProxy != NULL) {
        peerUnlock(masterPeer);
        LOGW("Alread connected to process %d.", credentials.pid);
        closeWithWarning(socket);
        return;
    }
    SelectableFd* socketFd = selectorAdd(masterPeer->selector, socket);
    if (socketFd == NULL) {
        peerUnlock(masterPeer);
        LOGW("malloc() failed.");
        closeWithWarning(socket);
        return;
    }
    peerProxy = peerProxyCreate(masterPeer, credentials);
    peerUnlock(masterPeer);
    if (peerProxy == NULL) {
        LOGW("malloc() failed.");
        socketFd->remove = true;
        closeWithWarning(socket);
    }
    peerProxy->connections = hashmapCreate(10, &pidHash, &pidEquals);
    peerProxySetFd(peerProxy, socketFd);
}
static void masterAcceptConnection ( SelectableFd * listenerFd ) { int socket = accept ( listenerFd -> fd , NULL , NULL ) ; if ( socket == -1 ) { LOGW ( " " , strerror ( errno ) ) ; return ; } LOGD ( " " , socket ) ; Credentials credentials ; struct ucred ucredentials ; socklen_t credentialsSize = sizeof ( struct ucred ) ; int result = getsockopt ( socket , SOL_SOCKET , SO_PEERCRED , & ucredentials , & credentialsSize ) ; if ( result == -1 ) { LOGW ( " " , strerror ( errno ) ) ; closeWithWarning ( socket ) ; return ; } credentials . pid = ucredentials . pid ; credentials . uid = ucredentials . uid ; credentials . gid = ucredentials . gid ; LOGI ( " " , credentials . pid ) ; Peer * masterPeer = ( Peer * ) listenerFd -> data ; peerLock ( masterPeer ) ; PeerProxy * peerProxy = hashmapGet ( masterPeer -> peerProxies , & credentials . pid ) ; if ( peerProxy != NULL ) { peerUnlock ( masterPeer ) ; LOGW ( " " , credentials . pid ) ; closeWithWarning ( socket ) ; return ; } SelectableFd * socketFd = selectorAdd ( masterPeer -> selector , socket ) ; if ( socketFd == NULL ) { peerUnlock ( masterPeer ) ; LOGW ( " " ) ; closeWithWarning ( socket ) ; return ; } peerProxy = peerProxyCreate ( masterPeer , credentials ) ; peerUnlock ( masterPeer ) ; if ( peerProxy == NULL ) { LOGW ( " " ) ; socketFd -> remove = true ; closeWithWarning ( socket ) ; } peerProxy -> connections = hashmapCreate ( 10 , & pidHash , & pidEquals ) ; peerProxySetFd ( peerProxy , socketFd ) ; }
Accepts a connection to the master peer.
Accepts a connection to the master peer .
// Accept connection. // Get credentials. // We might want to verify credentialsSize. // Copy values into our own structure so we know we have the types right. // Make sure we don't already have a connection from that process. // Add connection to the selector. // Create a peer proxy.