Пример #1
0
int main(int argc, char *argv[])
{
    char buff[1024];
    int fd, nr, nw;
    if (argc < 2) {
        fprintf(stderr,
                "usage: %s output-filename\n"
                "       %s |output-command\n"
                "       %s :host:port\n", argv[0], argv[0], argv[0]);
        return 1;
    }
    fd = open_gen_fd(argv[1]);
    if (fd < 0) {
        perror("open_gen_fd");
        exit(1);
    }
    while ((nr = read(0, buff, sizeof (buff))) != 0) {
        if (nr < 0) {
            if (errno == EINTR)
                continue;
            perror("read");
            exit(1);
        }
        nw = write(fd, buff, nr);
        if (nw < 0) {
            perror("write");
            exit(1);
        }
    }
    return 0;
}
Пример #2
0
FILE *fopen_gen_fd(char *spec, const char *mode)
{
    int fd;
    if (!spec || *spec == '\0')
        return NULL;
    fd = open_gen_fd(spec);
    if (fd == -1)
        return NULL;
    return fdopen(fd, mode);
}
Пример #3
0
void open_logs(void)
{
    int access_log;

    /* if error_log_name is set, dup2 stderr to it */
    /* otherwise, leave stderr alone */
    /* we don't want to tie stderr to /dev/null */
    if (error_log_name) {
        int error_log;

        /* open the log file */
        if (!(error_log = open_gen_fd(error_log_name))) {
            DIE("unable to open error log");
        }

        /* redirect stderr to error_log */
        if (dup2(error_log, STDERR_FILENO) == -1) {
            DIE("unable to dup2 the error log");
        }
        close(error_log);
    }

    if (access_log_name) {
        access_log = open_gen_fd(access_log_name);
    } else {
        access_log = open("/dev/null", 0);
    }
    if (access_log < 0) {
        DIE("unable to open access log");
    }

    if (dup2(access_log, STDOUT_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDOUT_FILENO");
    }
    close(access_log);

    if (cgi_log_name) {
        cgi_log_fd = open_gen_fd(cgi_log_name);
        if (cgi_log_fd == -1) {
            WARN("open cgi_log");
            free(cgi_log_name);
            cgi_log_name = NULL;
            cgi_log_fd = 0;
        } else {
            if (fcntl(cgi_log_fd, F_SETFD, 1) == -1) {
                WARN("unable to set close-on-exec flag for cgi_log");
                free(cgi_log_name);
                cgi_log_name = NULL;
                close(cgi_log_fd);
                cgi_log_fd = 0;
            }
        }
    }
#ifdef SETVBUF_REVERSED
    setvbuf(stderr, _IONBF, (char *) NULL, 0);
    setvbuf(stdout, _IOLBF, (char *) NULL, 0);
#else
    setvbuf(stderr, (char *) NULL, _IONBF, 0);
    setvbuf(stdout, (char *) NULL, _IOLBF, 0);
#endif
}