libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <sys/types.h>
15#include <promeki/core/string.h>
16#include <promeki/core/list.h>
17#include <promeki/core/map.h>
18#include <promeki/core/buffer.h>
20#include <promeki/core/error.h>
21
23
46class Process : public ObjectBase {
47 PROMEKI_OBJECT(Process, ObjectBase)
48 public:
57
59 Process(const Process &) = delete;
61 Process &operator=(const Process &) = delete;
62
68
73
78 const String &program() const { return _program; }
79
84 void setProgram(const String &program) { _program = program; }
85
90 const List<String> &arguments() const { return _arguments; }
91
96 void setArguments(const List<String> &args) { _arguments = args; }
97
102 const FilePath &workingDirectory() const { return _workingDirectory; }
103
110 void setWorkingDirectory(const FilePath &dir) { _workingDirectory = dir; }
111
116 const Map<String, String> &environment() const { return _environment; }
117
124 void setEnvironment(const Map<String, String> &env) { _environment = env; }
125
131
139
145 Error waitForStarted(unsigned int timeoutMs = 0);
146
154 Error waitForFinished(unsigned int timeoutMs = 0);
155
162 int exitCode() const { return _exitCode; }
163
168 bool isRunning() const { return _state == Running; }
169
174 State state() const { return _state; }
175
182 pid_t pid() const { return _pid; }
183
187 void kill();
188
192 void terminate();
193
200 ssize_t writeToStdin(const void *buf, size_t bytes);
201
207
213
220
224
228
232
236
240
241 private:
242 String _program;
243 List<String> _arguments;
244 FilePath _workingDirectory;
245 Map<String, String> _environment;
246 State _state = NotRunning;
247 pid_t _pid = -1;
248 int _exitCode = -1;
249 int _stdinPipe[2] = {-1, -1};
250 int _stdoutPipe[2] = {-1, -1};
251 int _stderrPipe[2] = {-1, -1};
252 List<Buffer> _stdoutChunks;
253 List<Buffer> _stderrChunks;
254 size_t _stdoutTotal = 0;
255 size_t _stderrTotal = 0;
256
258 void closeFd(int &fd);
259
261 void closeAllPipes();
262
265 void drainFd(int &fd, List<Buffer> &chunks, size_t &total);
266
268 void drainPipes();
269
271 Buffer assembleChunks(List<Buffer> &chunks, size_t &total);
272
274 bool collectExitStatus();
275};
276
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Simple value type wrapping std::filesystem::path.
Definition filepath.h:27
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
ObjectBase * parent() const
Returns the parent object, if one. nullptr if none.
Definition objectbase.h:258
Subprocess execution with pipe-based I/O.
Definition process.h:46
void setWorkingDirectory(const FilePath &dir)
Sets the working directory for the subprocess.
Definition process.h:110
State
Process state.
Definition process.h:52
@ Running
Process is running.
Definition process.h:55
@ NotRunning
Process is not running.
Definition process.h:53
@ Starting
Process is being started.
Definition process.h:54
PROMEKI_SIGNAL(readyReadStdout)
Emitted when data is available on the child's stdout.
PROMEKI_SIGNAL(readyReadStderr)
Emitted when data is available on the child's stderr.
Buffer readAllStderr()
Reads all available data from the child's stderr.
void setProgram(const String &program)
Sets the program to execute.
Definition process.h:84
void closeWriteChannel()
Closes the write end of the stdin pipe.
PROMEKI_SIGNAL(errorOccurred, Error)
Emitted when an error occurs during subprocess execution.
void setArguments(const List< String > &args)
Sets the argument list for the subprocess.
Definition process.h:96
int exitCode() const
Returns the exit code of the finished process.
Definition process.h:162
ssize_t writeToStdin(const void *buf, size_t bytes)
Writes data to the child's stdin pipe.
Buffer readAllStdout()
Reads all available data from the child's stdout.
Process & operator=(const Process &)=delete
Deleted copy assignment operator (non-copyable).
void setEnvironment(const Map< String, String > &env)
Sets the environment variables for the subprocess.
Definition process.h:124
const String & program() const
Returns the program path.
Definition process.h:78
const List< String > & arguments() const
Returns the argument list.
Definition process.h:90
const Map< String, String > & environment() const
Returns the environment variables for the subprocess.
Definition process.h:116
PROMEKI_SIGNAL(started)
Emitted when the subprocess starts running.
pid_t pid() const
Returns the child process ID.
Definition process.h:182
void kill()
Sends SIGKILL to the child process.
const FilePath & workingDirectory() const
Returns the working directory for the subprocess.
Definition process.h:102
Error waitForFinished(unsigned int timeoutMs=0)
Blocks until the subprocess has finished or the timeout expires.
~Process()
Destructor. Kills the child process if still running.
bool isRunning() const
Returns true if the subprocess is currently running.
Definition process.h:168
State state() const
Returns the current process state.
Definition process.h:174
Process(const Process &)=delete
Deleted copy constructor (non-copyable).
void terminate()
Sends SIGTERM to the child process.
Error waitForStarted(unsigned int timeoutMs=0)
Blocks until the subprocess has started or the timeout expires.
Error start()
Starts the subprocess using the previously set program and arguments.
Process(ObjectBase *parent=nullptr)
Default constructor.
Error start(const String &program, const List< String > &args)
Convenience: sets program and arguments, then starts.
PROMEKI_SIGNAL(finished, int)
Emitted when the subprocess finishes (carries the exit code).
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19