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>
|
2007-06-06 07:40:16 +00:00
|
|
|
* SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
2007-07-17 21:42:59 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-10-22 12:21:15 +00:00
|
|
|
*/
|
|
|
|
|
2002-11-24 01:32:56 +00:00
|
|
|
#include "libbb.h"
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2006-02-13 22:04:27 +00:00
|
|
|
|
2011-03-01 15:27:13 +00:00
|
|
|
typedef struct id_to_name_map_t {
|
|
|
|
uid_t id;
|
2007-01-19 21:24:17 +00:00
|
|
|
char name[USERNAME_MAX_SIZE];
|
2011-03-01 15:27:13 +00:00
|
|
|
} id_to_name_map_t;
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-11-24 14:57:31 +00:00
|
|
|
typedef struct cache_t {
|
2011-03-01 15:27:13 +00:00
|
|
|
id_to_name_map_t *cache;
|
2006-11-24 14:57:31 +00:00
|
|
|
int size;
|
|
|
|
} cache_t;
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-11-24 14:57:31 +00:00
|
|
|
static cache_t username, groupname;
|
|
|
|
|
|
|
|
static void clear_cache(cache_t *cp)
|
|
|
|
{
|
|
|
|
free(cp->cache);
|
|
|
|
cp->cache = NULL;
|
|
|
|
cp->size = 0;
|
|
|
|
}
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC clear_username_cache(void)
|
2006-11-05 00:43:51 +00:00
|
|
|
{
|
2006-11-24 14:57:31 +00:00
|
|
|
clear_cache(&username);
|
|
|
|
clear_cache(&groupname);
|
2006-11-05 00:43:51 +00:00
|
|
|
}
|
|
|
|
|
2006-11-24 14:59:45 +00:00
|
|
|
#if 0 /* more generic, but we don't need that yet */
|
2006-11-24 14:57:31 +00:00
|
|
|
/* Returns -N-1 if not found. */
|
|
|
|
/* cp->cache[N] is allocated and must be filled in this case */
|
2011-03-01 15:27:13 +00:00
|
|
|
static int get_cached(cache_t *cp, uid_t id)
|
2006-11-05 00:43:51 +00:00
|
|
|
{
|
|
|
|
int i;
|
2006-11-24 14:57:31 +00:00
|
|
|
for (i = 0; i < cp->size; i++)
|
|
|
|
if (cp->cache[i].id == id)
|
|
|
|
return i;
|
2008-08-04 13:20:36 +00:00
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc_vector(cp->cache, 2, i);
|
2006-11-24 14:57:31 +00:00
|
|
|
cp->cache[i++].id = id;
|
|
|
|
return -i;
|
|
|
|
}
|
2006-11-24 14:59:45 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-01 15:27:13 +00:00
|
|
|
static char* get_cached(cache_t *cp, uid_t id,
|
|
|
|
char* FAST_FUNC x2x_utoa(uid_t id))
|
2006-11-24 14:59:45 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cp->size; i++)
|
|
|
|
if (cp->cache[i].id == id)
|
|
|
|
return cp->cache[i].name;
|
2008-08-04 13:20:36 +00:00
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc_vector(cp->cache, 2, i);
|
2006-11-24 14:59:45 +00:00
|
|
|
cp->cache[i].id = id;
|
2007-07-27 11:20:10 +00:00
|
|
|
/* Never fails. Generates numeric string if name isn't found */
|
2008-12-02 22:56:59 +00:00
|
|
|
safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name));
|
2006-11-24 14:59:45 +00:00
|
|
|
return cp->cache[i].name;
|
|
|
|
}
|
2008-06-27 02:52:20 +00:00
|
|
|
const char* FAST_FUNC get_cached_username(uid_t uid)
|
2006-11-24 14:57:31 +00:00
|
|
|
{
|
2008-12-02 22:56:59 +00:00
|
|
|
return get_cached(&username, uid, uid2uname_utoa);
|
2006-11-24 14:57:31 +00:00
|
|
|
}
|
2008-06-27 02:52:20 +00:00
|
|
|
const char* FAST_FUNC get_cached_groupname(gid_t gid)
|
2006-11-24 14:57:31 +00:00
|
|
|
{
|
2008-12-02 22:56:59 +00:00
|
|
|
return get_cached(&groupname, gid, gid2group_utoa);
|
2006-11-05 00:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 10:17:09 +00:00
|
|
|
#define PROCPS_BUFSIZE 1024
|
|
|
|
|
2008-07-17 18:39:36 +00:00
|
|
|
static int read_to_buf(const char *filename, void *buf)
|
2006-02-13 22:04:27 +00:00
|
|
|
{
|
2007-06-10 17:11:59 +00:00
|
|
|
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 02:23:43 +00:00
|
|
|
((char *)buf)[ret > 0 ? ret : 0] = '\0';
|
2006-02-14 10:17:09 +00:00
|
|
|
return ret;
|
2006-02-13 22:04:27 +00:00
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
static procps_status_t* FAST_FUNC alloc_procps_scan(void)
|
2006-11-05 00:43:51 +00:00
|
|
|
{
|
2007-08-26 18:23:13 +00:00
|
|
|
unsigned n = getpagesize();
|
2006-11-05 00:43:51 +00:00
|
|
|
procps_status_t* sp = xzalloc(sizeof(procps_status_t));
|
|
|
|
sp->dir = xopendir("/proc");
|
2007-08-26 18:23:13 +00:00
|
|
|
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 00:43:51 +00:00
|
|
|
return sp;
|
|
|
|
}
|
2006-02-13 22:04:27 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC free_procps_scan(procps_status_t* sp)
|
2002-10-22 12:21:15 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
closedir(sp->dir);
|
2009-09-19 20:29:42 +00:00
|
|
|
#if ENABLE_FEATURE_SHOW_THREADS
|
|
|
|
if (sp->task_dir)
|
|
|
|
closedir(sp->task_dir);
|
|
|
|
#endif
|
2007-06-30 14:47:41 +00:00
|
|
|
free(sp->argv0);
|
2009-07-28 23:35:13 +00:00
|
|
|
free(sp->exe);
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_SELINUX(free(sp->context);)
|
2006-11-05 00:43:51 +00:00
|
|
|
free(sp);
|
|
|
|
}
|
2006-11-01 09:16:49 +00:00
|
|
|
|
2010-09-19 15:13:21 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
2007-09-08 16:51:19 +00:00
|
|
|
static unsigned long fast_strtoul_16(char **endptr)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
char *str = *endptr;
|
|
|
|
unsigned long n = 0;
|
|
|
|
|
2012-02-28 10:16:21 +00:00
|
|
|
/* Need to stop on both ' ' and '\n' */
|
2012-02-28 02:36:49 +00:00
|
|
|
while ((c = *str++) > ' ') {
|
2007-09-08 16:51:19 +00:00
|
|
|
c = ((c|0x20) - '0');
|
|
|
|
if (c > 9)
|
2012-02-28 10:16:21 +00:00
|
|
|
/* c = c + '0' - 'a' + 10: */
|
2007-09-08 16:51:19 +00:00
|
|
|
c = c - ('a' - '0' - 10);
|
|
|
|
n = n*16 + c;
|
|
|
|
}
|
|
|
|
*endptr = str; /* We skip trailing space! */
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-28 21:20:34 +00:00
|
|
|
#if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
2007-06-19 13:46:24 +00:00
|
|
|
/* We cut a lot of corners here for speed */
|
2007-06-30 08:01:04 +00:00
|
|
|
static unsigned long fast_strtoul_10(char **endptr)
|
2007-06-19 13:46:24 +00:00
|
|
|
{
|
2012-02-28 10:16:21 +00:00
|
|
|
unsigned char c;
|
2007-06-30 08:01:04 +00:00
|
|
|
char *str = *endptr;
|
2007-06-19 13:46:24 +00:00
|
|
|
unsigned long n = *str - '0';
|
|
|
|
|
2012-02-28 10:16:21 +00:00
|
|
|
/* Need to stop on both ' ' and '\n' */
|
|
|
|
while ((c = *++str) > ' ')
|
2007-06-19 13:46:24 +00:00
|
|
|
n = n*10 + (c - '0');
|
|
|
|
|
|
|
|
*endptr = str + 1; /* We skip trailing space! */
|
|
|
|
return n;
|
|
|
|
}
|
2009-07-03 20:16:17 +00:00
|
|
|
|
2011-01-07 12:51:16 +00:00
|
|
|
# if ENABLE_FEATURE_FAST_TOP
|
2009-07-03 20:16:17 +00:00
|
|
|
static long fast_strtol_10(char **endptr)
|
|
|
|
{
|
|
|
|
if (**endptr != '-')
|
|
|
|
return fast_strtoul_10(endptr);
|
|
|
|
|
|
|
|
(*endptr)++;
|
|
|
|
return - (long)fast_strtoul_10(endptr);
|
|
|
|
}
|
2011-01-07 12:51:16 +00:00
|
|
|
# endif
|
2009-07-03 20:16:17 +00:00
|
|
|
|
2007-06-19 13:46:24 +00:00
|
|
|
static char *skip_fields(char *str, int count)
|
|
|
|
{
|
|
|
|
do {
|
2007-06-30 08:01:04 +00:00
|
|
|
while (*str++ != ' ')
|
|
|
|
continue;
|
|
|
|
/* we found a space char, str points after it */
|
2007-06-19 13:46:24 +00:00
|
|
|
} while (--count);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-28 21:20:34 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
|
|
|
int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
|
2013-01-14 14:57:44 +00:00
|
|
|
void (*cb)(struct smaprec *, void *), void *data)
|
2010-08-28 21:20:34 +00:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
struct smaprec currec;
|
|
|
|
char filename[sizeof("/proc/%u/smaps") + sizeof(int)*3];
|
|
|
|
char buf[PROCPS_BUFSIZE];
|
|
|
|
#if !ENABLE_PMAP
|
|
|
|
void (*cb)(struct smaprec *, void *) = NULL;
|
|
|
|
void *data = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sprintf(filename, "/proc/%u/smaps", (int)pid);
|
|
|
|
|
|
|
|
file = fopen_for_read(filename);
|
|
|
|
if (!file)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
memset(&currec, 0, sizeof(currec));
|
|
|
|
while (fgets(buf, PROCPS_BUFSIZE, file)) {
|
|
|
|
// Each mapping datum has this form:
|
2012-02-02 10:29:43 +00:00
|
|
|
// f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
|
2010-08-28 21:20:34 +00:00
|
|
|
// Size: nnn kB
|
|
|
|
// Rss: nnn kB
|
|
|
|
// .....
|
|
|
|
|
2015-03-12 16:48:34 +00:00
|
|
|
char *tp, *p;
|
2010-08-28 21:20:34 +00:00
|
|
|
|
|
|
|
#define SCAN(S, X) \
|
2015-03-12 16:48:34 +00:00
|
|
|
if ((tp = is_prefixed_with(buf, S)) != NULL) { \
|
|
|
|
tp = skip_whitespace(tp); \
|
2010-08-28 21:20:34 +00:00
|
|
|
total->X += currec.X = fast_strtoul_10(&tp); \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
if (cb) {
|
|
|
|
SCAN("Pss:" , smap_pss );
|
|
|
|
SCAN("Swap:" , smap_swap );
|
|
|
|
}
|
|
|
|
SCAN("Private_Dirty:", private_dirty);
|
|
|
|
SCAN("Private_Clean:", private_clean);
|
|
|
|
SCAN("Shared_Dirty:" , shared_dirty );
|
|
|
|
SCAN("Shared_Clean:" , shared_clean );
|
|
|
|
#undef SCAN
|
|
|
|
tp = strchr(buf, '-');
|
|
|
|
if (tp) {
|
|
|
|
// We reached next mapping - the line of this form:
|
2012-02-02 10:29:43 +00:00
|
|
|
// f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
|
2010-08-28 21:20:34 +00:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
/* If we have a previous record, there's nothing more
|
|
|
|
* for it, call the callback and clear currec
|
|
|
|
*/
|
|
|
|
if (currec.smap_size)
|
|
|
|
cb(&currec, data);
|
|
|
|
free(currec.smap_name);
|
|
|
|
}
|
|
|
|
memset(&currec, 0, sizeof(currec));
|
|
|
|
|
|
|
|
*tp = ' ';
|
|
|
|
tp = buf;
|
|
|
|
currec.smap_start = fast_strtoul_16(&tp);
|
|
|
|
currec.smap_size = (fast_strtoul_16(&tp) - currec.smap_start) >> 10;
|
|
|
|
|
|
|
|
strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
|
|
|
|
|
2012-02-02 10:29:43 +00:00
|
|
|
// skipping "rw-s FILEOFS M:m INODE "
|
2010-08-28 21:20:34 +00:00
|
|
|
tp = skip_whitespace(skip_fields(tp, 4));
|
|
|
|
// filter out /dev/something (something != zero)
|
2015-03-12 16:48:34 +00:00
|
|
|
if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
|
2010-08-28 21:20:34 +00:00
|
|
|
if (currec.smap_mode[1] == 'w') {
|
|
|
|
currec.mapped_rw = currec.smap_size;
|
|
|
|
total->mapped_rw += currec.smap_size;
|
|
|
|
} else if (currec.smap_mode[1] == '-') {
|
|
|
|
currec.mapped_ro = currec.smap_size;
|
|
|
|
total->mapped_ro += currec.smap_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(tp, "[stack]\n") == 0)
|
|
|
|
total->stack += currec.smap_size;
|
|
|
|
if (cb) {
|
|
|
|
p = skip_non_whitespace(tp);
|
|
|
|
if (p == tp) {
|
|
|
|
currec.smap_name = xstrdup(" [ anon ]");
|
|
|
|
} else {
|
|
|
|
*p = '\0';
|
|
|
|
currec.smap_name = xstrdup(tp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total->smap_size += currec.smap_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
if (currec.smap_size)
|
|
|
|
cb(&currec, data);
|
|
|
|
free(currec.smap_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
2006-11-05 00:43:51 +00:00
|
|
|
{
|
|
|
|
if (!sp)
|
2008-03-17 09:00:54 +00:00
|
|
|
sp = alloc_procps_scan();
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-07-12 20:04:00 +00:00
|
|
|
for (;;) {
|
2011-08-28 09:57:36 +00:00
|
|
|
struct dirent *entry;
|
|
|
|
char buf[PROCPS_BUFSIZE];
|
|
|
|
long tasknice;
|
|
|
|
unsigned pid;
|
|
|
|
int n;
|
|
|
|
char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
|
|
|
|
char *filename_tail;
|
|
|
|
|
2009-09-19 20:29:42 +00:00
|
|
|
#if ENABLE_FEATURE_SHOW_THREADS
|
2011-08-28 09:57:36 +00:00
|
|
|
if (sp->task_dir) {
|
2009-09-19 20:29:42 +00:00
|
|
|
entry = readdir(sp->task_dir);
|
|
|
|
if (entry)
|
|
|
|
goto got_entry;
|
|
|
|
closedir(sp->task_dir);
|
|
|
|
sp->task_dir = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-05 00:43:51 +00:00
|
|
|
entry = readdir(sp->dir);
|
2006-10-12 19:29:44 +00:00
|
|
|
if (entry == NULL) {
|
2006-11-05 00:43:51 +00:00
|
|
|
free_procps_scan(sp);
|
|
|
|
return NULL;
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2009-09-19 20:29:42 +00:00
|
|
|
IF_FEATURE_SHOW_THREADS(got_entry:)
|
2006-11-27 14:43:21 +00:00
|
|
|
pid = bb_strtou(entry->d_name, NULL, 10);
|
|
|
|
if (errno)
|
2002-10-22 12:21:15 +00:00
|
|
|
continue;
|
2009-09-19 20:29:42 +00:00
|
|
|
#if ENABLE_FEATURE_SHOW_THREADS
|
|
|
|
if ((flags & PSSCAN_TASKS) && !sp->task_dir) {
|
|
|
|
/* We found another /proc/PID. Do not use it,
|
|
|
|
* there will be /proc/PID/task/PID (same PID!),
|
|
|
|
* so just go ahead and dive into /proc/PID/task. */
|
2011-08-28 09:57:36 +00:00
|
|
|
sprintf(filename, "/proc/%u/task", pid);
|
|
|
|
/* Note: if opendir fails, we just go to next /proc/XXX */
|
|
|
|
sp->task_dir = opendir(filename);
|
2010-12-05 04:22:29 +00:00
|
|
|
sp->main_thread_pid = pid;
|
2009-09-19 20:29:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2009-09-22 21:06:07 +00:00
|
|
|
/* After this point we can:
|
|
|
|
* "break": stop parsing, return the data
|
|
|
|
* "continue": try next /proc/XXX
|
|
|
|
*/
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2007-02-08 08:21:58 +00:00
|
|
|
memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
|
2004-01-27 20:17:39 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
sp->pid = pid;
|
2009-09-22 21:06:07 +00:00
|
|
|
if (!(flags & ~PSSCAN_PID))
|
|
|
|
break; /* we needed only pid, we got it */
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2007-06-06 07:40:16 +00:00
|
|
|
#if ENABLE_SELINUX
|
|
|
|
if (flags & PSSCAN_CONTEXT) {
|
|
|
|
if (getpidcon(sp->pid, &sp->context) < 0)
|
|
|
|
sp->context = NULL;
|
2007-07-19 22:50:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-06-06 07:40:16 +00:00
|
|
|
|
2011-08-28 09:57:36 +00:00
|
|
|
#if ENABLE_FEATURE_SHOW_THREADS
|
|
|
|
if (sp->task_dir)
|
|
|
|
filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
|
2006-11-05 00:43:51 +00:00
|
|
|
|
|
|
|
if (flags & PSSCAN_UIDGID) {
|
2011-08-28 09:57:36 +00:00
|
|
|
struct stat sb;
|
2006-11-05 00:43:51 +00:00
|
|
|
if (stat(filename, &sb))
|
2009-09-22 21:06:07 +00:00
|
|
|
continue; /* process probably exited */
|
2009-07-03 20:16:17 +00:00
|
|
|
/* Effective UID/GID, not real */
|
2006-11-05 00:43:51 +00:00
|
|
|
sp->uid = sb.st_uid;
|
|
|
|
sp->gid = sb.st_gid;
|
|
|
|
}
|
2007-01-11 17:20:00 +00:00
|
|
|
|
2011-06-18 13:49:47 +00:00
|
|
|
/* These are all retrieved from proc/NN/stat in one go: */
|
|
|
|
if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
|
|
|
|
| PSSCAN_COMM | PSSCAN_STATE
|
|
|
|
| PSSCAN_VSZ | PSSCAN_RSS
|
|
|
|
| PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
|
|
|
|
| PSSCAN_TTY | PSSCAN_NICE
|
|
|
|
| PSSCAN_CPU)
|
|
|
|
) {
|
2007-06-30 08:01:04 +00:00
|
|
|
char *cp, *comm1;
|
|
|
|
int tty;
|
2007-06-19 13:46:24 +00:00
|
|
|
#if !ENABLE_FEATURE_FAST_TOP
|
2007-04-19 14:46:14 +00:00
|
|
|
unsigned long vsz, rss;
|
2007-06-19 13:46:24 +00:00
|
|
|
#endif
|
2006-11-05 00:43:51 +00:00
|
|
|
/* see proc(5) for some details on this */
|
2009-07-28 23:35:13 +00:00
|
|
|
strcpy(filename_tail, "stat");
|
2006-11-05 00:43:51 +00:00
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n < 0)
|
2009-09-22 21:06:07 +00:00
|
|
|
continue; /* process probably exited */
|
2006-11-05 00:43:51 +00:00
|
|
|
cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
|
2007-06-30 08:01:04 +00:00
|
|
|
/*if (!cp || cp[1] != ' ')
|
2009-09-22 21:06:07 +00:00
|
|
|
continue;*/
|
2006-11-05 00:43:51 +00:00
|
|
|
cp[0] = '\0';
|
2015-10-13 12:50:20 +00:00
|
|
|
BUILD_BUG_ON(sizeof(sp->comm) < 16);
|
2007-06-30 08:01:04 +00:00
|
|
|
comm1 = strchr(buf, '(');
|
|
|
|
/*if (comm1)*/
|
|
|
|
safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
|
2007-06-19 13:46:24 +00:00
|
|
|
|
|
|
|
#if !ENABLE_FEATURE_FAST_TOP
|
2006-11-05 00:43:51 +00:00
|
|
|
n = sscanf(cp+2,
|
|
|
|
"%c %u " /* state, ppid */
|
2007-04-19 14:46:14 +00:00
|
|
|
"%u %u %d %*s " /* pgid, sid, tty, tpgid */
|
2006-11-05 00:43:51 +00:00
|
|
|
"%*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 03:26:41 +00:00
|
|
|
"%*s %*s " /* timeout, it_real_value */
|
|
|
|
"%lu " /* start_time */
|
2007-04-19 14:46:14 +00:00
|
|
|
"%lu " /* vsize */
|
|
|
|
"%lu " /* rss */
|
2009-09-22 21:06:07 +00:00
|
|
|
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
2008-09-25 10:48:06 +00:00
|
|
|
"%*s %*s %*s %*s %*s %*s " /*rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
|
|
|
|
"%*s %*s %*s %*s " /*signal, blocked, sigignore, sigcatch */
|
|
|
|
"%*s %*s %*s %*s " /*wchan, nswap, cnswap, exit_signal */
|
|
|
|
"%d" /*cpu last seen on*/
|
2009-09-22 21:06:07 +00:00
|
|
|
# endif
|
2007-04-19 14:46:14 +00:00
|
|
|
,
|
2006-11-05 00:43:51 +00:00
|
|
|
sp->state, &sp->ppid,
|
2007-04-19 14:46:14 +00:00
|
|
|
&sp->pgid, &sp->sid, &tty,
|
2006-11-05 00:43:51 +00:00
|
|
|
&sp->utime, &sp->stime,
|
|
|
|
&tasknice,
|
2008-01-05 03:26:41 +00:00
|
|
|
&sp->start_time,
|
2007-04-19 14:46:14 +00:00
|
|
|
&vsz,
|
2008-09-25 10:48:06 +00:00
|
|
|
&rss
|
2009-09-22 21:06:07 +00:00
|
|
|
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
2008-09-25 10:48:06 +00:00
|
|
|
, &sp->last_seen_on_cpu
|
2009-09-22 21:06:07 +00:00
|
|
|
# endif
|
2008-09-25 10:48:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (n < 11)
|
2009-09-22 21:06:07 +00:00
|
|
|
continue; /* bogus data, get next /proc/XXX */
|
|
|
|
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
2012-09-06 10:43:22 +00:00
|
|
|
if (n == 11)
|
2008-09-25 10:48:06 +00:00
|
|
|
sp->last_seen_on_cpu = 0;
|
2009-09-22 21:06:07 +00:00
|
|
|
# endif
|
2008-09-25 10:48:06 +00:00
|
|
|
|
2007-08-26 18:23:13 +00:00
|
|
|
/* 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 14:43:17 +00:00
|
|
|
sp->tty_major = (tty >> 8) & 0xfff;
|
2007-06-21 07:09:18 +00:00
|
|
|
sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
|
2007-06-19 13:46:24 +00:00
|
|
|
#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 14:43:17 +00:00
|
|
|
sp->state[0] = cp[2];
|
2007-06-30 08:01:04 +00:00
|
|
|
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 14:43:17 +00:00
|
|
|
sp->tty_major = (tty >> 8) & 0xfff;
|
|
|
|
sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
|
2007-06-19 13:46:24 +00:00
|
|
|
cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
|
2007-06-30 08:01:04 +00:00
|
|
|
sp->utime = fast_strtoul_10(&cp);
|
|
|
|
sp->stime = fast_strtoul_10(&cp);
|
2007-06-19 13:46:24 +00:00
|
|
|
cp = skip_fields(cp, 3); /* cutime, cstime, priority */
|
2009-07-03 20:16:17 +00:00
|
|
|
tasknice = fast_strtol_10(&cp);
|
2008-01-05 03:26:41 +00:00
|
|
|
cp = skip_fields(cp, 2); /* timeout, it_real_value */
|
|
|
|
sp->start_time = fast_strtoul_10(&cp);
|
2007-08-26 18:23:13 +00:00
|
|
|
/* 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;
|
2009-09-22 21:06:07 +00:00
|
|
|
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
2008-09-25 10:48:06 +00:00
|
|
|
/* (6): rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
|
|
|
|
/* (4): signal, blocked, sigignore, sigcatch */
|
|
|
|
/* (4): wchan, nswap, cnswap, exit_signal */
|
|
|
|
cp = skip_fields(cp, 14);
|
|
|
|
//FIXME: is it safe to assume this field exists?
|
|
|
|
sp->last_seen_on_cpu = fast_strtoul_10(&cp);
|
2009-09-22 21:06:07 +00:00
|
|
|
# endif
|
2011-01-07 12:51:16 +00:00
|
|
|
#endif /* FEATURE_FAST_TOP */
|
2007-06-19 13:46:24 +00:00
|
|
|
|
2009-07-03 20:16:17 +00:00
|
|
|
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
|
|
|
|
sp->niceness = tasknice;
|
|
|
|
#endif
|
|
|
|
|
2007-02-08 08:21:58 +00:00
|
|
|
if (sp->vsz == 0 && sp->state[0] != 'Z')
|
2006-11-05 00:43:51 +00:00
|
|
|
sp->state[1] = 'W';
|
|
|
|
else
|
|
|
|
sp->state[1] = ' ';
|
|
|
|
if (tasknice < 0)
|
|
|
|
sp->state[2] = '<';
|
2007-04-19 14:46:14 +00:00
|
|
|
else if (tasknice) /* > 0 */
|
2006-11-05 00:43:51 +00:00
|
|
|
sp->state[2] = 'N';
|
|
|
|
else
|
|
|
|
sp->state[2] = ' ';
|
|
|
|
}
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2007-09-08 16:51:19 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
2010-08-28 21:20:34 +00:00
|
|
|
if (flags & PSSCAN_SMAPS)
|
|
|
|
procps_read_smaps(pid, &sp->smaps, NULL, NULL);
|
2007-09-08 16:51:19 +00:00
|
|
|
#endif /* TOPMEM */
|
2009-07-03 20:16:17 +00:00
|
|
|
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
|
|
|
|
if (flags & PSSCAN_RUIDGID) {
|
|
|
|
FILE *file;
|
2007-09-08 16:51:19 +00:00
|
|
|
|
2009-07-28 23:35:13 +00:00
|
|
|
strcpy(filename_tail, "status");
|
2009-07-03 20:16:17 +00:00
|
|
|
file = fopen_for_read(filename);
|
2009-09-22 21:06:07 +00:00
|
|
|
if (file) {
|
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
char *tp;
|
2009-07-03 20:16:17 +00:00
|
|
|
#define SCAN_TWO(str, name, statement) \
|
2015-03-12 16:48:34 +00:00
|
|
|
if ((tp = is_prefixed_with(buf, str)) != NULL) { \
|
|
|
|
tp = skip_whitespace(tp); \
|
2009-07-03 20:16:17 +00:00
|
|
|
sscanf(tp, "%u", &sp->name); \
|
|
|
|
statement; \
|
|
|
|
}
|
2009-09-22 21:06:07 +00:00
|
|
|
SCAN_TWO("Uid:", ruid, continue);
|
|
|
|
SCAN_TWO("Gid:", rgid, break);
|
2009-07-03 20:16:17 +00:00
|
|
|
#undef SCAN_TWO
|
2009-09-22 21:06:07 +00:00
|
|
|
}
|
|
|
|
fclose(file);
|
2009-07-03 20:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PS_ADDITIONAL_COLUMNS */
|
2009-09-22 21:06:07 +00:00
|
|
|
if (flags & PSSCAN_EXE) {
|
|
|
|
strcpy(filename_tail, "exe");
|
|
|
|
free(sp->exe);
|
|
|
|
sp->exe = xmalloc_readlink(filename);
|
|
|
|
}
|
|
|
|
/* Note: if /proc/PID/cmdline is empty,
|
|
|
|
* code below "breaks". Therefore it must be
|
|
|
|
* the last code to parse /proc/PID/xxx data
|
|
|
|
* (we used to have /proc/PID/exe parsing after it
|
|
|
|
* and were getting stale sp->exe).
|
|
|
|
*/
|
2007-06-30 14:47:41 +00:00
|
|
|
#if 0 /* PSSCAN_CMD is not used */
|
2007-06-30 08:03:26 +00:00
|
|
|
if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
|
2007-09-28 22:13:55 +00:00
|
|
|
free(sp->argv0);
|
|
|
|
sp->argv0 = NULL;
|
|
|
|
free(sp->cmd);
|
|
|
|
sp->cmd = NULL;
|
2009-07-28 23:35:13 +00:00
|
|
|
strcpy(filename_tail, "cmdline");
|
2007-06-30 08:03:26 +00:00
|
|
|
/* TODO: to get rid of size limits, read into malloc buf,
|
|
|
|
* then realloc it down to real size. */
|
2006-11-05 00:43:51 +00:00
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2007-06-30 08:03:26 +00:00
|
|
|
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 12:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-30 14:47:41 +00:00
|
|
|
#else
|
2007-09-29 22:49:26 +00:00
|
|
|
if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {
|
2007-09-28 22:13:55 +00:00
|
|
|
free(sp->argv0);
|
|
|
|
sp->argv0 = NULL;
|
2009-07-28 23:35:13 +00:00
|
|
|
strcpy(filename_tail, "cmdline");
|
2007-06-30 14:47:41 +00:00
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
2007-09-29 22:26:01 +00:00
|
|
|
if (flags & PSSCAN_ARGVN) {
|
2008-07-17 18:39:36 +00:00
|
|
|
sp->argv_len = n;
|
2015-07-19 21:05:20 +00:00
|
|
|
sp->argv0 = xmemdup(buf, n + 1);
|
2008-07-17 18:39:36 +00:00
|
|
|
/* sp->argv0[n] = '\0'; - buf has it */
|
|
|
|
} else {
|
|
|
|
sp->argv_len = 0;
|
|
|
|
sp->argv0 = xstrdup(buf);
|
2007-09-29 22:26:01 +00:00
|
|
|
}
|
2007-06-30 14:47:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
2006-11-05 00:43:51 +00:00
|
|
|
break;
|
2009-09-22 21:06:07 +00:00
|
|
|
} /* for (;;) */
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
return sp;
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2007-06-30 14:47:41 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
|
2007-06-30 14:47:41 +00:00
|
|
|
{
|
2009-09-11 22:15:34 +00:00
|
|
|
int sz;
|
2011-06-18 13:51:16 +00:00
|
|
|
char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
|
2007-06-30 14:47:41 +00:00
|
|
|
|
|
|
|
sprintf(filename, "/proc/%u/cmdline", pid);
|
2009-09-11 22:15:34 +00:00
|
|
|
sz = open_read_close(filename, buf, col - 1);
|
2007-06-30 14:47:41 +00:00
|
|
|
if (sz > 0) {
|
2011-06-18 13:51:16 +00:00
|
|
|
const char *base;
|
|
|
|
int comm_len;
|
|
|
|
|
2007-06-30 14:47:41 +00:00
|
|
|
buf[sz] = '\0';
|
2009-09-11 21:29:45 +00:00
|
|
|
while (--sz >= 0 && buf[sz] == '\0')
|
|
|
|
continue;
|
2012-10-02 09:31:37 +00:00
|
|
|
/* Prevent basename("process foo/bar") = "bar" */
|
|
|
|
strchrnul(buf, ' ')[0] = '\0';
|
2011-06-18 13:51:16 +00:00
|
|
|
base = bb_basename(buf); /* before we replace argv0's NUL with space */
|
|
|
|
while (sz >= 0) {
|
2007-06-30 14:47:41 +00:00
|
|
|
if ((unsigned char)(buf[sz]) < ' ')
|
|
|
|
buf[sz] = ' ';
|
2011-06-18 13:51:16 +00:00
|
|
|
sz--;
|
|
|
|
}
|
2015-10-24 01:45:57 +00:00
|
|
|
if (base[0] == '-') /* "-sh" (login shell)? */
|
|
|
|
base++;
|
2011-06-18 13:51:16 +00:00
|
|
|
|
|
|
|
/* If comm differs from argv0, prepend "{comm} ".
|
|
|
|
* It allows to see thread names set by prctl(PR_SET_NAME).
|
|
|
|
*/
|
2015-10-24 01:45:57 +00:00
|
|
|
if (!comm)
|
|
|
|
return;
|
2011-06-18 13:51:16 +00:00
|
|
|
comm_len = strlen(comm);
|
|
|
|
/* Why compare up to comm_len, not COMM_LEN-1?
|
|
|
|
* Well, some processes rewrite argv, and use _spaces_ there
|
|
|
|
* while rewriting. (KDE is observed to do it).
|
|
|
|
* I prefer to still treat argv0 "process foo bar"
|
|
|
|
* as 'equal' to comm "process".
|
|
|
|
*/
|
|
|
|
if (strncmp(base, comm, comm_len) != 0) {
|
|
|
|
comm_len += 3;
|
|
|
|
if (col > comm_len)
|
|
|
|
memmove(buf + comm_len, buf, col - comm_len);
|
|
|
|
snprintf(buf, col, "{%s}", comm);
|
|
|
|
if (col <= comm_len)
|
|
|
|
return;
|
|
|
|
buf[comm_len - 1] = ' ';
|
|
|
|
buf[col - 1] = '\0';
|
|
|
|
}
|
2007-06-30 14:47:41 +00:00
|
|
|
} else {
|
2015-10-24 01:45:57 +00:00
|
|
|
snprintf(buf, col, "[%s]", comm ? comm : "?");
|
2007-06-30 14:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
/* from kernel:
|
|
|
|
// pid comm S ppid pgid sid tty_nr tty_pgrp flg
|
2006-11-24 21:54:44 +00:00
|
|
|
sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
|
2006-11-05 00:43:51 +00:00
|
|
|
%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-24 21:54:44 +00:00
|
|
|
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 00:43:51 +00:00
|
|
|
the rest is some obsolete cruft
|
|
|
|
*/
|