blob: 4439328b21809b4b205371b1f953353497b31f11 [file] [log] [blame]
Greg Kerr888f1ce2019-01-31 10:49:11 -08001// Copyright 2019 The Chromium OS 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.
4
5#include "debugd/src/scheduler_configuration_tool.h"
6
7#include "debugd/src/error_utils.h"
Hardik Goyalb09d6b02019-08-13 16:15:50 -07008#include "debugd/src/helper_utils.h"
Greg Kerr888f1ce2019-01-31 10:49:11 -08009#include "debugd/src/process_with_output.h"
10#include "debugd/src/sandboxed_process.h"
11
12#include <string>
13
14#include <base/files/file_util.h>
15#include <base/logging.h>
16#include <base/strings/string_number_conversions.h>
17#include <base/strings/string_split.h>
18#include <base/strings/stringprintf.h>
19#include <brillo/errors/error_codes.h>
20#include <build/build_config.h>
21#include <build/buildflag.h>
22#include <chromeos/dbus/service_constants.h>
23
24namespace debugd {
25
26namespace {
27
28constexpr char kErrorPath[] =
29 "org.chromium.debugd.SchedulerConfigurationPolicyError";
30
31constexpr bool IsX86_64() {
32#if defined(__x86_64__)
33 return true;
34#else
35 return false;
36#endif
37}
38
39// Executes a helper process with the expectation that any message printed to
40// stderr indicates a failure that should be passed back over D-Bus.
41// Returns false if any errors launching the process occur. Returns true
42// otherwise, and sets |exit_status| if it isn't null.
43bool RunHelper(const std::string& command,
44 const ProcessWithOutput::ArgList& arguments,
45 int* exit_status,
46 brillo::ErrorPtr* error) {
47 std::string helper_path;
Hardik Goyalb09d6b02019-08-13 16:15:50 -070048 if (!GetHelperPath(command, &helper_path)) {
Greg Kerr888f1ce2019-01-31 10:49:11 -080049 DEBUGD_ADD_ERROR(error, kErrorPath, "Path too long");
50 return false;
51 }
52
53 // Note: This runs the helper as root and without a sandbox only because the
54 // helper immediately drops privileges and enforces its own sandbox. debugd
55 // should not be used to launch unsandboxed executables.
56 std::string stderr;
57 int result = ProcessWithOutput::RunProcess(
58 helper_path, arguments, true /*requires_root*/,
59 true /* disable_sandbox */, nullptr, nullptr, &stderr, error);
60
61 if (!stderr.empty()) {
62 DEBUGD_ADD_ERROR(error, kErrorPath, stderr.c_str());
63 return false;
64 }
65
66 if (exit_status)
67 *exit_status = result;
68 return true;
69}
70
71} // namespace
72
73bool SchedulerConfigurationTool::SetPolicy(const std::string& policy,
74 brillo::ErrorPtr* error) {
75 if (!IsX86_64()) {
76 DEBUGD_ADD_ERROR(error, kErrorPath, "Invalid architecture");
77 return false;
78 }
79
Greg Kerr888f1ce2019-01-31 10:49:11 -080080 int exit_status;
81 bool result = RunHelper("scheduler_configuration_helper",
82 ProcessWithOutput::ArgList{"--policy=" + policy},
83 &exit_status, error);
Greg Kerre0d17fd2019-02-19 14:51:30 -080084
85 bool status = result && (exit_status == 0);
86 if (!status) {
87 DEBUGD_ADD_ERROR(error, kErrorPath,
88 "scheduler_configuration_helper failed");
89 }
90
91 return status;
Greg Kerr888f1ce2019-01-31 10:49:11 -080092}
93
94} // namespace debugd