DebugServer2
Loading...
Searching...
No Matches
ProcessSpawner.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Types.h"
14
15#include <functional>
16#include <thread>
17
18namespace ds2 {
19namespace Host {
20
21class ProcessSpawner {
22protected:
23 enum RedirectMode {
24 kRedirectUnset,
25 kRedirectConsole,
26 kRedirectNull,
27 kRedirectFile,
28 kRedirectBuffer,
29 kRedirectDelegate,
30 kRedirectTerminal,
31 };
32
33 typedef std::function<void(void *buf, size_t size)> RedirectDelegate;
34
35protected:
37 RedirectMode mode;
38 RedirectDelegate delegate;
39 std::string path;
40 int fd;
41
42 RedirectDescriptor() : mode(kRedirectUnset), delegate(nullptr), fd(-1) {}
43 };
44
45protected:
46 std::string _executablePath;
47 StringCollection _arguments;
48 EnvironmentBlock _environment;
49 std::string _workingDirectory;
50 std::thread _delegateThread;
51 RedirectDescriptor _descriptors[3];
52 std::string _outputBuffer;
53 int _exitStatus;
54 int _signalCode;
55 ProcessId _pid;
56 bool _shell;
57
58public:
61
62public:
63 bool setExecutable(std::string const &path);
64 bool setShellCommand(std::string const &command);
65 bool setWorkingDirectory(std::string const &path);
66
67public:
68 bool setArguments(StringCollection const &args);
69
70 template <typename... Args> inline bool setArguments(Args const &... args) {
71 std::string args_[] = {args...};
72 return setArguments(StringCollection(&args_[0], &args_[sizeof...(Args)]));
73 }
74
75public:
76 bool setEnvironment(EnvironmentBlock const &env);
77 bool addEnvironment(std::string const &key, std::string const &val);
78
79public:
80 bool redirectInputToConsole();
81 bool redirectOutputToConsole();
82 bool redirectErrorToConsole();
83
84public:
85 bool redirectInputToNull();
86 bool redirectOutputToNull();
87 bool redirectErrorToNull();
88
89public:
90 bool redirectInputToFile(std::string const &path);
91 bool redirectOutputToFile(std::string const &path);
92 bool redirectErrorToFile(std::string const &path);
93
94public:
95 bool redirectOutputToBuffer();
96 bool redirectErrorToBuffer();
97
98public:
99 bool redirectInputToTerminal();
100
101public:
102 bool redirectOutputToDelegate(RedirectDelegate delegate);
103 bool redirectErrorToDelegate(RedirectDelegate delegate);
104
105public:
106 ErrorCode run(std::function<bool()> preExecAction = []() { return true; });
107 ErrorCode wait();
108 bool isRunning() const;
109 void flushAndExit();
110
111public:
112 inline ProcessId pid() const { return _pid; }
113 inline int exitStatus() const { return _exitStatus; }
114 inline int signalCode() const { return _signalCode; }
115 inline std::string const &executable() const { return _executablePath; }
116 inline StringCollection const &arguments() const { return _arguments; }
117 inline EnvironmentBlock const &environment() const { return _environment; }
118
119public:
120 ErrorCode input(ByteVector const &buf);
121 inline std::string const &output() const { return _outputBuffer; }
122
123private:
124 void redirectionThread();
125};
126} // namespace Host
127} // namespace ds2
Definition ProcessSpawner.h:20
Definition ProcessSpawner.h:36