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

Unified Diff: base/command_line.cc

Issue 490028: Linux: use readlink() and prctl() in SetProcTitle() to fix "exe" showing in process listings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 12 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 | « no previous file | base/setproctitle_linux.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/command_line.cc
===================================================================
--- base/command_line.cc (revision 35439)
+++ base/command_line.cc (working copy)
@@ -7,10 +7,14 @@
#if defined(OS_WIN)
#include <windows.h>
#include <shellapi.h>
-#elif defined(OS_FREEBSD)
+#elif defined(OS_LINUX) || defined(OS_FREEBSD)
Evan Martin 2010/01/03 01:52:33 seems harmless to use this on osx, so why not just
+#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#endif
+#if defined(OS_LINUX)
+#include <sys/prctl.h>
+#endif
#include <algorithm>
@@ -207,12 +211,41 @@
// 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 as a security
+ // feature, but this makes us show up as "exe" in process listings. Read
Evan Martin 2010/01/03 01:52:33 not a security feature, it's so updates don't swap
+ // 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 r = readlink("/proc/self/exe", buffer, sizeof(buffer));
Evan Martin 2010/01/03 01:52:33 google style frowns on one-letter variable names.
+ DCHECK(r <= static_cast<ssize_t>(sizeof(buffer)));
+ if (r > 0) {
+ have_argv0 = true;
+ title.assign(buffer, r);
+ // 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
« no previous file with comments | « no previous file | base/setproctitle_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698