Debian Codegraph @

lib/ProcessInfo.cpp

raw symbols SCIP
1 /*
2 Copyright 2007-2008 by Robert Knight <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
18 */
19
20 // Own
21 #include "ProcessInfo.h"cxx . . $ `<file>/lib/ProcessInfo.h`/
22
23 // Unix
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <sys/param.h>
30
31 // Qt
32 #include <QtCore/QDir>
33 #include <QtCore/QFileInfo>
34 #include <QtCore/QFlags>
35 #include <QtCore/QTextStream>
36 #include <QtCore/QStringList>
37 #include <QtNetwork/QHostInfo>
38
39 // KDE
40 // #include <KConfigGroup>
41 // #include <KSharedConfig>
42 #include <QDebug>
43
44 #if defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_MAC)
45 #include <sys/sysctl.h>
46 #endif
47
48 #if defined(Q_OS_MAC)
49 #include <libproc.h>
50 #endif
51
52 #if defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD)
53 #include <sys/types.h>
54 #include <sys/user.h>
55 #include <sys/syslimits.h>
56 # if defined(Q_OS_FREEBSD)
57 # include <libutil.h>
58 # endif
59 #endif
60
61 using namespace Konsole;
62
63 ProcessInfocxx . . $ Konsole/ProcessInfo#::ProcessInfocxx . . $ Konsole/ProcessInfo#ProcessInfo(918ebd27a9ddbad3).(int aPid , bool enableEnvironmentRead)
64 : _fieldscxx . . $ Konsole/ProcessInfo#_fields.(ARGUMENTScxx . . $ Konsole/ProcessInfo#ARGUMENTS. |cxx . . $ Konsole/`operator|`(e6c567be70d95ede). ENVIRONMENTcxx . . $ Konsole/ProcessInfo#ENVIRONMENT.) // arguments and environments
65 // are currently always valid,
66 // they just return an empty
67 // vector / map respectively
68 // if no arguments
69 // or environment bindings
70 // have been explicitly set
71 , _enableEnvironmentReadcxx . . $ Konsole/ProcessInfo#_enableEnvironmentRead.(enableEnvironmentRead)
72 , _pidcxx . . $ Konsole/ProcessInfo#_pid.(aPid)
73 , _parentPidcxx . . $ Konsole/ProcessInfo#_parentPid.(0)
74 , _foregroundPidcxx . . $ Konsole/ProcessInfo#_foregroundPid.(0)
75 , _userIdcxx . . $ Konsole/ProcessInfo#_userId.(0)
76 , _lastErrorcxx . . $ Konsole/ProcessInfo#_lastError.(NoErrorcxx . . $ Konsole/ProcessInfo#NoError.)
77 , _userNamecxx . . $ Konsole/ProcessInfo#_userName.(QStringcxx . . $ QString#QString(ced63f7c635d850d).())
78 , _userHomeDircxx . . $ Konsole/ProcessInfo#_userHomeDir.(QStringcxx . . $ QString#QString(ced63f7c635d850d).())
79 {
80 }
81
82 ProcessInfocxx . . $ Konsole/ProcessInfo#::Errorcxx . . $ Konsole/ProcessInfo#Error# ProcessInfocxx . . $ Konsole/ProcessInfo#::errorcxx . . $ Konsole/ProcessInfo#error(a76b47da7438de78).() const
83 {
84 return _lastErrorcxx . . $ Konsole/ProcessInfo#_lastError.;
85 }
86 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(Errorcxx . . $ Konsole/ProcessInfo#Error# error)
87 {
88 _lastErrorcxx . . $ Konsole/ProcessInfo#_lastError. = error;
89 }
90
91 void ProcessInfocxx . . $ Konsole/ProcessInfo#::updatecxx . . $ Konsole/ProcessInfo#update(49f6e7a06ebc5aa8).()
92 {
93 readProcessInfocxx . . $ Konsole/ProcessInfo#readProcessInfo(9609253e83e89946).(_pidcxx . . $ Konsole/ProcessInfo#_pid., _enableEnvironmentReadcxx . . $ Konsole/ProcessInfo#_enableEnvironmentRead.);
94 }
95
96 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::validCurrentDircxx . . $ Konsole/ProcessInfo#validCurrentDir(6ef7b6c27b00b43d).() const
97 {
98 bool ok = false;
99
100 // read current dir, if an error occurs try the parent as the next
101 // best option
102 int currentPid = parentPidcxx . . $ Konsole/ProcessInfo#parentPid(5bd9a3493f6a7a21).(&ok);
103 QStringcxx . . $ QString# dir = currentDircxx . . $ Konsole/ProcessInfo#currentDir(527c08bc36b7c847).(&ok);
104 while (!ok && currentPid != 0) {
105 ProcessInfocxx . . $ Konsole/ProcessInfo#* current = ProcessInfocxx . . $ Konsole/ProcessInfo#::newInstancecxx . . $ Konsole/ProcessInfo#newInstance(76b029d04fe475b6).(currentPid);
106 current->updatecxx . . $ Konsole/ProcessInfo#update(49f6e7a06ebc5aa8).();
107 currentPid = current->parentPidcxx . . $ Konsole/ProcessInfo#parentPid(5bd9a3493f6a7a21).(&ok);
108 dir =cxx . . $ QString#`operator=`(4f35bf974e7f8568). current->currentDircxx . . $ Konsole/ProcessInfo#currentDir(527c08bc36b7c847).(&ok);
109 delete current;
110 }
111
112 return dircxx . . $ QString#QString(28715ad661ad3cec).;
113 }
114
115 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::formatcxx . . $ Konsole/ProcessInfo#format(5555d9024dcf93fb).(const QStringcxx . . $ QString#& input) const
116 {
117 bool ok = false;
118
119 QStringcxx . . $ QString# outputcxx . . $ QString#QString(132dd6869045752b).(input);
120
121 // search for and replace known marker
122 output.replacecxx . . $ QString#replace(192e628656427d1c).("%u"cxx . . $ QString#QString(85c52e162fed56f9)., userNamecxx . . $ Konsole/ProcessInfo#userName(6ef7b6c27b00b43d).());
123 output.replacecxx . . $ QString#replace(192e628656427d1c).("%h"cxx . . $ QString#QString(85c52e162fed56f9)., localHostcxx . . $ Konsole/ProcessInfo#localHost(4365c3963150523e).());
124 output.replacecxx . . $ QString#replace(192e628656427d1c).("%n"cxx . . $ QString#QString(85c52e162fed56f9)., namecxx . . $ Konsole/ProcessInfo#name(527c08bc36b7c847).(&ok));
125
126 QStringcxx . . $ QString# dir = validCurrentDircxx . . $ Konsole/ProcessInfo#validCurrentDir(6ef7b6c27b00b43d).();
127 if (output.containscxx . . $ QString#contains(8cce4688c16f48f3).("%D"cxx . . $ QString#QString(85c52e162fed56f9).)) {
128 QStringcxx . . $ QString# homeDir = userHomeDircxx . . $ Konsole/ProcessInfo#userHomeDir(6ef7b6c27b00b43d).();
129 QStringcxx . . $ QString# tempDir = dircxx . . $ QString#QString(132dd6869045752b).;
130 // Change User's Home Dir w/ ~ only at the beginning
131 if (tempDir.startsWithcxx . . $ QString#startsWith(8cce4688c16f48f3).(homeDir)) {
132 tempDir.removecxx . . $ QString#remove(6483560ce2afa901).(0, homeDir.lengthcxx . . $ QString#length(455f465bc33b4cdf).());
133 tempDir.prependcxx . . $ QString#prepend(3a2ffbdf8ecc25e2).('~'cxx . . $ QChar#QChar(f349c77f591a7878).);
134 }
135 output.replacecxx . . $ QString#replace(192e628656427d1c).("%D"cxx . . $ QString#QString(85c52e162fed56f9)., tempDir);
136 }
137 output.replacecxx . . $ QString#replace(192e628656427d1c).("%d"cxx . . $ QString#QString(85c52e162fed56f9)., formatShortDircxx . . $ Konsole/ProcessInfo#formatShortDir(5555d9024dcf93fb).(dir));
138
139 return outputcxx . . $ QString#QString(28715ad661ad3cec).;
140 }
141
142 QSetcxx . . $ QSet#<QStringcxx . . $ QString#> ProcessInfocxx . . $ Konsole/ProcessInfo#::_commonDirNamescxx . . $ Konsole/ProcessInfo#_commonDirNames.;
143
144 QSetcxx . . $ QSet#<QStringcxx . . $ QString#> ProcessInfocxx . . $ Konsole/ProcessInfo#::commonDirNamescxx . . $ Konsole/ProcessInfo#commonDirNames(3d52db697563fcb6).()
145 {
146 // static bool forTheFirstTime = true;
147 //
148 // if (forTheFirstTime) {
149 // const KSharedConfigPtr& config = KSharedConfig::openConfig();
150 // const KConfigGroup& configGroup = config->group("ProcessInfo");
151 // _commonDirNames = QSet<QString>::fromList(configGroup.readEntry("CommonDirNames", QStringList()));
152 //
153 // forTheFirstTime = false;
154 // }
155
156 return _commonDirNamescxx . . $ Konsole/ProcessInfo#_commonDirNames.;
157 }
158
159 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::formatShortDircxx . . $ Konsole/ProcessInfo#formatShortDir(5555d9024dcf93fb).(const QStringcxx . . $ QString#& input) const
160 {
161 QStringcxx . . $ QString# resultcxx . . $ QString#QString(ced63f7c635d850d).;
162
163 const QStringListcxx . . $ QStringList#& parts = input.splitcxx . . $ QString#split(92030b8bf74e4227).(QDircxx . . $ QDir#::separatorcxx . . $ QDir#separator(44eb9225326fcbf5).());
164
165 QSetcxx . . $ QSet#<QStringcxx . . $ QString#> dirNamesToShorten = commonDirNamescxx . . $ Konsole/ProcessInfo#commonDirNames(3d52db697563fcb6).();
166
167 QListIteratorcxx . . $ QListIterator#<QStringcxx . . $ QString#> itercxx . . $ QListIterator#QListIterator(742a15f66a895bc5).(parts);
168 iter.toBackcxx . . $ QListIterator#toBack(49f6e7a06ebc5aa8).();
169
170 // go backwards through the list of the path's parts
171 // adding abbreviations of common directory names
172 // and stopping when we reach a dir name which is not
173 // in the commonDirNames set
174 while (iter.hasPreviouscxx . . $ QListIterator#hasPrevious(50ce9a9e25b4a850).()) {
175 const QStringcxx . . $ QString#& part = iter.previouscxx . . $ QListIterator#previous(1306adbac230aeaa).();
176
177 if (dirNamesToShorten.containscxx . . $ QSet#contains(8877ebe272b9620d).(part)) {
178 result.prependcxx . . $ QString#prepend(6a89d0b7f4d2681c).(QStringcxx . . $ QString#QString(f2c90172ce699d92).(QDircxx . . $ QDir#::separatorcxx . . $ QDir#separator(44eb9225326fcbf5).()) +cxx . . $ operator+(eab1ea5b6c36e9b2). part[cxx . . $ QString#`operator[]`(13af7ae5a875439e).0]);
179 } else {
180 result.prependcxx . . $ QString#prepend(6a89d0b7f4d2681c).(part);
181 break;
182 }
183 }
184
185 return resultcxx . . $ QString#QString(28715ad661ad3cec).;
186 }
187
188 QVectorcxx . . $ QVector#<QStringcxx . . $ QString#> ProcessInfocxx . . $ Konsole/ProcessInfo#::argumentscxx . . $ Konsole/ProcessInfo#arguments(362fb25d3f176df0).(bool* ok) const
189 {
190 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(ARGUMENTScxx . . $ Konsole/ProcessInfo#ARGUMENTS.);
191
192 return _argumentscxx . . $ QVector#QVector(8c6ebb725bc04eaa).;
193 }
194
195 QMapcxx . . $ QMap#<QStringcxx . . $ QString#, QStringcxx . . $ QString#> ProcessInfocxx . . $ Konsole/ProcessInfo#::environmentcxx . . $ Konsole/ProcessInfo#environment(ef83c2157f405d62).(bool* ok) const
196 {
197 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(ENVIRONMENTcxx . . $ Konsole/ProcessInfo#ENVIRONMENT.);
198
199 return _environmentcxx . . $ QMap#QMap(21739d87639e1d56).;
200 }
201
202 bool ProcessInfocxx . . $ Konsole/ProcessInfo#::isValidcxx . . $ Konsole/ProcessInfo#isValid(50ce9a9e25b4a850).() const
203 {
204 return _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(PROCESS_IDcxx . . $ Konsole/ProcessInfo#PROCESS_ID.);
205 }
206
207 int ProcessInfocxx . . $ Konsole/ProcessInfo#::pidcxx . . $ Konsole/ProcessInfo#pid(5bd9a3493f6a7a21).(bool* ok) const
208 {
209 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(PROCESS_IDcxx . . $ Konsole/ProcessInfo#PROCESS_ID.);
210
211 return _pidcxx . . $ Konsole/ProcessInfo#_pid.;
212 }
213
214 int ProcessInfocxx . . $ Konsole/ProcessInfo#::parentPidcxx . . $ Konsole/ProcessInfo#parentPid(5bd9a3493f6a7a21).(bool* ok) const
215 {
216 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(PARENT_PIDcxx . . $ Konsole/ProcessInfo#PARENT_PID.);
217
218 return _parentPidcxx . . $ Konsole/ProcessInfo#_parentPid.;
219 }
220
221 int ProcessInfocxx . . $ Konsole/ProcessInfo#::foregroundPidcxx . . $ Konsole/ProcessInfo#foregroundPid(5bd9a3493f6a7a21).(bool* ok) const
222 {
223 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(FOREGROUND_PIDcxx . . $ Konsole/ProcessInfo#FOREGROUND_PID.);
224
225 return _foregroundPidcxx . . $ Konsole/ProcessInfo#_foregroundPid.;
226 }
227
228 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::namecxx . . $ Konsole/ProcessInfo#name(527c08bc36b7c847).(bool* ok) const
229 {
230 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(NAMEcxx . . $ Konsole/ProcessInfo#NAME.);
231
232 return _namecxx . . $ QString#QString(132dd6869045752b).;
233 }
234
235 int ProcessInfocxx . . $ Konsole/ProcessInfo#::userIdcxx . . $ Konsole/ProcessInfo#userId(5bd9a3493f6a7a21).(bool* ok) const
236 {
237 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields..testFlagcxx . . $ QFlags#testFlag(b276817fac6c20d5).(UIDcxx . . $ Konsole/ProcessInfo#UID.);
238
239 return _userIdcxx . . $ Konsole/ProcessInfo#_userId.;
240 }
241
242 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::userNamecxx . . $ Konsole/ProcessInfo#userName(6ef7b6c27b00b43d).() const
243 {
244 return _userNamecxx . . $ QString#QString(132dd6869045752b).;
245 }
246
247 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::userHomeDircxx . . $ Konsole/ProcessInfo#userHomeDir(6ef7b6c27b00b43d).() const
248 {
249 return _userHomeDircxx . . $ QString#QString(132dd6869045752b).;
250 }
251
252 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::localHostcxx . . $ Konsole/ProcessInfo#localHost(4365c3963150523e).()
253 {
254 return QHostInfocxx . . $ QHostInfo#::localHostNamecxx . . $ QHostInfo#localHostName(4365c3963150523e).();
255 }
256
257 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setPidcxx . . $ Konsole/ProcessInfo#setPid(d4f767463ce0a6b3).(int aPid)
258 {
259 _pidcxx . . $ Konsole/ProcessInfo#_pid. = aPid;
260 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). PROCESS_IDcxx . . $ Konsole/ProcessInfo#PROCESS_ID.;
261 }
262
263 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setUserIdcxx . . $ Konsole/ProcessInfo#setUserId(d4f767463ce0a6b3).(int uid)
264 {
265 _userIdcxx . . $ Konsole/ProcessInfo#_userId. = uid;
266 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). UIDcxx . . $ Konsole/ProcessInfo#UID.;
267 }
268
269 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setUserNamecxx . . $ Konsole/ProcessInfo#setUserName(3520e46b33348bf6).(const QStringcxx . . $ QString#& name)
270 {
271 _userNamecxx . . $ Konsole/ProcessInfo#_userName. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). name;
272 setUserHomeDircxx . . $ Konsole/ProcessInfo#setUserHomeDir(49f6e7a06ebc5aa8).();
273 }
274
275 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setUserHomeDircxx . . $ Konsole/ProcessInfo#setUserHomeDir(49f6e7a06ebc5aa8).()
276 {
277 _userHomeDircxx . . $ Konsole/ProcessInfo#_userHomeDir. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). QDircxx . . $ QDir#::homePathcxx . . $ QDir#homePath(4365c3963150523e).();
278 }
279
280 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setParentPidcxx . . $ Konsole/ProcessInfo#setParentPid(d4f767463ce0a6b3).(int aPid)
281 {
282 _parentPidcxx . . $ Konsole/ProcessInfo#_parentPid. = aPid;
283 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). PARENT_PIDcxx . . $ Konsole/ProcessInfo#PARENT_PID.;
284 }
285 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setForegroundPidcxx . . $ Konsole/ProcessInfo#setForegroundPid(d4f767463ce0a6b3).(int aPid)
286 {
287 _foregroundPidcxx . . $ Konsole/ProcessInfo#_foregroundPid. = aPid;
288 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). FOREGROUND_PIDcxx . . $ Konsole/ProcessInfo#FOREGROUND_PID.;
289 }
290
291 QStringcxx . . $ QString# ProcessInfocxx . . $ Konsole/ProcessInfo#::currentDircxx . . $ Konsole/ProcessInfo#currentDir(527c08bc36b7c847).(bool* ok) const
292 {
293 if (ok)
294 *ok = _fieldscxx . . $ Konsole/ProcessInfo#_fields. &cxx . . $ QFlags#`operator&`(2cc1994e0be6f18e). CURRENT_DIRcxx . . $ Konsole/ProcessInfo#CURRENT_DIR.;
295
296 return _currentDircxx . . $ QString#QString(132dd6869045752b).;
297 }
298 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setCurrentDircxx . . $ Konsole/ProcessInfo#setCurrentDir(3520e46b33348bf6).(const QStringcxx . . $ QString#& dir)
299 {
300 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). CURRENT_DIRcxx . . $ Konsole/ProcessInfo#CURRENT_DIR.;
301 _currentDircxx . . $ Konsole/ProcessInfo#_currentDir. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). dir;
302 }
303
304 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setNamecxx . . $ Konsole/ProcessInfo#setName(3520e46b33348bf6).(const QStringcxx . . $ QString#& name)
305 {
306 _namecxx . . $ Konsole/ProcessInfo#_name. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). name;
307 _fieldscxx . . $ Konsole/ProcessInfo#_fields. |=cxx . . $ QFlags#`operator|=`(fd64b2b07d6098e0). NAMEcxx . . $ Konsole/ProcessInfo#NAME.;
308 }
309 void ProcessInfocxx . . $ Konsole/ProcessInfo#::addArgumentcxx . . $ Konsole/ProcessInfo#addArgument(3520e46b33348bf6).(const QStringcxx . . $ QString#& argument)
310 {
311 _argumentscxx . . $ Konsole/ProcessInfo#_arguments. <<cxx . . $ QVector#`operator<<`(85c729c83851d88d). argument;
312 }
313
314 void ProcessInfocxx . . $ Konsole/ProcessInfo#::clearArgumentscxx . . $ Konsole/ProcessInfo#clearArguments(49f6e7a06ebc5aa8).()
315 {
316 _argumentscxx . . $ Konsole/ProcessInfo#_arguments..clearcxx . . $ QVector#clear(49f6e7a06ebc5aa8).();
317 }
318
319 void ProcessInfocxx . . $ Konsole/ProcessInfo#::addEnvironmentBindingcxx . . $ Konsole/ProcessInfo#addEnvironmentBinding(c1427772cd1877b0).(const QStringcxx . . $ QString#& name , const QStringcxx . . $ QString#& value)
320 {
321 _environmentcxx . . $ Konsole/ProcessInfo#_environment..insertcxx . . $ QMap#insert(929a1ed410fe8423).(name, value);
322 }
323
324 void ProcessInfocxx . . $ Konsole/ProcessInfo#::setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(QFilecxx . . $ QFile#::FileErrorcxx . . $ QFileDevice#FileError# error)
325 {
326 switch (error) {
327 case QFilecxx . . $ QFile#::PermissionsErrorcxx . . $ QFileDevice#PermissionsError.:
328 setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(ProcessInfocxx . . $ Konsole/ProcessInfo#::PermissionsErrorcxx . . $ Konsole/ProcessInfo#PermissionsError.);
329 break;
330 case QFilecxx . . $ QFile#::NoErrorcxx . . $ QFileDevice#NoError.:
331 setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(ProcessInfocxx . . $ Konsole/ProcessInfo#::NoErrorcxx . . $ Konsole/ProcessInfo#NoError.);
332 break;
333 default:
334 setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(ProcessInfocxx . . $ Konsole/ProcessInfo#::UnknownErrorcxx . . $ Konsole/ProcessInfo#UnknownError.);
335 }
336 }
337
338 //
339 // ProcessInfo::newInstance() is way at the bottom so it can see all of the
340 // implementations of the UnixProcessInfo abstract class.
341 //
342
343 NullProcessInfocxx . . $ Konsole/NullProcessInfo#::NullProcessInfocxx . . $ Konsole/NullProcessInfo#NullProcessInfo(918ebd27a9ddbad3).(int aPid, bool enableEnvironmentRead)
344 : ProcessInfocxx . . $ Konsole/ProcessInfo#(aPid, enableEnvironmentRead)
345 {
346 }
347
348 bool NullProcessInfocxx . . $ Konsole/NullProcessInfo#::readProcessInfocxx . . $ Konsole/NullProcessInfo#readProcessInfo(9609253e83e89946).(int /*pid*/ , bool /*enableEnvironmentRead*/)
349 {
350 return false;
351 }
352
353 void NullProcessInfocxx . . $ Konsole/NullProcessInfo#::readUserNamecxx . . $ Konsole/NullProcessInfo#readUserName(49f6e7a06ebc5aa8).()
354 {
355 }
356
357 #if !defined(Q_OS_WIN)
358 UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#::UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#UnixProcessInfo(918ebd27a9ddbad3).(int aPid, bool enableEnvironmentRead)
359 : ProcessInfocxx . . $ Konsole/ProcessInfo#(aPid, enableEnvironmentRead)
360 {
361 }
362
363 bool UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#::readProcessInfocxx . . $ Konsole/UnixProcessInfo#readProcessInfo(9609253e83e89946).(int aPid , bool enableEnvironmentRead)
364 {
365 // prevent _arguments from growing longer and longer each time this
366 // method is called.
367 clearArgumentscxx . . $ Konsole/ProcessInfo#clearArguments(49f6e7a06ebc5aa8).();
368
369 bool ok = readProcInfocxx . . $ Konsole/UnixProcessInfo#readProcInfo(83c267b20bac841a).(aPid);
370 if (ok) {
371 ok |= readArgumentscxx . . $ Konsole/UnixProcessInfo#readArguments(83c267b20bac841a).(aPid);
372 ok |= readCurrentDircxx . . $ Konsole/UnixProcessInfo#readCurrentDir(83c267b20bac841a).(aPid);
373 if (enableEnvironmentRead) {
374 ok |= readEnvironmentcxx . . $ Konsole/UnixProcessInfo#readEnvironment(83c267b20bac841a).(aPid);
375 }
376 }
377 return ok;
378 }
379
380 void UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#::readUserNamecxx . . $ Konsole/UnixProcessInfo#readUserName(49f6e7a06ebc5aa8).()
381 {
382 bool ok = false;
383 const int uid = userIdcxx . . $ Konsole/ProcessInfo#userId(5bd9a3493f6a7a21).(&ok);
384 if (!ok) return;
385
386 struct passwd passwdStruct;
387 struct passwd* getpwResult;
388 char* getpwBuffer;
389 long getpwBufferSize;
390 int getpwStatus;
391
392 getpwBufferSize = sysconf(_SC_GETPW_R_SIZE_MAXcxx . . $ `<external>/6393649343499073842/confname.h:224:9`!);
393 if (getpwBufferSize == -1)
394 getpwBufferSize = 16384;
395
396 getpwBuffer = new char[getpwBufferSize];
397 if (getpwBuffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)
398 return;
399 getpwStatus = getpwuid_r(uid, &passwdStruct, getpwBuffer, getpwBufferSize, &getpwResult);
400 if ((getpwStatus == 0) && (getpwResult != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) {
401 setUserNamecxx . . $ Konsole/ProcessInfo#setUserName(3520e46b33348bf6).(QStringcxx . . $ QString#QString(85c52e162fed56f9).(passwdStruct.pw_name));
402 } else {
403 setUserNamecxx . . $ Konsole/ProcessInfo#setUserName(3520e46b33348bf6).(QStringcxx . . $ QString#QString(ced63f7c635d850d).());
404 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "getpwuid_r returned error : " <<cxx . . $ QDebug#`operator<<`(6e3b7ae53f380c3). getpwStatus;
405 }
406 delete [] getpwBuffer;
407 }
408 #endif
409
410 #if defined(Q_OS_LINUXcxx . . $ `<external>/5130987379601705141/qsystemdetection.h:150:11`!)
411 class LinuxProcessInfocxx . . $ LinuxProcessInfo# : public UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#
412 {
413 public:
414 LinuxProcessInfocxx . . $ LinuxProcessInfo#LinuxProcessInfo(918ebd27a9ddbad3).(int aPid, bool env) :
415 UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#(aPid, env) {
416 }
417
418 private:
419 virtual bool readProcInfocxx . . $ LinuxProcessInfo#readProcInfo(83c267b20bac841a).(int aPid) {
420 // indicies of various fields within the process status file which
421 // contain various information about the process
422 const int PARENT_PID_FIELD = 3;
423 const int PROCESS_NAME_FIELD = 1;
424 const int GROUP_PROCESS_FIELD = 7;
425
426 QStringcxx . . $ QString# parentPidStringcxx . . $ QString#QString(ced63f7c635d850d).;
427 QStringcxx . . $ QString# processNameStringcxx . . $ QString#QString(ced63f7c635d850d).;
428 QStringcxx . . $ QString# foregroundPidStringcxx . . $ QString#QString(ced63f7c635d850d).;
429 QStringcxx . . $ QString# uidLinecxx . . $ QString#QString(ced63f7c635d850d).;
430 QStringcxx . . $ QString# uidStringcxx . . $ QString#QString(ced63f7c635d850d).;
431 QStringListcxx . . $ QStringList# uidStringscxx . . $ QStringList#QStringList(ced63f7c635d850d).;
432
433 // For user id read process status file ( /proc/<pid>/status )
434 // Can not use getuid() due to it does not work for 'su'
435 QFilecxx . . $ QFile# statusInfocxx . . $ QFile#QFile(3520e46b33348bf6).(QStringcxx . . $ QString#QString(85c52e162fed56f9).("/proc/%1/status").argcxx . . $ QString#arg(64ab2ed17cc11715).(aPid));
436 if (statusInfo.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) {
437 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&statusInfo);
438 QStringcxx . . $ QString# statusLinecxx . . $ QString#QString(ced63f7c635d850d).;
439 do {
440 statusLine =cxx . . $ QString#`operator=`(4f35bf974e7f8568). stream.readLinecxx . . $ QTextStream#readLine(20e0a2766677f583).();
441 if (statusLine.startsWithcxx . . $ QString#startsWith(9b0c247ef39438e0).(QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("Uid:")))
442 uidLine =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). statusLine;
443 } while (!statusLine.isNullcxx . . $ QString#isNull(50ce9a9e25b4a850).() && uidLine.isNullcxx . . $ QString#isNull(50ce9a9e25b4a850).());
444
445 uidStrings <<cxx . . $ QStringList#`operator<<`(cb3c3d1614140851). uidLine.splitcxx . . $ QString#split(2ba03f79dd8e9c12).('\t'cxx . . $ QChar#QChar(f349c77f591a7878)., QStringcxx . . $ QString#::SkipEmptyPartscxx . . $ QString#SkipEmptyParts.);
446 // Must be 5 entries: 'Uid: %d %d %d %d' and
447 // uid string must be less than 5 chars (uint)
448 if (uidStrings.sizecxx . . $ QList#size(ea6fe5115c70a13f).() == 5)
449 uidString =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). uidStrings[cxx . . $ QList#`operator[]`(7a0ef1c7079e8533).1];
450 if (uidString.sizecxx . . $ QString#size(455f465bc33b4cdf).() > 5)
451 uidString.clearcxx . . $ QString#clear(49f6e7a06ebc5aa8).();
452
453 bool ok = false;
454 const int uid = uidString.toIntcxx . . $ QString#toInt(d7972accae11ee1f).(&ok);
455 if (ok)
456 setUserIdcxx . . $ Konsole/ProcessInfo#setUserId(d4f767463ce0a6b3).(uid);
457 readUserNamecxx . . $ Konsole/UnixProcessInfo#readUserName(49f6e7a06ebc5aa8).();
458 } else {
459 setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(statusInfo.errorcxx . . $ QFileDevice#error(2eea7c6a1bd47f0a).());
460 return false;
461 }
462
463 // read process status file ( /proc/<pid/stat )
464 //
465 // the expected file format is a list of fields separated by spaces, using
466 // parenthesies to escape fields such as the process name which may itself contain
467 // spaces:
468 //
469 // FIELD FIELD (FIELD WITH SPACES) FIELD FIELD
470 //
471 QFilecxx . . $ QFile# processInfocxx . . $ QFile#QFile(3520e46b33348bf6).(QStringcxx . . $ QString#QString(85c52e162fed56f9).("/proc/%1/stat").argcxx . . $ QString#arg(64ab2ed17cc11715).(aPid));
472 if (processInfo.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) {
473 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&processInfo);
474 const QStringcxx . . $ QString#& data = stream.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
475
476 int stack = 0;
477 int field = 0;
478 int pos = 0;
479
480 while (pos < data.countcxx . . $ QString#count(455f465bc33b4cdf).()) {
481 QCharcxx . . $ QChar# c = data[cxx . . $ QString#`operator[]`(13af7ae5a875439e).pos];
482
483 if (c ==cxx . . $ `operator==`(a468b2f18a3afe8). '('cxx . . $ QChar#QChar(f349c77f591a7878).) {
484 stack++;
485 } else if (c ==cxx . . $ `operator==`(a468b2f18a3afe8). ')'cxx . . $ QChar#QChar(f349c77f591a7878).) {
486 stack--;
487 } else if (stack == 0 && c ==cxx . . $ `operator==`(a468b2f18a3afe8). ' 'cxx . . $ QChar#QChar(f349c77f591a7878).) {
488 field++;
489 } else {
490 switch (field) {
491 case PARENT_PID_FIELD:
492 parentPidString.appendcxx . . $ QString#append(3a2ffbdf8ecc25e2).(c);
493 break;
494 case PROCESS_NAME_FIELD:
495 processNameString.appendcxx . . $ QString#append(3a2ffbdf8ecc25e2).(c);
496 break;
497 case GROUP_PROCESS_FIELD:
498 foregroundPidString.appendcxx . . $ QString#append(3a2ffbdf8ecc25e2).(c);
499 break;
500 }
501 }
502
503 pos++;
504 }
505 } else {
506 setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(processInfo.errorcxx . . $ QFileDevice#error(2eea7c6a1bd47f0a).());
507 return false;
508 }
509
510 // check that data was read successfully
511 bool ok = false;
512 const int foregroundPid = foregroundPidString.toIntcxx . . $ QString#toInt(d7972accae11ee1f).(&ok);
513 if (ok)
514 setForegroundPidcxx . . $ Konsole/ProcessInfo#setForegroundPid(d4f767463ce0a6b3).(foregroundPid);
515
516 const int parentPid = parentPidString.toIntcxx . . $ QString#toInt(d7972accae11ee1f).(&ok);
517 if (ok)
518 setParentPidcxx . . $ Konsole/ProcessInfo#setParentPid(d4f767463ce0a6b3).(parentPid);
519
520 if (!processNameString.isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).())
521 setNamecxx . . $ Konsole/ProcessInfo#setName(3520e46b33348bf6).(processNameString);
522
523 // update object state
524 setPidcxx . . $ Konsole/ProcessInfo#setPid(d4f767463ce0a6b3).(aPid);
525
526 return ok;
527 }
528
529 virtual bool readArgumentscxx . . $ LinuxProcessInfo#readArguments(83c267b20bac841a).(int aPid) {
530 // read command-line arguments file found at /proc/<pid>/cmdline
531 // the expected format is a list of strings delimited by null characters,
532 // and ending in a double null character pair.
533
534 QFilecxx . . $ QFile# argumentsFilecxx . . $ QFile#QFile(3520e46b33348bf6).(QStringcxx . . $ QString#QString(85c52e162fed56f9).("/proc/%1/cmdline").argcxx . . $ QString#arg(64ab2ed17cc11715).(aPid));
535 if (argumentsFile.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) {
536 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&argumentsFile);
537 const QStringcxx . . $ QString#& data = stream.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
538
539 const QStringListcxx . . $ QStringList#& argList = data.splitcxx . . $ QString#split(92030b8bf74e4227).(QCharcxx . . $ QChar#('\0'));
540
541 foreachcxx . . $ `<external>/15045578679651060896/qglobal.h:1145:13`!(const QString & entry , argList) {
542 if (!entry.isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).())
543 addArgumentcxx . . $ Konsole/ProcessInfo#addArgument(3520e46b33348bf6).(entry);
544 }
545 } else {
546 setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(argumentsFile.errorcxx . . $ QFileDevice#error(2eea7c6a1bd47f0a).());
547 }
548
549 return true;
550 }
551
552 virtual bool readCurrentDircxx . . $ LinuxProcessInfo#readCurrentDir(83c267b20bac841a).(int aPid) {
553 char path_buffer[MAXPATHLENcxx . . $ `<external>/1231830743887005719/param.h:48:10`! + 1];
554 path_buffer[MAXPATHLENcxx . . $ `<external>/1231830743887005719/param.h:48:10`!] = 0;
555 QByteArraycxx . . $ QByteArray# procCwd = QFilecxx . . $ QFile#::encodeNamecxx . . $ QFile#encodeName(743b31b35d83e992).(QStringcxx . . $ QString#QString(85c52e162fed56f9).("/proc/%1/cwd").argcxx . . $ QString#arg(64ab2ed17cc11715).(aPid));
556 const int length = readlink(procCwd.constDatacxx . . $ QByteArray#constData(a9f41ea0e82d88cf).(), path_buffer, MAXPATHLENcxx . . $ `<external>/1231830743887005719/param.h:48:10`!);
557 if (length == -1) {
558 setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(UnknownErrorcxx . . $ Konsole/ProcessInfo#UnknownError.);
559 return false;
560 }
561
562 path_buffer[length] = '\0';
563 QStringcxx . . $ QString# path = QFilecxx . . $ QFile#::decodeNamecxx . . $ QFile#decodeName(89a325da49db1678).(path_buffer);
564
565 setCurrentDircxx . . $ Konsole/ProcessInfo#setCurrentDir(3520e46b33348bf6).(path);
566 return true;
567 }
568
569 virtual bool readEnvironmentcxx . . $ LinuxProcessInfo#readEnvironment(83c267b20bac841a).(int aPid) {
570 // read environment bindings file found at /proc/<pid>/environ
571 // the expected format is a list of KEY=VALUE strings delimited by null
572 // characters and ending in a double null character pair.
573
574 QFilecxx . . $ QFile# environmentFilecxx . . $ QFile#QFile(3520e46b33348bf6).(QStringcxx . . $ QString#QString(85c52e162fed56f9).("/proc/%1/environ").argcxx . . $ QString#arg(64ab2ed17cc11715).(aPid));
575 if (environmentFile.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) {
576 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&environmentFile);
577 const QStringcxx . . $ QString#& data = stream.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
578
579 const QStringListcxx . . $ QStringList#& bindingList = data.splitcxx . . $ QString#split(92030b8bf74e4227).(QCharcxx . . $ QChar#('\0'));
580
581 foreachcxx . . $ `<external>/15045578679651060896/qglobal.h:1145:13`!(const QString & entry , bindingList) {
582 QStringcxx . . $ QString# namecxx . . $ QString#QString(ced63f7c635d850d).;
583 QStringcxx . . $ QString# valuecxx . . $ QString#QString(ced63f7c635d850d).;
584
585 const int splitPos = entry.indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).('='cxx . . $ QChar#QChar(f349c77f591a7878).);
586
587 if (splitPos != -1) {
588 name =cxx . . $ QString#`operator=`(4f35bf974e7f8568). entry.midcxx . . $ QString#mid(44b7563da18aea72).(0, splitPos);
589 value =cxx . . $ QString#`operator=`(4f35bf974e7f8568). entry.midcxx . . $ QString#mid(44b7563da18aea72).(splitPos + 1, -1);
590
591 addEnvironmentBindingcxx . . $ Konsole/ProcessInfo#addEnvironmentBinding(c1427772cd1877b0).(name, value);
592 }
593 }
594 } else {
595 setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(environmentFile.errorcxx . . $ QFileDevice#error(2eea7c6a1bd47f0a).());
596 }
597
598 return true;
599 }
600 };
601
602 #elif defined(Q_OS_FREEBSD)
603 class FreeBSDProcessInfo : public UnixProcessInfo
604 {
605 public:
606 FreeBSDProcessInfo(int aPid, bool readEnvironment) :
607 UnixProcessInfo(aPid, readEnvironment) {
608 }
609
610 private:
611 virtual bool readProcInfo(int aPid) {
612 int managementInfoBase[4];
613 size_t mibLength;
614 struct kinfo_proc* kInfoProc;
615
616 managementInfoBase[0] = CTL_KERN;
617 managementInfoBase[1] = KERN_PROC;
618 managementInfoBase[2] = KERN_PROC_PID;
619 managementInfoBase[3] = aPid;
620
621 if (sysctl(managementInfoBase, 4, NULL, &mibLength, NULL, 0) == -1)
622 return false;
623
624 kInfoProc = new struct kinfo_proc [mibLength];
625
626 if (sysctl(managementInfoBase, 4, kInfoProc, &mibLength, NULL, 0) == -1) {
627 delete [] kInfoProc;
628 return false;
629 }
630
631 #if defined(HAVE_OS_DRAGONFLYBSD)
632 setName(kInfoProc->kp_comm);
633 setPid(kInfoProc->kp_pid);
634 setParentPid(kInfoProc->kp_ppid);
635 setForegroundPid(kInfoProc->kp_pgid);
636 setUserId(kInfoProc->kp_uid);
637 #else
638 setName(kInfoProc->ki_comm);
639 setPid(kInfoProc->ki_pid);
640 setParentPid(kInfoProc->ki_ppid);
641 setForegroundPid(kInfoProc->ki_pgid);
642 setUserId(kInfoProc->ki_uid);
643 #endif
644
645 readUserName();
646
647 delete [] kInfoProc;
648 return true;
649 }
650
651 virtual bool readArguments(int aPid) {
652 char args[ARG_MAX];
653 int managementInfoBase[4];
654 size_t len;
655
656 managementInfoBase[0] = CTL_KERN;
657 managementInfoBase[1] = KERN_PROC;
658 managementInfoBase[2] = KERN_PROC_PID;
659 managementInfoBase[3] = aPid;
660
661 len = sizeof(args);
662 if (sysctl(managementInfoBase, 4, args, &len, NULL, 0) == -1)
663 return false;
664
665 const QStringList& argumentList = QString(args).split(QChar('\0'));
666
667 for (QStringList::const_iterator it = argumentList.begin(); it != argumentList.end(); ++it) {
668 addArgument(*it);
669 }
670
671 return true;
672 }
673
674 virtual bool readEnvironment(int aPid) {
675 Q_UNUSED(aPid);
676 // Not supported in FreeBSD?
677 return false;
678 }
679
680 virtual bool readCurrentDir(int aPid) {
681 #if defined(HAVE_OS_DRAGONFLYBSD)
682 char buf[PATH_MAX];
683 int managementInfoBase[4];
684 size_t len;
685
686 managementInfoBase[0] = CTL_KERN;
687 managementInfoBase[1] = KERN_PROC;
688 managementInfoBase[2] = KERN_PROC_CWD;
689 managementInfoBase[3] = aPid;
690
691 len = sizeof(buf);
692 if (sysctl(managementInfoBase, 4, buf, &len, NULL, 0) == -1)
693 return false;
694
695 setCurrentDir(buf);
696
697 return true;
698 #else
699 int numrecords;
700 struct kinfo_file* info = 0;
701
702 info = kinfo_getfile(aPid, &numrecords);
703
704 if (!info)
705 return false;
706
707 for (int i = 0; i < numrecords; ++i) {
708 if (info[i].kf_fd == KF_FD_TYPE_CWD) {
709 setCurrentDir(info[i].kf_path);
710
711 free(info);
712 return true;
713 }
714 }
715
716 free(info);
717 return false;
718 #endif
719 }
720 };
721
722 #elif defined(Q_OS_OPENBSD)
723 class OpenBSDProcessInfo : public UnixProcessInfo
724 {
725 public:
726 OpenBSDProcessInfo(int aPid, bool readEnvironment) :
727 UnixProcessInfo(aPid, readEnvironment) {
728 }
729
730 private:
731 virtual bool readProcInfo(int aPid) {
732 int managementInfoBase[6];
733 size_t mibLength;
734 struct kinfo_proc* kInfoProc;
735
736 managementInfoBase[0] = CTL_KERN;
737 managementInfoBase[1] = KERN_PROC;
738 managementInfoBase[2] = KERN_PROC_PID;
739 managementInfoBase[3] = aPid;
740 managementInfoBase[4] = sizeof(struct kinfo_proc);
741 managementInfoBase[5] = 1;
742
743 if (::sysctl(managementInfoBase, 6, NULL, &mibLength, NULL, 0) == -1) {
744 qWarning() << "first sysctl() call failed with code" << errno;
745 return false;
746 }
747
748 kInfoProc = (struct kinfo_proc*)malloc(mibLength);
749
750 if (::sysctl(managementInfoBase, 6, kInfoProc, &mibLength, NULL, 0) == -1) {
751 qWarning() << "second sysctl() call failed with code" << errno;
752 free(kInfoProc);
753 return false;
754 }
755
756 setName(kInfoProc->p_comm);
757 setPid(kInfoProc->p_pid);
758 setParentPid(kInfoProc->p_ppid);
759 setForegroundPid(kInfoProc->p_tpgid);
760 setUserId(kInfoProc->p_uid);
761 setUserName(kInfoProc->p_login);
762
763 free(kInfoProc);
764 return true;
765 }
766
767 char** readProcArgs(int aPid, int whatMib) {
768 void* buf = NULL;
769 void* nbuf;
770 size_t len = 4096;
771 int rc = -1;
772 int managementInfoBase[4];
773
774 managementInfoBase[0] = CTL_KERN;
775 managementInfoBase[1] = KERN_PROC_ARGS;
776 managementInfoBase[2] = aPid;
777 managementInfoBase[3] = whatMib;
778
779 do {
780 len *= 2;
781 nbuf = realloc(buf, len);
782 if (nbuf == NULL) {
783 break;
784 }
785
786 buf = nbuf;
787 rc = ::sysctl(managementInfoBase, 4, buf, &len, NULL, 0);
788 qWarning() << "sysctl() call failed with code" << errno;
789 } while (rc == -1 && errno == ENOMEM);
790
791 if (nbuf == NULL || rc == -1) {
792 free(buf);
793 return NULL;
794 }
795
796 return (char**)buf;
797 }
798
799 virtual bool readArguments(int aPid) {
800 char** argv;
801
802 argv = readProcArgs(aPid, KERN_PROC_ARGV);
803 if (argv == NULL) {
804 return false;
805 }
806
807 for (char** p = argv; *p != NULL; p++) {
808 addArgument(QString(*p));
809 }
810 free(argv);
811 return true;
812 }
813
814 virtual bool readEnvironment(int aPid) {
815 char** envp;
816 char* eqsign;
817
818 envp = readProcArgs(aPid, KERN_PROC_ENV);
819 if (envp == NULL) {
820 return false;
821 }
822
823 for (char **p = envp; *p != NULL; p++) {
824 eqsign = strchr(*p, '=');
825 if (eqsign == NULL || eqsign[1] == '\0') {
826 continue;
827 }
828 *eqsign = '\0';
829 addEnvironmentBinding(QString((const char *)p),
830 QString((const char *)eqsign + 1));
831 }
832 free(envp);
833 return true;
834 }
835
836 virtual bool readCurrentDir(int aPid) {
837 char buf[PATH_MAX];
838 int managementInfoBase[3];
839 size_t len;
840
841 managementInfoBase[0] = CTL_KERN;
842 managementInfoBase[1] = KERN_PROC_CWD;
843 managementInfoBase[2] = aPid;
844
845 len = sizeof(buf);
846 if (::sysctl(managementInfoBase, 3, buf, &len, NULL, 0) == -1) {
847 qWarning() << "sysctl() call failed with code" << errno;
848 return false;
849 }
850
851 setCurrentDir(buf);
852 return true;
853 }
854 };
855
856 #elif defined(Q_OS_MAC)
857 class MacProcessInfo : public UnixProcessInfo
858 {
859 public:
860 MacProcessInfo(int aPid, bool env) :
861 UnixProcessInfo(aPid, env) {
862 }
863
864 private:
865 virtual bool readProcInfo(int aPid) {
866 int managementInfoBase[4];
867 size_t mibLength;
868 struct kinfo_proc* kInfoProc;
869 /*
870 KDE_struct_stat statInfo;
871
872 // Find the tty device of 'pid' (Example: /dev/ttys001)
873 managementInfoBase[0] = CTL_KERN;
874 managementInfoBase[1] = KERN_PROC;
875 managementInfoBase[2] = KERN_PROC_PID;
876 managementInfoBase[3] = aPid;
877
878 if (sysctl(managementInfoBase, 4, NULL, &mibLength, NULL, 0) == -1) {
879 return false;
880 } else {
881 kInfoProc = new struct kinfo_proc [mibLength];
882 if (sysctl(managementInfoBase, 4, kInfoProc, &mibLength, NULL, 0) == -1) {
883 delete [] kInfoProc;
884 return false;
885 } else {
886 const QString deviceNumber = QString(devname(((&kInfoProc->kp_eproc)->e_tdev), S_IFCHR));
887 const QString fullDeviceName = QString("/dev/") + deviceNumber.rightJustified(3, '0');
888 delete [] kInfoProc;
889
890 const QByteArray deviceName = fullDeviceName.toLatin1();
891 const char* ttyName = deviceName.data();
892
893 if (KDE::stat(ttyName, &statInfo) != 0)
894 return false;
895
896 // Find all processes attached to ttyName
897 managementInfoBase[0] = CTL_KERN;
898 managementInfoBase[1] = KERN_PROC;
899 managementInfoBase[2] = KERN_PROC_TTY;
900 managementInfoBase[3] = statInfo.st_rdev;
901
902 mibLength = 0;
903 if (sysctl(managementInfoBase, sizeof(managementInfoBase) / sizeof(int), NULL, &mibLength, NULL, 0) == -1)
904 return false;
905
906 kInfoProc = new struct kinfo_proc [mibLength];
907 if (sysctl(managementInfoBase, sizeof(managementInfoBase) / sizeof(int), kInfoProc, &mibLength, NULL, 0) == -1)
908 return false;
909
910 // The foreground program is the first one
911 setName(QString(kInfoProc->kp_proc.p_comm));
912
913 delete [] kInfoProc;
914 }
915 setPid(aPid);
916 }
917 return true;
918 */
919 return false;
920 }
921
922 virtual bool readArguments(int aPid) {
923 Q_UNUSED(aPid);
924 return false;
925 }
926 virtual bool readCurrentDir(int aPid) {
927 struct proc_vnodepathinfo vpi;
928 const int nb = proc_pidinfo(aPid, PROC_PIDVNODEPATHINFO, 0, &vpi, sizeof(vpi));
929 if (nb == sizeof(vpi)) {
930 setCurrentDir(QString(vpi.pvi_cdir.vip_path));
931 return true;
932 }
933 return false;
934 }
935 virtual bool readEnvironment(int aPid) {
936 Q_UNUSED(aPid);
937 return false;
938 }
939 };
940
941 #elif defined(Q_OS_SOLARIS)
942 // The procfs structure definition requires off_t to be
943 // 32 bits, which only applies if FILE_OFFSET_BITS=32.
944 // Futz around here to get it to compile regardless,
945 // although some of the structure sizes might be wrong.
946 // Fortunately, the structures we actually use don't use
947 // off_t, and we're safe.
948 #if defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS==64)
949 #undef _FILE_OFFSET_BITS
950 #endif
951 #include <procfs.h>
952
953 class SolarisProcessInfo : public UnixProcessInfo
954 {
955 public:
956 SolarisProcessInfo(int aPid, bool readEnvironment)
957 : UnixProcessInfo(aPid, readEnvironment) {
958 }
959 private:
960 virtual bool readProcInfo(int aPid) {
961 QFile psinfo(QString("/proc/%1/psinfo").arg(aPid));
962 if (psinfo.open(QIODevice::ReadOnly)) {
963 struct psinfo info;
964 if (psinfo.read((char *)&info, sizeof(info)) != sizeof(info)) {
965 return false;
966 }
967
968 setParentPid(info.pr_ppid);
969 setForegroundPid(info.pr_pgid);
970 setName(info.pr_fname);
971 setPid(aPid);
972
973 // Bogus, because we're treating the arguments as one single string
974 info.pr_psargs[PRARGSZ - 1] = 0;
975 addArgument(info.pr_psargs);
976 }
977 return true;
978 }
979
980 virtual bool readArguments(int /*pid*/) {
981 // Handled in readProcInfo()
982 return false;
983 }
984
985 virtual bool readEnvironment(int /*pid*/) {
986 // Not supported in Solaris
987 return false;
988 }
989
990 // FIXME: This will have the same issues as BKO 251351; the Linux
991 // version uses readlink.
992 virtual bool readCurrentDir(int aPid) {
993 QFileInfo info(QString("/proc/%1/path/cwd").arg(aPid));
994 const bool readable = info.isReadable();
995
996 if (readable && info.isSymLink()) {
997 setCurrentDir(info.symLinkTarget());
998 return true;
999 } else {
1000 if (!readable)
1001 setError(PermissionsError);
1002 else
1003 setError(UnknownError);
1004
1005 return false;
1006 }
1007 }
1008 };
1009 #endif
1010
1011 SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#SSHProcessInfo(f279fac147a2bcab).(const ProcessInfocxx . . $ Konsole/ProcessInfo#& process)
1012 : _processcxx . . $ Konsole/SSHProcessInfo#_process.(process)
1013 {
1014 bool ok = false;
1015
1016 // check that this is a SSH process
1017 const QStringcxx . . $ QString#& name = _processcxx . . $ Konsole/SSHProcessInfo#_process..namecxx . . $ Konsole/ProcessInfo#name(527c08bc36b7c847).(&ok);
1018
1019 if (!ok || name !=cxx . . $ QString#`operator!=`(7d4bdb1abc417636). "ssh") {
1020 if (!ok)
1021 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Could not read process info";
1022 else
1023 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Process is not a SSH process";
1024
1025 return;
1026 }
1027
1028 // read arguments
1029 const QVectorcxx . . $ QVector#<QStringcxx . . $ QString#>& args = _processcxx . . $ Konsole/SSHProcessInfo#_process..argumentscxx . . $ Konsole/ProcessInfo#arguments(362fb25d3f176df0).(&ok);
1030
1031 // SSH options
1032 // these are taken from the SSH manual ( accessed via 'man ssh' )
1033
1034 // options which take no arguments
1035 static const QStringcxx . . $ QString# noArgumentOptionscxx . . $ QString#QString(85c52e162fed56f9).("1246AaCfgKkMNnqsTtVvXxYy");
1036 // options which take one argument
1037 static const QStringcxx . . $ QString# singleArgumentOptionscxx . . $ QString#QString(85c52e162fed56f9).("bcDeFIiLlmOopRSWw");
1038
1039 if (ok) {
1040 // find the username, host and command arguments
1041 //
1042 // the username/host is assumed to be the first argument
1043 // which is not an option
1044 // ( ie. does not start with a dash '-' character )
1045 // or an argument to a previous option.
1046 //
1047 // the command, if specified, is assumed to be the argument following
1048 // the username and host
1049 //
1050 // note that we skip the argument at index 0 because that is the
1051 // program name ( expected to be 'ssh' in this case )
1052 for (int i = 1 ; i < args.countcxx . . $ QVector#count(455f465bc33b4cdf).() ; i++) {
1053 // If this one is an option ...
1054 // Most options together with its argument will be skipped.
1055 if (args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].startsWithcxx . . $ QString#startsWith(7cf081d1b6a1defd).('-'cxx . . $ QChar#QChar(f349c77f591a7878).)) {
1056 const QCharcxx . . $ QChar# optionChar = (args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].lengthcxx . . $ QString#length(455f465bc33b4cdf).() > 1) ? args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i][cxx . . $ QString#`operator[]`(13af7ae5a875439e).1] : '\0'cxx . . $ QChar#QChar(f349c77f591a7878).;
1057 // for example: -p2222 or -p 2222 ?
1058 const bool optionArgumentCombined = args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].lengthcxx . . $ QString#length(455f465bc33b4cdf).() > 2;
1059
1060 if (noArgumentOptions.containscxx . . $ QString#contains(7cf081d1b6a1defd).(optionChar)) {
1061 continue;
1062 } else if (singleArgumentOptions.containscxx . . $ QString#contains(7cf081d1b6a1defd).(optionChar)) {
1063 QStringcxx . . $ QString# argumentcxx . . $ QString#QString(ced63f7c635d850d).;
1064 if (optionArgumentCombined) {
1065 argument =cxx . . $ QString#`operator=`(4f35bf974e7f8568). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].midcxx . . $ QString#mid(44b7563da18aea72).(2);
1066 } else {
1067 // Verify correct # arguments are given
1068 if ((i + 1) < args.countcxx . . $ QVector#count(455f465bc33b4cdf).()) {
1069 argument =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i + 1];
1070 }
1071 i++;
1072 }
1073
1074 // support using `-l user` to specify username.
1075 if (optionChar ==cxx . . $ `operator==`(a468b2f18a3afe8). 'l'cxx . . $ QChar#QChar(f349c77f591a7878).)
1076 _usercxx . . $ Konsole/SSHProcessInfo#_user. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). argument;
1077 // support using `-p port` to specify port.
1078 else if (optionChar ==cxx . . $ `operator==`(a468b2f18a3afe8). 'p'cxx . . $ QChar#QChar(f349c77f591a7878).)
1079 _portcxx . . $ Konsole/SSHProcessInfo#_port. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). argument;
1080
1081 continue;
1082 }
1083 }
1084
1085 // check whether the host has been found yet
1086 // if not, this must be the username/host argument
1087 if (_hostcxx . . $ Konsole/SSHProcessInfo#_host..isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).()) {
1088 // check to see if only a hostname is specified, or whether
1089 // both a username and host are specified ( in which case they
1090 // are separated by an '@' character: username@host )
1091
1092 const int separatorPosition = args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).('@'cxx . . $ QChar#QChar(f349c77f591a7878).);
1093 if (separatorPosition != -1) {
1094 // username and host specified
1095 _usercxx . . $ Konsole/SSHProcessInfo#_user. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].leftcxx . . $ QString#left(3f3c3d0e9c0eb37f).(separatorPosition);
1096 _hostcxx . . $ Konsole/SSHProcessInfo#_host. =cxx . . $ QString#`operator=`(4f35bf974e7f8568). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i].midcxx . . $ QString#mid(44b7563da18aea72).(separatorPosition + 1);
1097 } else {
1098 // just the host specified
1099 _hostcxx . . $ Konsole/SSHProcessInfo#_host. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i];
1100 }
1101 } else {
1102 // host has already been found, this must be the command argument
1103 _commandcxx . . $ Konsole/SSHProcessInfo#_command. =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). args[cxx . . $ QVector#`operator[]`(e2681f9bb6b2f357).i];
1104 }
1105 }
1106 } else {
1107 qWarningcxx . . $ `<external>/10450329605257866924/qlogging.h:166:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "Could not read arguments";
1108
1109 return;
1110 }
1111 }
1112
1113 QStringcxx . . $ QString# SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::userNamecxx . . $ Konsole/SSHProcessInfo#userName(6ef7b6c27b00b43d).() const
1114 {
1115 return _usercxx . . $ QString#QString(132dd6869045752b).;
1116 }
1117 QStringcxx . . $ QString# SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::hostcxx . . $ Konsole/SSHProcessInfo#host(6ef7b6c27b00b43d).() const
1118 {
1119 return _hostcxx . . $ QString#QString(132dd6869045752b).;
1120 }
1121 QStringcxx . . $ QString# SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::portcxx . . $ Konsole/SSHProcessInfo#port(6ef7b6c27b00b43d).() const
1122 {
1123 return _portcxx . . $ QString#QString(132dd6869045752b).;
1124 }
1125 QStringcxx . . $ QString# SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::commandcxx . . $ Konsole/SSHProcessInfo#command(6ef7b6c27b00b43d).() const
1126 {
1127 return _commandcxx . . $ QString#QString(132dd6869045752b).;
1128 }
1129 QStringcxx . . $ QString# SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#::formatcxx . . $ Konsole/SSHProcessInfo#format(5555d9024dcf93fb).(const QStringcxx . . $ QString#& input) const
1130 {
1131 QStringcxx . . $ QString# outputcxx . . $ QString#QString(132dd6869045752b).(input);
1132
1133 // test whether host is an ip address
1134 // in which case 'short host' and 'full host'
1135 // markers in the input string are replaced with
1136 // the full address
1137 bool isIpAddress = false;
1138
1139 struct in_addr address;
1140 if (inet_aton(_hostcxx . . $ Konsole/SSHProcessInfo#_host..toLocal8Bitcxx . . $ QString#toLocal8Bit(ef52b713090bc85a).().constDatacxx . . $ QByteArray#constData(a9f41ea0e82d88cf).(), &address) != 0)
1141 isIpAddress = true;
1142 else
1143 isIpAddress = false;
1144
1145 // search for and replace known markers
1146 output.replacecxx . . $ QString#replace(192e628656427d1c).("%u"cxx . . $ QString#QString(85c52e162fed56f9)., _usercxx . . $ Konsole/SSHProcessInfo#_user.);
1147
1148 if (isIpAddress)
1149 output.replacecxx . . $ QString#replace(192e628656427d1c).("%h"cxx . . $ QString#QString(85c52e162fed56f9)., _hostcxx . . $ Konsole/SSHProcessInfo#_host.);
1150 else
1151 output.replacecxx . . $ QString#replace(192e628656427d1c).("%h"cxx . . $ QString#QString(85c52e162fed56f9)., _hostcxx . . $ Konsole/SSHProcessInfo#_host..leftcxx . . $ QString#left(3f3c3d0e9c0eb37f).(_hostcxx . . $ Konsole/SSHProcessInfo#_host..indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).('.'cxx . . $ QChar#QChar(f349c77f591a7878).)));
1152
1153 output.replacecxx . . $ QString#replace(192e628656427d1c).("%H"cxx . . $ QString#QString(85c52e162fed56f9)., _hostcxx . . $ Konsole/SSHProcessInfo#_host.);
1154 output.replacecxx . . $ QString#replace(192e628656427d1c).("%c"cxx . . $ QString#QString(85c52e162fed56f9)., _commandcxx . . $ Konsole/SSHProcessInfo#_command.);
1155
1156 return outputcxx . . $ QString#QString(28715ad661ad3cec).;
1157 }
1158
1159 ProcessInfocxx . . $ Konsole/ProcessInfo#* ProcessInfocxx . . $ Konsole/ProcessInfo#::newInstancecxx . . $ Konsole/ProcessInfo#newInstance(76b029d04fe475b6).(int aPid, bool enableEnvironmentRead)
1160 {
1161 #if defined(Q_OS_LINUXcxx . . $ `<external>/5130987379601705141/qsystemdetection.h:150:11`!)
1162 return new LinuxProcessInfocxx . . $ LinuxProcessInfo#(aPid, enableEnvironmentRead);
1163 #elif defined(Q_OS_SOLARIS)
1164 return new SolarisProcessInfo(aPid, enableEnvironmentRead);
1165 #elif defined(Q_OS_MAC)
1166 return new MacProcessInfo(aPid, enableEnvironmentRead);
1167 #elif defined(Q_OS_FREEBSD)
1168 return new FreeBSDProcessInfo(aPid, enableEnvironmentRead);
1169 #elif defined(Q_OS_OPENBSD)
1170 return new OpenBSDProcessInfo(aPid, enableEnvironmentRead);
1171 #else
1172 return new NullProcessInfo(aPid, enableEnvironmentRead);
1173 #endif
1174 }