Debian Codegraph @

lib/ProcessInfo.h

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 #ifndef PROCESSINFO_H
21 #define PROCESSINFO_Hcxx . . $ `lib/ProcessInfo.h:21:9`!
22
23 // Qt
24 #include <QtCore/QFile>
25 #include <QtCore/QMap>
26 #include <QtCore/QString>
27 #include <QtCore/QVector>
28
29 namespace Konsolecxx . . $ Konsole/
30 {
31 /**
32 * Takes a snapshot of the state of a process and provides access to
33 * information such as the process name, parent process,
34 * the foreground process in the controlling terminal,
35 * the arguments with which the process was started and the
36 * environment.
37 *
38 * To create a new snapshot, construct a new ProcessInfo instance,
39 * using ProcessInfo::newInstance(),
40 * passing the process identifier of the process you are interested in.
41 *
42 * After creating a new instance, call the update() method to take a
43 * snapshot of the current state of the process.
44 *
45 * Before calling any additional methods, check that the process state
46 * was read successfully using the isValid() method.
47 *
48 * Each accessor method which provides information about the process state ( such as pid(),
49 * currentDir(), name() ) takes a pointer to a boolean as an argument. If the information
50 * requested was read successfully then the boolean is set to true, otherwise it is set
51 * to false, in which case the return value from the function should be ignored.
52 * If this boolean is set to false, it may indicate an error reading the process information,
53 * or it may indicate that the information is not available on the current platform.
54 *
55 * eg.
56 *
57 * @code
58 * ProcessInfo* info = ProcessInfo::newInstance(pid);
59 * info->update();
60 *
61 * if ( info->isValid() )
62 * {
63 * bool ok;
64 *
65 * QString name = info->name(&ok);
66 * if ( ok ) qDebug() << "process name - " << name;
67 * int parentPid = info->parentPid(&ok);
68 * if ( ok ) qDebug() << "parent process - " << parentPid;
69 * int foregroundPid = info->foregroundPid(&ok);
70 * if ( ok ) qDebug() << "foreground process - " << foregroundPid;
71 * }
72 * @endcode
73 */
74 class ProcessInfocxx . . $ Konsole/ProcessInfo#
75 {
76 public:
77 /**
78 * Constructs a new instance of a suitable ProcessInfo sub-class for
79 * the current platform which provides information about a given process.
80 *
81 * @param pid The pid of the process to examine
82 * @param readEnvironment Specifies whether environment bindings should
83 * be read. If this is false, then environment() calls will
84 * always fail. This is an optimization to avoid the overhead
85 * of reading the (potentially large) environment data when it
86 * is not required.
87 */
88 static ProcessInfocxx . . $ Konsole/ProcessInfo#* newInstancecxx . . $ Konsole/ProcessInfo#newInstance(76b029d04fe475b6).(int pid, bool readEnvironment = false);
89
90 virtual ~cxx . . $ Konsole/ProcessInfo#`~ProcessInfo`(ced63f7c635d850d).ProcessInfocxx . . $ Konsole/ProcessInfo#() {}
91
92 /**
93 * Updates the information about the process. This must
94 * be called before attempting to use any of the accessor methods.
95 */
96 void updatecxx . . $ Konsole/ProcessInfo#update(49f6e7a06ebc5aa8).();
97
98 /** Returns true if the process state was read successfully. */
99 bool isValidcxx . . $ Konsole/ProcessInfo#isValid(50ce9a9e25b4a850).() const;
100 /**
101 * Returns the process id.
102 *
103 * @param ok Set to true if the process id was read successfully or false otherwise
104 */
105 int pidcxx . . $ Konsole/ProcessInfo#pid(5bd9a3493f6a7a21).(bool* ok) const;
106 /**
107 * Returns the id of the parent process id was read successfully or false otherwise
108 *
109 * @param ok Set to true if the parent process id
110 */
111 int parentPidcxx . . $ Konsole/ProcessInfo#parentPid(5bd9a3493f6a7a21).(bool* ok) const;
112
113 /**
114 * Returns the id of the current foreground process
115 *
116 * NOTE: Using the foregroundProcessGroup() method of the Pty
117 * instance associated with the terminal of interest is preferred
118 * over using this method.
119 *
120 * @param ok Set to true if the foreground process id was read successfully or false otherwise
121 */
122 int foregroundPidcxx . . $ Konsole/ProcessInfo#foregroundPid(5bd9a3493f6a7a21).(bool* ok) const;
123
124 /* Returns the user id of the process */
125 int userIdcxx . . $ Konsole/ProcessInfo#userId(5bd9a3493f6a7a21).(bool* ok) const;
126
127 /** Returns the user's name of the process */
128 QStringcxx . . $ QString# userNamecxx . . $ Konsole/ProcessInfo#userName(6ef7b6c27b00b43d).() const;
129
130 /** Returns the user's home directory of the process */
131 QStringcxx . . $ QString# userHomeDircxx . . $ Konsole/ProcessInfo#userHomeDir(6ef7b6c27b00b43d).() const;
132
133 /** Returns the local host */
134 static QStringcxx . . $ QString# localHostcxx . . $ Konsole/ProcessInfo#localHost(4365c3963150523e).();
135
136 /** Returns the name of the current process */
137 QStringcxx . . $ QString# namecxx . . $ Konsole/ProcessInfo#name(527c08bc36b7c847).(bool* ok) const;
138
139 /**
140 * Returns the command-line arguments which the process
141 * was started with.
142 *
143 * The first argument is the name used to launch the process.
144 *
145 * @param ok Set to true if the arguments were read successfully or false otherwise.
146 */
147 QVectorcxx . . $ QVector#<QStringcxx . . $ QString#> argumentscxx . . $ Konsole/ProcessInfo#arguments(362fb25d3f176df0).(bool* ok) const;
148 /**
149 * Returns the environment bindings which the process
150 * was started with.
151 * In the returned map, the key is the name of the environment variable,
152 * and the value is the corresponding value.
153 *
154 * @param ok Set to true if the environment bindings were read successfully or false otherwise
155 */
156 QMapcxx . . $ QMap#<QStringcxx . . $ QString#, QStringcxx . . $ QString#> environmentcxx . . $ Konsole/ProcessInfo#environment(ef83c2157f405d62).(bool* ok) const;
157
158 /**
159 * Returns the current working directory of the process
160 *
161 * @param ok Set to true if the current working directory was read successfully or false otherwise
162 */
163 QStringcxx . . $ QString# currentDircxx . . $ Konsole/ProcessInfo#currentDir(527c08bc36b7c847).(bool* ok) const;
164
165 /**
166 * Returns the current working directory of the process (or its parent)
167 */
168 QStringcxx . . $ QString# validCurrentDircxx . . $ Konsole/ProcessInfo#validCurrentDir(6ef7b6c27b00b43d).() const;
169
170 /** Forces the user home directory to be calculated */
171 void setUserHomeDircxx . . $ Konsole/ProcessInfo#setUserHomeDir(49f6e7a06ebc5aa8).();
172
173 /**
174 * Parses an input string, looking for markers beginning with a '%'
175 * character and returns a string with the markers replaced
176 * with information from this process description.
177 * <br>
178 * The markers recognized are:
179 * <ul>
180 * <li> %u - Name of the user which owns the process. </li>
181 * <li> %n - Replaced with the name of the process. </li>
182 * <li> %d - Replaced with the last part of the path name of the
183 * process' current working directory.
184 *
185 * (eg. if the current directory is '/home/bob' then
186 * 'bob' would be returned)
187 * </li>
188 * <li> %D - Replaced with the current working directory of the process. </li>
189 * </ul>
190 */
191 QStringcxx . . $ QString# formatcxx . . $ Konsole/ProcessInfo#format(5555d9024dcf93fb).(const QStringcxx . . $ QString#& text) const;
192
193 /**
194 * This enum describes the errors which can occur when trying to read
195 * a process's information.
196 */
197 enum Errorcxx . . $ Konsole/ProcessInfo#Error# {
198 /** No error occurred. */
199 NoErrorcxx . . $ Konsole/ProcessInfo#NoError.,
200 /** The nature of the error is unknown. */
201 UnknownErrorcxx . . $ Konsole/ProcessInfo#UnknownError.,
202 /** Konsole does not have permission to obtain the process information. */
203 PermissionsErrorcxx . . $ Konsole/ProcessInfo#PermissionsError.
204 };
205
206 /**
207 * Returns the last error which occurred.
208 */
209 Errorcxx . . $ Konsole/ProcessInfo#Error# errorcxx . . $ Konsole/ProcessInfo#error(a76b47da7438de78).() const;
210
211 enum Fieldcxx . . $ Konsole/ProcessInfo#Field# {
212 PROCESS_IDcxx . . $ Konsole/ProcessInfo#PROCESS_ID. = 1,
213 PARENT_PIDcxx . . $ Konsole/ProcessInfo#PARENT_PID. = 2,
214 FOREGROUND_PIDcxx . . $ Konsole/ProcessInfo#FOREGROUND_PID. = 4,
215 ARGUMENTScxx . . $ Konsole/ProcessInfo#ARGUMENTS. = 8,
216 ENVIRONMENTcxx . . $ Konsole/ProcessInfo#ENVIRONMENT. = 16,
217 NAMEcxx . . $ Konsole/ProcessInfo#NAME. = 32,
218 CURRENT_DIRcxx . . $ Konsole/ProcessInfo#CURRENT_DIR. = 64,
219 UIDcxx . . $ Konsole/ProcessInfo#UID. = 128
220 };
221 Q_DECLARE_FLAGScxx . . $ Konsole/ProcessInfo#Fields#(Fields, Field)
222
223 protected:
224 /**
225 * Constructs a new process instance. You should not call the constructor
226 * of ProcessInfo or its subclasses directly. Instead use the
227 * static ProcessInfo::newInstance() method which will return
228 * a suitable ProcessInfo instance for the current platform.
229 */
230 explicit ProcessInfocxx . . $ Konsole/ProcessInfo#ProcessInfo(918ebd27a9ddbad3).(int pid , bool readEnvironment = false);
231
232 /**
233 * This is called on construction to read the process state
234 * Subclasses should reimplement this function to provide
235 * platform-specific process state reading functionality.
236 *
237 * When called, readProcessInfo() should attempt to read all
238 * of the necessary state information. If the attempt is successful,
239 * it should set the process id using setPid(), and update
240 * the other relevant information using setParentPid(), setName(),
241 * setArguments() etc.
242 *
243 * Calls to isValid() will return true only if the process id
244 * has been set using setPid()
245 *
246 * @param pid The process id of the process to read
247 * @param readEnvironment Specifies whether the environment bindings
248 * for the process should be read
249 */
250 virtual bool readProcessInfocxx . . $ Konsole/ProcessInfo#readProcessInfo(9609253e83e89946).(int pid , bool readEnvironment) = 0;
251
252 /* Read the user name */
253 virtual void readUserNamecxx . . $ Konsole/ProcessInfo#readUserName(49f6e7a06ebc5aa8).(void) = 0;
254
255 /** Sets the process id associated with this ProcessInfo instance */
256 void setPidcxx . . $ Konsole/ProcessInfo#setPid(d4f767463ce0a6b3).(int pid);
257 /** Sets the parent process id as returned by parentPid() */
258 void setParentPidcxx . . $ Konsole/ProcessInfo#setParentPid(d4f767463ce0a6b3).(int pid);
259 /** Sets the foreground process id as returned by foregroundPid() */
260 void setForegroundPidcxx . . $ Konsole/ProcessInfo#setForegroundPid(d4f767463ce0a6b3).(int pid);
261 /** Sets the user id associated with this ProcessInfo instance */
262 void setUserIdcxx . . $ Konsole/ProcessInfo#setUserId(d4f767463ce0a6b3).(int uid);
263 /** Sets the user name of the process as set by readUserName() */
264 void setUserNamecxx . . $ Konsole/ProcessInfo#setUserName(3520e46b33348bf6).(const QStringcxx . . $ QString#& name);
265 /** Sets the name of the process as returned by name() */
266 void setNamecxx . . $ Konsole/ProcessInfo#setName(3520e46b33348bf6).(const QStringcxx . . $ QString#& name);
267 /** Sets the current working directory for the process */
268 void setCurrentDircxx . . $ Konsole/ProcessInfo#setCurrentDir(3520e46b33348bf6).(const QStringcxx . . $ QString#& dir);
269
270 /** Sets the error */
271 void setErrorcxx . . $ Konsole/ProcessInfo#setError(240223c605ac1c32).(Errorcxx . . $ Konsole/ProcessInfo#Error# error);
272
273 /** Convenience method. Sets the error based on a QFile error code. */
274 void setFileErrorcxx . . $ Konsole/ProcessInfo#setFileError(ecc1dbbfe5d2dfd3).(QFilecxx . . $ QFile#::FileErrorcxx . . $ QFileDevice#FileError# error);
275
276 /**
277 * Adds a commandline argument for the process, as returned
278 * by arguments()
279 */
280 void addArgumentcxx . . $ Konsole/ProcessInfo#addArgument(3520e46b33348bf6).(const QStringcxx . . $ QString#& argument);
281
282 /**
283 * clear the commandline arguments for the process, as returned
284 * by arguments()
285 */
286 void clearArgumentscxx . . $ Konsole/ProcessInfo#clearArguments(49f6e7a06ebc5aa8).();
287
288 /**
289 * Adds an environment binding for the process, as returned by
290 * environment()
291 *
292 * @param name The name of the environment variable, eg. "PATH"
293 * @param value The value of the environment variable, eg. "/bin"
294 */
295 void addEnvironmentBindingcxx . . $ Konsole/ProcessInfo#addEnvironmentBinding(c1427772cd1877b0).(const QStringcxx . . $ QString#& name , const QStringcxx . . $ QString#& value);
296
297 private:
298 // takes a full directory path and returns a
299 // shortened version suitable for display in
300 // space-constrained UI elements (eg. tabs)
301 QStringcxx . . $ QString# formatShortDircxx . . $ Konsole/ProcessInfo#formatShortDir(5555d9024dcf93fb).(const QStringcxx . . $ QString#& dirPath) const;
302
303 Fieldscxx . . $ Konsole/ProcessInfo#Fields# _fieldscxx . . $ Konsole/ProcessInfo#_fields.;
304
305 bool _enableEnvironmentReadcxx . . $ Konsole/ProcessInfo#_enableEnvironmentRead.; // specifies whether to read the environment
306 // bindings when update() is called
307 int _pidcxx . . $ Konsole/ProcessInfo#_pid.;
308 int _parentPidcxx . . $ Konsole/ProcessInfo#_parentPid.;
309 int _foregroundPidcxx . . $ Konsole/ProcessInfo#_foregroundPid.;
310 int _userIdcxx . . $ Konsole/ProcessInfo#_userId.;
311
312 Errorcxx . . $ Konsole/ProcessInfo#Error# _lastErrorcxx . . $ Konsole/ProcessInfo#_lastError.;
313
314 QStringcxx . . $ QString# _namecxx . . $ Konsole/ProcessInfo#_name.;
315 QStringcxx . . $ QString# _userNamecxx . . $ Konsole/ProcessInfo#_userName.;
316 QStringcxx . . $ QString# _userHomeDircxx . . $ Konsole/ProcessInfo#_userHomeDir.;
317 QStringcxx . . $ QString# _currentDircxx . . $ Konsole/ProcessInfo#_currentDir.;
318
319 QVectorcxx . . $ QVector#<QStringcxx . . $ QString#> _argumentscxx . . $ Konsole/ProcessInfo#_arguments.;
320 QMapcxx . . $ QMap#<QStringcxx . . $ QString#, QStringcxx . . $ QString#> _environmentcxx . . $ Konsole/ProcessInfo#_environment.;
321
322 static QSetcxx . . $ QSet#<QStringcxx . . $ QString#> commonDirNamescxx . . $ Konsole/ProcessInfo#commonDirNames(3d52db697563fcb6).();
323 static QSetcxx . . $ QSet#<QStringcxx . . $ QString#> _commonDirNamescxx . . $ Konsole/ProcessInfo#_commonDirNames.;
324 };
325 Q_DECLARE_OPERATORS_FOR_FLAGScxx . . $ Konsole/`operator|`(b06255e1ae41a2b4).(ProcessInfo::Fields)
326
327 /**
328 * Implementation of ProcessInfo which does nothing.
329 * Used on platforms where a suitable ProcessInfo subclass is not
330 * available.
331 *
332 * isValid() will always return false for instances of NullProcessInfo
333 */
334 class NullProcessInfocxx . . $ Konsole/NullProcessInfo# : public ProcessInfocxx . . $ Konsole/ProcessInfo#
335 {
336 public:
337 /**
338 * Constructs a new NullProcessInfo instance.
339 * See ProcessInfo::newInstance()
340 */
341 explicit NullProcessInfocxx . . $ Konsole/NullProcessInfo#NullProcessInfo(918ebd27a9ddbad3).(int pid, bool readEnvironment = false);
342 protected:
343 virtual bool readProcessInfocxx . . $ Konsole/NullProcessInfo#readProcessInfo(9609253e83e89946).(int pid, bool readEnvironment);
344 virtual void readUserNamecxx . . $ Konsole/NullProcessInfo#readUserName(49f6e7a06ebc5aa8).(void);
345 };
346
347 #if !defined(Q_OS_WIN)
348 /**
349 * Implementation of ProcessInfo for Unix platforms which uses
350 * the /proc filesystem
351 */
352 class UnixProcessInfocxx . . $ Konsole/UnixProcessInfo# : public ProcessInfocxx . . $ Konsole/ProcessInfo#
353 {
354 public:
355 /**
356 * Constructs a new instance of UnixProcessInfo.
357 * See ProcessInfo::newInstance()
358 */
359 explicit UnixProcessInfocxx . . $ Konsole/UnixProcessInfo#UnixProcessInfo(918ebd27a9ddbad3).(int pid, bool readEnvironment = false);
360
361 protected:
362 /**
363 * Implementation of ProcessInfo::readProcessInfo(); calls the
364 * four private methods below in turn.
365 */
366 virtual bool readProcessInfocxx . . $ Konsole/UnixProcessInfo#readProcessInfo(9609253e83e89946).(int pid , bool readEnvironment);
367
368 virtual void readUserNamecxx . . $ Konsole/UnixProcessInfo#readUserName(49f6e7a06ebc5aa8).(void);
369
370 private:
371 /**
372 * Read the standard process information -- PID, parent PID, foreground PID.
373 * @param pid process ID to use
374 * @return true on success
375 */
376 virtual bool readProcInfocxx . . $ Konsole/UnixProcessInfo#readProcInfo(83c267b20bac841a).(int pid) = 0;
377
378 /**
379 * Read the environment of the process. Sets _environment.
380 * @param pid process ID to use
381 * @return true on success
382 */
383 virtual bool readEnvironmentcxx . . $ Konsole/UnixProcessInfo#readEnvironment(83c267b20bac841a).(int pid) = 0;
384
385 /**
386 * Determine what arguments were passed to the process. Sets _arguments.
387 * @param pid process ID to use
388 * @return true on success
389 */
390 virtual bool readArgumentscxx . . $ Konsole/UnixProcessInfo#readArguments(83c267b20bac841a).(int pid) = 0;
391
392 /**
393 * Determine the current directory of the process.
394 * @param pid process ID to use
395 * @return true on success
396 */
397 virtual bool readCurrentDircxx . . $ Konsole/UnixProcessInfo#readCurrentDir(83c267b20bac841a).(int pid) = 0;
398 };
399 #endif
400
401 /**
402 * Lightweight class which provides additional information about SSH processes.
403 */
404 class SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#
405 {
406 public:
407 /**
408 * Constructs a new SSHProcessInfo instance which provides additional
409 * information about the specified SSH process.
410 *
411 * @param process A ProcessInfo instance for a SSH process.
412 */
413 explicit SSHProcessInfocxx . . $ Konsole/SSHProcessInfo#SSHProcessInfo(f279fac147a2bcab).(const ProcessInfocxx . . $ Konsole/ProcessInfo#& process);
414
415 /**
416 * Returns the user name which the user initially logged into on
417 * the remote computer.
418 */
419 QStringcxx . . $ QString# userNamecxx . . $ Konsole/SSHProcessInfo#userName(6ef7b6c27b00b43d).() const;
420
421 /**
422 * Returns the host which the user has connected to.
423 */
424 QStringcxx . . $ QString# hostcxx . . $ Konsole/SSHProcessInfo#host(6ef7b6c27b00b43d).() const;
425
426 /**
427 * Returns the port on host which the user has connected to.
428 */
429 QStringcxx . . $ QString# portcxx . . $ Konsole/SSHProcessInfo#port(6ef7b6c27b00b43d).() const;
430
431 /**
432 * Returns the command which the user specified to execute on the
433 * remote computer when starting the SSH process.
434 */
435 QStringcxx . . $ QString# commandcxx . . $ Konsole/SSHProcessInfo#command(6ef7b6c27b00b43d).() const;
436
437 /**
438 * Operates in the same way as ProcessInfo::format(), except
439 * that the set of markers understood is different:
440 *
441 * %u - Replaced with user name which the user initially logged
442 * into on the remote computer.
443 * %h - Replaced with the first part of the host name which
444 * is connected to.
445 * %H - Replaced with the full host name of the computer which
446 * is connected to.
447 * %c - Replaced with the command which the user specified
448 * to execute when starting the SSH process.
449 */
450 QStringcxx . . $ QString# formatcxx . . $ Konsole/SSHProcessInfo#format(5555d9024dcf93fb).(const QStringcxx . . $ QString#& input) const;
451
452 private:
453 const ProcessInfocxx . . $ Konsole/ProcessInfo#& _processcxx . . $ Konsole/SSHProcessInfo#_process.;
454 QStringcxx . . $ QString# _usercxx . . $ Konsole/SSHProcessInfo#_user.;
455 QStringcxx . . $ QString# _hostcxx . . $ Konsole/SSHProcessInfo#_host.;
456 QStringcxx . . $ QString# _portcxx . . $ Konsole/SSHProcessInfo#_port.;
457 QStringcxx . . $ QString# _commandcxx . . $ Konsole/SSHProcessInfo#_command.;
458 };
459 }
460 #endif //PROCESSINFO_H