-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandexecutor.cpp
More file actions
143 lines (118 loc) · 4.28 KB
/
Copy pathcommandexecutor.cpp
File metadata and controls
143 lines (118 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "commandexecutor.h"
#include <QDebug>
#include <QCoreApplication>
#include <unistd.h>
CommandExecutor::CommandExecutor(QObject *parent) : QObject(parent)
{
m_process = new QProcess(this);
setupProcessConnections();
}
CommandExecutor::~CommandExecutor()
{
if (m_process->state() == QProcess::Running) {
m_process->terminate();
if (!m_process->waitForFinished(3000)) {
m_process->kill();
}
}
}
void CommandExecutor::setupProcessConnections()
{
connect(m_process, &QProcess::started, [this]() {
emit started(m_process->program() + " " + m_process->arguments().join(" "));
});
connect(m_process, &QProcess::errorOccurred, this, &CommandExecutor::handleError);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &CommandExecutor::handleFinished);
connect(m_process, &QProcess::readyReadStandardOutput,
this, &CommandExecutor::handleStandardOutput);
connect(m_process, &QProcess::readyReadStandardError,
this, &CommandExecutor::handleErrorOutput);
}
bool CommandExecutor::executeAsAdmin(const QString &command, const QStringList &args)
{
if (m_process->state() != QProcess::NotRunning) {
qWarning() << "Попытка запустить команду, когда процесс уже выполняется";
return false;
}
QStringList fullArgs;
fullArgs << command << args;
qDebug() << fullArgs;
m_process->start("sudo", fullArgs);
return m_process->waitForStarted();
}
bool CommandExecutor::execute(const QString &command, const QStringList &args)
{
if (m_process->state() != QProcess::NotRunning) {
qWarning() << "Попытка запустить команду, когда процесс уже выполняется";
return false;
}
m_process->start(command, args);
return m_process->waitForStarted();
}
int CommandExecutor::executeAsAdminSync(const QString &command, const QStringList &args,
QString &output, QString &errorOutput)
{
QProcess process;
QStringList fullArgs;
fullArgs << command << args;
process.start("sudo", fullArgs);
if (!process.waitForStarted()) {
qWarning() << "Не удалось запустить команду с повышенными привелегиями:" << command;
return -1;
}
process.waitForFinished(-1); // Ждем завершения
output = QString::fromUtf8(process.readAllStandardOutput());
errorOutput = QString::fromUtf8(process.readAllStandardError());
return process.exitCode();
}
int CommandExecutor::executeSync(const QString &command, const QStringList &args,
QString &output, QString &errorOutput)
{
QProcess process;
process.start(command, args);
if (!process.waitForStarted()) {
qWarning() << "Не удалось запустить команду:" << command;
return -1;
}
process.waitForFinished(-1); // Ждем завершения
output = QString::fromUtf8(process.readAllStandardOutput());
errorOutput = QString::fromUtf8(process.readAllStandardError());
return process.exitCode();
}
void CommandExecutor::abort()
{
if (m_process->state() == QProcess::Running) {
m_process->terminate();
if (!m_process->waitForFinished(3000)) {
m_process->kill();
}
}
}
bool CommandExecutor::hasAdminRights()
{
// Проверяем, запущен ли процесс с правами root (uid = 0)
return geteuid() == 0;
}
void CommandExecutor::handleStandardOutput()
{
QByteArray data = m_process->readAllStandardOutput();
qDebug() << "Есть вывод";
emit outputAvailable(QString::fromUtf8(data));
}
void CommandExecutor::handleErrorOutput()
{
QByteArray data = m_process->readAllStandardError();
qDebug() << "Есть ошибка";
emit errorOutputAvailable(QString::fromUtf8(data));
}
void CommandExecutor::handleFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "Процесс завершился";
emit finished(exitCode, exitStatus);
}
void CommandExecutor::handleError(QProcess::ProcessError error)
{
qDebug() << "Ошибка";
emit this->error(error);
}