| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/service_manager/embedder/set_process_title.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "build/build_config.h" | |
| 10 | |
| 11 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) | |
| 12 #include <limits.h> | |
| 13 #include <stdlib.h> | |
| 14 #include <unistd.h> | |
| 15 | |
| 16 #include <string> | |
| 17 | |
| 18 #include "base/command_line.h" | |
| 19 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) | |
| 20 | |
| 21 #if defined(OS_LINUX) | |
| 22 #include <sys/prctl.h> | |
| 23 | |
| 24 #include "base/files/file_path.h" | |
| 25 #include "base/files/file_util.h" | |
| 26 #include "base/process/process_metrics.h" | |
| 27 #include "base/strings/string_util.h" | |
| 28 #include "base/threading/platform_thread.h" | |
| 29 // Linux/glibc doesn't natively have setproctitle(). | |
| 30 #include "services/service_manager/embedder/set_process_title_linux.h" | |
| 31 #endif // defined(OS_LINUX) | |
| 32 | |
| 33 namespace service_manager { | |
| 34 | |
| 35 // TODO(jrg): Find out if setproctitle or equivalent is available on Android. | |
| 36 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \ | |
| 37 !defined(OS_ANDROID) | |
| 38 | |
| 39 void SetProcessTitleFromCommandLine(const char** main_argv) { | |
| 40 // Build a single string which consists of all the arguments separated | |
| 41 // by spaces. We can't actually keep them separate due to the way the | |
| 42 // setproctitle() function works. | |
| 43 std::string title; | |
| 44 bool have_argv0 = false; | |
| 45 | |
| 46 #if defined(OS_LINUX) | |
| 47 DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); | |
| 48 | |
| 49 if (main_argv) | |
| 50 setproctitle_init(main_argv); | |
| 51 | |
| 52 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | |
| 53 // show up as "exe" in process listings. Read the symlink /proc/self/exe and | |
| 54 // use the path it points at for our process title. Note that this is only for | |
| 55 // display purposes and has no TOCTTOU security implications. | |
| 56 base::FilePath target; | |
| 57 base::FilePath self_exe(base::kProcSelfExe); | |
| 58 if (base::ReadSymbolicLink(self_exe, &target)) { | |
| 59 have_argv0 = true; | |
| 60 title = target.value(); | |
| 61 // If the binary has since been deleted, Linux appends " (deleted)" to the | |
| 62 // symlink target. Remove it, since this is not really part of our name. | |
| 63 const std::string kDeletedSuffix = " (deleted)"; | |
| 64 if (base::EndsWith(title, kDeletedSuffix, base::CompareCase::SENSITIVE)) | |
| 65 title.resize(title.size() - kDeletedSuffix.size()); | |
| 66 | |
| 67 // PR_SET_NAME is available in Linux 2.6.9 and newer. | |
| 68 // When available at run time, this sets the short process name that shows | |
| 69 // when the full command line is not being displayed in most process | |
| 70 // listings. | |
| 71 prctl(PR_SET_NAME, base::FilePath(title).BaseName().value().c_str()); | |
| 72 } | |
| 73 #endif // defined(OS_LINUX) | |
| 74 | |
| 75 const base::CommandLine* command_line = | |
| 76 base::CommandLine::ForCurrentProcess(); | |
| 77 for (size_t i = 1; i < command_line->argv().size(); ++i) { | |
| 78 if (!title.empty()) | |
| 79 title += " "; | |
| 80 title += command_line->argv()[i]; | |
| 81 } | |
| 82 // Disable prepending argv[0] with '-' if we prepended it ourselves above. | |
| 83 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); | |
| 84 } | |
| 85 | |
| 86 #else | |
| 87 | |
| 88 // All other systems (basically Windows & Mac) have no need or way to implement | |
| 89 // this function. | |
| 90 void SetProcessTitleFromCommandLine(const char** /* main_argv */) {} | |
| 91 | |
| 92 #endif | |
| 93 | |
| 94 } // namespace service_manager | |
| OLD | NEW |