ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 892d84ca24c9b8f2c3e4a8f1491423bbd41899ff
parent 1f783e7d96c01e9b40da81098d67a785b800a801
Author: sin <sin@2f30.org>
Date:   Thu, 13 Feb 2014 12:25:29 +0000

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

Diffstat:
Mpidof.c | 3++-
Mps.c | 6++++--
Mutil/proc.c | 6+++---
3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/pidof.c b/pidof.c @@ -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; diff --git a/ps.c b/ps.c @@ -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); } diff --git a/util/proc.c b/util/proc.c @@ -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);