diff options
-rw-r--r-- | base/base.gypi | 4 | ||||
-rw-r--r-- | base/command_line.cc | 70 | ||||
-rw-r--r-- | base/command_line.h | 10 | ||||
-rw-r--r-- | chrome/app/chrome_main.cc | 5 | ||||
-rw-r--r-- | chrome/browser/zygote_main_linux.cc | 8 | ||||
-rw-r--r-- | chrome/chrome_common.gypi | 4 | ||||
-rw-r--r-- | chrome/common/set_process_title.cc | 81 | ||||
-rw-r--r-- | chrome/common/set_process_title.h | 25 | ||||
-rw-r--r-- | chrome/common/set_process_title_linux.cc (renamed from base/setproctitle_linux.c) | 2 | ||||
-rw-r--r-- | chrome/common/set_process_title_linux.h (renamed from base/setproctitle_linux.h) | 17 | ||||
-rw-r--r-- | chrome/gpu/gpu_main.cc | 7 | ||||
-rw-r--r-- | chrome/plugin/plugin_main.cc | 4 | ||||
-rw-r--r-- | chrome/ppapi_plugin/ppapi_plugin_main.cc | 7 |
13 files changed, 138 insertions, 106 deletions
diff --git a/base/base.gypi b/base/base.gypi index 3eaedfa..b7bbcc4 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -352,8 +352,6 @@ 'gtk_util.cc', 'gtk_util.h', 'linux_util.cc', - 'setproctitle_linux.c', - 'setproctitle_linux.h', ], }, ], @@ -634,8 +632,6 @@ 'nss_util.h', 'openssl_util.cc', 'openssl_util.h', - 'setproctitle_linux.c', - 'setproctitle_linux.h', 'sha2.cc', 'sha2.h', 'sha2_openssl.cc', diff --git a/base/command_line.cc b/base/command_line.cc index 70d6872..66b4437 100644 --- a/base/command_line.cc +++ b/base/command_line.cc @@ -4,18 +4,6 @@ #include "base/command_line.h" -#if defined(OS_WIN) -#include <windows.h> -#include <shellapi.h> -#elif defined(OS_POSIX) -#include <limits.h> -#include <stdlib.h> -#include <unistd.h> -#endif -#if defined(OS_LINUX) -#include <sys/prctl.h> -#endif - #include <algorithm> #include "base/file_path.h" @@ -26,10 +14,15 @@ #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" +#include "build/build_config.h" -#if defined(OS_LINUX) -// Linux/glibc doesn't natively have setproctitle(). -#include "base/setproctitle_linux.h" +#if defined(OS_WIN) +#include <windows.h> +#include <shellapi.h> +#elif defined(OS_POSIX) +#include <limits.h> +#include <stdlib.h> +#include <unistd.h> #endif CommandLine* CommandLine::current_process_commandline_ = NULL; @@ -218,55 +211,8 @@ void CommandLine::Init(int argc, const char* const* argv) { #elif defined(OS_POSIX) current_process_commandline_->InitFromArgv(argc, argv); #endif - -#if defined(OS_LINUX) - if (argv) - setproctitle_init(const_cast<char**>(argv)); -#endif } -#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) -// static -void CommandLine::SetProcTitle() { - // Build a single string which consists of all the arguments separated - // by spaces. We can't actually keep them separate due to the way the - // setproctitle() function works. - std::string title; - bool have_argv0 = false; -#if defined(OS_LINUX) - // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us - // show up as "exe" in process listings. Read the symlink /proc/self/exe and - // use the path it points at for our process title. Note that this is only for - // display purposes and has no TOCTTOU security implications. - FilePath target; - FilePath self_exe("/proc/self/exe"); - if (file_util::ReadSymbolicLink(self_exe, &target)) { - have_argv0 = true; - title = target.value(); - // If the binary has since been deleted, Linux appends " (deleted)" to the - // symlink target. Remove it, since this is not really part of our name. - const std::string kDeletedSuffix = " (deleted)"; - if (EndsWith(title, kDeletedSuffix, true)) - title.resize(title.size() - kDeletedSuffix.size()); -#if defined(PR_SET_NAME) - // If PR_SET_NAME is available at compile time, we try using it. We ignore - // any errors if the kernel does not support it at runtime though. When - // available, this lets us set the short process name that shows when the - // full command line is not being displayed in most process listings. - prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); -#endif - } -#endif - for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { - if (!title.empty()) - title += " "; - title += current_process_commandline_->argv_[i]; - } - // Disable prepending argv[0] with '-' if we prepended it ourselves above. - setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); -} -#endif - void CommandLine::Reset() { DCHECK(current_process_commandline_ != NULL); delete current_process_commandline_; diff --git a/base/command_line.h b/base/command_line.h index df0293c..0e6ac26 100644 --- a/base/command_line.h +++ b/base/command_line.h @@ -17,13 +17,12 @@ #define BASE_COMMAND_LINE_H_ #pragma once -#include "build/build_config.h" - #include <map> #include <string> #include <vector> #include "base/basictypes.h" +#include "build/build_config.h" class FilePath; class InProcessBrowserTest; @@ -66,13 +65,6 @@ class CommandLine { // line, but it still must be called to set up the command line. static void Init(int argc, const char* const* argv); -#if defined(OS_POSIX) && !defined(OS_MACOSX) - // Sets the current process' arguments that show in "ps" etc. to those - // in |current_process_commandline_|. Used by the zygote host so that - // renderers show up with --type=renderer. - static void SetProcTitle(); -#endif - // Destroys the current process CommandLine singleton. This is necessary if // you want to reset the base library to its initial state (for example in an // outer library that needs to be able to terminate, and be re-initialized). diff --git a/chrome/app/chrome_main.cc b/chrome/app/chrome_main.cc index 86d2697..1b464a6 100644 --- a/chrome/app/chrome_main.cc +++ b/chrome/app/chrome_main.cc @@ -33,6 +33,7 @@ #include "chrome/common/logging_chrome.h" #include "chrome/common/main_function_params.h" #include "chrome/common/sandbox_init_wrapper.h" +#include "chrome/common/set_process_title.h" #include "chrome/common/url_constants.h" #include "ipc/ipc_switches.h" @@ -905,6 +906,10 @@ int ChromeMain(int argc, char** argv) { // as our process name since we exec() via that to be update-safe. #endif +#if defined(OS_POSIX) + SetProcessTitleFromCommandLine(argv); +#endif + int exit_code = RunNamedProcessTypeMain(process_type, main_params); if (SubprocessNeedsResourceBundle(process_type)) diff --git a/chrome/browser/zygote_main_linux.cc b/chrome/browser/zygote_main_linux.cc index c0ba88c..7199e14 100644 --- a/chrome/browser/zygote_main_linux.cc +++ b/chrome/browser/zygote_main_linux.cc @@ -42,6 +42,7 @@ #include "chrome/common/process_watcher.h" #include "chrome/common/result_codes.h" #include "chrome/common/sandbox_methods_linux.h" +#include "chrome/common/set_process_title.h" #include "chrome/common/unix_domain_socket_posix.h" #include "media/base/media.h" #include "seccompsandbox/sandbox.h" @@ -394,7 +395,12 @@ class Zygote { CommandLine::Reset(); CommandLine::Init(0, NULL); CommandLine::ForCurrentProcess()->InitFromArgv(args); - CommandLine::SetProcTitle(); + + // Update the process title. The argv was already cached by the call to + // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here + // (we don't have the original argv at this point). + SetProcessTitleFromCommandLine(NULL); + // The fork() request is handled further up the call stack. return true; } else if (child < 0) { diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi index 20b3093..a7cd981 100644 --- a/chrome/chrome_common.gypi +++ b/chrome/chrome_common.gypi @@ -150,6 +150,10 @@ 'common/sandbox_policy.h', 'common/serialized_script_value.cc', 'common/serialized_script_value.h', + 'common/set_process_title.cc', + 'common/set_process_title.h', + 'common/set_process_title_linux.cc', + 'common/set_process_title_linux.h', 'common/switch_utils.cc', 'common/switch_utils.h', 'common/time_format.cc', diff --git a/chrome/common/set_process_title.cc b/chrome/common/set_process_title.cc new file mode 100644 index 0000000..2a772db --- /dev/null +++ b/chrome/common/set_process_title.cc @@ -0,0 +1,81 @@ +// Copyright (c) 2010 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/common/set_process_title.h" + +#include "base/command_line.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/string_util.h" +#include "build/build_config.h" + +#if defined(OS_POSIX) +#include <limits.h> +#include <stdlib.h> +#include <unistd.h> +#endif + +#if defined(OS_LINUX) +#include <sys/prctl.h> + +// Linux/glibc doesn't natively have setproctitle(). +#include "chrome/common/set_process_title_linux.h" +#endif + +#if defined(OS_POSIX) && !defined(OS_MACOSX) + +void SetProcessTitleFromCommandLine(char** main_argv) { + // Build a single string which consists of all the arguments separated + // by spaces. We can't actually keep them separate due to the way the + // setproctitle() function works. + std::string title; + bool have_argv0 = false; + +#if defined(OS_LINUX) + if (main_argv) + setproctitle_init(main_argv); + + // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us + // show up as "exe" in process listings. Read the symlink /proc/self/exe and + // use the path it points at for our process title. Note that this is only for + // display purposes and has no TOCTTOU security implications. + FilePath target; + FilePath self_exe("/proc/self/exe"); + if (file_util::ReadSymbolicLink(self_exe, &target)) { + have_argv0 = true; + title = target.value(); + // If the binary has since been deleted, Linux appends " (deleted)" to the + // symlink target. Remove it, since this is not really part of our name. + const std::string kDeletedSuffix = " (deleted)"; + if (EndsWith(title, kDeletedSuffix, true)) + title.resize(title.size() - kDeletedSuffix.size()); +#if defined(PR_SET_NAME) + // If PR_SET_NAME is available at compile time, we try using it. We ignore + // any errors if the kernel does not support it at runtime though. When + // available, this lets us set the short process name that shows when the + // full command line is not being displayed in most process listings. + prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); +#endif + } +#endif + + const CommandLine* command_line = CommandLine::ForCurrentProcess(); + for (size_t i = 1; i < command_line->argv().size(); ++i) { + if (!title.empty()) + title += " "; + title += command_line->argv()[i]; + } + // Disable prepending argv[0] with '-' if we prepended it ourselves above. + setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); +} + +#else + +// All other systems (basically Windows & Mac) have no need or way to implement +// this function. +void SetProcessTitleFromCommandLine(char** /* main_argv */) { +} + +#endif + diff --git a/chrome/common/set_process_title.h b/chrome/common/set_process_title.h new file mode 100644 index 0000000..95defe8 --- /dev/null +++ b/chrome/common/set_process_title.h @@ -0,0 +1,25 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_COMMON_SET_PROCESS_TITLE_H_ +#define CHROME_COMMON_SET_PROCESS_TITLE_H_ + +// Sets OS-specific process title information based on the command line. This +// does nothing if the OS doesn't support or need this capability. +// +// Pass in the argv from main(). On Windows, where there is no argv, you can +// pass NULL or just don't call this function, since it does nothing. This +// argv pointer will be cached so if you call this function again, you can pass +// NULL in the second call. This is to support the case where it's called once +// at startup, and later when a zygote is fork()ed. The later call doesn't have +// easy access to main's argv. +// +// On non-Mac Unix platforms, we exec ourselves from /proc/self/exe, but that +// makes the process name that shows up in "ps" etc. for the child processes +// show as "exe" instead of "chrome" or something reasonable. This function +// will try to fix it so the "effective" command line shows up instead. +void SetProcessTitleFromCommandLine(char** main_argv); + +#endif // CHROME_COMMON_SET_PROCESS_TITLE_H_ + diff --git a/base/setproctitle_linux.c b/chrome/common/set_process_title_linux.cc index 9924c99..837eb2a 100644 --- a/base/setproctitle_linux.c +++ b/chrome/common/set_process_title_linux.cc @@ -37,7 +37,7 @@ // this position within the glibc project, leaving applications caught in the // middle. (Also, only a very few applications need or want this anyway.) -#include "base/setproctitle_linux.h" +#include "chrome/common/set_process_title_linux.h" #include <stdarg.h> #include <stdint.h> diff --git a/base/setproctitle_linux.h b/chrome/common/set_process_title_linux.h index 769338c..92fbf70 100644 --- a/base/setproctitle_linux.h +++ b/chrome/common/set_process_title_linux.h @@ -2,27 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef BASE_SETPROCTITLE_LINUX_H_ -#define BASE_SETPROCTITLE_LINUX_H_ +#ifndef CHROME_COMMON_SET_PROCESS_TITLE_LINUX_H_ +#define CHROME_COMMON_SET_PROCESS_TITLE_LINUX_H_ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - // Set the process title that will show in "ps" and similar tools. Takes // printf-style format string and arguments. After calling setproctitle() // the original main() argv[] array should not be used. By default, the // original argv[0] is prepended to the format; this can be disabled by // including a '-' as the first character of the format string. +// +// This signature and naming is to be compatible with most other Unix +// implementations of setproctitle(). void setproctitle(const char* fmt, ...); // Initialize state needed for setproctitle() on Linux. Pass the argv pointer // from main() to setproctitle_init() before calling setproctitle(). void setproctitle_init(char** main_argv); -#ifdef __cplusplus -} -#endif - -#endif // BASE_SETPROCTITLE_LINUX_H_ +#endif // CHROME_COMMON_SET_PROCESS_TITLE_LINUX_H_ diff --git a/chrome/gpu/gpu_main.cc b/chrome/gpu/gpu_main.cc index 2464c2f..2ef1b10 100644 --- a/chrome/gpu/gpu_main.cc +++ b/chrome/gpu/gpu_main.cc @@ -64,13 +64,6 @@ int GpuMain(const MainFunctionParams& parameters) { InitCrashReporter(); #endif -#if defined(OS_LINUX) - // On Linux we exec ourselves from /proc/self/exe, but that makes the - // process name that shows up in "ps" etc. for the GPU process show as - // "exe" instead of "chrome" or something reasonable. Try to fix it. - CommandLine::SetProcTitle(); -#endif - const CommandLine& command_line = parameters.command_line_; if (command_line.HasSwitch(switches::kGpuStartupDialog)) { ChildProcess::WaitForDebugger(L"Gpu"); diff --git a/chrome/plugin/plugin_main.cc b/chrome/plugin/plugin_main.cc index 9d97ed9..a59a5b5 100644 --- a/chrome/plugin/plugin_main.cc +++ b/chrome/plugin/plugin_main.cc @@ -99,10 +99,6 @@ int PluginMain(const MainFunctionParams& parameters) { const CommandLine& parsed_command_line = parameters.command_line_; #if defined(OS_LINUX) - // On Linux we exec ourselves from /proc/self/exe, but that makes the - // process name that shows up in "ps" etc. for plugins show as "exe" - // instead of "chrome" or something reasonable. Try to fix it. - CommandLine::SetProcTitle(); #if defined(ARCH_CPU_64_BITS) WorkaroundFlashLAHF(); diff --git a/chrome/ppapi_plugin/ppapi_plugin_main.cc b/chrome/ppapi_plugin/ppapi_plugin_main.cc index 6738860..3f3ccfe 100644 --- a/chrome/ppapi_plugin/ppapi_plugin_main.cc +++ b/chrome/ppapi_plugin/ppapi_plugin_main.cc @@ -11,13 +11,6 @@ // Main function for starting the PPAPI plugin process. int PpapiPluginMain(const MainFunctionParams& parameters) { -#if defined(OS_LINUX) - // On Linux we exec ourselves from /proc/self/exe, but that makes the - // process name that shows up in "ps" etc. for this process show as - // "exe" instead of "chrome" or something reasonable. Try to fix it. - CommandLine::SetProcTitle(); -#endif - const CommandLine& command_line = parameters.command_line_; if (command_line.HasSwitch(switches::kPpapiStartupDialog)) { ChildProcess::WaitForDebugger(L"Ppapi"); |