blob: 167771d75b4a8c712379b507887d468d0b724ff6 [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>;
David Pursell300498a2014-11-03 15:47:36 -080026 enum { kRunError = -1 };
27
Elly Jones1c4c3a12011-12-20 15:01:59 -050028 ProcessWithOutput();
Ben Chan4b110122014-08-29 08:22:59 -070029 ~ProcessWithOutput() override;
30 bool Init() override;
Elly Jones1c4c3a12011-12-20 15:01:59 -050031 bool GetOutput(std::string* output);
32 bool GetOutputLines(std::vector<std::string>* output);
Ben Chana0011d82014-05-13 00:19:29 -070033
David Pursell300498a2014-11-03 15:47:36 -080034 // 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,
Eric Carusocc7106c2017-04-27 14:22:42 -070065 brillo::ErrorPtr* error);
David Pursell300498a2014-11-03 15:47:36 -080066
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,
Eric Carusocc7106c2017-04-27 14:22:42 -070075 brillo::ErrorPtr* error);
David Pursell300498a2014-11-03 15:47:36 -080076
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 Jones1c4c3a12011-12-20 15:01:59 -050086 private:
David Pursell300498a2014-11-03 15:47:36 -080087 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,
Eric Carusocc7106c2017-04-27 14:22:42 -070097 brillo::ErrorPtr* error,
David Pursell300498a2014-11-03 15:47:36 -080098 ProcessWithOutput* process);
Elly Jones1c4c3a12011-12-20 15:01:59 -050099};
100
Ben Chana0011d82014-05-13 00:19:29 -0700101} // namespace debugd
Elly Jones1c4c3a12011-12-20 15:01:59 -0500102
Alex Vakulenko262be3f2014-07-30 15:25:50 -0700103#endif // DEBUGD_SRC_PROCESS_WITH_OUTPUT_H_