| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "chrome/common/set_process_title.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "build/build_config.h" | |
| 12 | |
| 13 #if defined(OS_POSIX) | |
| 14 #include <limits.h> | |
| 15 #include <stdlib.h> | |
| 16 #include <unistd.h> | |
| 17 #endif | |
| 18 | |
| 19 #if defined(OS_LINUX) | |
| 20 #include <sys/prctl.h> | |
| 21 | |
| 22 // Linux/glibc doesn't natively have setproctitle(). | |
| 23 #include "chrome/common/set_process_title_linux.h" | |
| 24 #endif | |
| 25 | |
| 26 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 27 | |
| 28 void SetProcessTitleFromCommandLine(char** main_argv) { | |
| 29 // Build a single string which consists of all the arguments separated | |
| 30 // by spaces. We can't actually keep them separate due to the way the | |
| 31 // setproctitle() function works. | |
| 32 std::string title; | |
| 33 bool have_argv0 = false; | |
| 34 | |
| 35 #if defined(OS_LINUX) | |
| 36 if (main_argv) | |
| 37 setproctitle_init(main_argv); | |
| 38 | |
| 39 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | |
| 40 // show up as "exe" in process listings. Read the symlink /proc/self/exe and | |
| 41 // use the path it points at for our process title. Note that this is only for | |
| 42 // display purposes and has no TOCTTOU security implications. | |
| 43 FilePath target; | |
| 44 FilePath self_exe("/proc/self/exe"); | |
| 45 if (file_util::ReadSymbolicLink(self_exe, &target)) { | |
| 46 have_argv0 = true; | |
| 47 title = target.value(); | |
| 48 // If the binary has since been deleted, Linux appends " (deleted)" to the | |
| 49 // symlink target. Remove it, since this is not really part of our name. | |
| 50 const std::string kDeletedSuffix = " (deleted)"; | |
| 51 if (EndsWith(title, kDeletedSuffix, true)) | |
| 52 title.resize(title.size() - kDeletedSuffix.size()); | |
| 53 #if defined(PR_SET_NAME) | |
| 54 // If PR_SET_NAME is available at compile time, we try using it. We ignore | |
| 55 // any errors if the kernel does not support it at runtime though. When | |
| 56 // available, this lets us set the short process name that shows when the | |
| 57 // full command line is not being displayed in most process listings. | |
| 58 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); | |
| 59 #endif | |
| 60 } | |
| 61 #endif | |
| 62 | |
| 63 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 64 for (size_t i = 1; i < command_line->argv().size(); ++i) { | |
| 65 if (!title.empty()) | |
| 66 title += " "; | |
| 67 title += command_line->argv()[i]; | |
| 68 } | |
| 69 // Disable prepending argv[0] with '-' if we prepended it ourselves above. | |
| 70 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); | |
| 71 } | |
| 72 | |
| 73 #else | |
| 74 | |
| 75 // All other systems (basically Windows & Mac) have no need or way to implement | |
| 76 // this function. | |
| 77 void SetProcessTitleFromCommandLine(char** /* main_argv */) { | |
| 78 } | |
| 79 | |
| 80 #endif | |
| 81 | |
| OLD | NEW |