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_H
22
23 // Qt
24 #include <QtCore/QFile>
25 #include <QtCore/QMap>
26 #include <QtCore/QString>
27 #include <QtCore/QVector>
28
29 namespace 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 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 ProcessInfo* newInstance(int pid, bool readEnvironment = false);
89
90 virtual ~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 update();
97
98 /** Returns true if the process state was read successfully. */
99 bool isValid() 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 pid(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 parentPid(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 foregroundPid(bool* ok) const;
123
124 /* Returns the user id of the process */
125 int userId(bool* ok) const;
126
127 /** Returns the user's name of the process */
128 QString userName() const;
129
130 /** Returns the user's home directory of the process */
131 QString userHomeDir() const;
132
133 /** Returns the local host */
134 static QString localHost();
135
136 /** Returns the name of the current process */
137 QString name(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 QVector<QString> arguments(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 QMap<QString, QString> environment(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 QString currentDir(bool* ok) const;
164
165 /**
166 * Returns the current working directory of the process (or its parent)
167 */
168 QString validCurrentDir() const;
169
170 /** Forces the user home directory to be calculated */
171 void setUserHomeDir();
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 QString format(const 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 Error {
198 /** No error occurred. */
199 NoError,
200 /** The nature of the error is unknown. */
201 UnknownError,
202 /** Konsole does not have permission to obtain the process information. */
203 PermissionsError
204 };
205
206 /**
207 * Returns the last error which occurred.
208 */
209 Error error() const;
210
211 enum Field {
212 PROCESS_ID = 1,
213 PARENT_PID = 2,
214 FOREGROUND_PID = 4,
215 ARGUMENTS = 8,
216 ENVIRONMENT = 16,
217 NAME = 32,
218 CURRENT_DIR = 64,
219 UID = 128
220 };
221 Q_DECLARE_FLAGS(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 ProcessInfo(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 readProcessInfo(int pid , bool readEnvironment) = 0;
251
252 /* Read the user name */
253 virtual void readUserName(void) = 0;
254
255 /** Sets the process id associated with this ProcessInfo instance */
256 void setPid(int pid);
257 /** Sets the parent process id as returned by parentPid() */
258 void setParentPid(int pid);
259 /** Sets the foreground process id as returned by foregroundPid() */
260 void setForegroundPid(int pid);
261 /** Sets the user id associated with this ProcessInfo instance */
262 void setUserId(int uid);
263 /** Sets the user name of the process as set by readUserName() */
264 void setUserName(const QString& name);
265 /** Sets the name of the process as returned by name() */
266 void setName(const QString& name);
267 /** Sets the current working directory for the process */
268 void setCurrentDir(const QString& dir);
269
270 /** Sets the error */
271 void setError(Error error);
272
273 /** Convenience method. Sets the error based on a QFile error code. */
274 void setFileError(QFile::FileError error);
275
276 /**
277 * Adds a commandline argument for the process, as returned
278 * by arguments()
279 */
280 void addArgument(const QString& argument);
281
282 /**
283 * clear the commandline arguments for the process, as returned
284 * by arguments()
285 */
286 void clearArguments();
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 addEnvironmentBinding(const QString& name , const 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 QString formatShortDir(const QString& dirPath) const;
302
303 Fields _fields;
304
305 bool _enableEnvironmentRead; // specifies whether to read the environment
306 // bindings when update() is called
307 int _pid;
308 int _parentPid;
309 int _foregroundPid;
310 int _userId;
311
312 Error _lastError;
313
314 QString _name;
315 QString _userName;
316 QString _userHomeDir;
317 QString _currentDir;
318
319 QVector<QString> _arguments;
320 QMap<QString, QString> _environment;
321
322 static QSet<QString> commonDirNames();
323 static QSet<QString> _commonDirNames;
324 };
325 Q_DECLARE_OPERATORS_FOR_FLAGS(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 NullProcessInfo : public ProcessInfo
335 {
336 public:
337 /**
338 * Constructs a new NullProcessInfo instance.
339 * See ProcessInfo::newInstance()
340 */
341 explicit NullProcessInfo(int pid, bool readEnvironment = false);
342 protected:
343 virtual bool readProcessInfo(int pid, bool readEnvironment);
344 virtual void readUserName(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 UnixProcessInfo : public ProcessInfo
353 {
354 public:
355 /**
356 * Constructs a new instance of UnixProcessInfo.
357 * See ProcessInfo::newInstance()
358 */
359 explicit UnixProcessInfo(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 readProcessInfo(int pid , bool readEnvironment);
367
368 virtual void readUserName(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 readProcInfo(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 readEnvironment(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 readArguments(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 readCurrentDir(int pid) = 0;
398 };
399 #endif
400
401 /**
402 * Lightweight class which provides additional information about SSH processes.
403 */
404 class 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 SSHProcessInfo(const ProcessInfo& process);
414
415 /**
416 * Returns the user name which the user initially logged into on
417 * the remote computer.
418 */
419 QString userName() const;
420
421 /**
422 * Returns the host which the user has connected to.
423 */
424 QString host() const;
425
426 /**
427 * Returns the port on host which the user has connected to.
428 */
429 QString port() 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 QString command() 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 QString format(const QString& input) const;
451
452 private:
453 const ProcessInfo& _process;
454 QString _user;
455 QString _host;
456 QString _port;
457 QString _command;
458 };
459 }
460 #endif //PROCESSINFO_H