From ce8f3b447827eff0129822e0e2bd4aa9a2ace04c Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 20 Jan 2016 14:05:48 +0800 Subject: [PATCH] refine the dup2 of stdout and stderr. --- trunk/src/app/srs_app_process.cpp | 82 ++++++++++++++++----------- trunk/src/kernel/srs_kernel_error.hpp | 4 +- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/trunk/src/app/srs_app_process.cpp b/trunk/src/app/srs_app_process.cpp index 3480d0710..b91f6a8e3 100644 --- a/trunk/src/app/srs_app_process.cpp +++ b/trunk/src/app/srs_app_process.cpp @@ -141,6 +141,43 @@ int SrsProcess::initialize(string binary, vector argv) return ret; } +int srs_redirect_output(string from_file, int to_fd) +{ + int ret = ERROR_SUCCESS; + + // use default output. + if (from_file.empty()) { + return ret; + } + + // disable output. + if (from_file == SRS_CONSTS_NULL_FILE) { + ::close(to_fd); + return ret; + } + + // redirect the fd to file. + int fd = -1; + int flags = O_CREAT|O_WRONLY|O_APPEND; + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; + + if ((fd = ::open(from_file.c_str(), flags, mode)) < 0) { + ret = ERROR_FORK_OPEN_LOG; + fprintf(stderr, "open process %d %s failed. ret=%d", to_fd, from_file.c_str(), ret); + exit(ret); + } + + if (dup2(fd, to_fd) < 0) { + ret = ERROR_FORK_DUP2_LOG; + srs_error("dup2 process %d failed. ret=%d", to_fd, ret); + exit(ret); + } + + ::close(fd); + + return ret; +} + int SrsProcess::start() { int ret = ERROR_SUCCESS; @@ -173,42 +210,19 @@ int SrsProcess::start() signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); - // redirect stdout to file. - if (!stdout_file.empty()) { - int stdout_fd = -1; - int flags = O_CREAT|O_WRONLY|O_APPEND; - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; - - if ((stdout_fd = ::open(stdout_file.c_str(), flags, mode)) < 0) { - ret = ERROR_ENCODER_OPEN; - fprintf(stderr, "open process stdout %s failed. ret=%d", stdout_file.c_str(), ret); - exit(ret); - } - - if (dup2(stdout_fd, STDOUT_FILENO) < 0) { - ret = ERROR_ENCODER_DUP2; - srs_error("dup2 process stdout failed. ret=%d", ret); - exit(ret); - } + // for the stdin, + // should never close it or ffmpeg will error. + + // for the stdout, ignore when not specified. + // redirect stdout to file if possible. + if ((ret = srs_redirect_output(stdout_file, STDOUT_FILENO)) != ERROR_SUCCESS) { + return ret; } - // redirect stderr to file. - if (!stderr_file.empty()) { - int stderr_fd = -1; - int flags = O_CREAT|O_WRONLY|O_APPEND; - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; - - if ((stderr_fd = ::open(stderr_file.c_str(), flags, mode)) < 0) { - ret = ERROR_ENCODER_OPEN; - fprintf(stderr, "open process stderr %s failed. ret=%d", stderr_file.c_str(), ret); - exit(ret); - } - - if (dup2(stderr_fd, STDERR_FILENO) < 0) { - ret = ERROR_ENCODER_DUP2; - srs_error("dup2 process stderr failed. ret=%d", ret); - exit(ret); - } + // for the stderr, ignore when not specified. + // redirect stderr to file if possible. + if ((ret = srs_redirect_output(stderr_file, STDERR_FILENO)) != ERROR_SUCCESS) { + return ret; } // should never close the fd 3+, for it myabe used. diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index 89852d04d..48fa39078 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -202,8 +202,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_ENCODER_VBITRATE 3027 #define ERROR_ENCODER_FORK 3028 #define ERROR_ENCODER_LOOP 3029 -#define ERROR_ENCODER_OPEN 3030 -#define ERROR_ENCODER_DUP2 3031 +#define ERROR_FORK_OPEN_LOG 3030 +#define ERROR_FORK_DUP2_LOG 3031 #define ERROR_ENCODER_PARSE 3032 #define ERROR_ENCODER_NO_INPUT 3033 #define ERROR_ENCODER_NO_OUTPUT 3034