File: process_controller.h

package info (click to toggle)
libksysguard 4%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,596 kB
  • sloc: cpp: 13,691; xml: 297; sh: 23; makefile: 11
file content (197 lines) | stat: -rw-r--r-- 7,332 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
    SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#ifndef KSYSGUARD_PROCESSCONTROLLER_H
#define KSYSGUARD_PROCESSCONTROLLER_H

#include <memory>

#include <signal.h>

#include <QObject>
#include <QVariant>
#include <qqmlintegration.h>

#include "process.h"

#include "processcore_export.h"

class QWindow;

/**
 * Control processes' priority, scheduling and sending signals.
 *
 * This class contains methods for sending signals to processes, setting their
 * priority and setting their scheduler. It will first try to manipulate the
 * processes directly, if that fails, it will use KAuth to try and perform the
 * action as root.
 */
namespace KSysGuard
{
class PROCESSCORE_EXPORT ProcessController : public QObject
{
    Q_OBJECT
    QML_ELEMENT

public:
    ProcessController(QObject *parent = nullptr);
    ~ProcessController() override;

    /**
     * A signal that can be sent to a process.
     */
    enum Signal {
        StopSignal = SIGSTOP,
        ContinueSignal = SIGCONT,
        HangupSignal = SIGHUP,
        InterruptSignal = SIGINT,
        TerminateSignal = SIGTERM,
        KillSignal = SIGKILL,
        User1Signal = SIGUSR1,
        User2Signal = SIGUSR2
    };
    Q_ENUM(Signal)

    /**
     * What kind of result a call to one of the ProcessController methods had.
     */
    enum class Result {
        Unknown, ///< Something happened, we just do not know what.
        Success, ///< Everything went alright.
        InsufficientPermissions, ///< Some processes require privileges to modify and we failed getting those.
        NoSuchProcess, ///< Tried to modify a process that no longer exists.
        Unsupported, ///< The specified action is not supported.
        UserCancelled, ///< The user cancelled the action, usually when requesting privileges.
        Error, ///< An error occurred when requesting privileges.
    };
    Q_ENUM(Result)

    /**
     * The window used as parent for any dialogs that get shown.
     */
    QWindow *window() const;
    /**
     * Set the window to use as parent for dialogs.
     *
     * \param window The window to use.
     */
    void setWindow(QWindow *window);

    /**
     * Send a signal to a number of processes.
     *
     * This will send \p signal to all processes in \p pids. Should a number of
     * these be owned by different users, an attempt will be made to send the
     * signal as root using KAuth.
     *
     * \param pids A vector of pids to send the signal to.
     * \param signal The signal to send. See Signal for possible values.
     *
     * \return A Result value that indicates whether the action succeeded. Note
     *         that a non-Success result may indicate any of the processes in
     *         \p pids encountered that result.
     */
    Q_INVOKABLE Result sendSignal(const QList<int> &pids, int signal);
    /**
     * \overload Result sendSignal(const QList<int> &pids, int signal)
     */
    Q_INVOKABLE Result sendSignal(const QList<long long> &pids, int signal);
    /**
     * \overload Result sendSignal(const QList<int> &pids, int signal)
     */
    Q_INVOKABLE Result sendSignal(const QVariantList &pids, int signal);

    /**
     * Set the priority (niceness) of a number of processes.
     *
     * This will set the priority of all processes in \p pids to \p priority.
     * Should a number of these be owned by different users, an attempt will be
     * made to send the signal as root using KAuth.
     *
     * \param pids A vector of pids to set the priority of.
     * \param priority The priority to set. Lower means higher priority.
     *
     * \return A Result value that indicates whether the action succeeded. Note
     *         that a non-Success result may indicate any of the processes in
     *         \p pids encountered that result.
     */
    Q_INVOKABLE Result setPriority(const QList<int> &pids, int priority);
    /**
     * \overload Result setPriority(const QList<int> &pids, int priority)
     */
    Q_INVOKABLE Result setPriority(const QList<long long> &pids, int priority);
    /**
     * \overload Result setPriority(const QList<int> &pids, int priority)
     */
    Q_INVOKABLE Result setPriority(const QVariantList &pids, int priority);

    /**
     * Set the CPU scheduling policy and priority of a number of processes.
     *
     * This will set the CPU scheduling policy and priority of all processes in
     * \p pids to \p scheduler and \p priority. Should a number of these be
     * owned by different users, an attempt will be made to send the signal as
     * root using KAuth.
     *
     * \param pids A vector of pids to set the scheduler of.
     * \param scheduler The scheduling policy to use.
     * \param priority The priority to set. Lower means higher priority.
     *
     * \return A Result value that indicates whether the action succeeded. Note
     *         that a non-Success result may indicate any of the processes in
     *         \p pids encountered that result.
     */
    Q_INVOKABLE Result setCPUScheduler(const QList<int> &pids, Process::Scheduler scheduler, int priority);
    /**
     * \overload Result setCPUScheduler(const QList<int> &pids, Process::Scheduler scheduler, int priority)
     */
    Q_INVOKABLE Result setCPUScheduler(const QList<long long> &pids, Process::Scheduler scheduler, int priority);
    /**
     * \overload Result setCPUScheduler(const QList<int> &pids, Process::Scheduler scheduler, int priority)
     */
    Q_INVOKABLE Result setCPUScheduler(const QVariantList &pids, Process::Scheduler scheduler, int priority);

    /**
     * Set the IO scheduling policy and priority of a number of processes.
     *
     * This will set the IO scheduling policy and priority of all processes in
     * \p pids to \p priorityClass and \p priority. Should a number of these be
     * owned by different users, an attempt will be made to send the signal as
     * root using KAuth.
     *
     * \param pids A vector of pids to set the scheduler of.
     * \param priorityClass The scheduling policy to use.
     * \param priority The priority to set. Lower means higher priority.
     *
     * \return A Result value that indicates whether the action succeeded. Note
     *         that a non-Success result may indicate any of the processes in
     *         \p pids encountered that result.
     */
    Q_INVOKABLE Result setIOScheduler(const QList<int> &pids, Process::IoPriorityClass priorityClass, int priority);
    /**
     * \overload Result setIOScheduler(const QList<int> &pids, Process::IoPriorityClass priorityClass, int priority)
     */
    Q_INVOKABLE Result setIOScheduler(const QList<long long> &pids, Process::IoPriorityClass priorityClass, int priority);
    /**
     * \overload Result setIOScheduler(const QList<int> &pids, Process::IoPriorityClass priorityClass, int priority)
     */
    Q_INVOKABLE Result setIOScheduler(const QVariantList &pids, Process::IoPriorityClass priorityClass, int priority);

    /**
     * Convert a Result value to a user-visible string.
     *
     *
     */
    Q_INVOKABLE QString resultToString(Result result);

private:
    class Private;
    const std::unique_ptr<Private> d;
};

}

#endif // KSYSGUARD_PROCESSCONTROLLER_H