File indexing completed on 2024-04-28 05:31:36

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     }
0026 
0027 ActionReply KSysGuardProcessListHelper::sendsignal(const QVariantMap &parameters)
0028 {
0029     ActionReply reply(ActionReply::HelperErrorType);
0030     if (!parameters.contains(QLatin1String("signal"))) {
0031         reply.setErrorDescription(QStringLiteral("Internal error - no signal parameter was passed to the helper"));
0032         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::InvalidPid));
0033         return reply;
0034     }
0035     if (!parameters.contains(QLatin1String("pidcount"))) {
0036         reply.setErrorDescription(QStringLiteral("Internal error - no pidcount parameter was passed to the helper"));
0037         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::InvalidParameter));
0038         return reply;
0039     }
0040 
0041     KSysGuard::ProcessesLocal processes;
0042     int signal = qvariant_cast<int>(parameters.value(QStringLiteral("signal")));
0043     bool success = true;
0044     int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0045     QStringList errorList;
0046     for (int i = 0; i < numProcesses; ++i) {
0047         qlonglong pid = GET_PID(i);
0048         KSysGuard::Processes::Error error = processes.sendSignal(pid, signal);
0049         if (error != KSysGuard::Processes::NoError) {
0050             errorList << QString::number(pid);
0051             success = false;
0052         }
0053     }
0054     if (success) {
0055         return ActionReply::SuccessReply();
0056     } else {
0057         reply.setErrorDescription(QStringLiteral("Could not send signal to: ") + errorList.join(QLatin1String(", ")));
0058         reply.setErrorCode(static_cast<ActionReply::Error>(KSysGuard::Processes::Unknown));
0059         return reply;
0060     }
0061 }
0062 
0063 ActionReply KSysGuardProcessListHelper::renice(const QVariantMap &parameters)
0064 {
0065     if (!parameters.contains(QLatin1String("nicevalue")) || !parameters.contains(QLatin1String("pidcount"))) {
0066         return ActionReply(ActionReply::HelperErrorType);
0067     }
0068 
0069     KSysGuard::ProcessesLocal processes;
0070     int niceValue = qvariant_cast<int>(parameters.value(QStringLiteral("nicevalue")));
0071     bool success = true;
0072     int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0073     for (int i = 0; i < numProcesses; ++i) {
0074         qlonglong pid = GET_PID(i);
0075         success &= (processes.setNiceness(pid, niceValue) != KSysGuard::Processes::NoError);
0076     }
0077     if (success) {
0078         return ActionReply::SuccessReply();
0079     } else {
0080         return ActionReply(ActionReply::HelperErrorType);
0081     }
0082 }
0083 
0084 ActionReply KSysGuardProcessListHelper::changeioscheduler(const QVariantMap &parameters)
0085 {
0086     if (!parameters.contains(QLatin1String("ioScheduler")) || !parameters.contains(QLatin1String("ioSchedulerPriority"))
0087         || !parameters.contains(QLatin1String("pidcount"))) {
0088         return ActionReply(ActionReply::HelperErrorType);
0089     }
0090 
0091     KSysGuard::ProcessesLocal processes;
0092     int ioScheduler = qvariant_cast<int>(parameters.value(QStringLiteral("ioScheduler")));
0093     int ioSchedulerPriority = qvariant_cast<int>(parameters.value(QStringLiteral("ioSchedulerPriority")));
0094     bool success = true;
0095     const int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0096     for (int i = 0; i < numProcesses; ++i) {
0097         qlonglong pid = GET_PID(i);
0098         success &= (processes.setIoNiceness(pid, ioScheduler, ioSchedulerPriority) != KSysGuard::Processes::NoError);
0099     }
0100     if (success) {
0101         return ActionReply::SuccessReply();
0102     } else {
0103         return ActionReply(ActionReply::HelperErrorType);
0104     }
0105 }
0106 
0107 ActionReply KSysGuardProcessListHelper::changecpuscheduler(const QVariantMap &parameters)
0108 {
0109     if (!parameters.contains(QLatin1String("cpuScheduler")) || !parameters.contains(QLatin1String("cpuSchedulerPriority"))
0110         || !parameters.contains(QLatin1String("pidcount"))) {
0111         return ActionReply(ActionReply::HelperErrorType);
0112     }
0113 
0114     KSysGuard::ProcessesLocal processes;
0115     int cpuScheduler = qvariant_cast<int>(parameters.value(QStringLiteral("cpuScheduler")));
0116     int cpuSchedulerPriority = qvariant_cast<int>(parameters.value(QStringLiteral("cpuSchedulerPriority")));
0117     bool success = true;
0118 
0119     const int numProcesses = parameters.value(QStringLiteral("pidcount")).toInt();
0120     for (int i = 0; i < numProcesses; ++i) {
0121         qlonglong pid = GET_PID(i);
0122         success &= (processes.setScheduler(pid, cpuScheduler, cpuSchedulerPriority) != KSysGuard::Processes::NoError);
0123     }
0124     if (success) {
0125         return ActionReply::SuccessReply();
0126     } else {
0127         return ActionReply(ActionReply::HelperErrorType);
0128     }
0129 }
0130 
0131 KAUTH_HELPER_MAIN("org.kde.ksysguard.processlisthelper", KSysGuardProcessListHelper)