Return an error if we can't open the files in /proc

This commit is contained in:
sin 2014-02-13 12:25:29 +00:00
parent 1f783e7d96
commit 892d84ca24
3 changed files with 9 additions and 6 deletions

View File

@ -74,7 +74,8 @@ main(int argc, char *argv[])
if (onode)
continue;
}
parsestat(pid, &ps);
if (parsestat(pid, &ps) < 0)
continue;
if (parsecmdline(ps.pid, cmdline,
sizeof(cmdline)) < 0) {
cmd = ps.comm;

6
ps.c
View File

@ -95,7 +95,8 @@ psout(struct procstat *ps)
}
}
parsestatus(ps->pid, &pstatus);
if (parsestatus(ps->pid, &pstatus) < 0)
return;
/* This is the default case, only print processes that have
* the same controlling terminal as the invoker and the same
@ -175,6 +176,7 @@ psr(const char *file)
if (!pidfile(file))
return;
pid = estrtol(file, 10);
parsestat(pid, &ps);
if (parsestat(pid, &ps) < 0)
return;
psout(&ps);
}

View File

@ -20,7 +20,7 @@ parsecmdline(pid_t pid, char *buf, size_t siz)
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
fd = open(path, O_RDONLY);
if (fd < 0)
eprintf("open %s:", path);
return -1;
n = read(fd, buf, siz - 1);
if (n < 0)
eprintf("read %s:", path);
@ -44,7 +44,7 @@ parsestat(pid_t pid, struct procstat *ps)
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
if (!(fp = fopen(path, "r")))
eprintf("fopen %s:", path);
return -1;
fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu",
&ps->pid, ps->comm,
&ps->state, &ps->ppid, &ps->pgrp,
@ -73,7 +73,7 @@ parsestatus(pid_t pid, struct procstatus *pstatus)
snprintf(path, sizeof(path), "/proc/%d/status", pid);
fd = open(path, O_RDONLY);
if (fd < 0)
eprintf("open %s:", path);
return -1;
n = read(fd, buf, sizeof(buf) - 1);
if (n < 0)
eprintf("%s: read error:", path);