aboutsummaryrefslogtreecommitdiff
path: root/libbb/procps.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-02-13 22:04:27 +0000
committerRob Landley <rob@landley.net>2006-02-13 22:04:27 +0000
commitb2804551a0fafa83d0c5d602761e787f40821519 (patch)
tree9ca7e94ed410676810005e9d95ceaee6a70c6518 /libbb/procps.c
parent90632d021cecfb9db88f047ad195f91803b66c4a (diff)
downloadbusybox-b2804551a0fafa83d0c5d602761e787f40821519.tar.gz
Cleanups from Denis Vlasenko.
Diffstat (limited to 'libbb/procps.c')
-rw-r--r--libbb/procps.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/libbb/procps.c b/libbb/procps.c
index 3e863b0de..09561b533 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -13,9 +13,24 @@
#include <stdlib.h>
#include <unistd.h>
#include <asm/page.h>
+#include <fcntl.h>
#include "libbb.h"
+
+static int read_to_buf(char *filename, void *buf, int bufsize)
+{
+ int fd;
+
+ fd = open(filename, O_RDONLY);
+ if(fd < 0)
+ return -1;
+ bufsize = read(fd, buf, bufsize);
+ close(fd);
+ return bufsize;
+}
+
+
extern procps_status_t * procps_scan(int save_user_arg0)
{
static DIR *dir;
@@ -24,8 +39,8 @@ extern procps_status_t * procps_scan(int save_user_arg0)
char *name;
int n;
char status[32];
+ char *status_tail;
char buf[1024];
- FILE *fp;
procps_status_t curstatus;
int pid;
long tasknice;
@@ -50,18 +65,14 @@ extern procps_status_t * procps_scan(int save_user_arg0)
pid = atoi(name);
curstatus.pid = pid;
- sprintf(status, "/proc/%d", pid);
+ status_tail = status + sprintf(status, "/proc/%d", pid);
if(stat(status, &sb))
continue;
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
- sprintf(status, "/proc/%d/stat", pid);
-
- if((fp = fopen(status, "r")) == NULL)
- continue;
- name = fgets(buf, sizeof(buf), fp);
- fclose(fp);
- if(name == NULL)
+ strcpy(status_tail, "/stat");
+ n = read_to_buf(status, buf, sizeof(buf));
+ if(n < 0)
continue;
name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
if(name == 0 || name[1] != ' ')
@@ -113,10 +124,9 @@ extern procps_status_t * procps_scan(int save_user_arg0)
#endif
if(save_user_arg0) {
- sprintf(status, "/proc/%d/cmdline", pid);
- if((fp = fopen(status, "r")) == NULL)
- continue;
- if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
+ strcpy(status_tail, "/cmdline");
+ n = read_to_buf(status, buf, sizeof(buf));
+ if(n > 0) {
if(buf[n-1]=='\n')
buf[--n] = 0;
name = buf;
@@ -131,7 +141,6 @@ extern procps_status_t * procps_scan(int save_user_arg0)
curstatus.cmd = strdup(buf);
/* if NULL it work true also */
}
- fclose(fp);
}
return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
}