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

« back to all changes in this revision

Viewing changes to libs/ksysguard/processcore/processes_solaris_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 Adriaan de Groot <groot@kde.org>
 
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
/* Stop <sys/procfs.h> from crapping out on 32-bit architectures. */
 
21
 
 
22
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
 
23
# undef _FILE_OFFSET_BITS
 
24
# define _FILE_OFFSET_BITS 32
 
25
#endif
 
26
 
 
27
#include "processes_local_p.h"
 
28
#include "process.h"
 
29
 
 
30
#include <klocale.h>
 
31
 
 
32
#include <QSet>
 
33
 
 
34
#include <stdio.h>
 
35
#include <sys/param.h>
 
36
#include <sys/types.h>
 
37
#include <sys/user.h>
 
38
#include <sys/resource.h>
 
39
#include <string.h>
 
40
#include <errno.h>
 
41
#include <fcntl.h>
 
42
#include <signal.h>
 
43
#include <unistd.h>
 
44
#include <stdlib.h>
 
45
#include <sched.h>
 
46
#include <dirent.h>
 
47
#include <pwd.h>
 
48
#include <procfs.h>
 
49
#include <sys/proc.h>
 
50
#include <sys/resource.h>
 
51
 
 
52
#define PROCESS_BUFFER_SIZE 512
 
53
#define PROCDIR "/proc"
 
54
 
 
55
namespace KSysGuard
 
