summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-03 02:20:08 +0000
committermdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-03 02:20:08 +0000
commitf07bd1ecc85ac808de4efec5ba8a0f7372cfa694 (patch)
tree3b3a0a6f55f53fdcc731810c274fba78578acdb2
parenta88f9709ca82ecf4a2d6a95972d18b921b98c6af (diff)
downloadchromium_src-f07bd1ecc85ac808de4efec5ba8a0f7372cfa694.zip
chromium_src-f07bd1ecc85ac808de4efec5ba8a0f7372cfa694.tar.gz
chromium_src-f07bd1ecc85ac808de4efec5ba8a0f7372cfa694.tar.bz2
Linux: use readlink() and prctl() in SetProcTitle() to fix "exe" showing in process listings.
BUG=29118 TEST=none Review URL: http://codereview.chromium.org/490028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35441 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/command_line.cc36
-rw-r--r--base/setproctitle_linux.h4
-rw-r--r--chrome/plugin/plugin_main.cc7
3 files changed, 43 insertions, 4 deletions
diff --git a/base/command_line.cc b/base/command_line.cc
index 35f839c..5f27064 100644
--- a/base/command_line.cc
+++ b/base/command_line.cc
@@ -7,10 +7,14 @@
#if defined(OS_WIN)
#include <windows.h>
#include <shellapi.h>
-#elif defined(OS_FREEBSD)
+#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>
@@ -207,12 +211,40 @@ void CommandLine::SetProcTitle() {
// 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.
+ char buffer[PATH_MAX];
+ // Note: readlink() does not append a null byte to terminate the string.
+ ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer));
+ DCHECK(length <= static_cast<ssize_t>(sizeof(buffer)));
+ if (length > 0) {
+ have_argv0 = true;
+ title.assign(buffer, length);
+ // 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];
}
- setproctitle("%s", title.c_str());
+ // Disable prepending argv[0] with '-' if we prepended it ourselves above.
+ setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
}
#endif
diff --git a/base/setproctitle_linux.h b/base/setproctitle_linux.h
index 32cc297..c1cf689 100644
--- a/base/setproctitle_linux.h
+++ b/base/setproctitle_linux.h
@@ -11,7 +11,9 @@ extern "C" {
// 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.
+// 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.
void setproctitle(const char* fmt, ...);
// Initialize state needed for setproctitle() on Linux. Pass the argv pointer
diff --git a/chrome/plugin/plugin_main.cc b/chrome/plugin/plugin_main.cc
index 319ccce..1cbcc57 100644
--- a/chrome/plugin/plugin_main.cc
+++ b/chrome/plugin/plugin_main.cc
@@ -62,7 +62,12 @@ int PluginMain(const MainFunctionParams& parameters) {
const CommandLine& parsed_command_line = parameters.command_line_;
-#if defined(OS_WIN)
+#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();
+#elif defined(OS_WIN)
sandbox::TargetServices* target_services =
parameters.sandbox_info_.TargetServices();