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

« back to all changes in this revision

Viewing changes to libs/ksysguard/processcore/processes_freebsd_p.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
/*  This file is part of the KDE project
 
2
    Copyright (C) 2007 Manolo Valdes <nolis71cu@gmail.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "processes_local_p.h"
 
21
#include "process.h"
 
22
 
 
23
#include <klocale.h>
 
24
 
 
25
#include <QSet>
 
26
 
 
27
#include <sys/param.h>
 
28
#include <sys/sysctl.h>
 
29
#include <sys/types.h>
 
30
#include <sys/user.h>
 
31
#include <sys/resource.h>
 
32
#include <signal.h>
 
33
#include <unistd.h>
 
34
#include <stdlib.h>
 
35
#include <sched.h>
 
36
 
 
37
 
 
38
 
 
39
namespace KSysGuard
 
40
{
 
41
 
 
42
  class ProcessesLocal::Private
 
43
  {
 
44
    public:
 
45
      Private() {;}
 
46
      ~Private() {;}
 
47
      inline bool readProc(long pid, struct kinfo_proc *p);
 
48
      inline void readProcStatus(struct kinfo_proc *p, Process *process);
 
49
      inline void readProcStat(struct kinfo_proc *p, Process *process);
 
50
      inline void readProcStatm(struct kinfo_proc *p, Process *process);
 
51
      inline bool readProcCmdline(long pid, Process *process);
 
52
    };
 
53
 
 
54
bool ProcessesLocal::Private::readProc(long pid, struct kinfo_proc *p)
 
55
{
 
56
    int mib[4];
 
57
    size_t len;
 
58
 
 
59
    mib[0] = CTL_KERN;
 
60
    mib[1] = KERN_PROC;
 
61
    mib[2] = KERN_PROC_PID;
 
62
    mib[3] = pid;
 
63
 
 
64
    len = sizeof (struct kinfo_proc);
 
65
    if (sysctl(mib, 4, p, &len, NULL, 0) == -1 || !len)
 
66
        return false;
 
67
    return true;
 
68
}
 
69
 
 
70
void ProcessesLocal::Private::readProcStatus(struct kinfo_proc *p, Process *process)
 
71
{
 
72
    process->setUid(0);
 
73
    process->setGid(0);
 
74
    process->setTracerpid(-1);
 
75
 
 
76
    process->setEuid(p->ki_uid);
 
77
    process->setUid(p->ki_ruid);
 
78
    process->setEgid(p->ki_svgid);
 
79
    process->setGid(p->ki_rgid);
 
80
    process->setName(QString(p->ki_comm ? p->ki_comm : "????"));
 
81
}
 
82
 
 
83
void ProcessesLocal::Private::readProcStat(struct kinfo_proc *p, Process *ps)
 
84
{
 
85
    int status;
 
86
    struct rusage pru;
 
87
 
 
88
        ps->setUserTime(p->ki_rusage.ru_utime.tv_sec * 100 + p->ki_rusage.ru_utime.tv_usec / 10000);
 
89
        ps->setSysTime(p->ki_rusage.ru_stime.tv_sec * 100 + p->ki_rusage.ru_stime.tv_usec / 10000);
 
90
        ps->setNiceLevel(p->ki_nice);
 
91
        ps->setVmSize(p->ki_size / 1024);
 
92
        ps->setVmRSS(p->ki_rssize * getpagesize() / 1024);
 
93
        status = p->ki_stat;
 
94
 
 
95
// "idle","run","sleep","stop","zombie"
 
96
    switch( status ) {
 
97
      case SRUN:
 
98
         ps->setStatus(Process::Running);
 
99
         break;
 
100
      case SSLEEP:
 
101
      case SWAIT:
 
102
      case SLOCK:
 
103
         ps->setStatus(Process::Sleeping);
 
104
         break;
 
105
      case SSTOP:
 
106
         ps->setStatus(Process::Stopped);
 
107
         break;
 
108
      case SZOMB:
 
109
         ps->setStatus(Process::Zombie);
 
110
         break;
 
111
      default:
 
112
         ps->setStatus(Process::OtherStatus);
 
113
         break;
 
114
    }
 
115
}
 
116
 
 
117
void ProcessesLocal::Private::readProcStatm(struct kinfo_proc *p, Process *process)
 
118
{
 
119
    process->setVmURSS(-1);
 
120
}
 
121
 
 
122
bool ProcessesLocal::Private::readProcCmdline(long pid, Process *process)
 
123
{
 
124
    int mib[4];
 
125
    struct kinfo_proc p;
 
126
    size_t buflen = 256;
 
127
    char buf[256];
 
128
 
 
129
    mib[0] = CTL_KERN;
 
130
    mib[1] = KERN_PROC;
 
131
    mib[2] = KERN_PROC_ARGS;
 
132
    mib[3] = pid;
 
133
 
 
134
    if (sysctl(mib, 4, buf, &buflen, NULL, 0) == -1 || !buflen)
 
135
        return false;
 
136
    QString command = QString(buf);
 
137
 
 
138
    //cmdline seperates parameters with the NULL character
 
139
    command = command.replace('\0', ' ');
 
140
    process->setCommand(command.trimmed());
 
141
 
 
142
    return true;
 
143
}
 
144
 
 
145
ProcessesLocal::ProcessesLocal() : d(new Private())
 
146
{
 
147
 
 
148
}
 
149
 
 
150
long ProcessesLocal::getParentPid(long pid) {
 
151
    long long ppid = 0;
 
152
    struct kinfo_proc p;
 
153
    if(d->readProc(pid, &p))
 
154
    {
 
155
        ppid = p.ki_ppid;
 
156
    }
 
157
    return ppid;
 
158
}
 
159
 
 
160
bool ProcessesLocal::updateProcessInfo( long pid, Process *process)
 
161
{
 
162
    struct kinfo_proc p;
 
163
    if(!d->readProc(pid, &p)) return false;
 
164
    d->readProcStat(&p, process);
 
165
    d->readProcStatus(&p, process);
 
166
    d->readProcStatm(&p, process);
 
167
    if(!d->readProcCmdline(pid, process)) return false;
 
168
 
 
169
    return true;
 
170
}
 
171
 
 
172
QSet<long> ProcessesLocal::getAllPids( )
 
173
{
 
174
    QSet<long> pids;
 
175
    int mib[3];
 
176
    size_t len;
 
177
    size_t num;
 
178
    struct kinfo_proc *p;
 
179
 
 
180
    mib[0] = CTL_KERN;
 
181
    mib[1] = KERN_PROC;
 
182
    mib[2] = KERN_PROC_ALL;
 
183
    if (sysctl(mib, 3, NULL, &len, NULL, 0) == -1)
 
184
        return pids;
 
185
    if ((p = (kinfo_proc *) malloc(len)) == NULL)
 
186
        return pids;
 
187
    if (sysctl(mib, 3, p, &len, NULL, 0) == -1) {
 
188
        free(p);
 
189
        return pids;
 
190
    }
 
191
 
 
192
    for (num = 0; num < len / sizeof(struct kinfo_proc); num++)
 
193
    {
 
194
        long pid = p[num].ki_pid;
 
195
        long long ppid = p[num].ki_ppid;
 
196
 
 
197
        //skip all process with parent id = 0 but init
 
198
        if(ppid <= 0 && pid != 1)
 
199
            continue;
 
200
        pids.insert(pid);
 
201
    }
 
202
    free(p);
 
203
    return pids;
 
204
}
 
205
 
 
206
bool ProcessesLocal::sendSignal(long pid, int sig) {
 
207
    if ( kill( (pid_t)pid, sig ) ) {
 
208
        //Kill failed
 
209
        return false;
 
210
    }
 
211
    return true;
 
212
}
 
213
 
 
214
bool ProcessesLocal::setNiceness(long pid, int priority) {
 
215
    if ( setpriority( PRIO_PROCESS, pid, priority ) ) {
 
216
            //set niceness failed
 
217
            return false;
 
218
    }
 
219
    return true;
 
220
}
 
221
 
 
222
bool ProcessesLocal::setScheduler(long pid, int priorityClass, int priority)
 
223
{
 
224
    if(priorityClass == KSysGuard::Process::Other || priorityClass == KSysGuard::Process::Batch)
 
225
            priority = 0;
 
226
    if(pid <= 0) return false; // check the parameters
 
227
    struct sched_param params;
 
228
    params.sched_priority = priority;
 
229
    switch(priorityClass) {
 
230
      case (KSysGuard::Process::Other):
 
231
            return (sched_setscheduler( pid, SCHED_OTHER, &params) == 0);
 
232
      case (KSysGuard::Process::RoundRobin):
 
233
            return (sched_setscheduler( pid, SCHED_RR, &params) == 0);
 
234
      case (KSysGuard::Process::Fifo):
 
235
            return (sched_setscheduler( pid, SCHED_FIFO, &params) == 0);
 
236
#ifdef SCHED_BATCH
 
237
      case (KSysGuard::Process::Batch):
 
238
            return (sched_setscheduler( pid, SCHED_BATCH, &params) == 0);
 
239
#endif
 
240
      default:
 
241
            return false;
 
242
    }
 
243
}
 
244
 
 
245
bool ProcessesLocal::setIoNiceness(long pid, int priorityClass, int priority) {
 
246
    return false; //Not yet supported
 
247
}
 
248
 
 
249
bool ProcessesLocal::supportsIoNiceness() {
 
250
    return false;
 
251
}
 
252
 
 
253
long long ProcessesLocal::totalPhysicalMemory() {
 
254
 
 
255
    size_t Total;
 
256
    size_t len;
 
257
 
 
258
    len = sizeof (Total);
 
259
    if (sysctlbyname("hw.physmem", &Total, &len, NULL, 0) == -1)
 
260
        return 0;
 
261
 
 
262
    return Total /= 1024;
 
263
}
 
264
 
 
265
ProcessesLocal::~ProcessesLocal()
 
266
{
 
267
   delete d;
 
268
}
 
269
 
 
270
}