[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / common / set_process_title.cc
blob425b44aad1a07d928cbe03da2a9f80d8aacc91ce
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_path.h"
23 #include "base/file_util.h"
24 #include "base/process_util.h"
25 #include "base/string_util.h"
26 // Linux/glibc doesn't natively have setproctitle().
27 #include "content/common/set_process_title_linux.h"
28 #endif // defined(OS_LINUX)
30 namespace content {
32 // TODO(jrg): Find out if setproctitle or equivalent is available on Android.
33 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_SOLARIS) && \
34 !defined(OS_ANDROID)
36 void SetProcessTitleFromCommandLine(const char** main_argv) {
37 // Build a single string which consists of all the arguments separated
38 // by spaces. We can't actually keep them separate due to the way the
39 // setproctitle() function works.
40 std::string title;
41 bool have_argv0 = false;
43 #if defined(OS_LINUX)
44 if (main_argv)
45 setproctitle_init(main_argv);
47 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us
48 // show up as "exe" in process listings. Read the symlink /proc/self/exe and
49 // use the path it points at for our process title. Note that this is only for
50 // display purposes and has no TOCTTOU security implications.
51 FilePath target;
52 FilePath self_exe(base::kProcSelfExe);
53 if (file_util::ReadSymbolicLink(self_exe, &target)) {
54 have_argv0 = true;
55 title = target.value();
56 // If the binary has since been deleted, Linux appends " (deleted)" to the
57 // symlink target. Remove it, since this is not really part of our name.
58 const std::string kDeletedSuffix = " (deleted)";
59 if (EndsWith(title, kDeletedSuffix, true))
60 title.resize(title.size() - kDeletedSuffix.size());
61 #if defined(PR_SET_NAME)
62 // If PR_SET_NAME is available at compile time, we try using it. We ignore
63 // any errors if the kernel does not support it at runtime though. When
64 // available, this lets us set the short process name that shows when the
65 // full command line is not being displayed in most process listings.
66 prctl(PR_SET_NAME, FilePath(title).BaseName().value().c_str());
67 #endif // defined(PR_SET_NAME)
69 #endif // defined(OS_LINUX)
71 const CommandLine* command_line = CommandLine::ForCurrentProcess();
72 for (size_t i = 1; i < command_line->argv().size(); ++i) {
73 if (!title.empty())
74 title += " ";
75 title += command_line->argv()[i];
77 // Disable prepending argv[0] with '-' if we prepended it ourselves above.
78 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
81 #else
83 // All other systems (basically Windows & Mac) have no need or way to implement
84 // this function.
85 void SetProcessTitleFromCommandLine(const char** /* main_argv */) {
88 #endif
90 } // namespace content