openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/plugins/imuxsock/imuxsock.c
static rsRetVal readSocket(lstn_t* pLstn)
{
    DEFiRet;
    int iRcvd;
    int iMaxLine;
    struct msghdr msgh;
    struct iovec msgiov;
    struct ucred cred;
    struct timeval ts;
    int cred_set = 0;
    int ts_set = 0;
    uchar bufRcv[4096 + 1];
    uchar* pRcv = NULL; /* receive buffer */
#ifdef HAVE_SCM_CREDENTIALS
    /* aux is a union rather than a direct char array to force alignment with
     * cmsghdr */
    union
    {
        char buf[128];
        struct cmsghdr cm;
    } aux;
#endif

    assert(pLstn->fd >= 0);

    iMaxLine = glbl.GetMaxLine();

    /* we optimize performance: if iMaxLine is below 4K (which it is in almost
     * all cases, we use a fixed buffer on the stack. Only if it is higher, heap
     * memory is used. We could use alloca() to achive a similar aspect, but
     * there are so many issues with alloca() that I do not want to take that
     * route. rgerhards, 2008-09-02
     */
    if ((size_t)iMaxLine < sizeof(bufRcv) - 1)
    {
        pRcv = bufRcv;
    }
    else
    {
        CHKmalloc(pRcv = (uchar*)malloc(iMaxLine + 1));
    }

    memset(&msgh, 0, sizeof(msgh));
    memset(&msgiov, 0, sizeof(msgiov));
#ifdef HAVE_SCM_CREDENTIALS
    if (pLstn->bUseCreds)
    {
        memset(&aux, 0, sizeof(aux));
        msgh.msg_control = &aux;
        msgh.msg_controllen = sizeof(aux);
    }
#endif
    msgiov.iov_base = (char*)pRcv;
    msgiov.iov_len = iMaxLine;
    msgh.msg_iov = &msgiov;
    msgh.msg_iovlen = 1;
/*  AIXPORT : MSG_DONTWAIT not supported */
#if defined(_AIX)
#define MSG_DONTWAIT MSG_NONBLOCK
#endif
    iRcvd = recvmsg(pLstn->fd, &msgh, MSG_DONTWAIT);

    DBGPRINTF("Message from UNIX socket: #%d, size %d\n", pLstn->fd,
              (int)iRcvd);
    if (iRcvd > 0)
    {
#if defined(HAVE_SCM_CREDENTIALS) || defined(HAVE_SO_TIMESTAMP)
        if (pLstn->bUseCreds)
        {
            struct cmsghdr* cm;
            for (cm = CMSG_FIRSTHDR(&msgh); cm; cm = CMSG_NXTHDR(&msgh, cm))
            {
#ifdef HAVE_SCM_CREDENTIALS
                if (pLstn->bUseCreds && cm->cmsg_level == SOL_SOCKET &&
                    cm->cmsg_type == SCM_CREDENTIALS)
                {
                    memcpy(&cred, CMSG_DATA(cm), sizeof(cred));
                    cred_set = 1;
                }
#endif /* HAVE_SCM_CREDENTIALS */
#if HAVE_SO_TIMESTAMP
                if (pLstn->bUseSysTimeStamp && cm->cmsg_level == SOL_SOCKET &&
                    cm->cmsg_type == SO_TIMESTAMP)
                {
                    memcpy(&ts, CMSG_DATA(cm), sizeof(ts));
                    ts_set = 1;
                }
#endif /* HAVE_SO_TIMESTAMP */
            }
        }
#endif /* defined(HAVE_SCM_CREDENTIALS) || defined(HAVE_SO_TIMESTAMP) */
        CHKiRet(SubmitMsg(pRcv, iRcvd, pLstn, (cred_set ? &cred : NULL),
                          (ts_set ? &ts : NULL)));
    }
    else if (iRcvd < 0 && errno != EINTR && errno != EAGAIN)
    {
        char errStr[1024];
        rs_strerror_r(errno, errStr, sizeof(errStr));
        DBGPRINTF("UNIX socket error: %d = %s.\n", errno, errStr);
        LogError(errno, NO_ERRCODE, "imuxsock: recvfrom UNIX");
    }

finalize_it:
    if (pRcv != NULL && (size_t)iMaxLine >= sizeof(bufRcv) - 1)
        free(pRcv);

    RETiRet;
}
/* This function receives data from a socket indicated to be ready
 * to receive and submits the message received for processing.
 * rgerhards, 2007-12-20
 * Interface changed so that this function is passed the array index
 * of the socket which is to be processed. This eases access to the
 * growing number of properties. -- rgerhards, 2008-08-01
 */
