Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #ifndef DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_ |
| 6 | #define DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_ |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 11 | #include <base/files/file_path.h> |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 12 | #include <base/files/scoped_file.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 13 | #include <brillo/errors/error.h> |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 14 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 15 | #include "debugd/src/sandboxed_process.h" |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 16 | |
| 17 | namespace debugd { |
| 18 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 19 | // Represents a process whose output can be collected. |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 20 | // |
| 21 | // The process must be Run() to completion before its output can be collected. |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 22 | // By default both stdout and stderr are included in the output. |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 23 | class ProcessWithOutput : public SandboxedProcess { |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 24 | public: |
Ben Chan | 81905fb | 2017-02-08 22:03:11 -0800 | [diff] [blame] | 25 | using ArgList = std::vector<std::string>; |
Ben Chan | 5399a35 | 2017-07-27 23:47:22 -0700 | [diff] [blame] | 26 | |
| 27 | static constexpr int kRunError = -1; |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 28 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 29 | ProcessWithOutput(); |
Ben Chan | 4b11012 | 2014-08-29 08:22:59 -0700 | [diff] [blame] | 30 | ~ProcessWithOutput() override; |
| 31 | bool Init() override; |
Wei-Cheng Xiao | 9076cf5 | 2018-10-08 14:33:42 +0800 | [diff] [blame^] | 32 | bool GetOutput(std::string* output) const; |
| 33 | bool GetOutputLines(std::vector<std::string>* output) const; |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 34 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 35 | // 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 66 | brillo::ErrorPtr* error); |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 67 | |
| 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 76 | brillo::ErrorPtr* error); |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 77 | |
| 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 Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 87 | private: |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 88 | 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 Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 98 | brillo::ErrorPtr* error, |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 99 | ProcessWithOutput* process); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 102 | } // namespace debugd |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 103 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 104 | #endif // DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_ |