56
{
 
57
 
 
58
class ProcessesLocal::Private
 
59
{
 
60
  public:
 
61
    Private() { mProcDir = opendir( PROCDIR ); };
 
62
    ~Private() { };
 
63
    char mBuf[PROCESS_BUFFER_SIZE+1]; //used as a buffer to read data into
 
64
    DIR* mProcDir;
 
65
} ;
 
66
 
 
67
ProcessesLocal::ProcessesLocal() : d(new Private())
 
68
{
 
69
}
 
70
 
 
71
long ProcessesLocal::getParentPid(long pid) {
 
72
    long long ppid = -1;
 
73
    int        fd;
 
74
    psinfo_t    psinfo;
 
75
 
 
76
    snprintf( d->mBuf, PROCESS_BUFFER_SIZE - 1, "%s/%ld/psinfo", PROCDIR, pid );
 
77
    if( (fd = open( d->mBuf, O_RDONLY )) < 0 ) {
 
78
        return -1; /* process has terminated in the meantime */
 
79
    }
 
80
 
 
81
    if( read( fd, &psinfo, sizeof( psinfo_t )) != sizeof( psinfo_t )) {
 
82
        close( fd );
 
83
        return -1;
 
84
    }
 
85
    close( fd );
 
86
    ppid = psinfo.pr_ppid;
 
87
 
 
88
    return ppid;
 
89
}
 
90
 
 
91
bool ProcessesLocal::updateProcessInfo( long pid, Process *process)
 
92
{
 
93
    int        fd, pfd;
 
94
    psinfo_t    psinfo;
 
95
    prusage_t    prusage;
 
96
 
 
97
    snprintf( d->mBuf, PROCESS_BUFFER_SIZE - 1, "%s/%ld/psinfo", PROCDIR, pid );
 
98
    if( (fd = open( d->mBuf, O_RDONLY )) < 0 ) {
 
99
        return false; /* process has terminated in the meantime */
 
100
    }
 
101
 
 
102
    snprintf( d->mBuf, PROCESS_BUFFER_SIZE - 1, "%s/%ld/usage", PROCDIR, pid );
 
103
    if( (pfd = open( d->mBuf, O_RDONLY )) < 0 ) {
 
104
        close( fd );
 
105
        return false; /* process has terminated in the meantime */
 
106
    }
 
107
 
 
108
    process->uid = 0;
 
109
    process->gid = 0;
 
110
    process->tracerpid = -1;
 
111
    process->pid = pid;
 
112
 
 
113
    if( read( fd, &psinfo, sizeof( psinfo_t )) != sizeof( psinfo_t )) {
 
114
        close( fd );
 
115
        return false;
 
116
    }
 
117
    close( fd );
 
118
 
 
119
    if( read( pfd, &prusage, sizeof( prusage_t )) != sizeof( prusage_t )) {
 
120
    close( pfd );
 
121
    return false;
 
122
    }
 
123
    close( pfd );
 
124
 
 
125
    process->setUid( psinfo.pr_uid );
 
126
    process->setEuid( psinfo.pr_euid );
 
127
    process->setGid( psinfo.pr_gid );
 
128
    process->setEgid( psinfo.pr_egid );
 
129
 
 
130
    switch( (int) psinfo.pr_lwp.pr_state ) {
 
131
    case SIDL:
 
132
    case SWAIT:
 
133
    case SSLEEP:
 
134
        process->setStatus(Process::Sleeping);
 
135
        break;
 
136
    case SONPROC:
 
137
    case SRUN:
 
138
        process->setStatus(Process::Running);
 
139
        break;
 
140
    case SZOMB:
 
141
        process->setStatus(Process::Zombie);
 
142
        break;
 
143
    case SSTOP:
 
144
        process->setStatus(Process::Stopped);
 
145
        break;
 
146
    default:
 
147
        process->setStatus(Process::OtherStatus);
 
148
        break;
 
149
    }
 
150
 
 
151
    process->setVmRSS(psinfo.pr_rssize);
 
152
    process->setVmSize(psinfo.pr_size);
 
153
    process->setVmURSS(-1);
 
154
 
 
155
    if (process->command.isNull()) {
 
156
    QString name(psinfo.pr_fname);
 
157
 
 
158
    name = name.trimmed();
 
159
    if(!name.isEmpty()) {
 
160
        name.remove(QRegExp("^[^ ]*/"));
 
161
    }
 
162
    process->setName(name);
 
163
    name = psinfo.pr_fname;
 
164
    name.append(psinfo.pr_psargs);
 
165
    process->setCommand(name);
 
166
    }
 
167
 
 
168
    // Approximations, not quite accurate. Needs more changes in ksysguard to map
 
169
    // RR and FIFO to current Solaris classes.
 
170
    if (strcmp(psinfo.pr_lwp.pr_clname, "TS") == 0 || strcmp(psinfo.pr_lwp.pr_clname, "SYS") == 0 ||
 
171
        strcmp(psinfo.pr_lwp.pr_clname, "FSS") == 0) {
 
172
        process->setscheduler( KSysGuard::Process::Other );
 
173
 
 
174
    } else if (strcmp(psinfo.pr_lwp.pr_clname, "FX") == 0 || strcmp(psinfo.pr_lwp.pr_clname, "RT") == 0) {
 
175
        process->setscheduler( KSysGuard::Process::RoundRobin );
 
176
 
 
177
    } else if (strcmp(psinfo.pr_lwp.pr_clname, "IA") == 0) {
 
178
        process->setscheduler( KSysGuard::Process::Interactive );
 
179
    }
 
180
    process->setNiceLevel( psinfo.pr_lwp.pr_pri );
 
181
    process->setUserTime( prusage.pr_utime.tv_sec * 100 + prusage.pr_utime.tv_nsec / 10000000.0);
 
182
    process->setSysTime( prusage.pr_stime.tv_sec  * 100 + prusage.pr_stime.tv_nsec / 10000000.0);
 
183
    return false;
 
184
}
 
185
 
 
186
QSet<long> ProcessesLocal::getAllPids( )
 
187
{
 
188
    QSet<long> pids;
 
189
    long pid;
 
190
 
 
191
    if(d->mProcDir==NULL) return pids; //There's not much we can do without /proc
 
192
    struct dirent* entry;
 
193
    rewinddir(d->mProcDir);
 
194
    while ( ( entry = readdir( d->mProcDir ) ) )
 
195
        if ( entry->d_name[ 0 ] >= '0' && entry->d_name[ 0 ] <= '9' ) {
 
196
            pid = atol( entry->d_name );
 
197
            // Skip all processes with parent id = 0 except init
 
198
            if (pid == 1 || getParentPid(pid) > 0) {
 
199
            pids.insert(pid);
 
200
            }
 
201
        }
 
202
    return pids;
 
203
}
 
204
 
 
205
bool ProcessesLocal::sendSignal(long pid, int sig) {
 
206
    if ( kill( (pid_t)pid, sig ) ) {
 
207
        //Kill failed
 
208
        return false;
 
209
    }
 
210
    return false;
 
211
}
 
212
 
 
213
/*
 
214
 *
 
215
 */
 
216
bool ProcessesLocal::setNiceness(long pid, int priority) {
 
217
    return false;
 
218
}
 
219
 
 
220
bool ProcessesLocal::setScheduler(long pid, int priorityClass, int priority)
 
221
{
 
222
    return false;
 
223
}
 
224
 
 
225
bool ProcessesLocal::setIoNiceness(long pid, int priorityClass, int priority) {
 
226
    return false; //Not yet supported
 
227
}
 
228
 
 
229
bool ProcessesLocal::supportsIoNiceness() {
 
230
    return false;
 
231
}
 
232
 
 
233
long long ProcessesLocal::totalPhysicalMemory() {
 
234
    long long memory = ((long long)sysconf(_SC_PHYS_PAGES)) * (sysconf(_SC_PAGESIZE)/1024);
 
235
    if(memory > 0) return memory;
 
236
    return 0;
 
237
}
 
238
 
 
239
ProcessesLocal::~ProcessesLocal()
 
240
{
 
241
   delete d;
 
242
}
 
243
 
 
244
}