| 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/string_util.h" | 25 #include "base/string_util.h" |
| 25 // Linux/glibc doesn't natively have setproctitle(). | 26 // Linux/glibc doesn't natively have setproctitle(). |
| 26 #include "content/common/set_process_title_linux.h" | 27 #include "content/common/set_process_title_linux.h" |
| 27 #endif // defined(OS_LINUX) | 28 #endif // defined(OS_LINUX) |
| 28 | 29 |
| 29 // TODO(jrg): Find out if setproctitle or equivalent is available on Android. | 30 // TODO(jrg): Find out if setproctitle or equivalent is available on Android. |
| 30 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \ | 31 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \ |
| 31 !defined(OS_ANDROID) | 32 !defined(OS_ANDROID) |
| 32 | 33 |
| 33 void SetProcessTitleFromCommandLine(const char** main_argv) { | 34 void SetProcessTitleFromCommandLine(const char** main_argv) { |
| 34 // Build a single string which consists of all the arguments separated | 35 // Build a single string which consists of all the arguments separated |
| 35 // by spaces. We can't actually keep them separate due to the way the | 36 // by spaces. We can't actually keep them separate due to the way the |
| 36 // setproctitle() function works. | 37 // setproctitle() function works. |
| 37 std::string title; | 38 std::string title; |
| 38 bool have_argv0 = false; | 39 bool have_argv0 = false; |
| 39 | 40 |
| 40 #if defined(OS_LINUX) | 41 #if defined(OS_LINUX) |
| 41 if (main_argv) | 42 if (main_argv) |
| 42 setproctitle_init(main_argv); | 43 setproctitle_init(main_argv); |
| 43 | 44 |
| 44 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | 45 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us |
| 45 // show up as "exe" in process listings. Read the symlink /proc/self/exe and | 46 // show up as "exe" in process listings. Read the symlink /proc/self/exe and |
| 46 // use the path it points at for our process title. Note that this is only for | 47 // use the path it points at for our process title. Note that this is only for |
| 47 // display purposes and has no TOCTTOU security implications. | 48 // display purposes and has no TOCTTOU security implications. |
| 48 FilePath target; | 49 FilePath target; |
| 49 FilePath self_exe("/proc/self/exe"); | 50 FilePath self_exe(base::kProcSelfExe); |
| 50 if (file_util::ReadSymbolicLink(self_exe, &target)) { | 51 if (file_util::ReadSymbolicLink(self_exe, &target)) { |
| 51 have_argv0 = true; | 52 have_argv0 = true; |
| 52 title = target.value(); | 53 title = target.value(); |
| 53 // If the binary has since been deleted, Linux appends " (deleted)" to the | 54 // If the binary has since been deleted, Linux appends " (deleted)" to the |
| 54 // symlink target. Remove it, since this is not really part of our name. | 55 // symlink target. Remove it, since this is not really part of our name. |
| 55 const std::string kDeletedSuffix = " (deleted)"; | 56 const std::string kDeletedSuffix = " (deleted)"; |
| 56 if (EndsWith(title, kDeletedSuffix, true)) | 57 if (EndsWith(title, kDeletedSuffix, true)) |
| 57 title.resize(title.size() - kDeletedSuffix.size()); | 58 title.resize(title.size() - kDeletedSuffix.size()); |
| 58 #if defined(PR_SET_NAME) | 59 #if defined(PR_SET_NAME) |
| 59 // If PR_SET_NAME is available at compile time, we try using it. We ignore | 60 // If PR_SET_NAME is available at compile time, we try using it. We ignore |
| (...skipping 16 matching lines...) Expand all Loading... |
| 76 } | 77 } |
| 77 | 78 |
| 78 #else | 79 #else |
| 79 | 80 |
| 80 // All other systems (basically Windows & Mac) have no need or way to implement | 81 // All other systems (basically Windows & Mac) have no need or way to implement |
| 81 // this function. | 82 // this function. |
| 82 void SetProcessTitleFromCommandLine(const char** /* main_argv */) { | 83 void SetProcessTitleFromCommandLine(const char** /* main_argv */) { |
| 83 } | 84 } |
| 84 | 85 |
| 85 #endif | 86 #endif |
| OLD | NEW |