Repository: ossrs/srs
File: trunk/src/app/srs_app_process.cpp (C++)

Code before:
    }
    
    // for osx(lldb) to debug the child process.
    // user can use "lldb -p <pid>" to resume the parent or child process.
    //kill(0, SIGSTOP);
    
    // child process: ffmpeg encoder engine.
    if (pid == 0) {
        // ignore the SIGINT and SIGTERM
        signal(SIGINT, SIG_IGN);
        signal(SIGTERM, SIG_IGN);

        // should never close the fd 3+, for it myabe used.
        // for fd should close at exec, use fnctl to set it.
        
        // log basic info to stderr.
        if (true) {
            fprintf(stdout, "\n");
            fprintf(stdout, "process ppid=%d, cid=%s, pid=%d, in=%d, out=%d, err=%d\n",
                ppid, cid.c_str(), getpid(), STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
            fprintf(stdout, "process binary=%s, cli: %s\n", bin.c_str(), cli.c_str());
            fprintf(stdout, "process actual cli: %s\n", actual_cli.c_str());
        }
        
        // output error to stdout and exit child process.
        if ((err = redirect_io()) != srs_success) {
            fprintf(stdout, "child process error, %s\n", srs_error_desc(err).c_str());
            exit(-1);
        }
        
        // memory leak in child process, it's ok.
        char** argv = new char*[params.size() + 1];
        for (int i = 0; i < (int)params.size(); i++) {
            std::string& p = params[i];
            
            // memory leak in child process, it's ok.
            char* v = new char[p.length() + 1];
            argv[i] = strcpy(v, p.data());
        }
        argv[params.size()] = NULL;
        
        // use execv to start the program.
        int r0 = execv(bin.c_str(), argv);
        if (r0 < 0) {
            fprintf(stderr, "fork process failed, errno=%d(%s)", errno, strerror(errno));
        }
        exit(r0);
    }
    
    // parent.
    if (pid > 0) {

Review comment (suggestion):
Redirect_io must be done before printing, and after that, it will be printed to the console. This may result in the loss of logs.

```
err = redirect_io();

fprintf(xxx);

if (err != srs_success) {
```

`TRANS_BY_GPT3`

Code after:
    }
    
    // for osx(lldb) to debug the child process.
    // user can use "lldb -p <pid>" to resume the parent or child process.
    //kill(0, SIGSTOP);
    
    // child process: ffmpeg encoder engine.
    if (pid == 0) {
        // ignore the SIGINT and SIGTERM
        signal(SIGINT, SIG_IGN);
        signal(SIGTERM, SIG_IGN);
        
        // redirect standard I/O, if it failed, output error to stdout, and exit child process.
        if ((err = redirect_io()) != srs_success) {
            fprintf(stdout, "child process error, %s\n", srs_error_desc(err).c_str());
            exit(-1);
        }
        
        // should never close the fd 3+, for it myabe used.
        // for fd should close at exec, use fnctl to set it.
        
        // log basic info to stderr.
        if (true) {
            fprintf(stdout, "\n");
            fprintf(stdout, "process ppid=%d, cid=%s, pid=%d, in=%d, out=%d, err=%d\n",
                ppid, cid.c_str(), getpid(), STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
            fprintf(stdout, "process binary=%s, cli: %s\n", bin.c_str(), cli.c_str());
            fprintf(stdout, "process actual cli: %s\n", actual_cli.c_str());
        }
        
        // memory leak in child process, it's ok.
        char** argv = new char*[params.size() + 1];
        for (int i = 0; i < (int)params.size(); i++) {
            std::string& p = params[i];
            
            // memory leak in child process, it's ok.
            char* v = new char[p.length() + 1];
            argv[i] = strcpy(v, p.data());
        }
        argv[params.size()] = NULL;
        
        // use execv to start the program.
        int r0 = execv(bin.c_str(), argv);
        if (r0 < 0) {
            fprintf(stderr, "fork process failed, errno=%d(%s)", errno, strerror(errno));
        }
        exit(r0);
    }
    
    // parent.
    if (pid > 0) {