| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #elif defined(OS_FREEBSD) | 10 #elif defined(OS_POSIX) |
| 11 #include <limits.h> |
| 11 #include <stdlib.h> | 12 #include <stdlib.h> |
| 12 #include <unistd.h> | 13 #include <unistd.h> |
| 13 #endif | 14 #endif |
| 15 #if defined(OS_LINUX) |
| 16 #include <sys/prctl.h> |
| 17 #endif |
| 14 | 18 |
| 15 #include <algorithm> | 19 #include <algorithm> |
| 16 | 20 |
| 17 #include "base/file_path.h" | 21 #include "base/file_path.h" |
| 18 #include "base/logging.h" | 22 #include "base/logging.h" |
| 19 #include "base/singleton.h" | 23 #include "base/singleton.h" |
| 20 #include "base/string_piece.h" | 24 #include "base/string_piece.h" |
| 21 #include "base/string_util.h" | 25 #include "base/string_util.h" |
| 22 #include "base/sys_string_conversions.h" | 26 #include "base/sys_string_conversions.h" |
| 23 | 27 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 #endif | 204 #endif |
| 201 } | 205 } |
| 202 | 206 |
| 203 #if defined(OS_LINUX) || defined(OS_FREEBSD) | 207 #if defined(OS_LINUX) || defined(OS_FREEBSD) |
| 204 // static | 208 // static |
| 205 void CommandLine::SetProcTitle() { | 209 void CommandLine::SetProcTitle() { |
| 206 // Build a single string which consists of all the arguments separated | 210 // Build a single string which consists of all the arguments separated |
| 207 // by spaces. We can't actually keep them separate due to the way the | 211 // by spaces. We can't actually keep them separate due to the way the |
| 208 // setproctitle() function works. | 212 // setproctitle() function works. |
| 209 std::string title; | 213 std::string title; |
| 214 bool have_argv0 = false; |
| 215 #if defined(OS_LINUX) |
| 216 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us |
| 217 // show up as "exe" in process listings. Read the symlink /proc/self/exe and |
| 218 // use the path it points at for our process title. Note that this is only for |
| 219 // display purposes and has no TOCTTOU security implications. |
| 220 char buffer[PATH_MAX]; |
| 221 // Note: readlink() does not append a null byte to terminate the string. |
| 222 ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); |
| 223 DCHECK(length <= static_cast<ssize_t>(sizeof(buffer))); |
| 224 if (length > 0) { |
| 225 have_argv0 = true; |
| 226 title.assign(buffer, length); |
| 227 // If the binary has since been deleted, Linux appends " (deleted)" to the |
| 228 // symlink target. Remove it, since this is not really part of our name. |
| 229 const std::string kDeletedSuffix = " (deleted)"; |
| 230 if (EndsWith(title, kDeletedSuffix, true)) |
| 231 title.resize(title.size() - kDeletedSuffix.size()); |
| 232 #if defined(PR_SET_NAME) |
| 233 // If PR_SET_NAME is available at compile time, we try using it. We ignore |
| 234 // any errors if the kernel does not support it at runtime though. When |
| 235 // available, this lets us set the short process name that shows when the |
| 236 // full command line is not being displayed in most process listings. |
| 237 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); |
| 238 #endif |
| 239 } |
| 240 #endif |
| 210 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { | 241 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { |
| 211 if (!title.empty()) | 242 if (!title.empty()) |
| 212 title += " "; | 243 title += " "; |
| 213 title += current_process_commandline_->argv_[i]; | 244 title += current_process_commandline_->argv_[i]; |
| 214 } | 245 } |
| 215 setproctitle("%s", title.c_str()); | 246 // Disable prepending argv[0] with '-' if we prepended it ourselves above. |
| 247 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); |
| 216 } | 248 } |
| 217 #endif | 249 #endif |
| 218 | 250 |
| 219 void CommandLine::Reset() { | 251 void CommandLine::Reset() { |
| 220 DCHECK(current_process_commandline_ != NULL); | 252 DCHECK(current_process_commandline_ != NULL); |
| 221 delete current_process_commandline_; | 253 delete current_process_commandline_; |
| 222 current_process_commandline_ = NULL; | 254 current_process_commandline_ = NULL; |
| 223 } | 255 } |
| 224 | 256 |
| 225 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 257 bool CommandLine::HasSwitch(const std::string& switch_string) const { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { | 425 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { |
| 394 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 426 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 395 // we don't pretend to do anything fancy, we just split on spaces. | 427 // we don't pretend to do anything fancy, we just split on spaces. |
| 396 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); | 428 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); |
| 397 std::vector<std::string> wrapper_and_args; | 429 std::vector<std::string> wrapper_and_args; |
| 398 SplitString(wrapper, ' ', &wrapper_and_args); | 430 SplitString(wrapper, ' ', &wrapper_and_args); |
| 399 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | 431 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
| 400 } | 432 } |
| 401 | 433 |
| 402 #endif | 434 #endif |
| OLD | NEW |