File indexing completed on 2024-04-28 16:49:53

0001 /*
0002     KSysGuard, the KDE System Guard
0003 
0004     SPDX-FileCopyrightText: 2009 John Tapsell <john.tapsell@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008 */
0009 
0010 #include "helper.h"
0011 #include "processes_local_p.h"
0012 
0013 using namespace KAuth;
0014 
0015 KSysGuardProcessListHelper::KSysGuardProcessListHelper()
0016 {
0017     qRegisterMetaType<QList<long long>>();
0018 }
0019 
0020 /* The functions here run as ROOT.  So be careful.  DO NOT TRUST THE INPUTS TO BE SANE. */
0021 #define GET_PID(i)                                                                                                                                             \
0022     parameters.value(QStringLiteral("pid%1").arg(i), -1).toULongLong();                                                                                        \
0023     if (pid < 0)                                                                                                                                               \
0024         return ActionReply(ActionReply::HelperErrorType);
0025 ActionReply KSysGuardProcessListHelper::sendsignal(const QVariantMap &parameters)
0026 {
0027     ActionReply reply(ActionReply::HelperErrorType);
0028     if (!parameters.contains(QLatin1String("signal"))) {
0029         reply.setErrorDescription(QStringLiteral("Internal error - no signal parameter was passed to the helper"));
0030         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::InvalidPid));
0031         return reply;
0032     }
0033     if (!parameters.contains(QLatin1String("pidcount"))) {
0034         reply.setErrorDescription(QStringLiteral("Internal error - no pidcount parameter was passed to the helper"));
0035         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::InvalidParameter));
0036         return reply;
0037     }
0038 
0039     KSysGuard::ProcessesLocal processes;
0040     int signal = qvariant_cast<int>(parameters.value(QStringLiteral("signal")));
0041     bool success = true;
0042     int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0043     QStringList errorList;
0044     for (int i = 0; i < numProcesses; ++i) {
0045         qlonglong pid = GET_PID(i);
0046         KSysGuard::Processes::Error error = processes.sendSignal(pid, signal);
0047         if (error != KSysGuard::Processes::NoError) {
0048             errorList << QString::number(pid);
0049             success = false;
0050         }
0051     }
0052     if (success) {
0053         return ActionReply::SuccessReply();
0054     } else {
0055         reply.setErrorDescription(QStringLiteral("Could not send signal to: ") + errorList.join(QLatin1String(", ")));
0056         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::Unknown));
0057         return reply;
0058     }
0059 }
0060 
0061 ActionReply KSysGuardProcessListHelper::renice(const QVariantMap &parameters)
0062 {
0063     if (!parameters.contains(QLatin1String("nicevalue")) || !parameters.contains(QLatin1String("pidcount")))
0064         return ActionReply(ActionReply::HelperErrorType);
0065 
0066     KSysGuard::ProcessesLocal processes;
0067     int niceValue = qvariant_cast<int>(parameters.value(QStringLiteral("nicevalue")));
0068     bool success = true;
0069     int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0070     for (int i = 0; i < numProcesses; ++i) {
0071         qlonglong pid = GET_PID(i);
0072         success &= (processes.setNiceness(pid, niceValue) != KSysGuard::Processes::NoError);
0073     }
0074     if (success)
0075         return ActionReply::SuccessReply();
0076     else
0077         return ActionReply(ActionReply::HelperErrorType);
0078 }
0079 
0080 ActionReply KSysGuardProcessListHelper::changeioscheduler(const QVariantMap &parameters)
0081 {
0082     if (!parameters.contains(QLatin1String("ioScheduler")) || !parameters.contains(QLatin1String("ioSchedulerPriority"))
0083         || !parameters.contains(QLatin1String("pidcount")))
0084         return ActionReply(ActionReply::HelperErrorType);
0085 
0086     KSysGuard::ProcessesLocal processes;
0087     int ioScheduler = qvariant_cast<int>(parameters.value(QStringLiteral("ioScheduler")));
0088     int ioSchedulerPriority = qvariant_cast<int>(parameters.value(QStringLiteral("ioSchedulerPriority")));
0089     bool success = true;
0090     const int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0091     for (int i = 0; i < numProcesses; ++i) {
0092         qlonglong pid = GET_PID(i);
0093         success &= (processes.setIoNiceness(pid, ioScheduler, ioSchedulerPriority) != KSysGuard::Processes::NoError);
0094     }
0095     if (success)
0096         return ActionReply::SuccessReply();
0097     else
0098         return ActionReply(ActionReply::HelperErrorType);
0099 }
0100 ActionReply KSysGuardProcessListHelper::changecpuscheduler(const QVariantMap &parameters)
0101 {
0102     if (!parameters.contains(QLatin1String("cpuScheduler")) || !parameters.contains(QLatin1String("cpuSchedulerPriority"))
0103         || !parameters.contains(QLatin1String("pidcount")))
0104         return ActionReply(ActionReply::HelperErrorType);
0105 
0106     KSysGuard::ProcessesLocal processes;
0107     int cpuScheduler = qvariant_cast<int>(parameters.value(QStringLiteral("cpuScheduler")));
0108     int cpuSchedulerPriority = qvariant_cast<int>(parameters.value(QStringLiteral("cpuSchedulerPriority")));
0109     bool success = true;
0110 
0111     const int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0112     for (int i = 0; i < numProcesses; ++i) {
0113         qlonglong pid = GET_PID(i);
0114         success &= (processes.setScheduler(pid, cpuScheduler, cpuSchedulerPriority) != KSysGuard::Processes::NoError);
0115     }
0116     if (success)
0117         return ActionReply::SuccessReply();
0118     else
0119         return ActionReply(ActionReply::HelperErrorType);
0120 }
0121 KAUTH_HELPER_MAIN("org.kde.ksysguard.processlisthelper", KSysGuardProcessListHelper)