#include "build/build_config.h"
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <stddef.h>
#include "base/process/set_process_title.h"
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_SOLARIS)
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include "base/command_line.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <errno.h>
#include <sys/prctl.h>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/no_destructor.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_util.h"
#include "base/threading/platform_thread.h"
#include "base/process/set_process_title_linux.h"
#endif
namespace base {
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_SOLARIS) && \
!BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_OHOS)
void SetProcessTitleFromCommandLine(const char** main_argv) {
std::string title;
bool have_argv0 = false;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
if (main_argv) {
setproctitle_init(main_argv);
}
base::FilePath target;
base::FilePath self_exe(base::kProcSelfExe);
if (base::ReadSymbolicLink(self_exe, &target)) {
have_argv0 = true;
title = target.value();
const std::string kDeletedSuffix = " (deleted)";
if (base::EndsWith(title, kDeletedSuffix, base::CompareCase::SENSITIVE)) {
title.resize(title.size() - kDeletedSuffix.size());
}
base::FilePath::StringType base_name =
base::FilePath(title).BaseName().value();
prctl(PR_SET_NAME, base_name.c_str());
static base::NoDestructor<base::FilePath::StringType> base_name_storage;
*base_name_storage = std::move(base_name);
program_invocation_short_name = &(*base_name_storage)[0];
}
#endif
const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess();
for (size_t i = 1; i < command_line->argv().size(); ++i) {
if (!title.empty()) {
title += " ";
}
title += command_line->argv()[i];
}
setproctitle(have_argv0 ? "-%s" : "%s", title.c_str());
}
#else
void SetProcessTitleFromCommandLine(const char** ) {}
#endif
}