Cleanups from Denis Vlasenko.

This commit is contained in:
Rob Landley 2006-02-13 22:04:27 +00:00
parent 90632d021c
commit b2804551a0
2 changed files with 175 additions and 166 deletions

View File

@ -13,9 +13,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <asm/page.h> #include <asm/page.h>
#include <fcntl.h>
#include "libbb.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) extern procps_status_t * procps_scan(int save_user_arg0)
{ {
static DIR *dir; static DIR *dir;
@ -24,8 +39,8 @@ extern procps_status_t * procps_scan(int save_user_arg0)
char *name; char *name;
int n; int n;
char status[32]; char status[32];
char *status_tail;
char buf[1024]; char buf[1024];
FILE *fp;
procps_status_t curstatus; procps_status_t curstatus;
int pid; int pid;
long tasknice; long tasknice;
@ -50,18 +65,14 @@ extern procps_status_t * procps_scan(int save_user_arg0)
pid = atoi(name); pid = atoi(name);
curstatus.pid = pid; curstatus.pid = pid;
sprintf(status, "/proc/%d", pid); status_tail = status + sprintf(status, "/proc/%d", pid);
if(stat(status, &sb)) if(stat(status, &sb))
continue; continue;
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
sprintf(status, "/proc/%d/stat", pid); strcpy(status_tail, "/stat");
n = read_to_buf(status, buf, sizeof(buf));
if((fp = fopen(status, "r")) == NULL) if(n < 0)
continue;
name = fgets(buf, sizeof(buf), fp);
fclose(fp);
if(name == NULL)
continue; continue;
name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
if(name == 0 || name[1] != ' ') if(name == 0 || name[1] != ' ')
@ -113,10 +124,9 @@ extern procps_status_t * procps_scan(int save_user_arg0)
#endif #endif
if(save_user_arg0) { if(save_user_arg0) {
sprintf(status, "/proc/%d/cmdline", pid); strcpy(status_tail, "/cmdline");
if((fp = fopen(status, "r")) == NULL) n = read_to_buf(status, buf, sizeof(buf));
continue; if(n > 0) {
if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
if(buf[n-1]=='\n') if(buf[n-1]=='\n')
buf[--n] = 0; buf[--n] = 0;
name = buf; name = buf;
@ -131,7 +141,6 @@ extern procps_status_t * procps_scan(int save_user_arg0)
curstatus.cmd = strdup(buf); curstatus.cmd = strdup(buf);
/* if NULL it work true also */ /* if NULL it work true also */
} }
fclose(fp);
} }
return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
} }

View File

@ -130,26 +130,29 @@ static unsigned long Hertz;
* *
*/ */
#define FILE_TO_BUF(filename, fd) do{ \ static int file_to_buf(char *buf, int bufsize, char *filename, int *d)
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ {
bb_perror_msg_and_die("/proc not be mounted?"); \ int fd = *d;
} \ int sz;
lseek(fd, 0L, SEEK_SET); \
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
bb_perror_msg_and_die("%s", filename); \
} \
buf[local_n] = '\0'; \
}while(0)
#define FILE_TO_BUF2(filename, fd) do{ \ if (fd == -1) {
lseek(fd, 0L, SEEK_SET); \ fd = open(filename, O_RDONLY);
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ if(fd == -1)
bb_perror_msg_and_die("%s", filename); \ bb_perror_msg_and_die("is /proc mounted?");
} \ } else {
buf[local_n] = '\0'; \ lseek(fd, 0L, SEEK_SET);
}while(0) }
sz = read(fd, buf, bufsize - 1);
if (sz < 0) {
bb_perror_msg_and_die("%s", filename);
}
buf[sz] = '\0';
*d = fd;
return sz;
}
static void init_Hertz_value(void) { static void init_Hertz_value(void)
{
unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */ unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
double up_1, up_2, seconds; double up_1, up_2, seconds;
unsigned long jiffies, h; unsigned long jiffies, h;
@ -160,17 +163,15 @@ static void init_Hertz_value(void) {
long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
if(smp_num_cpus<1) smp_num_cpus=1; if(smp_num_cpus<1) smp_num_cpus=1;
do {
int local_n;
FILE_TO_BUF("uptime", uptime_fd); do {
file_to_buf(buf, sizeof(buf), "uptime", &uptime_fd);
up_1 = strtod(buf, 0); up_1 = strtod(buf, 0);
FILE_TO_BUF("stat", stat_fd); file_to_buf(buf, sizeof(buf), "stat", &stat_fd);
sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j); sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j);
FILE_TO_BUF2("uptime", uptime_fd); file_to_buf(buf, sizeof(buf), "uptime", &uptime_fd);
up_2 = strtod(buf, 0); up_2 = strtod(buf, 0);
} while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */ } while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
close(uptime_fd); close(uptime_fd);
close(stat_fd); close(stat_fd);
@ -265,7 +266,6 @@ static void do_stats(void)
if (i > 999) if (i > 999)
i = 999; i = 999;
cur->pcpu = i; cur->pcpu = i;
} }
/* /*
@ -335,7 +335,7 @@ static unsigned long display_generic(void)
/* read load average */ /* read load average */
fp = bb_xfopen("loadavg", "r"); fp = bb_xfopen("loadavg", "r");
if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) { if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) {
bb_error_msg_and_die("failed to read '%s'", "loadavg"); bb_error_msg_and_die("failed to read 'loadavg'");
} }
fclose(fp); fclose(fp);
@ -514,7 +514,7 @@ int top_main(int argc, char **argv)
memcpy(top + n, p, sizeof(procps_status_t)); memcpy(top + n, p, sizeof(procps_status_t));
} }
if (ntop == 0) { if (ntop == 0) {
bb_perror_msg_and_die("scandir('/proc')"); bb_error_msg_and_die("Can't find process info in /proc");
} }
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if(!Hertz) { if(!Hertz) {