static int logger_child()

in src/native/unix/native/jsvc-unix.c [977:1052]


static int logger_child(int out_fd, int err_fd, char *procname)
{
    fd_set rfds;
    struct timeval tv;
    int retval, nfd = -1, rc = 0;
    ssize_t n;
    char buf[LOGBUF_SIZE];

    if (out_fd == -1 && err_fd == -1)
        return EINVAL;
    if (out_fd == -1)
        nfd = err_fd;
    else if (err_fd == -1)
        nfd = out_fd;
    else
        nfd = out_fd > err_fd ? out_fd : err_fd;
    ++nfd;

    openlog(procname, LOG_PID, LOG_DAEMON);

    while (out_fd != -1 || err_fd != -1) {
        FD_ZERO(&rfds);
        if (out_fd != -1) {
            FD_SET(out_fd, &rfds);
        }
        if (err_fd != -1) {
            FD_SET(err_fd, &rfds);
        }
        tv.tv_sec = 60;
        tv.tv_usec = 0;
        retval = select(nfd, &rfds, NULL, NULL, &tv);
        if (retval == -1) {
            rc = errno;
            syslog(LOG_ERR, "select: %s", strerror(errno));
            /* If select failed no point to continue */
            break;
        }
        else if (retval) {
            if (out_fd != -1 && FD_ISSET(out_fd, &rfds)) {
                do {
                    n = read(out_fd, buf, LOGBUF_SIZE - 1);
                } while (n == -1 && errno == EINTR);
                if (n == -1) {
                    syslog(LOG_ERR, "read: %s", strerror(errno));
                    close(out_fd);
                    if (err_fd == -1)
                        break;
                    nfd = err_fd + 1;
                    out_fd = -1;
                }
                else if (n > 0 && buf[0] != '\n') {
                    buf[n] = 0;
                    syslog(LOG_INFO, "%s", buf);
                }
            }
            if (err_fd != -1 && FD_ISSET(err_fd, &rfds)) {
                do {
                    n = read(err_fd, buf, LOGBUF_SIZE - 1);
                } while (n == -1 && errno == EINTR);
                if (n == -1) {
                    syslog(LOG_ERR, "read: %s", strerror(errno));
                    close(err_fd);
                    if (out_fd == -1)
                        break;
                    nfd = out_fd + 1;
                    err_fd = -1;
                }
                else if (n > 0 && buf[0] != '\n') {
                    buf[n] = 0;
                    syslog(LOG_ERR, "%s", buf);
                }
            }
        }
    }
    return rc;
}