diff options
-rw-r--r-- | base/process_util.h | 7 | ||||
-rw-r--r-- | base/process_util_posix.cc | 10 | ||||
-rw-r--r-- | chrome/browser/process_singleton_linux.cc | 3 | ||||
-rw-r--r-- | chrome/chrome.gyp | 4 | ||||
-rw-r--r-- | chrome/common/chrome_constants.cc | 3 | ||||
-rw-r--r-- | chrome/common/chrome_constants.h | 1 | ||||
-rw-r--r-- | chrome/test/chrome_process_util_linux.cc | 34 | ||||
-rw-r--r-- | chrome/test/chrome_process_util_uitest.cc | 20 |
8 files changed, 55 insertions, 27 deletions
diff --git a/base/process_util.h b/base/process_util.h index 367c772..20cdd5e 100644 --- a/base/process_util.h +++ b/base/process_util.h @@ -126,9 +126,10 @@ bool LaunchApp(const CommandLine& cl, #if defined(OS_POSIX) // Execute the application specified by |cl| and wait for it to exit. Store -// the output (stdout and stderr) in |output|. Returns true on success -// (application launched and exited cleanly, with exit code indicating success). -// |output| is modified only when the function finished successfully. +// the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true +// on success (application launched and exited cleanly, with exit code +// indicating success). |output| is modified only when the function finished +// successfully. bool GetAppOutput(const CommandLine& cl, std::string* output); #endif // defined(OS_POSIX) diff --git a/base/process_util_posix.cc b/base/process_util_posix.cc index 75c9b96..3570a16 100644 --- a/base/process_util_posix.cc +++ b/base/process_util_posix.cc @@ -326,17 +326,23 @@ bool GetAppOutput(const CommandLine& cl, std::string* output) { return false; case 0: // child { + int dev_null = open("/dev/null", O_WRONLY); + if (dev_null < 0) + exit(127); + int rv; do { rv = dup2(pipe_fd[1], STDOUT_FILENO); } while (rv == -1 && errno == EINTR); do { - rv = dup2(pipe_fd[1], STDERR_FILENO); + rv = dup2(dev_null, STDERR_FILENO); } while (rv == -1 && errno == EINTR); if (pipe_fd[0] != STDOUT_FILENO && pipe_fd[0] != STDERR_FILENO) close(pipe_fd[0]); if (pipe_fd[1] != STDOUT_FILENO && pipe_fd[1] != STDERR_FILENO) close(pipe_fd[1]); + if (dev_null != STDOUT_FILENO && dev_null != STDERR_FILENO) + close(dev_null); const std::vector<std::string> argv = cl.argv(); char* argv_cstr[argv.size() + 1]; @@ -373,7 +379,7 @@ bool GetAppOutput(const CommandLine& cl, std::string* output) { if (bytes_read > 0) buf_output.append(buffer, bytes_read); } - output->assign(buf_output); + output->swap(buf_output); close(pipe_fd[0]); return true; } diff --git a/chrome/browser/process_singleton_linux.cc b/chrome/browser/process_singleton_linux.cc index f37445c..4fba373 100644 --- a/chrome/browser/process_singleton_linux.cc +++ b/chrome/browser/process_singleton_linux.cc @@ -11,9 +11,10 @@ #include "base/logging.h" #include "base/string_util.h" +#include "chrome/common/chrome_constants.h" ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) { - socket_path_ = user_data_dir.Append("SingletonSocket"); + socket_path_ = user_data_dir.Append(chrome::kSingletonSocketFilename); } ProcessSingleton::~ProcessSingleton() { diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 88c545c..ee35ce5 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -842,7 +842,7 @@ 'browser/importer/mork_reader.cc', 'browser/importer/mork_reader.h', 'browser/importer/toolbar_importer.cc', - 'browser/importer/toolbar_importer.h', + 'browser/importer/toolbar_importer.h', 'browser/jankometer.cc', 'browser/jankometer.h', 'browser/jsmessage_box_handler.cc', @@ -2009,6 +2009,7 @@ 'test/accessibility/tab_impl.cc', 'test/accessibility/tab_impl.h', 'test/automation/automation_proxy_uitest.cc', + 'test/chrome_process_util_uitest.cc', 'test/perf/mem_usage.cc', 'test/perf/mem_usage.h', 'test/reliability/page_load_test.cc', @@ -2051,6 +2052,7 @@ 'browser/metrics/metrics_service_uitest.cc', 'browser/renderer_host/resource_dispatcher_host_uitest.cc', 'browser/sessions/session_restore_uitest.cc', + 'test/chrome_process_util_uitest.cc', 'test/reliability/page_load_test.cc', 'test/ui/layout_plugin_uitest.cc', 'test/ui/omnibox_uitest.cc', diff --git a/chrome/common/chrome_constants.cc b/chrome/common/chrome_constants.cc index cba88da..94d8edb 100644 --- a/chrome/common/chrome_constants.cc +++ b/chrome/common/chrome_constants.cc @@ -54,6 +54,9 @@ const FilePath::CharType kHistoryFilename[] = FPL("History"); const FilePath::CharType kLocalStateFilename[] = FPL("Local State"); const FilePath::CharType kPreferencesFilename[] = FPL("Preferences"); const FilePath::CharType kSafeBrowsingFilename[] = FPL("Safe Browsing"); +// WARNING: SingletonSocket can't contain spaces, because otherwise +// chrome_process_util_linux would be broken. +const FilePath::CharType kSingletonSocketFilename[] = FPL("SingletonSocket"); const FilePath::CharType kThumbnailsFilename[] = FPL("Thumbnails"); const wchar_t kUserDataDirname[] = L"User Data"; const FilePath::CharType kUserScriptsDirname[] = FPL("User Scripts"); diff --git a/chrome/common/chrome_constants.h b/chrome/common/chrome_constants.h index c8ff5fa..9be6a3f 100644 --- a/chrome/common/chrome_constants.h +++ b/chrome/common/chrome_constants.h @@ -34,6 +34,7 @@ extern const FilePath::CharType kHistoryFilename[]; extern const FilePath::CharType kLocalStateFilename[]; extern const FilePath::CharType kPreferencesFilename[]; extern const FilePath::CharType kSafeBrowsingFilename[]; +extern const FilePath::CharType kSingletonSocketFilename[]; extern const FilePath::CharType kThumbnailsFilename[]; extern const wchar_t kUserDataDirname[]; extern const FilePath::CharType kUserScriptsDirname[]; diff --git a/chrome/test/chrome_process_util_linux.cc b/chrome/test/chrome_process_util_linux.cc index f5c1df1..f1c7601 100644 --- a/chrome/test/chrome_process_util_linux.cc +++ b/chrome/test/chrome_process_util_linux.cc @@ -9,41 +9,35 @@ #include <string> #include <vector> +#include "base/command_line.h" #include "base/logging.h" +#include "base/process_util.h" #include "base/string_util.h" +#include "chrome/common/chrome_constants.h" base::ProcessId ChromeBrowserProcessId(const FilePath& data_dir) { - char fuser_output[256]; - - FilePath socket_name = data_dir.Append("SingletonSocket"); - // TODO(phajdan.jr): Do better quoting around the socket name. - std::string cmd = "fuser \"" + socket_name.value() + "\""; - FILE* fuser_pipe = popen(cmd.c_str(), "r"); - if (!fuser_pipe) { - DLOG(ERROR) << "Error launching fuser."; - return -1; - } + FilePath socket_name = data_dir.Append(chrome::kSingletonSocketFilename); - char* rv = fgets(fuser_output, 256, fuser_pipe); - pclose(fuser_pipe); + std::vector<std::string> argv; + argv.push_back("fuser"); + argv.push_back(socket_name.value()); - if (!rv) + std::string fuser_output; + if (!base::GetAppOutput(CommandLine(argv), &fuser_output)) return -1; std::string trimmed_output; TrimWhitespace(fuser_output, TRIM_ALL, &trimmed_output); - for (size_t i = 0; i < trimmed_output.size(); ++i) { - if (trimmed_output[i] == ' '){ - DLOG(ERROR) << "Expected exactly 1 process to have socket open: " << - fuser_output; - return -1; - } + if (trimmed_output.find(' ') != std::string::npos) { + LOG(FATAL) << "Expected exactly 1 process to have socket open: " << + fuser_output; + return -1; } int pid; if (!StringToInt(trimmed_output, &pid)) { - DLOG(ERROR) << "Unexpected fuser output: " << fuser_output; + LOG(FATAL) << "Unexpected fuser output: " << fuser_output; return -1; } return pid; diff --git a/chrome/test/chrome_process_util_uitest.cc b/chrome/test/chrome_process_util_uitest.cc new file mode 100644 index 0000000..78f3fa9 --- /dev/null +++ b/chrome/test/chrome_process_util_uitest.cc @@ -0,0 +1,20 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/test/chrome_process_util.h" + +#include "chrome/test/ui/ui_test.h" + +class ChromeProcessUtilTest : public UITest { +}; + +TEST_F(ChromeProcessUtilTest, SanityTest) { + EXPECT_TRUE(IsBrowserRunning()); + EXPECT_NE(-1, ChromeBrowserProcessId(user_data_dir())); + EXPECT_GE(GetRunningChromeProcesses(user_data_dir()).size(), 1U); + QuitBrowser(); + EXPECT_FALSE(IsBrowserRunning()); + EXPECT_EQ(-1, ChromeBrowserProcessId(user_data_dir())); + EXPECT_EQ(0U, GetRunningChromeProcesses(user_data_dir()).size()); +} |