This function receives data from a socket indicated to be ready
to receive and submits the message received for processing.
rgerhards, 2007-12-20
Interface changed so that this function is passed the array index
of the socket which is to be processed. This eases access to the
growing number of properties.
This function receives data from a socket indicated to be ready to receive and submits the message received for processing . rgerhards 2007 - 12 - 20 Interface changed so that this function is passed the array index of the socket which is to be processed . This eases access to the growing number of properties .
static rsRetVal readSocket(lstn_t* pLstn)
{
    DEFiRet;
    int iRcvd;
    int iMaxLine;
    struct msghdr msgh;
    struct iovec msgiov;
    struct ucred cred;
    struct timeval ts;
    int cred_set = 0;
    int ts_set = 0;
    uchar bufRcv[4096 + 1];
    uchar* pRcv = NULL; 
#ifdef HAVE_SCM_CREDENTIALS
    union
    {
        char buf[128];
        struct cmsghdr cm;
    } aux;
#endif
    assert(pLstn->fd >= 0);
    iMaxLine = glbl.GetMaxLine();
    if ((size_t)iMaxLine < sizeof(bufRcv) - 1)
    {
        pRcv = bufRcv;
    }
    else
    {
        CHKmalloc(pRcv = (uchar*)malloc(iMaxLine + 1));
    }
    memset(&msgh, 0, sizeof(msgh));
    memset(&msgiov, 0, sizeof(msgiov));
#ifdef HAVE_SCM_CREDENTIALS
    if (pLstn->bUseCreds)
    {
        memset(&aux, 0, sizeof(aux));
        msgh.msg_control = &aux;
        msgh.msg_controllen = sizeof(aux);
    }
#endif
    msgiov.iov_base = (char*)pRcv;
    msgiov.iov_len = iMaxLine;
    msgh.msg_iov = &msgiov;
    msgh.msg_iovlen = 1;
#if defined(_AIX)
#define MSG_DONTWAIT MSG_NONBLOCK
#endif
    iRcvd = recvmsg(pLstn->fd, &msgh, MSG_DONTWAIT);
    DBGPRINTF("Message from UNIX socket: #%d, size %d\n", pLstn->fd,
              (int)iRcvd);
    if (iRcvd > 0)
    {
#if defined(HAVE_SCM_CREDENTIALS) || defined(HAVE_SO_TIMESTAMP)
        if (pLstn->bUseCreds)
        {
            struct cmsghdr* cm;
            for (cm = CMSG_FIRSTHDR(&msgh); cm; cm = CMSG_NXTHDR(&msgh, cm))
            {
#ifdef HAVE_SCM_CREDENTIALS
                if (pLstn->bUseCreds && cm->cmsg_level == SOL_SOCKET &&
                    cm->cmsg_type == SCM_CREDENTIALS)
                {
                    memcpy(&cred, CMSG_DATA(cm), sizeof(cred));
                    cred_set = 1;
                }
#endif 
#if HAVE_SO_TIMESTAMP
                if (pLstn->bUseSysTimeStamp && cm->cmsg_level == SOL_SOCKET &&
                    cm->cmsg_type == SO_TIMESTAMP)
                {
                    memcpy(&ts, CMSG_DATA(cm), sizeof(ts));
                    ts_set = 1;
                }
#endif 
            }
        }
#endif 
        CHKiRet(SubmitMsg(pRcv, iRcvd, pLstn, (cred_set ? &cred : NULL),
                          (ts_set ? &ts : NULL)));
    }
    else if (iRcvd < 0 && errno != EINTR && errno != EAGAIN)
    {
        char errStr[1024];
        rs_strerror_r(errno, errStr, sizeof(errStr));
        DBGPRINTF("UNIX socket error: %d = %s.\n", errno, errStr);
        LogError(errno, NO_ERRCODE, "imuxsock: recvfrom UNIX");
    }
finalize_it:
    if (pRcv != NULL && (size_t)iMaxLine >= sizeof(bufRcv) - 1)
        free(pRcv);
    RETiRet;
}
static rsRetVal readSocket ( lstn_t * pLstn ) { DEFiRet ; int iRcvd ; int iMaxLine ; struct msghdr msgh ; struct iovec msgiov ; struct ucred cred ; struct timeval ts ; int cred_set = 0 ; int ts_set = 0 ; uchar bufRcv [ 4096 + 1 ] ; uchar * pRcv = NULL ; #ifdef HAVE_SCM_CREDENTIALS union { char buf [ 128 ] ; struct cmsghdr cm ; } aux ; #endif assert ( pLstn -> fd >= 0 ) ; iMaxLine = glbl . GetMaxLine ( ) ; if ( ( size_t ) iMaxLine < sizeof ( bufRcv ) - 1 ) { pRcv = bufRcv ; } else { CHKmalloc ( pRcv = ( uchar * ) malloc ( iMaxLine + 1 ) ) ; } memset ( & msgh , 0 , sizeof ( msgh ) ) ; memset ( & msgiov , 0 , sizeof ( msgiov ) ) ; #ifdef HAVE_SCM_CREDENTIALS if ( pLstn -> bUseCreds ) { memset ( & aux , 0 , sizeof ( aux ) ) ; msgh . msg_control = & aux ; msgh . msg_controllen = sizeof ( aux ) ; } #endif msgiov . iov_base = ( char * ) pRcv ; msgiov . iov_len = iMaxLine ; msgh . msg_iov = & msgiov ; msgh . msg_iovlen = 1 ; #if defined ( _AIX ) #define MSG_DONTWAIT  MSG_NONBLOCK #endif iRcvd = recvmsg ( pLstn -> fd , & msgh , MSG_DONTWAIT ) ; DBGPRINTF ( " \n " , pLstn -> fd , ( int ) iRcvd ) ; if ( iRcvd > 0 ) { #if defined ( HAVE_SCM_CREDENTIALS ) || defined ( HAVE_SO_TIMESTAMP ) if ( pLstn -> bUseCreds ) { struct cmsghdr * cm ; for ( cm = CMSG_FIRSTHDR ( & msgh ) ; cm ; cm = CMSG_NXTHDR ( & msgh , cm ) ) { #ifdef HAVE_SCM_CREDENTIALS if ( pLstn -> bUseCreds && cm -> cmsg_level == SOL_SOCKET && cm -> cmsg_type == SCM_CREDENTIALS ) { memcpy ( & cred , CMSG_DATA ( cm ) , sizeof ( cred ) ) ; cred_set = 1 ; } #endif #if HAVE_SO_TIMESTAMP if ( pLstn -> bUseSysTimeStamp && cm -> cmsg_level == SOL_SOCKET && cm -> cmsg_type == SO_TIMESTAMP ) { memcpy ( & ts , CMSG_DATA ( cm ) , sizeof ( ts ) ) ; ts_set = 1 ; } #endif } } #endif CHKiRet ( SubmitMsg ( pRcv , iRcvd , pLstn , ( cred_set ? & cred : NULL ) , ( ts_set ? & ts : NULL ) ) ) ; } else if ( iRcvd < 0 && errno != EINTR && errno != EAGAIN ) { char errStr [ 1024 ] ; rs_strerror_r ( errno , errStr , sizeof ( errStr ) ) ; DBGPRINTF ( " \n " , errno , errStr ) ; LogError ( errno , NO_ERRCODE , " " ) ; } finalize_it : if ( pRcv != NULL && ( size_t ) iMaxLine >= sizeof ( bufRcv ) - 1 ) free ( pRcv ) ; RETiRet ; }
This function receives data from a socket indicated to be ready
 to receive and submits the message received for processing.
This function receives data from a socket indicated to be ready to receive and submits the message received for processing .
/* receive buffer */ /* aux is a union rather than a direct char array to force alignment with
     * cmsghdr */ /* we optimize performance: if iMaxLine is below 4K (which it is in almost
     * all cases, we use a fixed buffer on the stack. Only if it is higher, heap
     * memory is used. We could use alloca() to achive a similar aspect, but
     * there are so many issues with alloca() that I do not want to take that
     * route. rgerhards, 2008-09-02
     */ /*  AIXPORT : MSG_DONTWAIT not supported */ /* HAVE_SCM_CREDENTIALS */ /* HAVE_SO_TIMESTAMP */ /* defined(HAVE_SCM_CREDENTIALS) || defined(HAVE_SO_TIMESTAMP) */