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

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