openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/util-linux/2.33.2-r0/util-linux-2.33.2/misc-utils/logger.c
static void write_output(struct logger_ctl* ctl, const char* const msg)
{
    struct iovec iov[4];
    int iovlen = 0;
    char* octet = NULL;

    /* initial connect failed? */
    if (!ctl->noact && !is_connected(ctl))
        logger_reopen(ctl);

    /* 1) octen count */
    if (ctl->octet_count)
    {
        size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg));
        iovec_add_string(iov, iovlen, octet, len);
    }

    /* 2) header */
    iovec_add_string(iov, iovlen, ctl->hdr, 0);

    /* 3) message */
    iovec_add_string(iov, iovlen, msg, 0);

    if (!ctl->noact && is_connected(ctl))
    {
        struct msghdr message = {0};
#ifdef SCM_CREDENTIALS
        struct cmsghdr* cmhp;
        struct ucred* cred;
        union
        {
            struct cmsghdr cmh;
            char control[CMSG_SPACE(sizeof(struct ucred))];
        } cbuf;
#endif

        /* 4) add extra \n to make sure message is terminated */
        if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
            iovec_add_string(iov, iovlen, "\n", 1);

        message.msg_iov = iov;
        message.msg_iovlen = iovlen;

#ifdef SCM_CREDENTIALS
        /* syslog/journald may follow local socket credentials rather
         * than in the message PID. If we use --id as root than we can
         * force kernel to accept another valid PID than the real logger(1)
         * PID.
         */
        if (ctl->pid && !ctl->server && ctl->pid != getpid() &&
            geteuid() == 0 && kill(ctl->pid, 0) == 0)
        {

            message.msg_control = cbuf.control;
            message.msg_controllen = CMSG_SPACE(sizeof(struct ucred));

            cmhp = CMSG_FIRSTHDR(&message);
            cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
            cmhp->cmsg_level = SOL_SOCKET;
            cmhp->cmsg_type = SCM_CREDENTIALS;
            cred = (struct ucred*)CMSG_DATA(cmhp);

            cred->pid = ctl->pid;
        }
#endif
        /* Note that logger(1) maybe executed for long time (as pipe
         * reader) and connection endpoint (syslogd) may be restarted.
         *
         * The libc syslog() function reconnects on failed send().
         * Let's do the same to be robust.    [kzak -- Oct 2017]
         *
         * MSG_NOSIGNAL is POSIX.1-2008 compatible, but it for example
         * not supported by apple-darwin15.6.0.
         */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
        if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0)
        {
            logger_reopen(ctl);
            if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0)
                warn(_("send message failed"));
        }
    }

    if (ctl->stderr_printout)
    {
        /* make sure it's terminated for stderr */
        if (iovec_memcmp(iov, iovlen, "\n", 1) != 0)
            iovec_add_string(iov, iovlen, "\n", 1);

        ignore_result(writev(STDERR_FILENO, iov, iovlen));
    }

    free(octet);
}
/* writes generated buffer to desired destination. For TCP syslog,
 * we use RFC6587 octet-stuffing (unless octet-counting is selected).
 * This is not great, but doing full blown RFC5425 (TLS) looks like
 * it is too much for the logger utility. If octet-counting is
 * selected, we use that.
 */
