summaryrefslogtreecommitdiffstats
path: root/chrome/common
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/common
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/common')
-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
4 files changed, 244 insertions, 0 deletions
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_