File: enterprise_companion.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/enterprise_companion/enterprise_companion.h"

#include <cstdint>
#include <memory>
#include <optional>

#include "base/at_exit.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/process/process_handle.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_executor.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/threading/platform_thread.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/enterprise_companion/app/app.h"
#include "chrome/enterprise_companion/crash_client.h"
#include "chrome/enterprise_companion/enterprise_companion_client.h"
#include "chrome/enterprise_companion/enterprise_companion_status.h"
#include "chrome/enterprise_companion/enterprise_companion_version.h"
#include "chrome/enterprise_companion/flags.h"
#include "chrome/enterprise_companion/installer_paths.h"
#include "chrome/enterprise_companion/ipc_support.h"

#if BUILDFLAG(IS_WIN)
#include "base/strings/stringprintf.h"
#include "base/win/windows_version.h"
#endif

namespace enterprise_companion {

namespace {

constexpr int64_t kLogRotateAtSize = 1024 * 1024;  // 1 MiB.

void InitLogging() {
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (!command_line->HasSwitch(kLoggingModuleSwitch)) {
    command_line->AppendSwitchASCII(kLoggingModuleSwitch,
                                    kLoggingModuleSwitchValue);
  }

  logging::LoggingSettings settings{.logging_dest = logging::LOG_TO_STDERR};
  std::optional<base::FilePath> log_file_path = GetLogFilePath();
  if (!log_file_path) {
    VLOG(1) << "Error getting log file path.";
  } else {
    base::CreateDirectory(log_file_path->DirName());

    // Rotate the log if needed.
    std::optional<int64_t> size = base::GetFileSize(*log_file_path);
    if (size.has_value() && size.value() >= kLogRotateAtSize) {
      base::ReplaceFile(*log_file_path,
                        log_file_path->AddExtension(FILE_PATH_LITERAL(".old")),
                        nullptr);
    }
    settings.log_file_path = log_file_path->value().c_str();
    settings.logging_dest |= logging::LOG_TO_FILE;
  }

  logging::InitLogging(settings);
  logging::SetLogItems(/*enable_process_id=*/true,
                       /*enable_thread_id=*/true,
                       /*enable_timestamp=*/true,
                       /*enable_tickcount=*/false);
}

void InitThreadPool() {
  base::PlatformThread::SetName("EnterpriseCompanion");
  base::ThreadPoolInstance::Create("EnterpriseCompanion");

  // Reuses the logic in base::ThreadPoolInstance::StartWithDefaultParams.
  const size_t max_num_foreground_threads =
      static_cast<size_t>(std::max(3, base::SysInfo::NumberOfProcessors() - 1));
  base::ThreadPoolInstance::InitParams init_params(max_num_foreground_threads);
  base::ThreadPoolInstance::Get()->Start(init_params);
}

std::unique_ptr<App> CreateAppForCommandLine(base::CommandLine* command_line) {
  if (command_line->HasSwitch(kShutdownSwitch)) {
    return CreateAppShutdown();
  }

  if (command_line->HasSwitch(kFetchPoliciesSwitch)) {
    return CreateAppFetchPolicies();
  }

  if (command_line->HasSwitch(kInstallSwitch)) {
    return CreateAppInstall();
  }

  if (command_line->HasSwitch(kUninstallSwitch)) {
    return CreateAppUninstall();
  }

#if BUILDFLAG(IS_MAC)
  if (command_line->HasSwitch(kNetWorkerSwitch)) {
    return CreateAppNetWorker();
  }
#endif

  return CreateAppServer();
}

constexpr const char* BuildFlavor() {
#if defined(NDEBUG)
  return "opt";
#else
  return "debug";
#endif
}

constexpr const char* BuildArch() {
#if defined(ARCH_CPU_64_BITS)
  return "64 bits";
#elif defined(ARCH_CPU_32_BITS)
  return "32 bits";
#else
#error CPU architecture is unknown.
#endif
}

std::string OperatingSystemVersion() {
#if BUILDFLAG(IS_WIN)
  const base::win::OSInfo::VersionNumber v =
      base::win::OSInfo::GetInstance()->version_number();
  return base::StringPrintf("%u.%u.%u.%u", v.major, v.minor, v.build, v.patch);
#else
  return base::SysInfo().OperatingSystemVersion();
#endif
}

}  // namespace

int EnterpriseCompanionMain(int argc, const char* const* argv) {
  base::CommandLine::Init(argc, argv);
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  InitLogging();
  VLOG(1) << "Version: " << kEnterpriseCompanionVersion << ", " << BuildFlavor()
          << ", " << BuildArch()
          << ", command line: " << command_line->GetCommandLineString();
  VLOG(1) << "OS version: " << OperatingSystemVersion()
          << ", System uptime (seconds): "
          << base::SysInfo::Uptime().InSeconds() << ", parent pid: "
          << base::GetParentProcessId(base::GetCurrentProcessHandle());
  InitThreadPool();
  base::AtExitManager exit_manager;

  base::SingleThreadTaskExecutor main_task_executor;

  if (command_line->HasSwitch(kCrashHandlerSwitch)) {
    return CrashReporterMain();
  }
  InitializeCrashReporting();

  // Records a backtrace in the log, crashes the program, saves a crash dump,
  // and reports the crash.
  CHECK(!command_line->HasSwitch(kCrashMeSwitch))
      << kCrashMeSwitch << " switch was used.";

  ScopedIPCSupportWrapper ipc_support;

  EnterpriseCompanionStatus status =
      CreateAppForCommandLine(command_line)->Run();
  LOG_IF(ERROR, !status.ok())
      << "Application completed with error: " << status.description();
  return !status.ok();
}

std::optional<base::FilePath> GetLogFilePath() {
  std::optional<base::FilePath> path = GetInstallDirectory();
  return path ? path->Append(FILE_PATH_LITERAL("enterprise_companion.log"))
              : path;
}

}  // namespace enterprise_companion