Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: base/command_line.cc

Issue 490028: Linux: use readlink() and prctl() in SetProcTitle() to fix "exe" showing in process listings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/setproctitle_linux.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_LINUX) || defined(OS_FREEBSD)
Evan Martin 2010/01/03 01:52:33 seems harmless to use this on osx, so why not just
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
200 #endif 204 #endif
201 } 205 }
202 206
203 #if defined(OS_POSIX) && !defined(OS_MACOSX) 207 #if defined(OS_POSIX) && !defined(OS_MACOSX)
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 as a security
217 // feature, but this makes us show up as "exe" in process listings. Read
Evan Martin 2010/01/03 01:52:33 not a security feature, it's so updates don't swap
218 // the symlink /proc/self/exe and use the path it points at for our process
219 // title. Note that this is only for display purposes and has no TOCTTOU
220 // security implications.
221 char buffer[PATH_MAX];
222 // Note: readlink() does not append a null byte to terminate the string.
223 ssize_t r = readlink("/proc/self/exe", buffer, sizeof(buffer));
Evan Martin 2010/01/03 01:52:33 google style frowns on one-letter variable names.
224 DCHECK(r <= static_cast<ssize_t>(sizeof(buffer)));
225 if (r > 0) {
226 have_argv0 = true;
227 title.assign(buffer, r);
228 // If the binary has since been deleted, Linux appends " (deleted)" to the
229 // symlink target. Remove it, since this is not really part of our name.
230 const std::string kDeletedSuffix = " (deleted)";
231 if (EndsWith(title, kDeletedSuffix, true))
232 title.resize(title.size() - kDeletedSuffix.size());
233 #if defined(PR_SET_NAME)
234 // If PR_SET_NAME is available at compile time, we try using it. We ignore
235 // any errors if the kernel does not support it at runtime though. When
236 // available, this lets us set the short process name that shows when the
237 // full command line is not being displayed in most process listings.
238 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str());
239 #endif
240 }
241 #endif
210 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) { 242 for (size_t i = 1; i < current_process_commandline_->argv_.size(); ++i) {
211 if (!title.empty()) 243 if (!title.empty())
212 title += " "; 244 title += " ";
213 title += current_process_commandline_->argv_[i]; 245 title += current_process_commandline_->argv_[i];
214 } 246 }
215 setproctitle("%s", title.c_str()); 247 // Disable prepending argv[0] with '-' if we prepended it ourselves above.
248 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
216 } 249 }
217 #endif 250 #endif
218 251
219 void CommandLine::Reset() { 252 void CommandLine::Reset() {
220 DCHECK(current_process_commandline_ != NULL); 253 DCHECK(current_process_commandline_ != NULL);
221 delete current_process_commandline_; 254 delete current_process_commandline_;
222 current_process_commandline_ = NULL; 255 current_process_commandline_ = NULL;
223 } 256 }
224 257
225 bool CommandLine::HasSwitch(const std::string& switch_string) const { 258 bool CommandLine::HasSwitch(const std::string& switch_string) const {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) { 426 void CommandLine::PrependWrapper(const std::wstring& wrapper_wide) {
394 // The wrapper may have embedded arguments (like "gdb --args"). In this case, 427 // 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. 428 // we don't pretend to do anything fancy, we just split on spaces.
396 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); 429 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide);
397 std::vector<std::string> wrapper_and_args; 430 std::vector<std::string> wrapper_and_args;
398 SplitString(wrapper, ' ', &wrapper_and_args); 431 SplitString(wrapper, ' ', &wrapper_and_args);
399 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); 432 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end());
400 } 433 }
401 434
402 #endif 435 #endif
OLDNEW
« no previous file with comments | « no previous file | base/setproctitle_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698