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> |
| 13 | #include <dbus-c++/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>; |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 26 | enum { kRunError = -1 }; |
| 27 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 28 | ProcessWithOutput(); |
Ben Chan | 4b11012 | 2014-08-29 08:22:59 -0700 | [diff] [blame] | 29 | ~ProcessWithOutput() override; |
| 30 | bool Init() override; |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 31 | bool GetOutput(std::string* output); |
| 32 | bool GetOutputLines(std::vector<std::string>* output); |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 33 | |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 34 | // Reads the stderr output. Must have called set_separate_stderr(true) and |
| 35 | // run the process to completion. |
| 36 | bool GetError(std::string* error); |
| 37 | |
| 38 | // Separates stderr from stdout. Must be called before Init() to have effect. |
| 39 | void set_separate_stderr(bool separate_stderr) { |
| 40 | separate_stderr_ = separate_stderr; |
| 41 | } |
| 42 | |
| 43 | // Sets whether to use Minijail or not. Disabling Minijail will omit all |
| 44 | // sandboxing functionality from the process, and should only be used from |
| 45 | // within helper programs that are already sandboxed. The purpose of this is |
| 46 | // to allow the process to search PATH for the executable, since Minijail does |
| 47 | // not support this. |
| 48 | // This must be called before Init() to have any effect. Defaults to true. |
| 49 | void set_use_minijail(bool use_minijail) { use_minijail_ = use_minijail; } |
| 50 | |
| 51 | // Initializes, configures, and runs a ProcessWithOutput. The D-Bus error will |
| 52 | // only be set if process setup fails, it's up to the caller to check the |
| 53 | // process exit code and handle run failures as needed. |
| 54 | // |stdin| is a string to pipe into the process, and |stdout| and |stderr| |
| 55 | // will be filled with the corresponding process output. |error| will be |
| 56 | // set if process setup fails and the process was never able to run. All |
| 57 | // four of these parameters can be null. |
| 58 | // Returns the process exit code or kRunError on setup failure. |
| 59 | static int RunProcess(const std::string& command, |
| 60 | const ArgList& arguments, |
| 61 | bool requires_root, |
| 62 | const std::string* stdin, |
| 63 | std::string* stdout, |
| 64 | std::string* stderr, |
| 65 | DBus::Error* error); |
| 66 | |
| 67 | // Like RunProcess() but to run helper programs. |helper| should be specified |
| 68 | // relative to the helpers/ directory. |
| 69 | static int RunHelper(const std::string& helper, |
| 70 | const ArgList& arguments, |
| 71 | bool requires_root, |
| 72 | const std::string* stdin, |
| 73 | std::string* stdout, |
| 74 | std::string* stderr, |
| 75 | DBus::Error* error); |
| 76 | |
| 77 | // Like RunProcess() but from within helper programs. This function will |
| 78 | // not use minijail0 or any sandboxing (since helper programs should already |
| 79 | // be sandboxed) and $PATH will be searched to execute |command|. |
| 80 | static int RunProcessFromHelper(const std::string& command, |
| 81 | const ArgList& arguments, |
| 82 | const std::string* stdin, |
| 83 | std::string* stdout, |
| 84 | std::string* stderr); |
| 85 | |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 86 | private: |
David Pursell | 300498a | 2014-11-03 15:47:36 -0800 | [diff] [blame] | 87 | base::FilePath outfile_path_, errfile_path_; |
| 88 | base::ScopedFILE outfile_, errfile_; |
| 89 | bool separate_stderr_, use_minijail_; |
| 90 | |
| 91 | // Private function to do the work of running the process and handling I/O. |
| 92 | static int DoRunProcess(const std::string& command, |
| 93 | const ArgList& arguments, |
| 94 | const std::string* stdin, |
| 95 | std::string* stdout, |
| 96 | std::string* stderr, |
| 97 | DBus::Error* error, |
| 98 | ProcessWithOutput* process); |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 101 | } // namespace debugd |
Elly Jones | 1c4c3a1 | 2011-12-20 15:01:59 -0500 | [diff] [blame] | 102 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 103 | #endif // DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_ |