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

0001 /*
0002     SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef KSYSGUARD_PROCESSCONTROLLER_H
0008 #define KSYSGUARD_PROCESSCONTROLLER_H
0009 
0010 #include <memory>
0011 
0012 #include <signal.h>
0013 
0014 #include <QObject>
0015 #include <QVariant>
0016 
0017 #include "process.h"
0018 
0019 class QWidget;
0020 
0021 /**
0022  * Control processes' priority, scheduling and sending signals.
0023  *
0024  * This class contains methods for sending signals to processes, setting their
0025  * priority and setting their scheduler. It will first try to manipulate the
0026  * processes directly, if that fails, it will use KAuth to try and perform the
0027  * action as root.
0028  */
0029 namespace KSysGuard
0030 {
0031 class Q_DECL_EXPORT ProcessController : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     ProcessController(QObject *parent = nullptr);
0037     ~ProcessController() override;
0038 
0039     /**
0040      * A signal that can be sent to a process.
0041      */
0042     enum Signal {
0043         StopSignal = SIGSTOP,
0044         ContinueSignal = SIGCONT,
0045         HangupSignal = SIGHUP,
0046         InterruptSignal = SIGINT,
0047         TerminateSignal = SIGTERM,
0048         KillSignal = SIGKILL,
0049         User1Signal = SIGUSR1,
0050         User2Signal = SIGUSR2
0051     };
0052     Q_ENUM(Signal)
0053 
0054     /**
0055      * What kind of result a call to one of the ProcessController methods had.
0056      */
0057     enum class Result {
0058         Unknown, ///< Something happened, we just do not know what.
0059         Success, ///< Everything went alright.
0060         InsufficientPermissions, ///< Some processes require privileges to modify and we failed getting those.
0061         NoSuchProcess, ///< Tried to modify a process that no longer exists.
0062         Unsupported, ///< The specified action is not supported.
0063         UserCancelled, ///< The user cancelled the action, usually when requesting privileges.
0064         Error, ///< An error occurred when requesting privileges.
0065     };
0066     Q_ENUM(Result)
0067 
0068     /**
0069      * The widget used as parent for any dialogs that get shown.
0070      */
0071     QWidget *widget() const;
0072     /**
0073      * Set the widget to use as parent for dialogs.
0074      *
0075      * \param widget The widget to use.
0076      */
0077     void setWidget(QWidget *widget);
0078 
0079     /**
0080      * Send a signal to a number of processes.
0081      *
0082      * This will send \p signal to all processes in \p pids. Should a number of
0083      * these be owned by different users, an attempt will be made to send the
0084      * signal as root using KAuth.
0085      *
0086      * \param pids A vector of pids to send the signal to.
0087      * \param signal The signal to send. See Signal for possible values.
0088      *
0089      * \return A Result value that indicates whether the action succeeded. Note
0090      *         that a non-Success result may indicate any of the processes in
0091      *         \p pids encountered that result.
0092      */
0093     Q_INVOKABLE Result sendSignal(const QVector<int> &pids, int signal);
0094     /**
0095      * \overload Result sendSignal(const QVector<int> &pids, int signal)
0096      */
0097     Q_INVOKABLE Result sendSignal(const QList<long long> &pids, int signal);
0098     /**
0099      * \overload Result sendSignal(const QVector<int> &pids, int signal)
0100      */
0101     Q_INVOKABLE Result sendSignal(const QVariantList &pids, int signal);
0102 
0103     /**
0104      * Set the priority (niceness) of a number of processes.
0105      *
0106      * This will set the priority of all processes in \p pids to \p priority.
0107      * Should a number of these be owned by different users, an attempt will be
0108      * made to send the signal as root using KAuth.
0109      *
0110      * \param pids A vector of pids to set the priority of.
0111      * \param priority The priority to set. Lower means higher priority.
0112      *
0113      * \return A Result value that indicates whether the action succeeded. Note
0114      *         that a non-Success result may indicate any of the processes in
0115      *         \p pids encountered that result.
0116      */
0117     Q_INVOKABLE Result setPriority(const QVector<int> &pids, int priority);
0118     /**
0119      * \overload Result setPriority(const QVector<int> &pids, int priority)
0120      */
0121     Q_INVOKABLE Result setPriority(const QList<long long> &pids, int priority);
0122     /**
0123      * \overload Result setPriority(const QVector<int> &pids, int priority)
0124      */
0125     Q_INVOKABLE Result setPriority(const QVariantList &pids, int priority);
0126 
0127     /**
0128      * Set the CPU scheduling policy and priority of a number of processes.
0129      *
0130      * This will set the CPU scheduling policy and priority of all processes in
0131      * \p pids to \p scheduler and \p priority. Should a number of these be
0132      * owned by different users, an attempt will be made to send the signal as
0133      * root using KAuth.
0134      *
0135      * \param pids A vector of pids to set the scheduler of.
0136      * \param scheduler The scheduling policy to use.
0137      * \param priority The priority to set. Lower means higher priority.
0138      *
0139      * \return A Result value that indicates whether the action succeeded. Note
0140      *         that a non-Success result may indicate any of the processes in
0141      *         \p pids encountered that result.
0142      */
0143     Q_INVOKABLE Result setCPUScheduler(const QVector<int> &pids, Process::Scheduler scheduler, int priority);
0144     /**
0145      * \overload Result setCPUScheduler(const QVector<int> &pids, Process::Scheduler scheduler, int priority)
0146      */
0147     Q_INVOKABLE Result setCPUScheduler(const QList<long long> &pids, Process::Scheduler scheduler, int priority);
0148     /**
0149      * \overload Result setCPUScheduler(const QVector<int> &pids, Process::Scheduler scheduler, int priority)
0150      */
0151     Q_INVOKABLE Result setCPUScheduler(const QVariantList &pids, Process::Scheduler scheduler, int priority);
0152 
0153     /**
0154      * Set the IO scheduling policy and priority of a number of processes.
0155      *
0156      * This will set the IO scheduling policy and priority of all processes in
0157      * \p pids to \p priorityClass and \p priority. Should a number of these be
0158      * owned by different users, an attempt will be made to send the signal as
0159      * root using KAuth.
0160      *
0161      * \param pids A vector of pids to set the scheduler of.
0162      * \param priorityClass The scheduling policy to use.
0163      * \param priority The priority to set. Lower means higher priority.
0164      *
0165      * \return A Result value that indicates whether the action succeeded. Note
0166      *         that a non-Success result may indicate any of the processes in
0167      *         \p pids encountered that result.
0168      */
0169     Q_INVOKABLE Result setIOScheduler(const QVector<int> &pids, Process::IoPriorityClass priorityClass, int priority);
0170     /**
0171      * \overload Result setIOScheduler(const QVector<int> &pids, Process::IoPriorityClass priorityClass, int priority)
0172      */
0173     Q_INVOKABLE Result setIOScheduler(const QList<long long> &pids, Process::IoPriorityClass priorityClass, int priority);
0174     /**
0175      * \overload Result setIOScheduler(const QVector<int> &pids, Process::IoPriorityClass priorityClass, int priority)
0176      */
0177     Q_INVOKABLE Result setIOScheduler(const QVariantList &pids, Process::IoPriorityClass priorityClass, int priority);
0178 
0179     /**
0180      * Convert a Result value to a user-visible string.
0181      *
0182      *
0183      */
0184     Q_INVOKABLE QString resultToString(Result result);
0185 
0186 private:
0187     class Private;
0188     const std::unique_ptr<Private> d;
0189 };
0190 
0191 }
0192 
0193 #endif // KSYSGUARD_PROCESSCONTROLLER_H