2002-10-22 17:51:15 +05:30
|
|
|
/* 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>
|
2006-04-26 00:12:23 +05:30
|
|
|
#include <sys/param.h>
|
2002-10-22 17:51:15 +05:30
|
|
|
#include <unistd.h>
|
2006-02-14 03:34:27 +05:30
|
|
|
#include <fcntl.h>
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2002-11-24 07:02:56 +05:30
|
|
|
#include "libbb.h"
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2006-02-14 03:34:27 +05:30
|
|
|
|
2006-02-14 15:47:09 +05:30
|
|
|
#define PROCPS_BUFSIZE 1024
|
|
|
|
|
|
|
|
static int read_to_buf(const char *filename, void *buf)
|
2006-02-14 03:34:27 +05:30
|
|
|
{
|
|
|
|
int fd;
|
2006-02-14 15:47:09 +05:30
|
|
|
ssize_t ret;
|
2006-02-14 03:34:27 +05:30
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if(fd < 0)
|
|
|
|
return -1;
|
2006-02-14 15:47:09 +05:30
|
|
|
ret = read(fd, buf, PROCPS_BUFSIZE);
|
2006-02-14 03:34:27 +05:30
|
|
|
close(fd);
|
2006-02-14 15:47:09 +05:30
|
|
|
return ret;
|
2006-02-14 03:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
procps_status_t * procps_scan(int save_user_arg0)
|
2002-10-22 17:51:15 +05:30
|
|
|
{
|
|
|
|
static DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
static procps_status_t ret_status;
|
|
|
|
char *name;
|
|
|
|
int n;
|
|
|
|
char status[32];
|
2006-02-14 03:34:27 +05:30
|
|
|
char *status_tail;
|
2006-02-14 15:47:09 +05:30
|
|
|
char buf[PROCPS_BUFSIZE];
|
2002-10-22 17:51:15 +05:30
|
|
|
procps_status_t curstatus;
|
|
|
|
int pid;
|
|
|
|
long tasknice;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (!dir) {
|
2006-04-12 13:05:12 +05:30
|
|
|
dir = bb_xopendir("/proc");
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2006-02-14 03:34:27 +05:30
|
|
|
status_tail = status + sprintf(status, "/proc/%d", pid);
|
2004-01-28 01:47:39 +05:30
|
|
|
if(stat(status, &sb))
|
|
|
|
continue;
|
2005-09-21 02:36:17 +05:30
|
|
|
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
|
2004-01-28 01:47:39 +05:30
|
|
|
|
2006-02-14 03:34:27 +05:30
|
|
|
strcpy(status_tail, "/stat");
|
2006-02-14 15:47:09 +05:30
|
|
|
n = read_to_buf(status, buf);
|
2006-02-14 03:34:27 +05:30
|
|
|
if(n < 0)
|
2002-10-22 17:51:15 +05:30
|
|
|
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 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 17:51:15 +05:30
|
|
|
"%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 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 17:51:15 +05:30
|
|
|
&curstatus.utime, &curstatus.stime,
|
|
|
|
#endif
|
|
|
|
&tasknice,
|
|
|
|
&curstatus.rss);
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-22 17:51:15 +05:30
|
|
|
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 17:06:44 +05:30
|
|
|
#ifdef PAGE_SHIFT
|
2002-10-22 17:51:15 +05:30
|
|
|
curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
|
2004-01-21 17:06:44 +05:30
|
|
|
#else
|
|
|
|
curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
|
|
|
|
#endif
|
2002-10-22 17:51:15 +05:30
|
|
|
|
|
|
|
if(save_user_arg0) {
|
2006-02-14 03:34:27 +05:30
|
|
|
strcpy(status_tail, "/cmdline");
|
2006-02-14 15:47:09 +05:30
|
|
|
n = read_to_buf(status, buf);
|
2006-02-14 03:34:27 +05:30
|
|
|
if(n > 0) {
|
2002-11-26 04:01:37 +05:30
|
|
|
if(buf[n-1]=='\n')
|
|
|
|
buf[--n] = 0;
|
|
|
|
name = buf;
|
|
|
|
while(n) {
|
2002-12-03 02:48:10 +05:30
|
|
|
if(((unsigned char)*name) < ' ')
|
2002-11-26 04:01:37 +05:30
|
|
|
*name = ' ';
|
|
|
|
name++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
*name = 0;
|
2002-10-22 17:51:15 +05:30
|
|
|
if(buf[0])
|
|
|
|
curstatus.cmd = strdup(buf);
|
|
|
|
/* if NULL it work true also */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
*/
|