QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
executor.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
9#ifndef SRC_EXECUTOR_H_
10#define SRC_EXECUTOR_H_
11
12#include <QHash>
13#include <QObject>
14#include <QProcess>
15#include <QProcessEnvironment>
16#include <QQueue>
17#include <atomic>
18#include <optional>
19#include <utility>
20
31
32class Executor : public QObject {
33 Q_OBJECT
34
41 int id;
43 QString app;
45 QStringList args;
47 QString input;
56 QString workingDir;
57 };
58
63 QQueue<ExecQueueItem> m_execQueue;
65 QProcess m_process;
70 bool running{false};
81 void executeNext();
85 void postDrainedIfIdle();
90 bool m_drainCheckPosted = false;
93 quint64 m_runSerial = 0;
96 quint64 m_stoppedRun = 0;
99 bool m_outputLossless = true;
111 static void startProcess(QProcess &process, const QString &app,
112 const QStringList &args);
121 auto collectOutput(const ExecQueueItem &item, int exitCode)
122 -> std::pair<QString, QString>;
145 static auto runBlocking(QProcess &process, const QString &app,
146 const QStringList &args, const QString &input,
147 QString *process_out, QString *process_err,
148 const std::atomic_bool *cancel = nullptr,
149 int timeoutMs = -1) -> int;
165 static auto runBlockingRaw(QProcess &process, const QString &app,
166 const QStringList &args, const QByteArray &input,
167 QByteArray *process_out, QString *process_err,
168 const std::atomic_bool *cancel = nullptr,
169 int timeoutMs = -1) -> int;
170
171public:
181 static constexpr qsizetype kMaxStderrBytes = qsizetype{64} * 1024;
182
194 public:
199 void append(const QByteArray &chunk);
206 auto take() -> QByteArray;
217 [[nodiscard]] auto statusCounts() const -> QHash<QString, qint64> {
218 return m_status;
219 }
220
221 void clear() {
222 take();
223 m_status.clear();
224 }
225
226 static constexpr qsizetype kMaxStatusLineBytes = 1024;
228 static constexpr int kMaxStatusKeywords = 64;
229
230 private:
235 void countStatus(const QByteArray &line);
237 QByteArray m_line;
239 bool m_lineTooLong = false;
241 QHash<QString, qint64> m_status;
243 QByteArray m_head;
245 QByteArray m_tail;
247 qint64 m_dropped = 0;
248 };
249
250private:
257 QHash<QString, qint64> m_lastStatus;
259 bool m_discardStdout = false;
260
261public:
266 explicit Executor(QObject *parent = nullptr);
276 ~Executor() override;
277
286 void execute(int id, const QString &app, const QStringList &args,
287 bool readStdout, bool readStderr = true);
288
298 void execute(int id, const QString &workDir, const QString &app,
299 const QStringList &args, bool readStdout,
300 bool readStderr = true);
301
311 void execute(int id, const QString &app, const QStringList &args,
312 QString input = QString(), bool readStdout = false,
313 bool readStderr = true);
314
325 void execute(int id, const QString &workDir, const QString &app,
326 const QStringList &args, QString input = QString(),
327 bool readStdout = false, bool readStderr = true);
328
338 [[nodiscard]] static auto executeBlocking(const QString &app,
339 const QStringList &args,
340 const QString &input = QString(),
341 QString *process_out = nullptr,
342 QString *process_err = nullptr)
343 -> int;
344
377 [[nodiscard]] static auto executeBlocking(
378 QProcess &process, const QString &app, const QStringList &args,
379 const QString &input = QString(), QString *process_out = nullptr,
380 QString *process_err = nullptr, const std::atomic_bool *cancel = nullptr,
381 int timeoutMs = -1) -> int;
382
401 [[nodiscard]] static auto
402 executeBlockingRaw(QProcess &process, const QString &app,
403 const QStringList &args, const QByteArray &input,
404 QByteArray *process_out, QString *process_err = nullptr,
405 const std::atomic_bool *cancel = nullptr,
406 int timeoutMs = -1) -> int;
407
416 [[nodiscard]] static auto
417 executeBlocking(const QString &app, const QStringList &args,
418 QString *process_out, QString *process_err = nullptr) -> int;
419
429 [[nodiscard]] static auto
430 executeBlocking(const QProcessEnvironment &env, const QString &app,
431 const QStringList &args, QString *process_out = nullptr,
432 QString *process_err = nullptr) -> int;
433
445 static auto resolveExecutable(const QString &app) -> QString;
446
459 static void detachFromTerminal(QProcess &process);
460
471 static void setChildUmask(int mask);
472
477 [[nodiscard]] static auto childUmask() -> int;
478
484 struct WslCommand {
493 QString launcher;
498 QStringList options;
500 QString command;
512 auto argv(const QStringList &args) const -> QStringList;
518 auto with(const QString &program) const -> WslCommand;
519 };
520
536 static auto parseWslCommand(const QString &app) -> std::optional<WslCommand>;
537
545 static auto wslExecArgs(const QString &command, const QStringList &args)
546 -> QStringList;
547
556 [[nodiscard]] static auto decodeText(const QByteArray &bytes) -> QString;
557
568 static auto translatePathForWsl(const QString &path, const QString &exe)
569 -> QString;
570
575 void setEnvironment(const QProcessEnvironment &env);
580 auto environment() const -> QProcessEnvironment;
581
586 auto cancelNext() -> int;
587
599 auto stopRunning() -> bool;
600
605 [[nodiscard]] auto runningSerial() const -> quint64 {
606 return running ? m_runSerial : 0;
607 }
608
613 [[nodiscard]] auto runningId() const -> int {
614 return running && !m_execQueue.isEmpty() ? m_execQueue.head().id : -1;
615 }
616
620 auto isIdle() const -> bool { return !running && m_execQueue.isEmpty(); }
632 [[nodiscard]] auto lastOutputLossless() const -> bool {
633 return m_outputLossless;
634 }
635
645 [[nodiscard]] auto lastStatusCounts() const -> QHash<QString, qint64> {
646 return m_lastStatus;
647 }
648
659 auto hasQueued(int id) const -> bool {
660 for (const ExecQueueItem &item : m_execQueue) {
661 if (item.id == id) {
662 return true;
663 }
664 }
665 return false;
666 }
667
668private slots:
679 void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
680signals:
685 void drained();
695 void finished(int id, int exitCode, const QString &output,
696 const QString &errout);
701 void starting();
717 void dequeuing(int id, bool completes);
727 void error(int id, int exitCode, const QString &output,
728 const QString &errout);
729};
730
731#endif // SRC_EXECUTOR_H_
A stream of bytes held as its first and last kMaxStderrBytes / 2 bytes, with its gpg status records c...
Definition executor.h:193
void clear()
Drop everything, counts included, for the next command.
Definition executor.h:221
QByteArray m_head
The first bytes, up to half the cap.
Definition executor.h:243
static constexpr qsizetype kMaxStatusLineBytes
Longest line still looked at for a status keyword.
Definition executor.h:226
void countStatus(const QByteArray &line)
Count line if it is a status record.
Definition executor.cpp:72
bool m_lineTooLong
The line not ended yet ran past kMaxStatusLineBytes.
Definition executor.h:239
static constexpr int kMaxStatusKeywords
Most distinct status keywords counted.
Definition executor.h:228
void append(const QByteArray &chunk)
Add what the process wrote next.
Definition executor.cpp:89
qint64 m_dropped
Bytes that fell out between head and tail.
Definition executor.h:247
QHash< QString, qint64 > m_status
Status keyword to count, see statusCounts().
Definition executor.h:241
auto take() -> QByteArray
Everything kept, and start over.
Definition executor.cpp:131
auto statusCounts() const -> QHash< QString, qint64 >
How many complete lines of each gpg status keyword came by since the last take(), the left-out ones i...
Definition executor.h:217
QByteArray m_line
The line not ended yet; empty once it ran too long.
Definition executor.h:237
QByteArray m_tail
The latest bytes, up to half the cap.
Definition executor.h:245
void postDrainedIfIdle()
Definition executor.cpp:835
auto lastOutputLossless() const -> bool
Whether the stdout of the item whose finished() or error() is being emitted encodes back (toUtf8(),...
Definition executor.h:632
void dequeuing(int id, bool completes)
Emitted, synchronously, when the queue's head leaves it: just before it is started,...
auto stopRunning() -> bool
Stop the command that is running, if any: terminate it (its whole process group on Unix),...
Definition executor.cpp:739
QQueue< ExecQueueItem > m_execQueue
Commands waiting to run, head first. The head stays queued while it runs and is dequeued in onProcess...
Definition executor.h:63
auto isIdle() const -> bool
Whether no command is running and nothing is queued.
Definition executor.h:620
static auto childUmask() -> int
The umask setChildUmask() set.
Definition executor.cpp:225
static constexpr qsizetype kMaxStderrBytes
Most bytes of a command's stderr kept: the first and the last half of this, with a note of how much w...
Definition executor.h:181
quint64 m_stoppedRun
Definition executor.h:96
static auto executeBlocking(const QString &app, const QStringList &args, const QString &input=QString(), QString *process_out=nullptr, QString *process_err=nullptr) -> int
Run a command synchronously and return its exit code.
Definition executor.cpp:700
static auto runBlockingRaw(QProcess &process, const QString &app, const QStringList &args, const QByteArray &input, QByteArray *process_out, QString *process_err, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
runBlocking() on bytes: input is written and stdout returned as they are, with no text decoding or en...
Definition executor.cpp:633
static auto translatePathForWsl(const QString &path, const QString &exe) -> QString
Translate a native path for a WSL-routed executable.
Definition executor.cpp:335
auto environment() const -> QProcessEnvironment
Return the environment passed to child processes.
Definition executor.cpp:735
auto runningSerial() const -> quint64
Which run is going: a number no other run of this executor had.
Definition executor.h:605
bool m_discardStdout
The running queued command's stdout is not wanted: dropped as it comes.
Definition executor.h:259
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
Finish the running item when its process exits.
Definition executor.cpp:796
int m_pendingErrors
Definition executor.h:88
auto lastStatusCounts() const -> QHash< QString, qint64 >
The gpg status records on the stderr of the command whose finished() or error() is being emitted,...
Definition executor.h:645
auto collectOutput(const ExecQueueItem &item, int exitCode) -> std::pair< QString, QString >
Read what a finished process produced, according to the queue item's readStdout/readStderr flags; std...
Definition executor.cpp:854
CappedOutput m_stderr
stderr of the running queued command, read as it arrives and capped.
Definition executor.h:255
bool m_drainCheckPosted
A drained() check is posted and not run yet.
Definition executor.h:90
auto hasQueued(int id) const -> bool
Whether a command with process id id is queued or running.
Definition executor.h:659
auto runningId() const -> int
The id of the command that is running.
Definition executor.h:613
static void startProcess(QProcess &process, const QString &app, const QStringList &args)
Start process running app, handling the "wsl " prefix once for both the queued and the blocking path.
Definition executor.cpp:227
void execute(int id, const QString &app, const QStringList &args, bool readStdout, bool readStderr=true)
Queue a command without stdin input.
Definition executor.cpp:439
bool m_outputLossless
Definition executor.h:99
static auto resolveExecutable(const QString &app) -> QString
The one rule for turning a configured executable into what runs.
Definition executor.cpp:244
void drained()
Emitted once the last queued item is done and nothing runs or waits any more (isIdle()),...
void error(int id, int exitCode, const QString &output, const QString &errout)
Emitted instead of finished() when a queued process could not be started, had no executable configure...
static auto wslExecArgs(const QString &command, const QStringList &args) -> QStringList
Build the wsl.exe argument list that runs command directly.
Definition executor.cpp:330
static void setChildUmask(int mask)
The umask every child QtPass starts gets, as pass sets its own.
Definition executor.cpp:223
Executor(QObject *parent=nullptr)
Construct an Executor with an optional parent QObject.
Definition executor.cpp:33
QHash< QString, qint64 > m_lastStatus
lastStatusCounts(): set by collectOutput(), cleared after the emit.
Definition executor.h:257
static auto executeBlockingRaw(QProcess &process, const QString &app, const QStringList &args, const QByteArray &input, QByteArray *process_out, QString *process_err=nullptr, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
executeBlocking() on bytes, for an entry's plaintext: input is written to stdin and stdout returned e...
Definition executor.cpp:690
bool running
Whether the head of the queue has been started (or is being dequeued, see dequeuing()) and has not fi...
Definition executor.h:70
void finished(int id, int exitCode, const QString &output, const QString &errout)
Emitted when a queued process exits normally, whatever its exit code; a non-zero code is the receiver...
void starting()
Emitted just before a queued process is started; the main window shows the busy indicator on it.
static auto decodeText(const QByteArray &bytes) -> QString
Decode process output as executeBlocking() does: UTF-8 first, the system encoding when it is not UTF-...
Definition executor.cpp:490
quint64 m_runSerial
Definition executor.h:93
void setEnvironment(const QProcessEnvironment &env)
Set the environment passed to all child processes.
Definition executor.cpp:731
static auto runBlocking(QProcess &process, const QString &app, const QStringList &args, const QString &input, QString *process_out, QString *process_err, const std::atomic_bool *cancel=nullptr, int timeoutMs=-1) -> int
Shared blocking run: start process, optionally feed input on stdin, wait, and capture stdout/stderr....
Definition executor.cpp:618
static void detachFromTerminal(QProcess &process)
Make process start its child without a controlling terminal.
Definition executor.cpp:192
void executeNext()
Start the head of the queue unless something runs or the queue is empty.
Definition executor.cpp:359
static auto parseWslCommand(const QString &app) -> std::optional< WslCommand >
Parse a configured executable as a WSL command.
Definition executor.cpp:272
auto cancelNext() -> int
Cancel the next queued command without executing it.
Definition executor.cpp:780
QProcess m_process
The one process object; every queued command reuses it.
Definition executor.h:65
Execution queue item for non-interactive ordered execution.
Definition executor.h:39
bool readStdout
Whether stdout is captured for finished().
Definition executor.h:49
QString workingDir
Working directory to start in; empty for the current one.
Definition executor.h:56
QString input
Data written to the process's stdin, then closed.
Definition executor.h:47
int id
Identifier the caller gave; echoed in finished()/error().
Definition executor.h:41
bool readStderr
Whether stderr is captured; it is read regardless when the process exits non-zero.
Definition executor.h:54
QStringList args
Arguments for the executable.
Definition executor.h:45
QString app
Executable as configured; resolved when started.
Definition executor.h:43
A configured executable that runs a Linux program through WSL.
Definition executor.h:484
auto argv(const QStringList &args) const -> QStringList
Arguments for launcher that run command with args.
Definition executor.cpp:264
QString command
The Linux program to run, looked up on the WSL PATH.
Definition executor.h:500
QString launcher
What to start.
Definition executor.h:493
QStringList options
wsl.exe options that go before the program (-d Debian, -u me), without any -e/--exec: argv() always a...
Definition executor.h:498
auto with(const QString &program) const -> WslCommand
The same launcher and options running another program.
Definition executor.cpp:268