diff options
author | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-02-13 22:04:27 +0000 |
---|---|---|
committer | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-02-13 22:04:27 +0000 |
commit | ed909e8d3d9122ca382fd25bbadfc2fc3a284343 (patch) | |
tree | 9ca7e94ed410676810005e9d95ceaee6a70c6518 /libbb | |
parent | ab5f23fc263de26cd0925c4644c06476eb934e4f (diff) | |
download | busybox-w32-ed909e8d3d9122ca382fd25bbadfc2fc3a284343.tar.gz busybox-w32-ed909e8d3d9122ca382fd25bbadfc2fc3a284343.tar.bz2 busybox-w32-ed909e8d3d9122ca382fd25bbadfc2fc3a284343.zip |
Cleanups from Denis Vlasenko.
git-svn-id: svn://busybox.net/trunk/busybox@13957 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/procps.c | 37 |
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 @@ | |||
13 | #include <stdlib.h> | 13 | #include <stdlib.h> |
14 | #include <unistd.h> | 14 | #include <unistd.h> |
15 | #include <asm/page.h> | 15 | #include <asm/page.h> |
16 | #include <fcntl.h> | ||
16 | 17 | ||
17 | #include "libbb.h" | 18 | #include "libbb.h" |
18 | 19 | ||
20 | |||
21 | static int read_to_buf(char *filename, void *buf, int bufsize) | ||
22 | { | ||
23 | int fd; | ||
24 | |||
25 | fd = open(filename, O_RDONLY); | ||
26 | if(fd < 0) | ||
27 | return -1; | ||
28 | bufsize = read(fd, buf, bufsize); | ||
29 | close(fd); | ||
30 | return bufsize; | ||
31 | } | ||
32 | |||
33 | |||
19 | extern procps_status_t * procps_scan(int save_user_arg0) | 34 | extern procps_status_t * procps_scan(int save_user_arg0) |
20 | { | 35 | { |
21 | static DIR *dir; | 36 | static DIR *dir; |
@@ -24,8 +39,8 @@ extern procps_status_t * procps_scan(int save_user_arg0) | |||
24 | char *name; | 39 | char *name; |
25 | int n; | 40 | int n; |
26 | char status[32]; | 41 | char status[32]; |
42 | char *status_tail; | ||
27 | char buf[1024]; | 43 | char buf[1024]; |
28 | FILE *fp; | ||
29 | procps_status_t curstatus; | 44 | procps_status_t curstatus; |
30 | int pid; | 45 | int pid; |
31 | long tasknice; | 46 | long tasknice; |
@@ -50,18 +65,14 @@ extern procps_status_t * procps_scan(int save_user_arg0) | |||
50 | pid = atoi(name); | 65 | pid = atoi(name); |
51 | curstatus.pid = pid; | 66 | curstatus.pid = pid; |
52 | 67 | ||
53 | sprintf(status, "/proc/%d", pid); | 68 | status_tail = status + sprintf(status, "/proc/%d", pid); |
54 | if(stat(status, &sb)) | 69 | if(stat(status, &sb)) |
55 | continue; | 70 | continue; |
56 | bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); | 71 | bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); |
57 | 72 | ||
58 | sprintf(status, "/proc/%d/stat", pid); | 73 | strcpy(status_tail, "/stat"); |
59 | 74 | n = read_to_buf(status, buf, sizeof(buf)); | |
60 | if((fp = fopen(status, "r")) == NULL) | 75 | if(n < 0) |
61 | continue; | ||
62 | name = fgets(buf, sizeof(buf), fp); | ||
63 | fclose(fp); | ||
64 | if(name == NULL) | ||
65 | continue; | 76 | continue; |
66 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ | 77 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ |
67 | if(name == 0 || name[1] != ' ') | 78 | if(name == 0 || name[1] != ' ') |
@@ -113,10 +124,9 @@ extern procps_status_t * procps_scan(int save_user_arg0) | |||
113 | #endif | 124 | #endif |
114 | 125 | ||
115 | if(save_user_arg0) { | 126 | if(save_user_arg0) { |
116 | sprintf(status, "/proc/%d/cmdline", pid); | 127 | strcpy(status_tail, "/cmdline"); |
117 | if((fp = fopen(status, "r")) == NULL) | 128 | n = read_to_buf(status, buf, sizeof(buf)); |
118 | continue; | 129 | if(n > 0) { |
119 | if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) { | ||
120 | if(buf[n-1]=='\n') | 130 | if(buf[n-1]=='\n') |
121 | buf[--n] = 0; | 131 | buf[--n] = 0; |
122 | name = buf; | 132 | name = buf; |
@@ -131,7 +141,6 @@ extern procps_status_t * procps_scan(int save_user_arg0) | |||
131 | curstatus.cmd = strdup(buf); | 141 | curstatus.cmd = strdup(buf); |
132 | /* if NULL it work true also */ | 142 | /* if NULL it work true also */ |
133 | } | 143 | } |
134 | fclose(fp); | ||
135 | } | 144 | } |
136 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); | 145 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); |
137 | } | 146 | } |