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>
|
|
|
|
*
|
2006-05-19 19:29:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
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
|
|
|
|
2006-11-24 14:57:31 +00:00
|
|
|
typedef struct unsigned_to_name_map_t {
|
|
|
|
unsigned id;
|
2007-01-19 21:24:17 +00:00
|
|
|
char name[USERNAME_MAX_SIZE];
|
2006-11-24 14:57:31 +00:00
|
|
|
} unsigned_to_name_map_t;
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-11-24 14:57:31 +00:00
|
|
|
typedef struct cache_t {
|
|
|
|
unsigned_to_name_map_t *cache;
|
|
|
|
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;
|
|
|
|
}
|
2006-11-05 00:43:51 +00:00
|
|
|
void clear_username_cache(void)
|
|
|
|
{
|
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 */
|
|
|
|
static int get_cached(cache_t *cp, unsigned 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;
|
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
|
|
|
|
cp->cache[i++].id = id;
|
|
|
|
return -i;
|
|
|
|
}
|
2006-11-24 14:59:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef char* ug_func(char *name, long uid, int bufsize);
|
|
|
|
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;
|
|
|
|
i = cp->size++;
|
|
|
|
cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
|
|
|
|
cp->cache[i].id = id;
|
|
|
|
fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
|
|
|
|
return cp->cache[i].name;
|
|
|
|
}
|
2006-11-24 14:57:31 +00:00
|
|
|
const char* get_cached_username(uid_t uid)
|
|
|
|
{
|
2006-11-24 14:59:45 +00:00
|
|
|
return get_cached(&username, uid, bb_getpwuid);
|
2006-11-24 14:57:31 +00:00
|
|
|
}
|
2006-11-24 14:59:45 +00:00
|
|
|
const char* get_cached_groupname(gid_t gid)
|
2006-11-24 14:57:31 +00:00
|
|
|
{
|
2006-11-24 14:59:45 +00:00
|
|
|
return get_cached(&groupname, gid, bb_getgrgid);
|
2006-11-05 00:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 10:17:09 +00:00
|
|
|
#define PROCPS_BUFSIZE 1024
|
|
|
|
|
|
|
|
static int read_to_buf(const char *filename, void *buf)
|
2006-02-13 22:04:27 +00:00
|
|
|
{
|
2006-02-14 10:17:09 +00:00
|
|
|
ssize_t ret;
|
2006-10-14 02:23:43 +00:00
|
|
|
ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
|
|
|
|
((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
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
procps_status_t* alloc_procps_scan(int flags)
|
|
|
|
{
|
|
|
|
procps_status_t* sp = xzalloc(sizeof(procps_status_t));
|
|
|
|
sp->dir = xopendir("/proc");
|
|
|
|
return sp;
|
|
|
|
}
|
2006-02-13 22:04:27 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
void 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);
|
|
|
|
free(sp->cmd);
|
|
|
|
free(sp);
|
|
|
|
}
|
2006-11-01 09:16:49 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
void BUG_comm_size(void);
|
|
|
|
procps_status_t* procps_scan(procps_status_t* sp, int flags)
|
|
|
|
{
|
2006-11-01 09:16:49 +00:00
|
|
|
struct dirent *entry;
|
2006-02-14 10:17:09 +00:00
|
|
|
char buf[PROCPS_BUFSIZE];
|
2006-11-05 00:43:51 +00:00
|
|
|
char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
|
|
|
|
char *filename_tail;
|
2002-10-22 12:21:15 +00:00
|
|
|
long tasknice;
|
2006-11-05 00:43:51 +00:00
|
|
|
unsigned pid;
|
2006-11-01 20:20:37 +00:00
|
|
|
int n;
|
2002-10-22 12:21:15 +00:00
|
|
|
struct stat sb;
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
if (!sp)
|
|
|
|
sp = alloc_procps_scan(flags);
|
|
|
|
|
2006-07-12 20:04:00 +00:00
|
|
|
for (;;) {
|
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
|
|
|
}
|
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;
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
/* 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 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;
|
|
|
|
if (!(flags & ~PSSCAN_PID)) break;
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
filename_tail = filename + sprintf(filename, "/proc/%d", pid);
|
|
|
|
|
|
|
|
if (flags & PSSCAN_UIDGID) {
|
|
|
|
if (stat(filename, &sb))
|
|
|
|
break;
|
2007-01-19 21:24:17 +00:00
|
|
|
/* Need comment - is this effective or real UID/GID? */
|
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
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
if (flags & PSSCAN_STAT) {
|
|
|
|
char *cp;
|
2007-04-19 14:46:14 +00:00
|
|
|
unsigned long vsz, rss;
|
|
|
|
int tty;
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
/* 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>" */
|
|
|
|
if (!cp || cp[1] != ' ')
|
|
|
|
break;
|
|
|
|
cp[0] = '\0';
|
|
|
|
if (sizeof(sp->comm) < 16)
|
|
|
|
BUG_comm_size();
|
|
|
|
sscanf(buf, "%*s (%15c", sp->comm);
|
|
|
|
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 */
|
|
|
|
"%*s %*s %*s " /* timeout, it_real_value, start_time */
|
2007-04-19 14:46:14 +00:00
|
|
|
"%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 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,
|
2007-04-19 14:46:14 +00:00
|
|
|
&vsz,
|
|
|
|
&rss);
|
|
|
|
if (n != 10)
|
2006-11-05 00:43:51 +00:00
|
|
|
break;
|
|
|
|
|
2007-04-19 14:46:14 +00:00
|
|
|
sp->tty_str[0] = '?';
|
|
|
|
/* sp->tty_str[1] = '\0'; - done by memset */
|
2007-04-19 14:47:11 +00:00
|
|
|
if (tty) /* tty field of "0" means "no tty" */
|
2007-04-19 14:46:14 +00:00
|
|
|
snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
|
|
|
|
(tty >> 8) & 0xfff, /* major */
|
|
|
|
(tty & 0xff) | ((tty >> 12) & 0xfff00));
|
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-04-19 14:46:14 +00:00
|
|
|
sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
|
|
|
|
sp->rss = rss >> 10;
|
2006-11-05 00:43:51 +00:00
|
|
|
}
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
if (flags & PSSCAN_CMD) {
|
|
|
|
free(sp->cmd);
|
|
|
|
sp->cmd = NULL;
|
|
|
|
strcpy(filename_tail, "/cmdline");
|
|
|
|
n = read_to_buf(filename, buf);
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
|
|
|
if (buf[n-1] == '\n') {
|
|
|
|
if (!--n)
|
|
|
|
break;
|
|
|
|
buf[n] = '\0';
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2006-11-05 00:43:51 +00:00
|
|
|
do {
|
|
|
|
n--;
|
|
|
|
if ((unsigned char)(buf[n]) < ' ')
|
|
|
|
buf[n] = ' ';
|
|
|
|
} while (n);
|
2007-01-19 21:24:17 +00:00
|
|
|
sp->cmd = xstrdup(buf);
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2006-11-05 00:43:51 +00:00
|
|
|
break;
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2006-11-05 00:43:51 +00:00
|
|
|
return sp;
|
2002-10-22 12:21:15 +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
|
|
|
|
*/
|