| // Copyright 2021 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "runtime_probe/system/helper_invoker_direct_impl.h" |
| |
| #include <string> |
| #include <utility> |
| #include <vector> |
| |
| #include <base/command_line.h> |
| #include <base/logging.h> |
| #include <brillo/process/process.h> |
| |
| #include "runtime_probe/utils/pipe_utils.h" |
| |
| namespace runtime_probe { |
| |
| static_assert(USE_FACTORY_RUNTIME_PROBE, |
| "The compiler should never reach " __FILE__ |
| " while building a regular runtime_probe."); |
| |
| bool HelperInvokerDirectImpl::Invoke(const ProbeFunction* probe_function, |
| const std::string& probe_statement_str, |
| std::string* result) const { |
| brillo::ProcessImpl helper_proc; |
| helper_proc.AddArg( |
| base::CommandLine::ForCurrentProcess()->GetProgram().value()); |
| helper_proc.AddArg("--helper"); |
| helper_proc.AddArg(probe_statement_str); |
| helper_proc.RedirectInput("/dev/null"); |
| helper_proc.RedirectUsingPipe(STDOUT_FILENO, false); |
| helper_proc.RedirectUsingPipe(STDERR_FILENO, false); |
| |
| if (!helper_proc.Start()) { |
| LOG(ERROR) << "Failed to start the helper process."; |
| return false; |
| } |
| |
| std::vector<std::stirng> out; |
| bool res = ReadNonblockingPipeToString( |
| {helper_proc.GetPipe(STDOUT_FILENO), helper_proc.GetPipe(STDERR_FILENO)}, |
| &out); |
| if (out[1].size()) { |
| LOG(INFO) << "Helper stderr:\n" |
| << "^--------------------------------------------------------^\n" |
| << out[1] |
| << "$--------------------------------------------------------$"; |
| } |
| *result = std::move(out[0]); |
| return res; |
| } |
| |
| } // namespace runtime_probe |