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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
SPDX-FileCopyrightText: 2009 Jan Gerrit Marker <jangerrit@weiler-marker.com>
SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "killrunner.h"
#include <QAction>
#include <QDebug>
#include <QIcon>
#include <KAuth/Action>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KProcess>
#include <processcore/process.h>
#include <processcore/processes.h>
K_PLUGIN_CLASS_WITH_JSON(KillRunner, "plasma-runner-kill.json")
KillRunner::KillRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
: Plasma::AbstractRunner(parent, metaData, args)
, m_processes(nullptr)
{
setObjectName(QStringLiteral("Kill Runner"));
auto *sigterm = new QAction(QIcon::fromTheme(QStringLiteral("application-exit")), i18n("Send SIGTERM"), this);
sigterm->setData(15);
auto *sigkill = new QAction(QIcon::fromTheme(QStringLiteral("process-stop")), i18n("Send SIGKILL"), this);
sigkill->setData(9);
m_actionList = {sigterm, sigkill};
connect(this, &Plasma::AbstractRunner::prepare, this, &KillRunner::prep);
connect(this, &Plasma::AbstractRunner::teardown, this, &KillRunner::cleanup);
m_delayedCleanupTimer.setInterval(50);
m_delayedCleanupTimer.setSingleShot(true);
connect(&m_delayedCleanupTimer, &QTimer::timeout, this, &KillRunner::cleanup);
}
KillRunner::~KillRunner() = default;
void KillRunner::reloadConfiguration()
{
KConfigGroup grp = config();
m_triggerWord.clear();
if (grp.readEntry(CONFIG_USE_TRIGGERWORD, true)) {
m_triggerWord = grp.readEntry(CONFIG_TRIGGERWORD, i18n("kill")) + QLatin1Char(' ');
}
m_hasTrigger = !m_triggerWord.isEmpty();
m_sorting = (Sort)grp.readEntry(CONFIG_SORTING, static_cast<int>(Sort::NONE));
QList<Plasma::RunnerSyntax> syntaxes;
syntaxes << Plasma::RunnerSyntax(m_triggerWord + QStringLiteral(":q:"), i18n("Terminate running applications whose names match the query."));
setSyntaxes(syntaxes);
if (m_hasTrigger) {
setTriggerWords({m_triggerWord});
setMinLetterCount(minLetterCount() + 2); // Requires two characters after trigger word
} else {
setMinLetterCount(2);
setMatchRegex(QRegularExpression());
}
}
void KillRunner::prep()
{
m_delayedCleanupTimer.stop();
}
void KillRunner::cleanup()
{
if (!m_processes) {
return;
}
if (m_prepLock.tryLockForWrite()) {
delete m_processes;
m_processes = nullptr;
m_prepLock.unlock();
} else {
m_delayedCleanupTimer.stop();
}
}
void KillRunner::match(Plasma::RunnerContext &context)
{
QString term = context.query();
m_prepLock.lockForRead();
if (!m_processes) {
m_prepLock.unlock();
m_prepLock.lockForWrite();
if (!m_processes) {
suspendMatching(true);
m_processes = new KSysGuard::Processes();
m_processes->updateAllProcesses();
suspendMatching(false);
}
}
m_prepLock.unlock();
term = term.right(term.length() - m_triggerWord.length());
QList<Plasma::QueryMatch> matches;
const QList<KSysGuard::Process *> processlist = m_processes->getAllProcesses();
for (const KSysGuard::Process *process : processlist) {
if (!context.isValid()) {
return;
}
const QString name = process->name();
if (!name.contains(term, Qt::CaseInsensitive)) {
continue;
}
const quint64 pid = process->pid();
Plasma::QueryMatch match(this);
match.setText(i18n("Terminate %1", name));
match.setSubtext(i18n("Process ID: %1", QString::number(pid)));
match.setIconName(QStringLiteral("application-exit"));
match.setData(pid);
match.setId(name);
match.setActions(m_actionList);
// Set the relevance
switch (m_sorting) {
case Sort::CPU:
match.setRelevance((process->userUsage() + process->sysUsage()) / 100);
break;
case Sort::CPUI:
match.setRelevance(1 - (process->userUsage() + process->sysUsage()) / 100);
break;
case Sort::NONE:
match.setRelevance(name.compare(term, Qt::CaseInsensitive) == 0 ? 1 : 9);
break;
}
matches << match;
}
context.addMatches(matches);
}
void KillRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Q_UNUSED(context)
const quint64 pid = match.data().toUInt();
int signal;
if (match.selectedAction()) {
signal = match.selectedAction()->data().toInt();
} else {
signal = 9; // default: SIGKILL
}
const QStringList args = {QStringLiteral("-%1").arg(signal), QString::number(pid)};
int returnCode = KProcess::execute(QStringLiteral("kill"), args);
if (returnCode == 0) {
return;
}
KAuth::Action killAction = QStringLiteral("org.kde.ksysguard.processlisthelper.sendsignal");
killAction.setHelperId(QStringLiteral("org.kde.ksysguard.processlisthelper"));
killAction.addArgument(QStringLiteral("pid0"), pid);
killAction.addArgument(QStringLiteral("pidcount"), 1);
killAction.addArgument(QStringLiteral("signal"), signal);
killAction.execute();
}
#include "killrunner.moc"
|