blob: 70e13963ffafb24fa2c3042c1a9689c478a296c5 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones1c4c3a12011-12-20 15:01:59 -05002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#ifndef DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_
6#define DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_
Elly Jones1c4c3a12011-12-20 15:01:59 -05007
8#include <string>
9#include <vector>
10
Ben Chan9953a592014-02-05 23:32:00 -080011#include <base/files/file_path.h>
David Pursell300498a2014-11-03 15:47:36 -080012#include <base/files/scoped_file.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070013#include <brillo/errors/error.h>
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050014
Alex Vakulenko262be3f2014-07-30 15:25:50 -070015#include "debugd/src/sandboxed_process.h"
Elly Jones1c4c3a12011-12-20 15:01:59 -050016
17namespace debugd {
18
David Pursell300498a2014-11-03 15:47:36 -080019// Represents a process whose output can be collected.
Elly Jones1c4c3a12011-12-20 15:01:59 -050020//
21// The process must be Run() to completion before its output can be collected.
David Pursell300498a2014-11-03 15:47:36 -080022// By default both stdout and stderr are included in the output.
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050023class ProcessWithOutput : public SandboxedProcess {
Elly Jones1c4c3a12011-12-20 15:01:59 -050024 public:
Ben Chan81905fb2017-02-08 22:03:11 -080025 using ArgList = std::vector<std::string>;
Ben Chan5399a352017-07-27 23:47:22 -070026
27 static constexpr int kRunError = -1;
David Pursell300498a2014-11-03 15:47:36 -080028
Elly Jones1c4c3a12011-12-20 15:01:59 -050029 ProcessWithOutput();
Ben Chan4b110122014-08-29 08:22:59 -070030 ~ProcessWithOutput() override;
31 bool Init() override;
Wei-Cheng Xiao9076cf52018-10-08 14:33:42 +080032 bool GetOutput(std::string* output) const;
33 bool GetOutputLines(std::vector<std::string>* output) const;
Ben Chana0011d82014-05-13 00:19:29 -070034
David Pursell300498a2014-11-03 15:47:36 -080035 // Reads the stderr output. Must have called set_separate_stderr(true) and
36 // run the process to completion.
37 bool GetError(std::string* error);
38
39 // Separates stderr from stdout. Must be called before Init() to have effect.
40 void set_separate_stderr(bool separate_stderr) {
41 separate_stderr_ = separate_stderr;
42 }
43
44 // Sets whether to use Minijail or not. Disabling Minijail will omit all
45 // sandboxing functionality from the process, and should only be used from
46 // within helper programs that are already sandboxed. The purpose of this is
47 // to allow the process to search PATH for the executable, since Minijail does
48 // not support this.
49 // This must be called before Init() to have any effect. Defaults to true.
50 void set_use_minijail(bool use_minijail) { use_minijail_ = use_minijail; }
51
52 // Initializes, configures, and runs a ProcessWithOutput. The D-Bus error will
53 // only be set if process setup fails, it's up to the caller to check the
54 // process exit code and handle run failures as needed.
55 // |stdin| is a string to pipe into the process, and |stdout| and |stderr|
56 // will be filled with the corresponding process output. |error| will be
57 // set if process setup fails and the process was never able to run. All
58 // four of these parameters can be null.
59 // Returns the process exit code or kRunError on setup failure.
60 static int RunProcess(const std::string& command,
61 const ArgList& arguments,
62 bool requires_root,
63 const std::string* stdin,
64 std::string* stdout,
65 std::string* stderr,
Eric Carusocc7106c2017-04-27 14:22:42 -070066 brillo::ErrorPtr* error);
David Pursell300498a2014-11-03 15:47:36 -080067
68 // Like RunProcess() but to run helper programs. |helper| should be specified
69 // relative to the helpers/ directory.
70 static int RunHelper(const std::string& helper,
71 const ArgList& arguments,
72 bool requires_root,
73 const std::string* stdin,
74 std::string* stdout,
75 std::string* stderr,
Eric Carusocc7106c2017-04-27 14:22:42 -070076 brillo::ErrorPtr* error);
David Pursell300498a2014-11-03 15:47:36 -080077
78 // Like RunProcess() but from within helper programs. This function will
79 // not use minijail0 or any sandboxing (since helper programs should already
80 // be sandboxed) and $PATH will be searched to execute |command|.
81 static int RunProcessFromHelper(const std::string& command,
82 const ArgList& arguments,
83 const std::string* stdin,
84 std::string* stdout,
85 std::string* stderr);
86
Elly Jones1c4c3a12011-12-20 15:01:59 -050087 private:
David Pursell300498a2014-11-03 15:47:36 -080088 base::FilePath outfile_path_, errfile_path_;
89 base::ScopedFILE outfile_, errfile_;
90 bool separate_stderr_, use_minijail_;
91
92 // Private function to do the work of running the process and handling I/O.
93 static int DoRunProcess(const std::string& command,
94 const ArgList& arguments,
95 const std::string* stdin,
96 std::string* stdout,
97 std::string* stderr,
Eric Carusocc7106c2017-04-27 14:22:42 -070098 brillo::ErrorPtr* error,
David Pursell300498a2014-11-03 15:47:36 -080099 ProcessWithOutput* process);
Elly Jones1c4c3a12011-12-20 15:01:59 -0500100};
101
Ben Chana0011d82014-05-13 00:19:29 -0700102} // namespace debugd
Elly Jones1c4c3a12011-12-20 15:01:59 -0500103
Alex Vakulenko262be3f2014-07-30 15:25:50 -0700104#endif // DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_