| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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) | |
| 8 #include <windows.h> | |
| 9 #include <shellapi.h> | |
| 10 #elif defined(OS_POSIX) | |
| 11 #include <limits.h> | |
| 12 #include <stdlib.h> | |
| 13 #include <unistd.h> | |
| 14 #endif | |
| 15 #if defined(OS_LINUX) | |
| 16 #include <sys/prctl.h> | |
| 17 #endif | |
| 18 | |
| 19 #include <algorithm> | 7 #include <algorithm> |
| 20 | 8 |
| 21 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 22 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 23 #include "base/logging.h" | 11 #include "base/logging.h" |
| 24 #include "base/singleton.h" | 12 #include "base/singleton.h" |
| 25 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 26 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 27 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
| 28 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "build/build_config.h" |
| 29 | 18 |
| 30 #if defined(OS_LINUX) | 19 #if defined(OS_WIN) |
| 31 // Linux/glibc doesn't natively have setproctitle(). | 20 #include <windows.h> |
| 32 #include "base/setproctitle_linux.h" | 21 #include <shellapi.h> |
| 22 #elif defined(OS_POSIX) |
| 23 #include <limits.h> |
| 24 #include <stdlib.h> |
| 25 #include <unistd.h> |
| 33 #endif | 26 #endif |
| 34 | 27 |
| 35 CommandLine* CommandLine::current_process_commandline_ = NULL; | 28 CommandLine* CommandLine::current_process_commandline_ = NULL; |
| 36 | 29 |
| 37 // Since we use a lazy match, make sure that longer versions (like L"--") | 30 // Since we use a lazy match, make sure that longer versions (like L"--") |
| 38 // are listed before shorter versions (like L"-") of similar prefixes. | 31 // are listed before shorter versions (like L"-") of similar prefixes. |
| 39 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
| 40 const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | 33 const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| 41 const wchar_t kSwitchTerminator[] = L"--"; | 34 const wchar_t kSwitchTerminator[] = L"--"; |
| 42 const wchar_t kSwitchValueSeparator[] = L"="; | 35 const wchar_t kSwitchValueSeparator[] = L"="; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 204 |
| 212 // static | 205 // static |
| 213 void CommandLine::Init(int argc, const char* const* argv) { | 206 void CommandLine::Init(int argc, const char* const* argv) { |
| 214 delete current_process_commandline_; | 207 delete current_process_commandline_; |
| 215 current_process_commandline_ = new CommandLine; | 208 current_process_commandline_ = new CommandLine; |
| 216 #if defined(OS_WIN) | 209 #if defined(OS_WIN) |
| 217 current_process_commandline_->ParseFromString(::GetCommandLineW()); | 210 current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 218 #elif defined(OS_POSIX) | 211 #elif defined(OS_POSIX) |
| 219 current_process_commandline_->InitFromArgv(argc, argv); | 212 current_process_commandline_->InitFromArgv(argc, argv); |
| 220 #endif | 213 #endif |
| 221 | |
| 222 #if defined(OS_LINUX) | |
| 223 if (argv) | |
| 224 setproctitle_init(const_cast<char**>(argv)); | |
| 225 #endif | |
| 226 } | 214 } |
| 227 | 215 |
| 228 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) | |
| 229 // static | |
| 230 void CommandLine::SetProcTitle() { | |
| 231 // Build a single string which consists of all the arguments separated | |
| 232 // by spaces. We can't actually keep them separate due to the way the | |
| 233 // setproctitle() function works. | |
| 234 std::string title; | |
| 235 bool have_argv0 = false; | |
| 236 #if defined(OS_LINUX) | |
| 237 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | |
| 238 // show up as "exe" in process listings. Read the symlink /proc/self/exe and | |
| 239 // use the path it points at for our process title. Note that this is only for | |
| 240 // display purposes and has no TOCTTOU security implications. | |
| 241 FilePath target; | |
| 242 FilePath self_exe("/proc/self/exe"); | |
| 243 if (file_util::ReadSymbolicLink(self_exe, &target)) { | |
| 244 have_argv0 = true; | |
| 245 title = target.value(); | |
| 246 // If the binary has since been deleted, Linux appends " (deleted)" to the | |
| 247 // symlink target. Remove it, since this is not really part of our name. | |
| 248 const std::string kDeletedSuffix = " (deleted)"; | |
| 249 if (EndsWith(title, kDeletedSuffix, true)) | |
| 250 title.resize(title.size() - kDeletedSuffix.size()); | |
| 251 #if defined(PR_SET_NAME) | |
| 252 // If PR_SET_NAME is available at compile time, we try using it. We ignore | |
| 253 // any errors if the kernel does not support it at runtime though. When | |
| 254 // available, this lets us set the short process name that shows when the | |
| 255 // full command line is not being displayed in most process listings. | |
| 256 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); | |
| 257 #endif | |
| 258 } | |
| 259 #endif | |
| 260 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { | |
| 261 if (!title.empty()) | |
| 262 title += " "; | |
| 263 title += current_process_commandline_->argv_[i]; | |
| 264 } | |
| 265 // Disable prepending argv[0] with '-' if we prepended it ourselves above. | |
| 266 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); | |
| 267 } | |
| 268 #endif | |
| 269 | |
| 270 void CommandLine::Reset() { | 216 void CommandLine::Reset() { |
| 271 DCHECK(current_process_commandline_ != NULL); | 217 DCHECK(current_process_commandline_ != NULL); |
| 272 delete current_process_commandline_; | 218 delete current_process_commandline_; |
| 273 current_process_commandline_ = NULL; | 219 current_process_commandline_ = NULL; |
| 274 } | 220 } |
| 275 | 221 |
| 276 // static | 222 // static |
| 277 CommandLine* CommandLine::ForCurrentProcess() { | 223 CommandLine* CommandLine::ForCurrentProcess() { |
| 278 DCHECK(current_process_commandline_); | 224 DCHECK(current_process_commandline_); |
| 279 return current_process_commandline_; | 225 return current_process_commandline_; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 | 473 |
| 528 // private | 474 // private |
| 529 CommandLine::CommandLine() { | 475 CommandLine::CommandLine() { |
| 530 } | 476 } |
| 531 | 477 |
| 532 // static | 478 // static |
| 533 CommandLine* CommandLine::ForCurrentProcessMutable() { | 479 CommandLine* CommandLine::ForCurrentProcessMutable() { |
| 534 DCHECK(current_process_commandline_); | 480 DCHECK(current_process_commandline_); |
| 535 return current_process_commandline_; | 481 return current_process_commandline_; |
| 536 } | 482 } |
| OLD | NEW |