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>
|
2007-06-06 13:10:16 +05:30
|
|
|
* SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
2007-07-18 03:12:59 +05:30
|
|
|
*
|
2006-05-20 00:59:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
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-11-24 20:27:31 +05:30
|
|
|
typedef struct unsigned_to_name_map_t {
|
|
|
|
unsigned id;
|
2007-01-20 02:54:17 +05:30
|
|
|
char name[USERNAME_MAX_SIZE];
|
2006-11-24 20:27:31 +05:30
|
|
|
} unsigned_to_name_map_t;
|
2006-11-05 06:13:51 +05:30
|
|
|
|
2006-11-24 20:27:31 +05:30
|
|
|
typedef struct cache_t {
|
|
|
|
unsigned_to_name_map_t *cache;
|
|
|
|
int size;
|
|
|
|
} cache_t;
|
2006-11-05 06:13:51 +05:30
|
|
|
|
2006-11-24 20:27:31 +05:30
|
|
|
static cache_t username, groupname;
|
|
|
|
|
|
|
|
static void clear_cache(cache_t *cp)
|
|
|
|
{
|
|
|
|
free(cp->cache);
|
|
|
|
cp->cache = NULL;
|
|
|
|
cp->size = 0;
|
|
|
|
}
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC clear_username_cache(void)
|
2006-11-05 06:13:51 +05:30
|
|
|
{
|
2006-11-24 20:27:31 +05:30
|
|
|
clear_cache(&username);
|
|
|
|
clear_cache(&groupname);
|
2006-11-05 06:13:51 +05:30
|
|
|
}
|
|
|
|
|
2006-11-24 20:29:45 +05:30
|
|
|
#if 0 /* more generic, but we don't need that yet */
|
2006-11-24 20:27:31 +05:30
|
|
|
/* Returns -N-1 if not found. */
|
|
|
|
/* cp->cache[N] is allocated and must be filled in this case */
|
|
|
|
static int get_cached(cache_t *cp, unsigned id)
|
2006-11-05 06:13:51 +05:30
|
|
|
{
|
|
|
|
int i;
|
2006-11-24 20:27:31 +05:30
|
|
|
for (i = 0; i < cp->size; i++)
|
|
|
|
if (cp->cache[i].id == id)
|
|
|
|
return i;
|
2008-08-04 18:50:36 +05:30
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc_vector(cp->cache, 2, i);
|
2006-11-24 20:27:31 +05:30
|
|
|
cp->cache[i++].id = id;
|
|
|
|
return -i;
|
|
|
|
}
|
2006-11-24 20:29:45 +05:30
|
|
|
#endif
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
typedef char* FAST_FUNC ug_func(char *name, int bufsize, long uid);
|
2006-11-24 20:29:45 +05:30
|
|
|
static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cp->size; i++)
|
|
|
|
if (cp->cache[i].id == id)
|
|
|
|
return cp->cache[i].name;
|
2008-08-04 18:50:36 +05:30
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc_vector(cp->cache, 2, i);
|
2006-11-24 20:29:45 +05:30
|
|
|
cp->cache[i].id = id;
|
2007-07-27 16:50:10 +05:30
|
|
|
/* Never fails. Generates numeric string if name isn't found */
|
|
|
|
fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
|
2006-11-24 20:29:45 +05:30
|
|
|
return cp->cache[i].name;
|
|
|
|
}
|
2008-06-27 08:22:20 +05:30
|
|
|
const char* FAST_FUNC get_cached_username(uid_t uid)
|
2006-11-24 20:27:31 +05:30
|
|
|
{
|
2006-11-24 20:29:45 +05:30
|
|
|
return get_cached(&username, uid, bb_getpwuid);
|
2006-11-24 20:27:31 +05:30
|
|
|
}
|
2008-06-27 08:22:20 +05:30
|
|
|
const char* FAST_FUNC get_cached_groupname(gid_t gid)
|
2006-11-24 20:27:31 +05:30
|
|
|
{
|
2006-11-24 20:29:45 +05:30
|
|
|
return get_cached(&groupname, gid, bb_getgrgid);
|
2006-11-05 06:13:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 15:47:09 +05:30
|
|
|
#define PROCPS_BUFSIZE 1024
|
|
|
|
|
2008-07-18 00:09:36 +05:30
|
|
|
static int read_to_buf(const char *filename, void *buf)
|
2006-02-14 03:34:27 +05:30
|
|
|
{
|
2007-06-10 22:41:59 +05:30
|
|
|
int fd;
|
|
|
|
/* open_read_close() would do two reads, checking for EOF.
|
|
|
|
* When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
|
|
|
|
ssize_t ret = -1;
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
ret = read(fd, buf, PROCPS_BUFSIZE-1);
|
|
|
|
close(fd);
|
|
|
|
}
|
2006-10-14 07:53:43 +05:30
|
|
|
((char *)buf)[ret > 0 ? ret : 0] = '\0';
|
2006-02-14 15:47:09 +05:30
|
|
|
return ret;
|
2006-02-14 03:34:27 +05:30
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
static procps_status_t* FAST_FUNC alloc_procps_scan(void)
|
2006-11-05 06:13:51 +05:30
|
|
|
{
|
2007-08-26 23:53:13 +05:30
|
|
|
unsigned n = getpagesize();
|
2006-11-05 06:13:51 +05:30
|
|
|
procps_status_t* sp = xzalloc(sizeof(procps_status_t));
|
|
|
|
sp->dir = xopendir("/proc");
|
2007-08-26 23:53:13 +05:30
|
|
|
while (1) {
|
|
|
|
n >>= 1;
|
|
|
|
if (!n) break;
|
|
|
|
sp->shift_pages_to_bytes++;
|
|
|
|
}
|
|
|
|
sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10;
|
2006-11-05 06:13:51 +05:30
|
|
|
return sp;
|
|
|
|
}
|
2006-02-14 03:34:27 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC free_procps_scan(procps_status_t* sp)
|
2002-10-22 17:51:15 +05:30
|
|
|
{
|
2006-11-05 06:13:51 +05:30
|
|
|
closedir(sp->dir);
|
2007-06-30 20:17:41 +05:30
|
|
|
free(sp->argv0);
|
2007-06-06 13:10:16 +05:30
|
|
|
USE_SELINUX(free(sp->context);)
|
2006-11-05 06:13:51 +05:30
|
|
|
free(sp);
|
|
|
|
}
|
2006-11-01 14:46:49 +05:30
|
|
|
|
2007-09-08 22:21:19 +05:30
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
static unsigned long fast_strtoul_16(char **endptr)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
char *str = *endptr;
|
|
|
|
unsigned long n = 0;
|
|
|
|
|
|
|
|
while ((c = *str++) != ' ') {
|
|
|
|
c = ((c|0x20) - '0');
|
|
|
|
if (c > 9)
|
|
|
|
// c = c + '0' - 'a' + 10:
|
|
|
|
c = c - ('a' - '0' - 10);
|
|
|
|
n = n*16 + c;
|
|
|
|
}
|
|
|
|
*endptr = str; /* We skip trailing space! */
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
/* TOPMEM uses fast_strtoul_10, so... */
|
|
|
|
#undef ENABLE_FEATURE_FAST_TOP
|
|
|
|
#define ENABLE_FEATURE_FAST_TOP 1
|
|
|
|
#endif
|
|
|
|
|
2007-06-19 19:16:24 +05:30
|
|
|
#if ENABLE_FEATURE_FAST_TOP
|
|
|
|
/* We cut a lot of corners here for speed */
|
2007-06-30 13:31:04 +05:30
|
|
|
static unsigned long fast_strtoul_10(char **endptr)
|
2007-06-19 19:16:24 +05:30
|
|
|
{
|
|
|
|
char c;
|
2007-06-30 13:31:04 +05:30
|
|
|
char *str = *endptr;
|
2007-06-19 19:16:24 +05:30
|
|
|
unsigned long n = *str - '0';
|
|
|
|
|
|
|
|
while ((c = *++str) != ' ')
|
|
|
|
n = n*10 + (c - '0');
|
|
|
|
|
|
|
|
*endptr = str + 1; /* We skip trailing space! */
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
static char *skip_fields(char *str, int count)
|
|
|
|
{
|
|
|
|
do {
|
2007-06-30 13:31:04 +05:30
|
|
|
while (*str++ != ' ')
|
|
|
|
continue;
|
|
|
|
/* we found a space char, str points after it */
|
2007-06-19 19:16:24 +05:30
|
|
|
} while (--count);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
void BUG_comm_size(void);
|
2008-06-27 08:22:20 +05:30
|
|
|
procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
2006-11-05 06:13:51 +05:30
|
|
|
{
|
2006-11-01 14:46:49 +05:30
|
|
|
struct dirent *entry;
|
2006-02-14 15:47:09 +05:30
|
|
|
char buf[PROCPS_BUFSIZE];
|
2006-11-05 06:13:51 +05:30
|
|
|
char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
|
|
|
|
char *filename_tail;
|
2002-10-22 17:51:15 +05:30
|
|
|
long tasknice;
|
2006-11-05 06:13:51 +05:30
|
|
|
unsigned pid;
|
2006-11-02 01:50:37 +05:30
|
|
|
int n;
|
2002-10-22 17:51:15 +05:30
|
|
|
struct stat sb;
|
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
if (!sp)
|
2008-03-17 14:30:54 +05:30
|
|
|
sp = alloc_procps_scan();
|
2006-11-05 06:13:51 +05:30
|
|
|
|
2006-07-13 01:34:00 +05:30
|
|
|
for (;;) {
|
2006-11-05 06:13:51 +05:30
|
|
|
entry = readdir(sp->dir);
|
2006-10-13 00:59:44 +05:30
|
|
|
if (entry == NULL) {
|
2006-11-05 06:13:51 +05:30
|
|
|
free_procps_scan(sp);
|
|
|
|
return NULL;
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
2006-11-27 20:13:21 +05:30
|
|
|
pid = bb_strtou(entry->d_name, NULL, 10);
|
|
|
|
if (errno)
|
2002-10-22 17:51:15 +05:30
|
|
|
continue;
|
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
/* After this point we have to break, not continue
|
|
|
|
* ("continue" would mean that current /proc/NNN
|
|
|
|
* is not a valid process info) */
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2007-02-08 13:51:58 +05:30
|
|
|
memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
|
2004-01-28 01:47:39 +05:30
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
sp->pid = pid;
|
|
|
|
if (!(flags & ~PSSCAN_PID)) break;
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2007-06-06 13:10:16 +05:30
|
|
|
#if ENABLE_SELINUX
|
|
|
|
if (flags & PSSCAN_CONTEXT) {
|
|
|
|
if (getpidcon(sp->pid, &sp->context) < 0)
|
|
|
|
sp->context = NULL;
|
2007-07-20 04:20:47 +05:30
|
|
|
}
|
|
|
|
#endif
|
2007-06-06 13:10:16 +05:30
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
filename_tail = filename + sprintf(filename, "/proc/%d", pid);
|
|
|
|
|
|
|
|
if (flags & PSSCAN_UIDGID) {
|
|
|
|
if (stat(filename, &sb))
|
|
|
|
break;
|
2007-01-20 02:54:17 +05:30
|
|
|
/* Need comment - is this effective or real UID/GID? */
|
2006-11-05 06:13:51 +05:30
|
|
|
sp->uid = sb.st_uid;
|
|
|
|
sp->gid = sb.st_gid;
|
|
|
|
}
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
if (flags & PSSCAN_STAT) {
|
2007-06-30 13:31:04 +05:30
|
|
|
char *cp, *comm1;
|
|
|
|
int tty;
|
2007-06-19 19:16:24 +05:30
|
|
|
#if !ENABLE_FEATURE_FAST_TOP
|
2007-04-19 20:16:14 +05:30
|
|
|
unsigned long vsz, rss;
|
2007-06-19 19:16:24 +05:30
|
|
|
#endif
|
2007-04-19 20:16:14 +05:30
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
/* see proc(5) for some details on this */
|
|
|
|
strcpy(filename_tail, "/stat");
|
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n < 0)
|
|
|
|
break;
|
|
|
|
cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
|
2007-06-30 13:31:04 +05:30
|
|
|
/*if (!cp || cp[1] != ' ')
|
|
|
|
break;*/
|
2006-11-05 06:13:51 +05:30
|
|
|
cp[0] = '\0';
|
|
|
|
if (sizeof(sp->comm) < 16)
|
|
|
|
BUG_comm_size();
|
2007-06-30 13:31:04 +05:30
|
|
|
comm1 = strchr(buf, '(');
|
|
|
|
/*if (comm1)*/
|
|
|
|
safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
|
2007-06-19 19:16:24 +05:30
|
|
|
|
|
|
|
#if !ENABLE_FEATURE_FAST_TOP
|
2006-11-05 06:13:51 +05:30
|
|
|
n = sscanf(cp+2,
|
|
|
|
"%c %u " /* state, ppid */
|
2007-04-19 20:16:14 +05:30
|
|
|
"%u %u %d %*s " /* pgid, sid, tty, tpgid */
|
2006-11-05 06:13:51 +05:30
|
|
|
"%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
|
|
|
|
"%lu %lu " /* utime, stime */
|
|
|
|
"%*s %*s %*s " /* cutime, cstime, priority */
|
|
|
|
"%ld " /* nice */
|
2008-01-05 08:56:41 +05:30
|
|
|
"%*s %*s " /* timeout, it_real_value */
|
|
|
|
"%lu " /* start_time */
|
2007-04-19 20:16:14 +05:30
|
|
|
"%lu " /* vsize */
|
|
|
|
"%lu " /* rss */
|
|
|
|
/* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
|
|
|
|
/* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
|
|
|
|
/* "%lu %lu %lu" wchan, nswap, cnswap */
|
|
|
|
,
|
2006-11-05 06:13:51 +05:30
|
|
|
sp->state, &sp->ppid,
|
2007-04-19 20:16:14 +05:30
|
|
|
&sp->pgid, &sp->sid, &tty,
|
2006-11-05 06:13:51 +05:30
|
|
|
&sp->utime, &sp->stime,
|
|
|
|
&tasknice,
|
2008-01-05 08:56:41 +05:30
|
|
|
&sp->start_time,
|
2007-04-19 20:16:14 +05:30
|
|
|
&vsz,
|
|
|
|
&rss);
|
2008-04-26 04:50:25 +05:30
|
|
|
if (n != 11)
|
2006-11-05 06:13:51 +05:30
|
|
|
break;
|
2007-08-26 23:53:13 +05:30
|
|
|
/* vsz is in bytes and we want kb */
|
|
|
|
sp->vsz = vsz >> 10;
|
|
|
|
/* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
|
|
|
|
sp->rss = rss << sp->shift_pages_to_kb;
|
2007-06-19 20:13:17 +05:30
|
|
|
sp->tty_major = (tty >> 8) & 0xfff;
|
2007-06-21 12:39:18 +05:30
|
|
|
sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
|
2007-06-19 19:16:24 +05:30
|
|
|
#else
|
|
|
|
/* This costs ~100 bytes more but makes top faster by 20%
|
|
|
|
* If you run 10000 processes, this may be important for you */
|
2007-06-19 20:13:17 +05:30
|
|
|
sp->state[0] = cp[2];
|
2007-06-30 13:31:04 +05:30
|
|
|
cp += 4;
|
|
|
|
sp->ppid = fast_strtoul_10(&cp);
|
|
|
|
sp->pgid = fast_strtoul_10(&cp);
|
|
|
|
sp->sid = fast_strtoul_10(&cp);
|
|
|
|
tty = fast_strtoul_10(&cp);
|
2007-06-19 20:13:17 +05:30
|
|
|
sp->tty_major = (tty >> 8) & 0xfff;
|
|
|
|
sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
|
2007-06-19 19:16:24 +05:30
|
|
|
cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
|
2007-06-30 13:31:04 +05:30
|
|
|
sp->utime = fast_strtoul_10(&cp);
|
|
|
|
sp->stime = fast_strtoul_10(&cp);
|
2007-06-19 19:16:24 +05:30
|
|
|
cp = skip_fields(cp, 3); /* cutime, cstime, priority */
|
2007-06-30 13:31:04 +05:30
|
|
|
tasknice = fast_strtoul_10(&cp);
|
2008-01-05 08:56:41 +05:30
|
|
|
cp = skip_fields(cp, 2); /* timeout, it_real_value */
|
|
|
|
sp->start_time = fast_strtoul_10(&cp);
|
2007-08-26 23:53:13 +05:30
|
|
|
/* vsz is in bytes and we want kb */
|
|
|
|
sp->vsz = fast_strtoul_10(&cp) >> 10;
|
|
|
|
/* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
|
|
|
|
sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
|
2007-06-19 19:16:24 +05:30
|
|
|
#endif
|
|
|
|
|
2007-02-08 13:51:58 +05:30
|
|
|
if (sp->vsz == 0 && sp->state[0] != 'Z')
|
2006-11-05 06:13:51 +05:30
|
|
|
sp->state[1] = 'W';
|
|
|
|
else
|
|
|
|
sp->state[1] = ' ';
|
|
|
|
if (tasknice < 0)
|
|
|
|
sp->state[2] = '<';
|
2007-04-19 20:16:14 +05:30
|
|
|
else if (tasknice) /* > 0 */
|
2006-11-05 06:13:51 +05:30
|
|
|
sp->state[2] = 'N';
|
|
|
|
else
|
|
|
|
sp->state[2] = ' ';
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
}
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2007-09-08 22:21:19 +05:30
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
if (flags & (PSSCAN_SMAPS)) {
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
strcpy(filename_tail, "/smaps");
|
2008-07-22 04:35:26 +05:30
|
|
|
file = fopen_for_read(filename);
|
2007-09-08 22:21:19 +05:30
|
|
|
if (!file)
|
|
|
|
break;
|
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
unsigned long sz;
|
|
|
|
char *tp;
|
|
|
|
char w;
|
|
|
|
#define SCAN(str, name) \
|
|
|
|
if (strncmp(buf, str, sizeof(str)-1) == 0) { \
|
|
|
|
tp = skip_whitespace(buf + sizeof(str)-1); \
|
|
|
|
sp->name += fast_strtoul_10(&tp); \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
SCAN("Shared_Clean:" , shared_clean );
|
|
|
|
SCAN("Shared_Dirty:" , shared_dirty );
|
|
|
|
SCAN("Private_Clean:", private_clean);
|
|
|
|
SCAN("Private_Dirty:", private_dirty);
|
|
|
|
#undef SCAN
|
|
|
|
// f7d29000-f7d39000 rw-s ADR M:m OFS FILE
|
|
|
|
tp = strchr(buf, '-');
|
|
|
|
if (tp) {
|
|
|
|
*tp = ' ';
|
|
|
|
tp = buf;
|
|
|
|
sz = fast_strtoul_16(&tp); /* start */
|
|
|
|
sz = (fast_strtoul_16(&tp) - sz) >> 10; /* end - start */
|
|
|
|
// tp -> "rw-s" string
|
|
|
|
w = tp[1];
|
|
|
|
// skipping "rw-s ADR M:m OFS "
|
|
|
|
tp = skip_whitespace(skip_fields(tp, 4));
|
|
|
|
// filter out /dev/something (something != zero)
|
|
|
|
if (strncmp(tp, "/dev/", 5) != 0 || strcmp(tp, "/dev/zero\n") == 0) {
|
|
|
|
if (w == 'w') {
|
|
|
|
sp->mapped_rw += sz;
|
|
|
|
} else if (w == '-') {
|
|
|
|
sp->mapped_ro += sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//else printf("DROPPING %s (%s)\n", buf, tp);
|
|
|
|
if (strcmp(tp, "[stack]\n") == 0)
|
|
|
|
sp->stack += sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
#endif /* TOPMEM */
|
|
|
|
|
2007-06-30 20:17:41 +05:30
|
|
|
#if 0 /* PSSCAN_CMD is not used */
|
2007-06-30 13:33:26 +05:30
|
|
|
if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
|
2007-09-29 03:43:55 +05:30
|
|
|
free(sp->argv0);
|
|
|
|
sp->argv0 = NULL;
|
|
|
|
free(sp->cmd);
|
|
|
|
sp->cmd = NULL;
|
2006-11-05 06:13:51 +05:30
|
|
|
strcpy(filename_tail, "/cmdline");
|
2007-06-30 13:33:26 +05:30
|
|
|
/* TODO: to get rid of size limits, read into malloc buf,
|
|
|
|
* then realloc it down to real size. */
|
2006-11-05 06:13:51 +05:30
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2007-06-30 13:33:26 +05:30
|
|
|
if (flags & PSSCAN_ARGV0)
|
|
|
|
sp->argv0 = xstrdup(buf);
|
|
|
|
if (flags & PSSCAN_CMD) {
|
|
|
|
do {
|
|
|
|
n--;
|
|
|
|
if ((unsigned char)(buf[n]) < ' ')
|
|
|
|
buf[n] = ' ';
|
|
|
|
} while (n);
|
|
|
|
sp->cmd = xstrdup(buf);
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
|
|
|
}
|
2007-06-30 20:17:41 +05:30
|
|
|
#else
|
2007-09-30 04:19:26 +05:30
|
|
|
if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {
|
2007-09-29 03:43:55 +05:30
|
|
|
free(sp->argv0);
|
|
|
|
sp->argv0 = NULL;
|
2007-06-30 20:17:41 +05:30
|
|
|
strcpy(filename_tail, "/cmdline");
|
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2007-09-30 03:56:01 +05:30
|
|
|
if (flags & PSSCAN_ARGVN) {
|
2008-07-18 00:09:36 +05:30
|
|
|
sp->argv_len = n;
|
|
|
|
sp->argv0 = xmalloc(n + 1);
|
|
|
|
memcpy(sp->argv0, buf, n + 1);
|
|
|
|
/* sp->argv0[n] = '\0'; - buf has it */
|
|
|
|
} else {
|
|
|
|
sp->argv_len = 0;
|
|
|
|
sp->argv0 = xstrdup(buf);
|
2007-09-30 03:56:01 +05:30
|
|
|
}
|
2007-06-30 20:17:41 +05:30
|
|
|
}
|
|
|
|
#endif
|
2006-11-05 06:13:51 +05:30
|
|
|
break;
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
2006-11-05 06:13:51 +05:30
|
|
|
return sp;
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
2007-06-30 20:17:41 +05:30
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
|
2007-06-30 20:17:41 +05:30
|
|
|
{
|
|
|
|
ssize_t sz;
|
|
|
|
char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
|
|
|
|
|
|
|
|
sprintf(filename, "/proc/%u/cmdline", pid);
|
|
|
|
sz = open_read_close(filename, buf, col);
|
|
|
|
if (sz > 0) {
|
|
|
|
buf[sz] = '\0';
|
|
|
|
while (--sz >= 0)
|
|
|
|
if ((unsigned char)(buf[sz]) < ' ')
|
|
|
|
buf[sz] = ' ';
|
|
|
|
} else {
|
|
|
|
snprintf(buf, col, "[%s]", comm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 06:13:51 +05:30
|
|
|
/* from kernel:
|
|
|
|
// pid comm S ppid pgid sid tty_nr tty_pgrp flg
|
2006-11-25 03:24:44 +05:30
|
|
|
sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
|
2006-11-05 06:13:51 +05:30
|
|
|
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
|
|
|
|
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
|
2006-11-25 03:24:44 +05:30
|
|
|
task->pid,
|
|
|
|
tcomm,
|
|
|
|
state,
|
|
|
|
ppid,
|
|
|
|
pgid,
|
|
|
|
sid,
|
|
|
|
tty_nr,
|
|
|
|
tty_pgrp,
|
|
|
|
task->flags,
|
|
|
|
min_flt,
|
|
|
|
cmin_flt,
|
|
|
|
maj_flt,
|
|
|
|
cmaj_flt,
|
|
|
|
cputime_to_clock_t(utime),
|
|
|
|
cputime_to_clock_t(stime),
|
|
|
|
cputime_to_clock_t(cutime),
|
|
|
|
cputime_to_clock_t(cstime),
|
|
|
|
priority,
|
|
|
|
nice,
|
|
|
|
num_threads,
|
|
|
|
// 0,
|
|
|
|
start_time,
|
|
|
|
vsize,
|
|
|
|
mm ? get_mm_rss(mm) : 0,
|
|
|
|
rsslim,
|
|
|
|
mm ? mm->start_code : 0,
|
|
|
|
mm ? mm->end_code : 0,
|
|
|
|
mm ? mm->start_stack : 0,
|
|
|
|
esp,
|
|
|
|
eip,
|
2006-11-05 06:13:51 +05:30
|
|
|
the rest is some obsolete cruft
|
|
|
|
*/
|