writes generated buffer to desired destination. For TCP syslog,
we use RFC6587 octet-stuffing (unless octet-counting is selected).
This is not great, but doing full blown RFC5425 (TLS) looks like
it is too much for the logger utility. If octet-counting is
selected, we use that.
writes generated buffer to desired destination . For TCP syslog we use RFC6587 octet - stuffing ( unless octet - counting is selected ) . This is not great but doing full blown RFC5425 ( TLS ) looks like it is too much for the logger utility . If octet - counting is selected we use that .
static void write_output(struct logger_ctl* ctl, const char* const msg)
{
    struct iovec iov[4];
    int iovlen = 0;
    char* octet = NULL;
    if (!ctl->noact && !is_connected(ctl))
        logger_reopen(ctl);
    if (ctl->octet_count)
    {
        size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg));
        iovec_add_string(iov, iovlen, octet, len);
    }
    iovec_add_string(iov, iovlen, ctl->hdr, 0);
    iovec_add_string(iov, iovlen, msg, 0);
    if (!ctl->noact && is_connected(ctl))
    {
        struct msghdr message = {0};
#ifdef SCM_CREDENTIALS
        struct cmsghdr* cmhp;
        struct ucred* cred;
        union
        {
            struct cmsghdr cmh;
            char control[CMSG_SPACE(sizeof(struct ucred))];
        } cbuf;
#endif
        if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
            iovec_add_string(iov, iovlen, "\n", 1);
        message.msg_iov = iov;
        message.msg_iovlen = iovlen;
#ifdef SCM_CREDENTIALS
        if (ctl->pid && !ctl->server && ctl->pid != getpid() &&
            geteuid() == 0 && kill(ctl->pid, 0) == 0)
        {
            message.msg_control = cbuf.control;
            message.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
            cmhp = CMSG_FIRSTHDR(&message);
            cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
            cmhp->cmsg_level = SOL_SOCKET;
            cmhp->cmsg_type = SCM_CREDENTIALS;
            cred = (struct ucred*)CMSG_DATA(cmhp);
            cred->pid = ctl->pid;
        }
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
        if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0)
        {
            logger_reopen(ctl);
            if (sendmsg(ctl->fd, &message, MSG_NOSIGNAL) < 0)
                warn(_("send message failed"));
        }
    }
    if (ctl->stderr_printout)
    {
        if (iovec_memcmp(iov, iovlen, "\n", 1) != 0)
            iovec_add_string(iov, iovlen, "\n", 1);
        ignore_result(writev(STDERR_FILENO, iov, iovlen));
    }
    free(octet);
}
static void write_output ( struct logger_ctl * ctl , const char * const msg ) { struct iovec iov [ 4 ] ; int iovlen = 0 ; char * octet = NULL ; if ( ! ctl -> noact && ! is_connected ( ctl ) ) logger_reopen ( ctl ) ; if ( ctl -> octet_count ) { size_t len = xasprintf ( & octet , " " , strlen ( ctl -> hdr ) + strlen ( msg ) ) ; iovec_add_string ( iov , iovlen , octet , len ) ; } iovec_add_string ( iov , iovlen , ctl -> hdr , 0 ) ; iovec_add_string ( iov , iovlen , msg , 0 ) ; if ( ! ctl -> noact && is_connected ( ctl ) ) { struct msghdr message = { 0 } ; #ifdef SCM_CREDENTIALS struct cmsghdr * cmhp ; struct ucred * cred ; union { struct cmsghdr cmh ; char control [ CMSG_SPACE ( sizeof ( struct ucred ) ) ] ; } cbuf ; #endif if ( ( ctl -> socket_type == TYPE_TCP ) && ! ctl -> octet_count ) iovec_add_string ( iov , iovlen , " \n " , 1 ) ; message . msg_iov = iov ; message . msg_iovlen = iovlen ; #ifdef SCM_CREDENTIALS if ( ctl -> pid && ! ctl -> server && ctl -> pid != getpid ( ) && geteuid ( ) == 0 && kill ( ctl -> pid , 0 ) == 0 ) { message . msg_control = cbuf . control ; message . msg_controllen = CMSG_SPACE ( sizeof ( struct ucred ) ) ; cmhp = CMSG_FIRSTHDR ( & message ) ; cmhp -> cmsg_len = CMSG_LEN ( sizeof ( struct ucred ) ) ; cmhp -> cmsg_level = SOL_SOCKET ; cmhp -> cmsg_type = SCM_CREDENTIALS ; cred = ( struct ucred * ) CMSG_DATA ( cmhp ) ; cred -> pid = ctl -> pid ; } #endif #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL  0 #endif if ( sendmsg ( ctl -> fd , & message , MSG_NOSIGNAL ) < 0 ) { logger_reopen ( ctl ) ; if ( sendmsg ( ctl -> fd , & message , MSG_NOSIGNAL ) < 0 ) warn ( _ ( " " ) ) ; } } if ( ctl -> stderr_printout ) { if ( iovec_memcmp ( iov , iovlen , " \n " , 1 ) != 0 ) iovec_add_string ( iov , iovlen , " \n " , 1 ) ; ignore_result ( writev ( STDERR_FILENO , iov , iovlen ) ) ; } free ( octet ) ; }
writes generated buffer to desired destination.
writes generated buffer to desired destination .
/* initial connect failed? */ /* 1) octen count */ /* 2) header */ /* 3) message */ /* 4) add extra \n to make sure message is terminated */ /* syslog/journald may follow local socket credentials rather
         * than in the message PID. If we use --id as root than we can
         * force kernel to accept another valid PID than the real logger(1)
         * PID.
         */ /* Note that logger(1) maybe executed for long time (as pipe
         * reader) and connection endpoint (syslogd) may be restarted.
         *
         * The libc syslog() function reconnects on failed send().
         * Let's do the same to be robust.    [kzak -- Oct 2017]
         *
         * MSG_NOSIGNAL is POSIX.1-2008 compatible, but it for example
         * not supported by apple-darwin15.6.0.
         */ /* make sure it's terminated for stderr */