diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-29 21:06:43 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-29 21:06:43 +0000 |
commit | 74e9fa2c35b2fa8d293ad5369cc9cb6e9bbad2a1 (patch) | |
tree | 5a97c84fd8c1daee2e97e569ef9c8b43cf1a97f5 /base | |
parent | da827048d636a9c8cc5b0217077c2fe97cd6b8fe (diff) | |
download | chromium_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 'base')
-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-- | base/setproctitle_linux.c | 115 | ||||
-rw-r--r-- | base/setproctitle_linux.h | 28 |
5 files changed, 9 insertions, 218 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/base/setproctitle_linux.c b/base/setproctitle_linux.c deleted file mode 100644 index 9924c99..0000000 --- a/base/setproctitle_linux.c +++ /dev/null @@ -1,115 +0,0 @@ -// 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 "base/setproctitle_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/base/setproctitle_linux.h b/base/setproctitle_linux.h deleted file mode 100644 index 769338c..0000000 --- a/base/setproctitle_linux.h +++ /dev/null @@ -1,28 +0,0 @@ -// 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 BASE_SETPROCTITLE_LINUX_H_ -#define BASE_SETPROCTITLE_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. -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_ |