~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/ksysguard/processcore/helper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
        Copyright (c) 2009 John Tapsell <john.tapsell@kde.org>
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
 
 
11
    This library is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
    Library General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
    Boston, MA 02110-1301, USA.
 
20
 
 
21
*/
 
22
 
 
23
#include "helper.h"
 
24
#include "processes_local_p.h"
 
25
 
 
26
KSysGuardProcessListHelper::KSysGuardProcessListHelper()
 
27
{
 
28
    qRegisterMetaType<QList<long long> >();
 
29
}
 
30
 
 
31
/* The functions here run as ROOT.  So be careful.  DO NOT TRUST THE INPUTS TO BE SANE. */
 
32
#define GET_PID(i) parameters.value(QString("pid%1").arg(i), -1).toULongLong(); if(pid < 0) return KAuth::ActionReply::HelperErrorReply;
 
33
KAuth::ActionReply KSysGuardProcessListHelper::sendsignal(QVariantMap parameters) {
 
34
    KAuth::ActionReply errorReply(KAuth::ActionReply::HelperError);
 
35
    if(!parameters.contains("signal")) {
 
36
        errorReply.setErrorDescription("Internal error - no signal parameter was passed to the helper");
 
37
        errorReply.setErrorCode(1);
 
38
        return errorReply;
 
39
    }
 
40
    if(!parameters.contains("pidcount")) {
 
41
        errorReply.setErrorDescription("Internal error - no pidcount parameter was passed to the helper");
 
42
        errorReply.setErrorCode(2);
 
43
        return errorReply;
 
44
    }
 
45
 
 
46
    KSysGuard::ProcessesLocal processes;
 
47
    int signal = qvariant_cast<int>(parameters.value("signal"));
 
48
    bool success = true;
 
49
    int numProcesses = parameters.value("pidcount").toInt();
 
50
    QStringList errorList;
 
51
    for (int i = 0; i < numProcesses; ++i) {
 
52
        qlonglong pid = GET_PID(i);
 
53
        bool successForThisPid = processes.sendSignal(pid, signal);
 
54
        if (!successForThisPid)
 
55
            errorList << QString::number(pid);
 
56
        success = successForThisPid && success;
 
57
    }
 
58
    if(success) {
 
59
        return KAuth::ActionReply::SuccessReply;
 
60
    } else {
 
61
        errorReply.setErrorDescription(QString("Could not send signal to: ") + errorList.join(", "));
 
62
        errorReply.setErrorCode(0);
 
63
        return errorReply;
 
64
    }
 
65
}
 
66
 
 
67
KAuth::ActionReply KSysGuardProcessListHelper::renice(QVariantMap parameters) {
 
68
    if(!parameters.contains("nicevalue") || !parameters.contains("pidcount"))
 
69
        return KAuth::ActionReply::HelperErrorReply;
 
70
 
 
71
    KSysGuard::ProcessesLocal processes;
 
72
    int niceValue = qvariant_cast<int>(parameters.value("nicevalue"));
 
73
    bool success = true;
 
74
    int numProcesses = parameters.value("pidcount").toInt();
 
75
    for (int i = 0; i < numProcesses; ++i) {
 
76
        qlonglong pid = GET_PID(i);
 
77
        success = processes.setNiceness(pid, niceValue) && success;
 
78
    }
 
79
    if(success)
 
80
        return KAuth::ActionReply::SuccessReply;
 
81
    else
 
82
        return KAuth::ActionReply::HelperErrorReply;
 
83
}
 
84
 
 
85
KAuth::ActionReply KSysGuardProcessListHelper::changeioscheduler(QVariantMap parameters) {
 
86
    if(!parameters.contains("ioScheduler") || !parameters.contains("ioSchedulerPriority") || !parameters.contains("pidcount"))
 
87
        return KAuth::ActionReply::HelperErrorReply;
 
88
 
 
89
    KSysGuard::ProcessesLocal processes;
 
90
    int ioScheduler = qvariant_cast<int>(parameters.value("ioScheduler"));
 
91
    int ioSchedulerPriority = qvariant_cast<int>(parameters.value("ioSchedulerPriority"));
 
92
    bool success = true;
 
93
    int numProcesses = parameters.value("pidcount").toInt();
 
94
    for (int i = 0; i < numProcesses; ++i) {
 
95
        qlonglong pid = GET_PID(i);
 
96
        success = processes.setIoNiceness(pid, ioScheduler, ioSchedulerPriority) && success;
 
97
    }
 
98
    if(success)
 
99
        return KAuth::ActionReply::SuccessReply;
 
100
    else
 
101
        return KAuth::ActionReply::HelperErrorReply;
 
102
 
 
103
}
 
104
KAuth::ActionReply KSysGuardProcessListHelper::changecpuscheduler(QVariantMap parameters) {
 
105
    if(!parameters.contains("cpuScheduler") || !parameters.contains("cpuSchedulerPriority") || !parameters.contains("pidcount"))
 
106
        return KAuth::ActionReply::HelperErrorReply;
 
107
 
 
108
    KSysGuard::ProcessesLocal processes;
 
109
    int cpuScheduler = qvariant_cast<int>(parameters.value("cpuScheduler"));
 
110
    int cpuSchedulerPriority = qvariant_cast<int>(parameters.value("cpuSchedulerPriority"));
 
111
    bool success = true;
 
112
 
 
113
    int numProcesses = parameters.value("pidcount").toInt();
 
114
    for (int i = 0; i < numProcesses; ++i) {
 
115
        qlonglong pid = GET_PID(i);
 
116
        success = processes.setScheduler(pid, cpuScheduler, cpuSchedulerPriority) && success;
 
117
    }
 
118
    if(success)
 
119
        return KAuth::ActionReply::SuccessReply;
 
120
    else
 
121
        return KAuth::ActionReply::HelperErrorReply;
 
122
 
 
123
}
 
124
KDE4_AUTH_HELPER_MAIN("org.kde.ksysguard.processlisthelper", KSysGuardProcessListHelper)
 
125