Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2993)

Unified Diff: base/process_util_posix.cc

Issue 3036023: Revert 52613 - Revert 52608 - Add and alternative GetAppOutput() to process_u... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/process_util.h ('k') | base/process_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/process_util_posix.cc
===================================================================
--- base/process_util_posix.cc (revision 53899)
+++ base/process_util_posix.cc (working copy)
@@ -5,6 +5,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <poll.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/resource.h>
@@ -737,8 +738,10 @@
return ret;
}
-// Executes the application specified by |cl| and wait for it to exit. Stores
-// the output (stdout) in |output|. If |do_search_path| is set, it searches the
+// Executes the application specified by |cl| and waits for it to exit, but
+// at most |timeout_milliseconds| (use kNoTimeout for no timeout). Stores
+// the output (stdout) in |output|, and whether or not the timeout
+// triggered in |timed_out|. If |do_search_path| is set, it searches the
// path for the application; in that case, |envp| must be null, and it will use
// the current environment. If |do_search_path| is false, |cl| should fully
// specify the path of the application, and |envp| will be used as the
@@ -746,7 +749,8 @@
// (application launched and exited cleanly, with exit code indicating success).
static bool GetAppOutputInternal(const CommandLine& cl, char* const envp[],
std::string* output, size_t max_output,
- bool do_search_path) {
+ bool do_search_path, bool* timed_out,
+ int timeout_milliseconds) {
int pipe_fd[2];
pid_t pid;
InjectiveMultimap fd_shuffle1, fd_shuffle2;
@@ -788,7 +792,7 @@
fd_shuffle1.push_back(InjectionArc(pipe_fd[1], STDOUT_FILENO, true));
fd_shuffle1.push_back(InjectionArc(dev_null, STDERR_FILENO, true));
fd_shuffle1.push_back(InjectionArc(dev_null, STDIN_FILENO, true));
- // Adding another element here? Remeber to increase the argument to
+ // Adding another element here? Remember to increase the argument to
// reserve(), above.
std::copy(fd_shuffle1.begin(), fd_shuffle1.end(),
@@ -821,19 +825,50 @@
ssize_t bytes_read = 1; // A lie to properly handle |max_output == 0|
// case in the logic below.
+ *timed_out = false;
+ const Time timeout_time = Time::Now() +
+ TimeDelta::FromMilliseconds(timeout_milliseconds);
+ int remaining_ms = timeout_milliseconds;
while (output_buf_left > 0) {
+ struct pollfd poll_struct = { pipe_fd[0], POLLIN, 0 };
+ const int poll_ret_code = poll(&poll_struct, 1, remaining_ms);
+
+ // poll() should only fail due to interrupt.
+ DCHECK(poll_ret_code != -1 || errno == EINTR);
+ if (poll_ret_code == 0) {
+ *timed_out = true;
+ break;
+ }
+
bytes_read = HANDLE_EINTR(read(pipe_fd[0], buffer,
std::min(output_buf_left, sizeof(buffer))));
if (bytes_read <= 0)
break;
output->append(buffer, bytes_read);
output_buf_left -= static_cast<size_t>(bytes_read);
+
+ // Update remaining_ms if we're not using an infinite timeout.
+ if (timeout_milliseconds >= 0) {
+ remaining_ms = (timeout_time - Time::Now()).InMilliseconds();
+ if (remaining_ms < 0) {
+ *timed_out = true;
+ break;
+ }
+ }
}
close(pipe_fd[0]);
- // Always wait for exit code (even if we know we'll declare success).
- int exit_code = EXIT_FAILURE;
- bool success = WaitForExitCode(pid, &exit_code);
+ bool success = false;
+ int exit_code = PROCESS_END_PROCESS_WAS_HUNG;
+ if (*timed_out) {
+ KillProcess(pid, exit_code, true);
+ // This waits up to 60 seconds for process to actually be dead
+ // which means this may overrun the timeout.
+ LOG(ERROR) << "Process "<< pid << " killed by timeout (waited "
+ << timeout_milliseconds << " ms).";
+ } else {
+ success = WaitForExitCode(pid, &exit_code);
+ }
// If we stopped because we read as much as we wanted, we always declare
// success (because the child may exit due to |SIGPIPE|).
@@ -849,17 +884,26 @@
bool GetAppOutput(const CommandLine& cl, std::string* output) {
// Run |execve()| with the current environment and store "unlimited" data.
+ bool timed_out;
return GetAppOutputInternal(cl, NULL, output,
- std::numeric_limits<std::size_t>::max(), true);
+ std::numeric_limits<std::size_t>::max(), true,
+ &timed_out, base::kNoTimeout);
}
-// TODO(viettrungluu): Conceivably, we should have a timeout as well, so we
-// don't hang if what we're calling hangs.
+bool GetAppOutputWithTimeout(const CommandLine& cl, std::string* output,
+ bool* timed_out, int timeout_milliseconds) {
+ return GetAppOutputInternal(cl, NULL, output,
+ std::numeric_limits<std::size_t>::max(), true,
+ timed_out, timeout_milliseconds);
+}
+
bool GetAppOutputRestricted(const CommandLine& cl,
std::string* output, size_t max_output) {
// Run |execve()| with the empty environment.
char* const empty_environ = NULL;
- return GetAppOutputInternal(cl, &empty_environ, output, max_output, false);
+ bool timed_out;
+ return GetAppOutputInternal(cl, &empty_environ, output, max_output, false,
+ &timed_out, base::kNoTimeout);
}
bool WaitForProcessesToExit(const std::wstring& executable_name,
« no previous file with comments | « base/process_util.h ('k') | base/process_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698