Skip to content

Commit

Permalink
for #367, process support redirect stdout and stderr.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 24, 2015
1 parent 0e1861b commit 6fe88d0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
8 changes: 6 additions & 2 deletions trunk/src/app/srs_app_ffmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_ffmpeg.hpp>

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>

// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
#ifndef _WIN32
#include <unistd.h>
#endif

#include <vector>
using namespace std;

Expand Down Expand Up @@ -407,7 +411,7 @@ int SrsFFMPEG::start()
params.push_back(_output);

// when specified the log file.
if (false && !log_file.empty()) {
if (!log_file.empty()) {
// stdout
params.push_back("1");
params.push_back(">");
Expand Down
96 changes: 79 additions & 17 deletions trunk/src/app/srs_app_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#include <signal.h>
#include <sys/types.h>

// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
#ifndef _WIN32
#include <unistd.h>
#endif

using namespace std;

#include <srs_kernel_error.hpp>
Expand Down Expand Up @@ -57,8 +62,34 @@ int SrsProcess::initialize(string binary, vector<string> argv)
{
int ret = ERROR_SUCCESS;

bin = binary;
params = argv;
cli = bin = binary;

for (int i = 0; i < (int)argv.size(); i++) {
std::string ffp = argv[i];
cli += " " + ffp;
}

for (int i = 0; i < (int)argv.size(); i++) {
std::string ffp = argv[i];

// remove the stdout and stderr.
if (ffp == "1") {
if (i + 2 < (int)argv.size()) {
stdout_file = argv[i + 2];
i += 2;
}
continue;
} else if (ffp == "2") {
if (i + 2 < (int)argv.size()) {
stderr_file = argv[i + 2];
i += 2;
}
continue;
}

// startup params.
params.push_back(ffp);
}

return ret;
}
Expand All @@ -71,20 +102,12 @@ int SrsProcess::start()
return ret;
}

std::string cli;
if (true) {
for (int i = 0; i < (int)params.size(); i++) {
std::string ffp = params[i];
cli += ffp;
if (i < (int)params.size() - 1) {
cli += " ";
}
}
srs_trace("fork process: %s %s", bin.c_str(), cli.c_str());
}
// generate the argv of process.
srs_trace("fork process: %s", cli.c_str());

// for log
int cid = _srs_context->get_id();
int ppid = getpid();

// TODO: fork or vfork?
if ((pid = fork()) < 0) {
Expand All @@ -99,12 +122,51 @@ 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);
return ret;
}

if (dup2(stdout_fd, STDOUT_FILENO) < 0) {
ret = ERROR_ENCODER_DUP2;
srs_error("dup2 process stdout failed. ret=%d", ret);
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);
return ret;
}

if (dup2(stderr_fd, STDERR_FILENO) < 0) {
ret = ERROR_ENCODER_DUP2;
srs_error("dup2 process stderr failed. ret=%d", ret);
return ret;
}
}

// log basic info
if (true) {
fprintf(stderr, "\n");
fprintf(stderr, "process parent cid=%d", cid);
fprintf(stderr, "process binary=%s", bin.c_str());
fprintf(stderr, "process params: %s %s", bin.c_str(), cli.c_str());
fprintf(stderr, "process parent pid=%d\n", ppid);
fprintf(stderr, "process parent cid=%d\n", cid);
fprintf(stderr, "process binary=%s\n", bin.c_str());
fprintf(stderr, "process cli: %s\n", cli.c_str());
}

// close other fds
Expand Down Expand Up @@ -133,7 +195,7 @@ int SrsProcess::start()
// parent.
if (pid > 0) {
is_started = true;
srs_trace("vfored process, pid=%d, cli=%s", pid, bin.c_str());
srs_trace("vfored process, pid=%d, bin=%s", pid, bin.c_str());
return ret;
}

Expand Down
4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class SrsProcess
pid_t pid;
private:
std::string bin;
std::string stdout_file;
std::string stderr_file;
std::vector<std::string> params;
// the cli to fork process.
std::string cli;
public:
SrsProcess();
virtual ~SrsProcess();
Expand Down

0 comments on commit 6fe88d0

Please sign in to comment.