summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-29 21:06:43 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-29 21:06:43 +0000
commit74e9fa2c35b2fa8d293ad5369cc9cb6e9bbad2a1 (patch)
tree5a97c84fd8c1daee2e97e569ef9c8b43cf1a97f5 /chrome
parentda827048d636a9c8cc5b0217077c2fe97cd6b8fe (diff)
downloadchromium_src-74e9fa2c35b2fa8d293ad5369cc9cb6e9bbad2a1.zip
chromium_src-74e9fa2c35b2fa8d293ad5369cc9cb6e9bbad2a1.tar.gz
chromium_src-74e9fa2c35b2fa8d293ad5369cc9cb6e9bbad2a1.tar.bz2
Move the SetProcTitle code out of base and into chrome/common. This is only
used to support the weird way Chrome manages processes, so doesn't belong in the central CommandLine class. This also provides an empty implementation on Mac & Windows to avoid some ifdefs in the main functions. TEST=everything compiles BUG=none Review URL: http://codereview.chromium.org/6002013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70276 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/app/chrome_main.cc5
-rw-r--r--chrome/browser/zygote_main_linux.cc8
-rw-r--r--chrome/chrome_common.gypi4
-rw-r--r--chrome/common/set_process_title.cc81
-rw-r--r--chrome/common/set_process_title.h25
-rw-r--r--chrome/common/set_process_title_linux.cc115
-rw-r--r--chrome/common/set_process_title_linux.h23
-rw-r--r--chrome/gpu/gpu_main.cc7
-rw-r--r--chrome/plugin/plugin_main.cc4
-rw-r--r--chrome/ppapi_plugin/ppapi_plugin_main.cc7
10 files changed, 260 insertions, 19 deletions
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/chrome/common/set_process_title_linux.cc b/chrome/common/set_process_title_linux.cc
new file mode 100644
index 0000000..837eb2a
--- /dev/null
+++ b/chrome/common/set_process_title_linux.cc
@@ -0,0 +1,115 @@
+// 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.
+
+// This file implements BSD-style setproctitle() for Linux.
+// It is written such that it can easily be compiled outside Chromium.
+//
+// The Linux kernel sets up two locations in memory to pass arguments and
+// environment variables to processes. First, there are two char* arrays stored
+// one after another: argv and environ. A pointer to argv is passed to main(),
+// while glibc sets the global variable |environ| to point at the latter. Both
+// of these arrays are terminated by a NULL pointer; the environment array is
+// also followed by some empty space to allow additional variables to be added.
+//
+// These arrays contain pointers to a second location in memory, where the
+// strings themselves are stored one after another: first all the arguments,
+// then the environment variables. The kernel will allocate a single page of
+// memory for this purpose, so the end of the page containing argv[0] is the
+// end of the storage potentially available to store the process title.
+//
+// When the kernel reads the command line arguments for a process, it looks at
+// the range of memory within this page that it initially used for the argument
+// list. If the terminating '\0' character is still where it expects, nothing
+// further is done. If it has been overwritten, the kernel will scan up to the
+// size of a page looking for another. (Note, however, that in general not that
+// much space is actually mapped, since argv[0] is rarely page-aligned and only
+// one page is mapped.)
+//
+// Thus to change the process title, we must move any environment variables out
+// of the way to make room for a potentially longer title, and then overwrite
+// the memory pointed to by argv[0] with a single replacement string, making
+// sure its size does not exceed the available space.
+//
+// It is perhaps worth noting that patches to add a system call to Linux for
+// this, like in BSD, have never made it in: this is the "official" way to do
+// this on Linux. Presumably it is not in glibc due to some disagreement over
+// this position within the glibc project, leaving applications caught in the
+// middle. (Also, only a very few applications need or want this anyway.)
+
+#include "chrome/common/set_process_title_linux.h"
+
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+extern char** environ;
+
+static char** g_main_argv = NULL;
+static char* g_orig_argv0 = NULL;
+
+void setproctitle(const char* fmt, ...) {
+ va_list ap;
+ size_t i, avail_size;
+ uintptr_t page_size, page, page_end;
+ // Sanity check before we try and set the process title.
+ // The BSD version allows fmt == NULL to restore the original title.
+ if (!g_main_argv || !environ || !fmt)
+ return;
+ if (!g_orig_argv0) {
+ // Save the original argv[0].
+ g_orig_argv0 = strdup(g_main_argv[0]);
+ if (!g_orig_argv0)
+ return;
+ }
+ page_size = sysconf(_SC_PAGESIZE);
+ // Get the page on which the argument list and environment live.
+ page = (uintptr_t) g_main_argv[0];
+ page -= page % page_size;
+ page_end = page + page_size;
+ // Move the environment out of the way. Note that we are moving the values,
+ // not the environment array itself (which may not be on the page we need
+ // to overwrite anyway).
+ for (i = 0; environ[i]; ++i) {
+ uintptr_t env_i = (uintptr_t) environ[i];
+ // Only move the value if it's actually in the way. This avoids
+ // leaking copies of the values if this function is called again.
+ if (page <= env_i && env_i < page_end) {
+ char* copy = strdup(environ[i]);
+ // Be paranoid. Check for allocation failure and bail out.
+ if (!copy)
+ return;
+ environ[i] = copy;
+ }
+ }
+ // Put the title in argv[0]. We have to zero out the space first since the
+ // kernel doesn't actually look for a null terminator unless we make the
+ // argument list longer than it started.
+ avail_size = page_end - (uintptr_t) g_main_argv[0];
+ memset(g_main_argv[0], 0, avail_size);
+ va_start(ap, fmt);
+ if (fmt[0] == '-') {
+ vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap);
+ } else {
+ size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0);
+ if (size < avail_size)
+ vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap);
+ }
+ va_end(ap);
+ g_main_argv[1] = NULL;
+}
+
+// A version of this built into glibc would not need this function, since
+// it could stash the argv pointer in __libc_start_main(). But we need it.
+void setproctitle_init(char** main_argv) {
+ if (g_main_argv)
+ return;
+
+ uintptr_t page_size = sysconf(_SC_PAGESIZE);
+ // Check that the argv array is in fact on the same page of memory
+ // as the environment array just as an added measure of protection.
+ if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size)
+ g_main_argv = main_argv;
+}
diff --git a/chrome/common/set_process_title_linux.h b/chrome/common/set_process_title_linux.h
new file mode 100644
index 0000000..92fbf70
--- /dev/null
+++ b/chrome/common/set_process_title_linux.h
@@ -0,0 +1,23 @@
+// 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.
+
+#ifndef CHROME_COMMON_SET_PROCESS_TITLE_LINUX_H_
+#define CHROME_COMMON_SET_PROCESS_TITLE_LINUX_H_
+#pragma once
+
+// 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);
+
+#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");