From d940627c90386df7844092dae635ed2f20535f28 Mon Sep 17 00:00:00 2001 From: "phajdan.jr@chromium.org" Date: Sat, 18 Apr 2009 16:17:22 +0000 Subject: Cleanup in LaunchApp: - don't copy argv strings - use pid_t instead of int - simplify the control flow a bit Review URL: http://codereview.chromium.org/77024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14004 0039d316-1c4b-4281-b951-d872f2087c98 --- base/process_util_linux.cc | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'base/process_util_linux.cc') diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index dcac3bd..faec590 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -31,20 +31,14 @@ namespace base { bool LaunchApp(const std::vector& argv, const file_handle_mapping_vector& fds_to_remap, bool wait, ProcessHandle* process_handle) { - bool retval = true; - - char* argv_copy[argv.size() + 1]; - for (size_t i = 0; i < argv.size(); i++) { - argv_copy[i] = new char[argv[i].size() + 1]; - strcpy(argv_copy[i], argv[i].c_str()); - } - argv_copy[argv.size()] = NULL; - // Make sure we don't leak any FDs to the child process by marking all FDs // as close-on-exec. SetAllFDsToCloseOnExec(); - int pid = fork(); + pid_t pid = fork(); + if (pid < 0) + return false; + if (pid == 0) { for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); it != fds_to_remap.end(); @@ -61,9 +55,12 @@ bool LaunchApp(const std::vector& argv, } } - execvp(argv_copy[0], argv_copy); - } else if (pid < 0) { - retval = false; + char* argv_cstr[argv.size() + 1]; + for (size_t i = 0; i < argv.size(); i++) + argv_cstr[i] = const_cast(argv[i].c_str()); + argv_cstr[argv.size()] = NULL; + execvp(argv_cstr[0], argv_cstr); + exit(127); } else { if (wait) waitpid(pid, 0, 0); @@ -72,10 +69,7 @@ bool LaunchApp(const std::vector& argv, *process_handle = pid; } - for (size_t i = 0; i < argv.size(); i++) - delete[] argv_copy[i]; - - return retval; + return true; } bool LaunchApp(const CommandLine& cl, -- cgit v1.1