Cleanups from Denis Vlasenko.
This commit is contained in:
parent
90632d021c
commit
b2804551a0
@ -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));
|
||||
}
|
||||
|
58
procps/top.c
58
procps/top.c
@ -130,26 +130,29 @@ static unsigned long Hertz;
|
||||
*
|
||||
*/
|
||||
|
||||
#define FILE_TO_BUF(filename, fd) do{ \
|
||||
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
|
||||
bb_perror_msg_and_die("/proc not be mounted?"); \
|
||||
} \
|
||||
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)
|
||||
static int file_to_buf(char *buf, int bufsize, char *filename, int *d)
|
||||
{
|
||||
int fd = *d;
|
||||
int sz;
|
||||
|
||||
#define FILE_TO_BUF2(filename, fd) do{ \
|
||||
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)
|
||||
if (fd == -1) {
|
||||
fd = open(filename, O_RDONLY);
|
||||
if(fd == -1)
|
||||
bb_perror_msg_and_die("is /proc mounted?");
|
||||
} else {
|
||||
lseek(fd, 0L, SEEK_SET);
|
||||
}
|
||||
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) */
|
||||
double up_1, up_2, seconds;
|
||||
unsigned long jiffies, h;
|
||||
@ -160,17 +163,15 @@ static void init_Hertz_value(void) {
|
||||
long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||
|
||||
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);
|
||||
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);
|
||||
FILE_TO_BUF2("uptime", uptime_fd);
|
||||
file_to_buf(buf, sizeof(buf), "uptime", &uptime_fd);
|
||||
up_2 = strtod(buf, 0);
|
||||
} while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
|
||||
|
||||
close(uptime_fd);
|
||||
close(stat_fd);
|
||||
|
||||
@ -178,7 +179,7 @@ static void init_Hertz_value(void) {
|
||||
seconds = (up_1 + up_2) / 2;
|
||||
h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus );
|
||||
/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
|
||||
switch(h){
|
||||
switch(h) {
|
||||
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
|
||||
case 48 ... 52 : Hertz = 50; break;
|
||||
case 58 ... 62 : Hertz = 60; break;
|
||||
@ -265,7 +266,6 @@ static void do_stats(void)
|
||||
if (i > 999)
|
||||
i = 999;
|
||||
cur->pcpu = i;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@ -335,7 +335,7 @@ static unsigned long display_generic(void)
|
||||
/* read load average */
|
||||
fp = bb_xfopen("loadavg", "r");
|
||||
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);
|
||||
|
||||
@ -428,7 +428,7 @@ static void reset_term(void)
|
||||
#endif /* CONFIG_FEATURE_CLEAN_UP */
|
||||
}
|
||||
|
||||
static void sig_catcher (int sig ATTRIBUTE_UNUSED)
|
||||
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
|
||||
{
|
||||
reset_term();
|
||||
}
|
||||
@ -514,7 +514,7 @@ int top_main(int argc, char **argv)
|
||||
memcpy(top + n, p, sizeof(procps_status_t));
|
||||
}
|
||||
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
|
||||
if(!Hertz) {
|
||||
|
Loading…
Reference in New Issue
Block a user