Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/set_process_title.h" | 5 #include "content/common/set_process_title.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) | 9 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) |
| 10 #include <limits.h> | 10 #include <limits.h> |
| 11 #include <stdlib.h> | 11 #include <stdlib.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) | 17 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) |
| 18 | 18 |
| 19 #if defined(OS_LINUX) | 19 #if defined(OS_LINUX) |
| 20 #include <sys/prctl.h> | 20 #include <sys/prctl.h> |
| 21 | 21 |
| 22 #include "base/file_path.h" | 22 #include "base/file_path.h" |
| 23 #include "base/file_util.h" | 23 #include "base/file_util.h" |
| 24 #include "base/process_util.h" | 24 #include "base/process_util.h" |
| 25 #include "base/string_util.h" | 25 #include "base/string_util.h" |
| 26 // Linux/glibc doesn't natively have setproctitle(). | 26 // Linux/glibc doesn't natively have SetProcTitle(). |
| 27 #include "content/common/set_process_title_linux.h" | 27 #include "content/common/set_process_title_linux.h" |
| 28 #endif // defined(OS_LINUX) | 28 #endif // defined(OS_LINUX) |
| 29 | 29 |
| 30 // TODO(jrg): Find out if setproctitle or equivalent is available on Android. | 30 namespace content { |
| 31 | |
| 32 // TODO(jrg): Find out if SetProcTitle or equivalent is available on Android. | |
| 31 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \ | 33 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \ |
| 32 !defined(OS_ANDROID) | 34 !defined(OS_ANDROID) |
| 33 | 35 |
| 34 void SetProcessTitleFromCommandLine(const char** main_argv) { | 36 void SetProcessTitleFromCommandLine(const char** main_argv) { |
| 35 // Build a single string which consists of all the arguments separated | 37 // Build a single string which consists of all the arguments separated |
| 36 // by spaces. We can't actually keep them separate due to the way the | 38 // by spaces. We can't actually keep them separate due to the way the |
| 37 // setproctitle() function works. | 39 // SetProcTitle() function works. |
| 38 std::string title; | 40 std::string title; |
| 39 bool have_argv0 = false; | 41 bool have_argv0 = false; |
| 40 | 42 |
| 41 #if defined(OS_LINUX) | 43 #if defined(OS_LINUX) |
| 42 if (main_argv) | 44 if (main_argv) |
| 43 setproctitle_init(main_argv); | 45 SetProcTitleInit(main_argv); |
| 44 | 46 |
| 45 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | 47 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us |
| 46 // show up as "exe" in process listings. Read the symlink /proc/self/exe and | 48 // show up as "exe" in process listings. Read the symlink /proc/self/exe and |
| 47 // use the path it points at for our process title. Note that this is only for | 49 // use the path it points at for our process title. Note that this is only for |
| 48 // display purposes and has no TOCTTOU security implications. | 50 // display purposes and has no TOCTTOU security implications. |
| 49 FilePath target; | 51 FilePath target; |
| 50 FilePath self_exe(base::kProcSelfExe); | 52 FilePath self_exe(base::kProcSelfExe); |
| 51 if (file_util::ReadSymbolicLink(self_exe, &target)) { | 53 if (file_util::ReadSymbolicLink(self_exe, &target)) { |
| 52 have_argv0 = true; | 54 have_argv0 = true; |
| 53 title = target.value(); | 55 title = target.value(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 66 } | 68 } |
| 67 #endif // defined(OS_LINUX) | 69 #endif // defined(OS_LINUX) |
| 68 | 70 |
| 69 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 71 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 70 for (size_t i = 1; i < command_line->argv().size(); ++i) { | 72 for (size_t i = 1; i < command_line->argv().size(); ++i) { |
| 71 if (!title.empty()) | 73 if (!title.empty()) |
| 72 title += " "; | 74 title += " "; |
| 73 title += command_line->argv()[i]; | 75 title += command_line->argv()[i]; |
| 74 } | 76 } |
| 75 // Disable prepending argv[0] with '-' if we prepended it ourselves above. | 77 // Disable prepending argv[0] with '-' if we prepended it ourselves above. |
| 76 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); | 78 SetProcTitle(have_argv0 ? "-%s" : "%s", title.c_str()); |
|
scottmg
2012/10/22 23:42:19
this rename will break on other posix != linux? no
| |
| 77 } | 79 } |
| 78 | 80 |
| 79 #else | 81 #else |
| 80 | 82 |
| 81 // All other systems (basically Windows & Mac) have no need or way to implement | 83 // All other systems (basically Windows & Mac) have no need or way to implement |
| 82 // this function. | 84 // this function. |
| 83 void SetProcessTitleFromCommandLine(const char** /* main_argv */) { | 85 void SetProcessTitleFromCommandLine(const char** /* main_argv */) { |
| 84 } | 86 } |
| 85 | 87 |
| 86 #endif | 88 #endif |
| 89 | |
| 90 } // namespace content | |
| OLD | NEW |