1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

refine the dup2 of stdout and stderr.

This commit is contained in:
winlin 2016-01-20 14:05:48 +08:00
parent ae8bd4c2aa
commit ce8f3b4478
2 changed files with 50 additions and 36 deletions

View file

@ -141,6 +141,43 @@ int SrsProcess::initialize(string binary, vector<string> argv)
return ret; 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 SrsProcess::start()
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -173,42 +210,19 @@ int SrsProcess::start()
signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN); signal(SIGTERM, SIG_IGN);
// redirect stdout to file. // for the stdin,
if (!stdout_file.empty()) { // should never close it or ffmpeg will error.
int stdout_fd = -1;
int flags = O_CREAT|O_WRONLY|O_APPEND; // for the stdout, ignore when not specified.
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; // redirect stdout to file if possible.
if ((ret = srs_redirect_output(stdout_file, STDOUT_FILENO)) != ERROR_SUCCESS) {
if ((stdout_fd = ::open(stdout_file.c_str(), flags, mode)) < 0) { return ret;
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);
}
} }
// redirect stderr to file. // for the stderr, ignore when not specified.
if (!stderr_file.empty()) { // redirect stderr to file if possible.
int stderr_fd = -1; if ((ret = srs_redirect_output(stderr_file, STDERR_FILENO)) != ERROR_SUCCESS) {
int flags = O_CREAT|O_WRONLY|O_APPEND; return ret;
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);
}
} }
// should never close the fd 3+, for it myabe used. // should never close the fd 3+, for it myabe used.

View file

@ -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_VBITRATE 3027
#define ERROR_ENCODER_FORK 3028 #define ERROR_ENCODER_FORK 3028
#define ERROR_ENCODER_LOOP 3029 #define ERROR_ENCODER_LOOP 3029
#define ERROR_ENCODER_OPEN 3030 #define ERROR_FORK_OPEN_LOG 3030
#define ERROR_ENCODER_DUP2 3031 #define ERROR_FORK_DUP2_LOG 3031
#define ERROR_ENCODER_PARSE 3032 #define ERROR_ENCODER_PARSE 3032
#define ERROR_ENCODER_NO_INPUT 3033 #define ERROR_ENCODER_NO_INPUT 3033
#define ERROR_ENCODER_NO_OUTPUT 3034 #define ERROR_ENCODER_NO_OUTPUT 3034