コード例 #1
0
static void set_output( char *outfile, char *errfile ) {
	if( freopen( "/dev/null", "r", stdin ) );//hack
	__debug( "redirecting stdout to %s and stderr to %s", outfile, errfile );
	if( debug == 1 && strcmp( errfile, "/dev/null" ) == 0 ) return;
	if( strcmp( outfile, "&2" ) == 0 && strcmp( errfile, "&1" ) == 0 ) outfile = "/dev/null";
	if( strcmp( outfile, "&1" ) != 0 ) loc_freopen( outfile, "a", stdout );
	if( strcmp( errfile, "&2" ) != 0 ) loc_freopen( errfile, "a", stderr );
	else {
		close( 2 );
		if( dup( 1 ) );//hack
	}
	if( strcmp( outfile, "&2" ) == 0 ) {
		close( 1 );
		if( dup( 2 ) );//hack
	}
}
コード例 #2
0
ファイル: jsvc-unix.c プロジェクト: utcuong3010/nds
/**
 *  Redirect stdin, stdout, stderr.
 */
static void set_output(char *outfile, char *errfile, bool redirectstdin, char *procname)
{
    int out_pipe[2] = {-1, -1};
    int err_pipe[2] = {-1, -1};
    int fork_needed = 0;

    if (redirectstdin == true) {
        freopen("/dev/null", "r", stdin);
    }

    log_debug("redirecting stdout to %s and stderr to %s", outfile, errfile);

    /* make sure the debug goes out */
    if (log_debug_flag == true && strcmp(errfile, "/dev/null") == 0)
        return;
    if (strcmp(outfile, "&1") == 0 && strcmp(errfile, "&2") == 0)
        return;
    if (strcmp(outfile, "SYSLOG") == 0) {
        freopen("/dev/null", "a", stdout);
        /* Send stdout to syslog through a logger process */
        if (pipe(out_pipe) == -1) {
            log_error("cannot create stdout pipe: %s",
                      strerror(errno));
        }
        else {
            fork_needed = 1;
            log_stdout_syslog_flag = true;
        }
    }
    else if (strcmp(outfile, "&2")) {
        if (strcmp(outfile, "&1")) {
            /* Redirect stdout to a file */
            loc_freopen(outfile, "a", stdout);
        }
    }

    if (strcmp(errfile, "SYSLOG") == 0) {
        freopen("/dev/null", "a", stderr);
        /* Send stderr to syslog through a logger process */
        if (pipe(err_pipe) == -1) {
            log_error("cannot create stderr pipe: %s",
                      strerror(errno));
        }
        else {
            fork_needed = 1;
            log_stderr_syslog_flag = true;
        }
    }
    else if (strcmp(errfile, "&1")) {
        if (strcmp(errfile, "&2")) {
            /* Redirect stderr to a file */
            loc_freopen(errfile, "a", stderr);
        }
    }
    if (strcmp(errfile, "&1") == 0 && strcmp(outfile, "&1")) {
        /*
         * -errfile &1 -outfile foo
         * Redirect stderr to stdout
         */
        close(2);
        dup2(1, 2);
    }
    if (strcmp(outfile, "&2") == 0 && strcmp(errfile, "&2")) {
        /*
         * -outfile &2 -errfile foo
         * Redirect stdout to stderr
         */
        close(1);
        dup2(2, 1);
    }

    if (fork_needed) {
        pid_t pid = fork();
        if (pid == -1) {
            log_error("cannot create logger process: %s", strerror(errno));
        }
        else {
            if (pid != 0) {
                /* Parent process.
                 * Close child pipe endpoints.
                 */
                logger_pid = pid;
                if (out_pipe[0] != -1) {
                    close(out_pipe[0]);
                    if (dup2(out_pipe[1], 1) == -1) {
                        log_error("cannot redirect stdout to pipe for syslog: %s",
                                  strerror(errno));
                    }
                }
                if (err_pipe[0] != -1) {
                    close(err_pipe[0]);
                    if (dup2(err_pipe[1], 2) == -1) {
                        log_error("cannot redirect stderr to pipe for syslog: %s",
                                  strerror(errno));
                    }
                }
            }
            else {
                exit(logger_child(out_pipe[0], err_pipe[0], procname));
            }
        }
    }
}