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