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 "chrome/common/set_process_title.h" |
| 6 |
5 #include "base/command_line.h" | 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" |
6 | 12 |
7 #if defined(OS_WIN) | 13 #if defined(OS_POSIX) |
8 #include <windows.h> | |
9 #include <shellapi.h> | |
10 #elif defined(OS_POSIX) | |
11 #include <limits.h> | 14 #include <limits.h> |
12 #include <stdlib.h> | 15 #include <stdlib.h> |
13 #include <unistd.h> | 16 #include <unistd.h> |
14 #endif | 17 #endif |
| 18 |
15 #if defined(OS_LINUX) | 19 #if defined(OS_LINUX) |
16 #include <sys/prctl.h> | 20 #include <sys/prctl.h> |
| 21 |
| 22 // Linux/glibc doesn't natively have setproctitle(). |
| 23 #include "chrome/common/set_process_title_linux.h" |
17 #endif | 24 #endif |
18 | 25 |
19 #include <algorithm> | 26 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
20 | 27 |
21 #include "base/file_path.h" | 28 void SetProcessTitleFromCommandLine(char** main_argv) { |
22 #include "base/file_util.h" | |
23 #include "base/logging.h" | |
24 #include "base/singleton.h" | |
25 #include "base/string_split.h" | |
26 #include "base/string_util.h" | |
27 #include "base/sys_string_conversions.h" | |
28 #include "base/utf_string_conversions.h" | |
29 | |
30 #if defined(OS_LINUX) | |
31 // Linux/glibc doesn't natively have setproctitle(). | |
32 #include "base/setproctitle_linux.h" | |
33 #endif | |
34 | |
35 CommandLine* CommandLine::current_process_commandline_ = NULL; | |
36 | |
37 // Since we use a lazy match, make sure that longer versions (like L"--") | |
38 // are listed before shorter versions (like L"-") of similar prefixes. | |
39 #if defined(OS_WIN) | |
40 const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; | |
41 const wchar_t kSwitchTerminator[] = L"--"; | |
42 const wchar_t kSwitchValueSeparator[] = L"="; | |
43 #elif defined(OS_POSIX) | |
44 // Unixes don't use slash as a switch. | |
45 const char* const kSwitchPrefixes[] = {"--", "-"}; | |
46 const char kSwitchTerminator[] = "--"; | |
47 const char kSwitchValueSeparator[] = "="; | |
48 #endif | |
49 | |
50 #if defined(OS_WIN) | |
51 // Lowercase a string. This is used to lowercase switch names. | |
52 // Is this what we really want? It seems crazy to me. I've left it in | |
53 // for backwards compatibility on Windows. | |
54 static void Lowercase(std::string* parameter) { | |
55 transform(parameter->begin(), parameter->end(), parameter->begin(), | |
56 tolower); | |
57 } | |
58 #endif | |
59 | |
60 CommandLine::~CommandLine() { | |
61 } | |
62 | |
63 #if defined(OS_WIN) | |
64 CommandLine::CommandLine(NoProgram no_program) { | |
65 } | |
66 | |
67 void CommandLine::ParseFromString(const std::wstring& command_line) { | |
68 TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); | |
69 | |
70 if (command_line_string_.empty()) | |
71 return; | |
72 | |
73 int num_args = 0; | |
74 wchar_t** args = NULL; | |
75 | |
76 args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); | |
77 | |
78 // Populate program_ with the trimmed version of the first arg. | |
79 TrimWhitespace(args[0], TRIM_ALL, &program_); | |
80 | |
81 bool parse_switches = true; | |
82 for (int i = 1; i < num_args; ++i) { | |
83 std::wstring arg; | |
84 TrimWhitespace(args[i], TRIM_ALL, &arg); | |
85 | |
86 if (!parse_switches) { | |
87 args_.push_back(arg); | |
88 continue; | |
89 } | |
90 | |
91 if (arg == kSwitchTerminator) { | |
92 parse_switches = false; | |
93 continue; | |
94 } | |
95 | |
96 std::string switch_string; | |
97 std::wstring switch_value; | |
98 if (IsSwitch(arg, &switch_string, &switch_value)) { | |
99 switches_[switch_string] = switch_value; | |
100 } else { | |
101 args_.push_back(arg); | |
102 } | |
103 } | |
104 | |
105 if (args) | |
106 LocalFree(args); | |
107 } | |
108 | |
109 // static | |
110 CommandLine CommandLine::FromString(const std::wstring& command_line) { | |
111 CommandLine cmd; | |
112 cmd.ParseFromString(command_line); | |
113 return cmd; | |
114 } | |
115 | |
116 CommandLine::CommandLine(const FilePath& program) { | |
117 if (!program.empty()) { | |
118 program_ = program.value(); | |
119 // TODO(evanm): proper quoting here. | |
120 command_line_string_ = L'"' + program.value() + L'"'; | |
121 } | |
122 } | |
123 | |
124 #elif defined(OS_POSIX) | |
125 CommandLine::CommandLine(NoProgram no_program) { | |
126 // Push an empty argument, because we always assume argv_[0] is a program. | |
127 argv_.push_back(""); | |
128 } | |
129 | |
130 CommandLine::CommandLine(int argc, const char* const* argv) { | |
131 InitFromArgv(argc, argv); | |
132 } | |
133 | |
134 CommandLine::CommandLine(const std::vector<std::string>& argv) { | |
135 InitFromArgv(argv); | |
136 } | |
137 | |
138 void CommandLine::InitFromArgv(int argc, const char* const* argv) { | |
139 for (int i = 0; i < argc; ++i) | |
140 argv_.push_back(argv[i]); | |
141 InitFromArgv(argv_); | |
142 } | |
143 | |
144 void CommandLine::InitFromArgv(const std::vector<std::string>& argv) { | |
145 argv_ = argv; | |
146 bool parse_switches = true; | |
147 for (size_t i = 1; i < argv_.size(); ++i) { | |
148 const std::string& arg = argv_[i]; | |
149 | |
150 if (!parse_switches) { | |
151 args_.push_back(arg); | |
152 continue; | |
153 } | |
154 | |
155 if (arg == kSwitchTerminator) { | |
156 parse_switches = false; | |
157 continue; | |
158 } | |
159 | |
160 std::string switch_string; | |
161 std::string switch_value; | |
162 if (IsSwitch(arg, &switch_string, &switch_value)) { | |
163 switches_[switch_string] = switch_value; | |
164 } else { | |
165 args_.push_back(arg); | |
166 } | |
167 } | |
168 } | |
169 | |
170 CommandLine::CommandLine(const FilePath& program) { | |
171 argv_.push_back(program.value()); | |
172 } | |
173 | |
174 #endif | |
175 | |
176 // static | |
177 bool CommandLine::IsSwitch(const StringType& parameter_string, | |
178 std::string* switch_string, | |
179 StringType* switch_value) { | |
180 switch_string->clear(); | |
181 switch_value->clear(); | |
182 | |
183 for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { | |
184 StringType prefix(kSwitchPrefixes[i]); | |
185 if (parameter_string.find(prefix) != 0) | |
186 continue; | |
187 | |
188 const size_t switch_start = prefix.length(); | |
189 const size_t equals_position = parameter_string.find( | |
190 kSwitchValueSeparator, switch_start); | |
191 StringType switch_native; | |
192 if (equals_position == StringType::npos) { | |
193 switch_native = parameter_string.substr(switch_start); | |
194 } else { | |
195 switch_native = parameter_string.substr( | |
196 switch_start, equals_position - switch_start); | |
197 *switch_value = parameter_string.substr(equals_position + 1); | |
198 } | |
199 #if defined(OS_WIN) | |
200 *switch_string = WideToASCII(switch_native); | |
201 Lowercase(switch_string); | |
202 #else | |
203 *switch_string = switch_native; | |
204 #endif | |
205 | |
206 return true; | |
207 } | |
208 | |
209 return false; | |
210 } | |
211 | |
212 // static | |
213 void CommandLine::Init(int argc, const char* const* argv) { | |
214 delete current_process_commandline_; | |
215 current_process_commandline_ = new CommandLine; | |
216 #if defined(OS_WIN) | |
217 current_process_commandline_->ParseFromString(::GetCommandLineW()); | |
218 #elif defined(OS_POSIX) | |
219 current_process_commandline_->InitFromArgv(argc, argv); | |
220 #endif | |
221 | |
222 #if defined(OS_LINUX) | |
223 if (argv) | |
224 setproctitle_init(const_cast<char**>(argv)); | |
225 #endif | |
226 } | |
227 | |
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 | 29 // 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 | 30 // by spaces. We can't actually keep them separate due to the way the |
233 // setproctitle() function works. | 31 // setproctitle() function works. |
234 std::string title; | 32 std::string title; |
235 bool have_argv0 = false; | 33 bool have_argv0 = false; |
| 34 |
236 #if defined(OS_LINUX) | 35 #if defined(OS_LINUX) |
| 36 if (main_argv) |
| 37 setproctitle_init(main_argv); |
| 38 |
237 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us | 39 // 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 | 40 // 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 | 41 // 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. | 42 // display purposes and has no TOCTTOU security implications. |
241 FilePath target; | 43 FilePath target; |
242 FilePath self_exe("/proc/self/exe"); | 44 FilePath self_exe("/proc/self/exe"); |
243 if (file_util::ReadSymbolicLink(self_exe, &target)) { | 45 if (file_util::ReadSymbolicLink(self_exe, &target)) { |
244 have_argv0 = true; | 46 have_argv0 = true; |
245 title = target.value(); | 47 title = target.value(); |
246 // If the binary has since been deleted, Linux appends " (deleted)" to the | 48 // 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. | 49 // symlink target. Remove it, since this is not really part of our name. |
248 const std::string kDeletedSuffix = " (deleted)"; | 50 const std::string kDeletedSuffix = " (deleted)"; |
249 if (EndsWith(title, kDeletedSuffix, true)) | 51 if (EndsWith(title, kDeletedSuffix, true)) |
250 title.resize(title.size() - kDeletedSuffix.size()); | 52 title.resize(title.size() - kDeletedSuffix.size()); |
251 #if defined(PR_SET_NAME) | 53 #if defined(PR_SET_NAME) |
252 // If PR_SET_NAME is available at compile time, we try using it. We ignore | 54 // 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 | 55 // 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 | 56 // 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. | 57 // full command line is not being displayed in most process listings. |
256 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); | 58 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str()); |
257 #endif | 59 #endif |
258 } | 60 } |
259 #endif | 61 #endif |
260 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { | 62 |
| 63 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 64 for (size_t i = 1; i < command_line->argv().size(); ++i) { |
261 if (!title.empty()) | 65 if (!title.empty()) |
262 title += " "; | 66 title += " "; |
263 title += current_process_commandline_->argv_[i]; | 67 title += command_line->argv()[i]; |
264 } | 68 } |
265 // Disable prepending argv[0] with '-' if we prepended it ourselves above. | 69 // Disable prepending argv[0] with '-' if we prepended it ourselves above. |
266 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); | 70 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str()); |
267 } | 71 } |
268 #endif | |
269 | 72 |
270 void CommandLine::Reset() { | 73 #else |
271 DCHECK(current_process_commandline_ != NULL); | |
272 delete current_process_commandline_; | |
273 current_process_commandline_ = NULL; | |
274 } | |
275 | 74 |
276 // static | 75 // All other systems (basically Windows & Mac) have no need or way to implement |
277 CommandLine* CommandLine::ForCurrentProcess() { | 76 // this function. |
278 DCHECK(current_process_commandline_); | 77 void SetProcessTitleFromCommandLine(char** /* main_argv */) { |
279 return current_process_commandline_; | |
280 } | |
281 | |
282 bool CommandLine::HasSwitch(const std::string& switch_string) const { | |
283 std::string lowercased_switch(switch_string); | |
284 #if defined(OS_WIN) | |
285 Lowercase(&lowercased_switch); | |
286 #endif | |
287 return switches_.find(lowercased_switch) != switches_.end(); | |
288 } | |
289 | |
290 std::string CommandLine::GetSwitchValueASCII( | |
291 const std::string& switch_string) const { | |
292 CommandLine::StringType value = GetSwitchValueNative(switch_string); | |
293 if (!IsStringASCII(value)) { | |
294 LOG(WARNING) << "Value of --" << switch_string << " must be ASCII."; | |
295 return ""; | |
296 } | |
297 #if defined(OS_WIN) | |
298 return WideToASCII(value); | |
299 #else | |
300 return value; | |
301 #endif | |
302 } | |
303 | |
304 FilePath CommandLine::GetSwitchValuePath( | |
305 const std::string& switch_string) const { | |
306 return FilePath(GetSwitchValueNative(switch_string)); | |
307 } | |
308 | |
309 CommandLine::StringType CommandLine::GetSwitchValueNative( | |
310 const std::string& switch_string) const { | |
311 std::string lowercased_switch(switch_string); | |
312 #if defined(OS_WIN) | |
313 Lowercase(&lowercased_switch); | |
314 #endif | |
315 | |
316 std::map<std::string, StringType>::const_iterator result = | |
317 switches_.find(lowercased_switch); | |
318 | |
319 if (result == switches_.end()) { | |
320 return CommandLine::StringType(); | |
321 } else { | |
322 return result->second; | |
323 } | |
324 } | |
325 | |
326 FilePath CommandLine::GetProgram() const { | |
327 #if defined(OS_WIN) | |
328 return FilePath(program_); | |
329 #else | |
330 DCHECK_GT(argv_.size(), 0U); | |
331 return FilePath(argv_[0]); | |
332 #endif | |
333 } | |
334 | |
335 #if defined(OS_POSIX) | |
336 std::string CommandLine::command_line_string() const { | |
337 return JoinString(argv_, ' '); | |
338 } | |
339 #endif | |
340 | |
341 #if defined(OS_WIN) | |
342 void CommandLine::AppendSwitch(const std::string& switch_string) { | |
343 command_line_string_.append(L" "); | |
344 command_line_string_.append(kSwitchPrefixes[0] + ASCIIToWide(switch_string)); | |
345 switches_[switch_string] = L""; | |
346 } | |
347 | |
348 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | |
349 const std::string& value_string) { | |
350 AppendSwitchNative(switch_string, ASCIIToWide(value_string)); | |
351 } | |
352 | |
353 // Quote a string if necessary, such that CommandLineToArgvW() will | |
354 // always process it as a single argument. | |
355 static std::wstring WindowsStyleQuote(const std::wstring& arg) { | |
356 // We follow the quoting rules of CommandLineToArgvW. | |
357 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx | |
358 if (arg.find_first_of(L" \\\"") == std::wstring::npos) { | |
359 // No quoting necessary. | |
360 return arg; | |
361 } | |
362 | |
363 std::wstring out; | |
364 out.push_back(L'"'); | |
365 for (size_t i = 0; i < arg.size(); ++i) { | |
366 if (arg[i] == '\\') { | |
367 // Find the extent of this run of backslashes. | |
368 size_t start = i, end = start + 1; | |
369 for (; end < arg.size() && arg[end] == '\\'; ++end) | |
370 /* empty */; | |
371 size_t backslash_count = end - start; | |
372 | |
373 // Backslashes are escapes only if the run is followed by a double quote. | |
374 // Since we also will end the string with a double quote, we escape for | |
375 // either a double quote or the end of the string. | |
376 if (end == arg.size() || arg[end] == '"') { | |
377 // To quote, we need to output 2x as many backslashes. | |
378 backslash_count *= 2; | |
379 } | |
380 for (size_t j = 0; j < backslash_count; ++j) | |
381 out.push_back('\\'); | |
382 | |
383 // Advance i to one before the end to balance i++ in loop. | |
384 i = end - 1; | |
385 } else if (arg[i] == '"') { | |
386 out.push_back('\\'); | |
387 out.push_back('"'); | |
388 } else { | |
389 out.push_back(arg[i]); | |
390 } | |
391 } | |
392 out.push_back('"'); | |
393 | |
394 return out; | |
395 } | |
396 | |
397 void CommandLine::AppendSwitchNative(const std::string& switch_string, | |
398 const std::wstring& value) { | |
399 std::wstring combined_switch_string = | |
400 kSwitchPrefixes[0] + ASCIIToWide(switch_string); | |
401 if (!value.empty()) | |
402 combined_switch_string += kSwitchValueSeparator + WindowsStyleQuote(value); | |
403 | |
404 command_line_string_.append(L" "); | |
405 command_line_string_.append(combined_switch_string); | |
406 | |
407 switches_[switch_string] = value; | |
408 } | |
409 | |
410 void CommandLine::AppendArg(const std::string& value) { | |
411 DCHECK(IsStringUTF8(value)); | |
412 AppendArgNative(UTF8ToWide(value)); | |
413 } | |
414 | |
415 void CommandLine::AppendArgNative(const std::wstring& value) { | |
416 command_line_string_.append(L" "); | |
417 command_line_string_.append(WindowsStyleQuote(value)); | |
418 args_.push_back(value); | |
419 } | |
420 | |
421 void CommandLine::AppendArguments(const CommandLine& other, | |
422 bool include_program) { | |
423 // Verify include_program is used correctly. | |
424 // Logic could be shorter but this is clearer. | |
425 DCHECK_EQ(include_program, !other.GetProgram().empty()); | |
426 if (include_program) | |
427 program_ = other.program_; | |
428 | |
429 if (!command_line_string_.empty()) | |
430 command_line_string_ += L' '; | |
431 | |
432 command_line_string_ += other.command_line_string_; | |
433 | |
434 std::map<std::string, StringType>::const_iterator i; | |
435 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) | |
436 switches_[i->first] = i->second; | |
437 } | |
438 | |
439 void CommandLine::PrependWrapper(const std::wstring& wrapper) { | |
440 if (wrapper.empty()) | |
441 return; | |
442 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | |
443 // we don't pretend to do anything fancy, we just split on spaces. | |
444 std::vector<std::wstring> wrapper_and_args; | |
445 base::SplitString(wrapper, ' ', &wrapper_and_args); | |
446 program_ = wrapper_and_args[0]; | |
447 command_line_string_ = wrapper + L" " + command_line_string_; | |
448 } | |
449 | |
450 #elif defined(OS_POSIX) | |
451 void CommandLine::AppendSwitch(const std::string& switch_string) { | |
452 argv_.push_back(kSwitchPrefixes[0] + switch_string); | |
453 switches_[switch_string] = ""; | |
454 } | |
455 | |
456 void CommandLine::AppendSwitchNative(const std::string& switch_string, | |
457 const std::string& value) { | |
458 std::string combined_switch_string = kSwitchPrefixes[0] + switch_string; | |
459 if (!value.empty()) | |
460 combined_switch_string += kSwitchValueSeparator + value; | |
461 argv_.push_back(combined_switch_string); | |
462 switches_[switch_string] = value; | |
463 } | |
464 | |
465 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | |
466 const std::string& value_string) { | |
467 AppendSwitchNative(switch_string, value_string); | |
468 } | |
469 | |
470 void CommandLine::AppendArg(const std::string& value) { | |
471 AppendArgNative(value); | |
472 } | |
473 | |
474 void CommandLine::AppendArgNative(const std::string& value) { | |
475 DCHECK(IsStringUTF8(value)); | |
476 argv_.push_back(value); | |
477 } | |
478 | |
479 void CommandLine::AppendArguments(const CommandLine& other, | |
480 bool include_program) { | |
481 // Verify include_program is used correctly. | |
482 // Logic could be shorter but this is clearer. | |
483 DCHECK_EQ(include_program, !other.GetProgram().empty()); | |
484 | |
485 if (include_program) | |
486 argv_[0] = other.argv_[0]; | |
487 | |
488 // Skip the first arg when copying since it's the program but push all | |
489 // arguments to our arg vector. | |
490 for (size_t i = 1; i < other.argv_.size(); ++i) | |
491 argv_.push_back(other.argv_[i]); | |
492 | |
493 std::map<std::string, StringType>::const_iterator i; | |
494 for (i = other.switches_.begin(); i != other.switches_.end(); ++i) | |
495 switches_[i->first] = i->second; | |
496 } | |
497 | |
498 void CommandLine::PrependWrapper(const std::string& wrapper) { | |
499 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | |
500 // we don't pretend to do anything fancy, we just split on spaces. | |
501 std::vector<std::string> wrapper_and_args; | |
502 base::SplitString(wrapper, ' ', &wrapper_and_args); | |
503 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | |
504 } | 78 } |
505 | 79 |
506 #endif | 80 #endif |
507 | 81 |
508 void CommandLine::AppendArgPath(const FilePath& path) { | |
509 AppendArgNative(path.value()); | |
510 } | |
511 | |
512 void CommandLine::AppendSwitchPath(const std::string& switch_string, | |
513 const FilePath& path) { | |
514 AppendSwitchNative(switch_string, path.value()); | |
515 } | |
516 | |
517 void CommandLine::CopySwitchesFrom(const CommandLine& source, | |
518 const char* const switches[], | |
519 size_t count) { | |
520 for (size_t i = 0; i < count; ++i) { | |
521 if (source.HasSwitch(switches[i])) { | |
522 StringType value = source.GetSwitchValueNative(switches[i]); | |
523 AppendSwitchNative(switches[i], value); | |
524 } | |
525 } | |
526 } | |
527 | |
528 // private | |
529 CommandLine::CommandLine() { | |
530 } | |
531 | |
532 // static | |
533 CommandLine* CommandLine::ForCurrentProcessMutable() { | |
534 DCHECK(current_process_commandline_); | |
535 return current_process_commandline_; | |
536 } | |
OLD | NEW |