[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / content / common / set_process_title.cc
blob99eb243bba16345dd29f740f854c35f7caff4b16
1 // Copyright (c) 2012 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.
5 #include "content/common/set_process_title.h"
7 #include "build/build_config.h"
9 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS)
10 #include <limits.h>
11 #include <stdlib.h>
12 #include <unistd.h>
14 #include <string>
16 #include "base/command_line.h"
17 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS)
19 #if defined(OS_LINUX)
20 #include <sys/prctl.h>
22 #include "base/file_util.h"
23 #include "base/files/file_path.h"
24 #include "base/process/process_metrics.h"
25 #include "base/strings/string_util.h"
26 #include "base/threading/platform_thread.h"
27 // Linux/glibc doesn't natively have setproctitle().
28 #include "content/common/set_process_title_linux.h"
29 #endif // defined(OS_LINUX)
31 namespace content {
33 // TODO(jrg): Find out if setproctitle or equivalent is available on Android.
34 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \
35 !defined(OS_ANDROID)
37 void SetProcessTitleFromCommandLine(const char** main_argv) {
38 // Build a single string which consists of all the arguments separated
39 // by spaces. We can't actually keep them separate due to the way the
40 // setproctitle() function works.
41 std::string title;
42 bool have_argv0 = false;
44 #if defined(OS_LINUX)
45 DCHECK_EQ(base::PlatformThread::CurrentId(), getpid());
47 if (main_argv)
48 setproctitle_init(main_argv);
50 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us
51 // show up as "exe" in process listings. Read the symlink /proc/self/exe and
52 // use the path it points at for our process title. Note that this is only for
53 // display purposes and has no TOCTTOU security implications.
54 base::FilePath target;
55 base::FilePath self_exe(base::kProcSelfExe);
56 if (base::ReadSymbolicLink(self_exe, &target)) {
57 have_argv0 = true;
58 title = target.value();
59 // If the binary has since been deleted, Linux appends " (deleted)" to the
60 // symlink target. Remove it, since this is not really part of our name.
61 const std::string kDeletedSuffix = " (deleted)";
62 if (EndsWith(title, kDeletedSuffix, true))
63 title.resize(title.size() - kDeletedSuffix.size());
65 // PR_SET_NAME is available in Linux 2.6.9 and newer.
66 // When available at run time, this sets the short process name that shows
67 // when the full command line is not being displayed in most process
68 // listings.
69 prctl(PR_SET_NAME, base::FilePath(title).BaseName().value().c_str());
71 #endif // defined(OS_LINUX)
73 const CommandLine* command_line = CommandLine::ForCurrentProcess();
74 for (size_t i = 1; i < command_line->argv().size(); ++i) {
75 if (!title.empty())
76 title += " ";
77 title += command_line->argv()[i];
79 // Disable prepending argv[0] with '-' if we prepended it ourselves above.
80 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
83 #else
85 // All other systems (basically Windows & Mac) have no need or way to implement
86 // this function.
87 void SetProcessTitleFromCommandLine(const char** /* main_argv */) {
90 #endif
92 } // namespace content