summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMartin Schwenke <martin@meltin.net>2014-02-26 06:55:21 +1100
committerCraig Small <csmall@enc.com.au>2014-06-03 22:13:04 +1000
commit8b3113dbc6ad756559fe040f701586dda8989f93 (patch)
treeecd5ecb8fa15e9408f67ec803ec481afa68035b9 /src
parent0defe7e2ea45e1b14adaedde9b371ec7a8b00a84 (diff)
pstree: Ignore processes that disappear before reading cmdline
The worst case here is that the user has specific a PID on the command-line, read_proc() fails to read cmdline for an irrelevant, transient process and fails. It is better to simply ignore processes that have disappeared. That way pstree can provide useful output in more cases. An alternative is to read cmdline only for processes that the user is interested in. However, this probably increases the chances of an interesting process exiting before the cmdline is read. Signed-off-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'src')
-rw-r--r--src/pstree.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/pstree.c b/src/pstree.c
index b5b71cd..5540fe2 100644
--- a/src/pstree.c
+++ b/src/pstree.c
@@ -941,12 +941,22 @@ static void read_proc(void)
else {
sprintf(path, "%s/%d/cmdline", PROC_BASE, pid);
if ((fd = open(path, O_RDONLY)) < 0) {
- perror(path);
- exit(1);
- }
+ /* If this fails then the process is gone. If a PID
+ * was specified on the command-line then we might
+ * not even be interested in the current process.
+ * There's no sensible way of dealing with this race
+ * so we might as well behave as if the current
+ * process did not exist. */
+ (void) fclose(file);
+ free(path);
+ continue;
+ }
if ((size = read(fd, buffer, buffer_size)) < 0) {
- perror(path);
- exit(1);
+ /* As above. */
+ close(fd);
+ (void) fclose(file);
+ free(path);
+ continue;
}
(void) close(fd);
/* If we have read the maximum screen length of args, bring it back by one to stop overflow */