libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <sys/types.h>
15#include <promeki/namespace.h>
16#include <promeki/platform.h>
17#include <promeki/objectbase.h>
18#include <promeki/string.h>
19#include <promeki/list.h>
20#include <promeki/map.h>
21#include <promeki/buffer.h>
22#include <promeki/filepath.h>
23#include <promeki/error.h>
24
25PROMEKI_NAMESPACE_BEGIN
26
56class Process : public ObjectBase {
57 PROMEKI_OBJECT(Process, ObjectBase)
58 public:
62 enum State {
63 NotRunning = 0,
64 Starting,
65 Running
66 };
67
72 Process(ObjectBase *parent = nullptr);
73
77 ~Process();
78
83 const String &program() const { return _program; }
84
89 void setProgram(const String &program) { _program = program; }
90
95 const List<String> &arguments() const { return _arguments; }
96
101 void setArguments(const List<String> &args) { _arguments = args; }
102
107 const FilePath &workingDirectory() const { return _workingDirectory; }
108
115 void setWorkingDirectory(const FilePath &dir) { _workingDirectory = dir; }
116
121 const Map<String, String> &environment() const { return _environment; }
122
129 void setEnvironment(const Map<String, String> &env) { _environment = env; }
130
135 Error start();
136
143 Error start(const String &program, const List<String> &args);
144
150 Error waitForStarted(unsigned int timeoutMs = 0);
151
159 Error waitForFinished(unsigned int timeoutMs = 0);
160
167 int exitCode() const { return _exitCode; }
168
173 bool isRunning() const { return _state == Running; }
174
179 State state() const { return _state; }
180
187 pid_t pid() const { return _pid; }
188
192 void kill();
193
197 void terminate();
198
205 ssize_t writeToStdin(const void *buf, size_t bytes);
206
211 Buffer readAllStdout();
212
217 Buffer readAllStderr();
218
224 void closeWriteChannel();
225
228 PROMEKI_SIGNAL(started);
229
232 PROMEKI_SIGNAL(finished, int);
233
236 PROMEKI_SIGNAL(readyReadStdout);
237
240 PROMEKI_SIGNAL(readyReadStderr);
241
244 PROMEKI_SIGNAL(errorOccurred, Error);
245
246 private:
247 String _program;
248 List<String> _arguments;
249 FilePath _workingDirectory;
250 Map<String, String> _environment;
251 State _state = NotRunning;
252 pid_t _pid = -1;
253 int _exitCode = -1;
254 int _stdinPipe[2] = {-1, -1};
255 int _stdoutPipe[2] = {-1, -1};
256 int _stderrPipe[2] = {-1, -1};
257 List<Buffer> _stdoutChunks;
258 List<Buffer> _stderrChunks;
259 size_t _stdoutTotal = 0;
260 size_t _stderrTotal = 0;
261
263 void closeFd(int &fd);
264
266 void closeAllPipes();
267
270 void drainFd(int &fd, List<Buffer> &chunks, size_t &total);
271
273 void drainPipes();
274
276 Buffer assembleChunks(List<Buffer> &chunks, size_t &total);
277
279 bool collectExitStatus();
280};
281
282PROMEKI_NAMESPACE_END
283
284#endif // PROMEKI_ENABLE_CORE