1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "build/build_config.h"
6
7 // Define _GNU_SOURCE to ensure that <errno.h> defines
8 // program_invocation_short_name. Keep this at the top of the file since some
9 // system headers might include <errno.h> and the header could be skipped on
10 // subsequent includes.
11 #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !defined(_GNU_SOURCE)
12 #define _GNU_SOURCE
13 #endif
14
15 #include "base/process/set_process_title.h"
16
17 #include <stddef.h>
18
19 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_SOLARIS)
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include <string>
25
26 #include "base/command_line.h"
27 #endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_SOLARIS)
28
29 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
30 #include <errno.h> // Get program_invocation_short_name declaration.
31 #include <sys/prctl.h>
32
33 #include "base/files/file_path.h"
34 #include "base/files/file_util.h"
35 #include "base/no_destructor.h"
36 #include "base/process/process_metrics.h"
37 #include "base/strings/string_util.h"
38 #include "base/threading/platform_thread.h"
39 // Linux/glibc doesn't natively have setproctitle().
40 #include "base/process/set_process_title_linux.h"
41 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
42
43 namespace base {
44
45 // TODO(jrg): Find out if setproctitle or equivalent is available on Android.
46 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_SOLARIS) && \
47 !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_NACL)
48
SetProcessTitleFromCommandLine(const char ** main_argv)49 void SetProcessTitleFromCommandLine(const char** main_argv) {
50 // Build a single string which consists of all the arguments separated
51 // by spaces. We can't actually keep them separate due to the way the
52 // setproctitle() function works.
53 std::string title;
54 bool have_argv0 = false;
55
56 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
57 DCHECK_EQ(base::PlatformThread::CurrentId(), getpid());
58
59 if (main_argv)
60 setproctitle_init(main_argv);
61
62 // In Linux we sometimes exec ourselves from /proc/self/exe, but this makes us
63 // show up as "exe" in process listings. Read the symlink /proc/self/exe and
64 // use the path it points at for our process title. Note that this is only for
65 // display purposes and has no TOCTTOU security implications.
66 base::FilePath target;
67 base::FilePath self_exe(base::kProcSelfExe);
68 if (base::ReadSymbolicLink(self_exe, &target)) {
69 have_argv0 = true;
70 title = target.value();
71 // If the binary has since been deleted, Linux appends " (deleted)" to the
72 // symlink target. Remove it, since this is not really part of our name.
73 const std::string kDeletedSuffix = " (deleted)";
74 if (base::EndsWith(title, kDeletedSuffix, base::CompareCase::SENSITIVE))
75 title.resize(title.size() - kDeletedSuffix.size());
76
77 base::FilePath::StringType base_name =
78 base::FilePath(title).BaseName().value();
79 // PR_SET_NAME is available in Linux 2.6.9 and newer.
80 // When available at run time, this sets the short process name that shows
81 // when the full command line is not being displayed in most process
82 // listings.
83 prctl(PR_SET_NAME, base_name.c_str());
84
85 // This prevents program_invocation_short_name from being broken by
86 // setproctitle().
87 static base::NoDestructor<base::FilePath::StringType> base_name_storage;
88 *base_name_storage = std::move(base_name);
89 program_invocation_short_name = &(*base_name_storage)[0];
90 }
91 #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
92
93 const base::CommandLine* command_line =
94 base::CommandLine::ForCurrentProcess();
95 for (size_t i = 1; i < command_line->argv().size(); ++i) {
96 if (!title.empty())
97 title += " ";
98 title += command_line->argv()[i];
99 }
100 // Disable prepending argv[0] with '-' if we prepended it ourselves above.
101 setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
102 }
103
104 #else
105
106 // All other systems (basically Windows & Mac) have no need or way to implement
107 // this function.
108 void SetProcessTitleFromCommandLine(const char** /* main_argv */) {}
109
110 #endif
111
112 } // namespace base
113