winlin

refine code for process fork.

@@ -227,27 +227,28 @@ int SrsProcess::start() @@ -227,27 +227,28 @@ int SrsProcess::start()
227 227
228 // should never close the fd 3+, for it myabe used. 228 // should never close the fd 3+, for it myabe used.
229 // for fd should close at exec, use fnctl to set it. 229 // for fd should close at exec, use fnctl to set it.
230 -  
231 - // log basic info 230 +
  231 + // log basic info to stderr.
232 if (true) { 232 if (true) {
233 fprintf(stderr, "\n"); 233 fprintf(stderr, "\n");
234 fprintf(stderr, "process ppid=%d, cid=%d, pid=%d\n", ppid, cid, getpid()); 234 fprintf(stderr, "process ppid=%d, cid=%d, pid=%d\n", ppid, cid, getpid());
235 - fprintf(stderr, "process binary=%s\n", bin.c_str());  
236 - fprintf(stderr, "process cli: %s\n", cli.c_str()); 235 + fprintf(stderr, "process binary=%s, cli: %s\n", bin.c_str(), cli.c_str());
237 fprintf(stderr, "process actual cli: %s\n", actual_cli.c_str()); 236 fprintf(stderr, "process actual cli: %s\n", actual_cli.c_str());
238 } 237 }
239 238
240 // memory leak in child process, it's ok. 239 // memory leak in child process, it's ok.
241 - char** charpv_params = new char*[params.size() + 1]; 240 + char** argv = new char*[params.size() + 1];
242 for (int i = 0; i < (int)params.size(); i++) { 241 for (int i = 0; i < (int)params.size(); i++) {
243 std::string& p = params[i]; 242 std::string& p = params[i];
244 - charpv_params[i] = (char*)p.data(); 243 +
  244 + // memory leak in child process, it's ok.
  245 + char* v = new char[p.length() + 1];
  246 + argv[i] = strcpy(v, p.data());
245 } 247 }
246 - // EOF: NULL  
247 - charpv_params[params.size()] = NULL; 248 + argv[params.size()] = NULL;
248 249
249 - // TODO: execv or execvp  
250 - ret = execv(bin.c_str(), charpv_params); 250 + // use execv to start the program.
  251 + ret = execv(bin.c_str(), argv);
251 if (ret < 0) { 252 if (ret < 0) {
252 fprintf(stderr, "fork process failed, errno=%d(%s)", errno, strerror(errno)); 253 fprintf(stderr, "fork process failed, errno=%d(%s)", errno, strerror(errno));
253 } 254 }
@@ -258,7 +259,7 @@ int SrsProcess::start() @@ -258,7 +259,7 @@ int SrsProcess::start()
258 if (pid > 0) { 259 if (pid > 0) {
259 is_started = true; 260 is_started = true;
260 srs_trace("fored process, pid=%d, bin=%s, stdout=%s, stderr=%s, argv=%s", 261 srs_trace("fored process, pid=%d, bin=%s, stdout=%s, stderr=%s, argv=%s",
261 - pid, bin.c_str(), stdout_file.c_str(), stdout_file.c_str(), actual_cli.c_str()); 262 + pid, bin.c_str(), stdout_file.c_str(), stderr_file.c_str(), actual_cli.c_str());
262 return ret; 263 return ret;
263 } 264 }
264 265