2002-10-22 12:21:15 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
|
|
|
* Copyright 1998 by Albert Cahalan; all rights reserved.
|
|
|
|
* Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
|
|
|
|
* GNU Library General Public License Version 2, or any later version
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
|
2002-11-24 01:32:56 +00:00
|
|
|
#include "libbb.h"
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2005-05-03 06:25:50 +00:00
|
|
|
extern procps_status_t * procps_scan(int save_user_arg0)
|
2002-10-22 12:21:15 +00:00
|
|
|
{
|
|
|
|
static DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
static procps_status_t ret_status;
|
|
|
|
char *name;
|
|
|
|
int n;
|
|
|
|
char status[32];
|
|
|
|
char buf[1024];
|
|
|
|
FILE *fp;
|
|
|
|
procps_status_t curstatus;
|
|
|
|
int pid;
|
|
|
|
long tasknice;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (!dir) {
|
|
|
|
dir = opendir("/proc");
|
|
|
|
if(!dir)
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_error_msg_and_die("Can't open /proc");
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
|
|
|
for(;;) {
|
|
|
|
if((entry = readdir(dir)) == NULL) {
|
|
|
|
closedir(dir);
|
|
|
|
dir = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
name = entry->d_name;
|
|
|
|
if (!(*name >= '0' && *name <= '9'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memset(&curstatus, 0, sizeof(procps_status_t));
|
|
|
|
pid = atoi(name);
|
|
|
|
curstatus.pid = pid;
|
|
|
|
|
2004-01-27 20:17:39 +00:00
|
|
|
sprintf(status, "/proc/%d", pid);
|
|
|
|
if(stat(status, &sb))
|
|
|
|
continue;
|
2005-09-20 21:06:17 +00:00
|
|
|
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
|
2004-01-27 20:17:39 +00:00
|
|
|
|
2002-10-22 12:21:15 +00:00
|
|
|
sprintf(status, "/proc/%d/stat", pid);
|
2005-05-03 06:25:50 +00:00
|
|
|
|
2002-10-22 12:21:15 +00:00
|
|
|
if((fp = fopen(status, "r")) == NULL)
|
|
|
|
continue;
|
|
|
|
name = fgets(buf, sizeof(buf), fp);
|
|
|
|
fclose(fp);
|
|
|
|
if(name == NULL)
|
|
|
|
continue;
|
|
|
|
name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
|
|
|
|
if(name == 0 || name[1] != ' ')
|
|
|
|
continue;
|
|
|
|
*name = 0;
|
|
|
|
sscanf(buf, "%*s (%15c", curstatus.short_cmd);
|
|
|
|
n = sscanf(name+2,
|
|
|
|
"%c %d "
|
|
|
|
"%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
|
|
|
|
"%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
|
2005-09-16 12:55:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 12:21:15 +00:00
|
|
|
"%lu %lu "
|
|
|
|
#else
|
|
|
|
"%*s %*s "
|
|
|
|
#endif
|
|
|
|
"%*s %*s %*s " /* cutime, cstime, priority */
|
|
|
|
"%ld "
|
|
|
|
"%*s %*s %*s " /* timeout, it_real_value, start_time */
|
|
|
|
"%*s " /* vsize */
|
|
|
|
"%ld",
|
|
|
|
curstatus.state, &curstatus.ppid,
|
2005-09-16 12:55:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 12:21:15 +00:00
|
|
|
&curstatus.utime, &curstatus.stime,
|
|
|
|
#endif
|
|
|
|
&tasknice,
|
|
|
|
&curstatus.rss);
|
2005-09-16 12:55:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 12:21:15 +00:00
|
|
|
if(n != 6)
|
|
|
|
#else
|
|
|
|
if(n != 4)
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
|
|
|
|
curstatus.state[1] = 'W';
|
|
|
|
else
|
|
|
|
curstatus.state[1] = ' ';
|
|
|
|
if (tasknice < 0)
|
|
|
|
curstatus.state[2] = '<';
|
|
|
|
else if (tasknice > 0)
|
|
|
|
curstatus.state[2] = 'N';
|
|
|
|
else
|
|
|
|
curstatus.state[2] = ' ';
|
|
|
|
|
2004-01-21 11:36:44 +00:00
|
|
|
#ifdef PAGE_SHIFT
|
2002-10-22 12:21:15 +00:00
|
|
|
curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
|
2004-01-21 11:36:44 +00:00
|
|
|
#else
|
|
|
|
curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
|
|
|
|
#endif
|
2002-10-22 12:21:15 +00:00
|
|
|
|
|
|
|
if(save_user_arg0) {
|
2003-05-26 18:07:30 +00:00
|
|
|
sprintf(status, "/proc/%d/cmdline", pid);
|
2002-10-22 12:21:15 +00:00
|
|
|
if((fp = fopen(status, "r")) == NULL)
|
|
|
|
continue;
|
2002-11-25 22:31:37 +00:00
|
|
|
if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
|
|
|
|
if(buf[n-1]=='\n')
|
|
|
|
buf[--n] = 0;
|
|
|
|
name = buf;
|
|
|
|
while(n) {
|
2002-12-02 21:18:10 +00:00
|
|
|
if(((unsigned char)*name) < ' ')
|
2002-11-25 22:31:37 +00:00
|
|
|
*name = ' ';
|
|
|
|
name++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
*name = 0;
|
2002-10-22 12:21:15 +00:00
|
|
|
if(buf[0])
|
|
|
|
curstatus.cmd = strdup(buf);
|
|
|
|
/* if NULL it work true also */
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* END CODE */
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
c-file-style: "linux"
|
|
|
|
c-basic-offset: 4
|
|
|
|
tab-width: 4
|
|
|
|
End:
|
|
|
|
*/
|