[hackers] [ubase] Return an error if we can't open the files in /proc || sin
commit 892d84ca24c9b8f2c3e4a8f1491423bbd41899ff
Author: sin <sin_AT_2f30.org>
Date: Thu Feb 13 12:25:29 2014 +0000
Return an error if we can't open the files in /proc
diff --git a/pidof.c b/pidof.c
index 709598c..f91ad10 100644
--- a/pidof.c
+++ b/pidof.c
_AT_@ -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
index 7f2665c..1ab9003 100644
--- a/ps.c
+++ b/ps.c
_AT_@ -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
_AT_@ -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
index cb6a8f9..3b32a7b 100644
--- a/util/proc.c
+++ b/util/proc.c
_AT_@ -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);
_AT_@ -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,
_AT_@ -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);
Received on Thu Feb 13 2014 - 13:29:03 CET
This archive was generated by hypermail 2.3.0
: Thu Feb 13 2014 - 13:36:20 CET