1 /* vi: set sw=4 ts=4: */
5 * Copyright 1998 by Albert Cahalan; all rights reserved.
6 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 typedef struct unsigned_to_name_map_t {
16 char name[USERNAME_MAX_SIZE];
17 } unsigned_to_name_map_t;
19 typedef struct cache_t {
20 unsigned_to_name_map_t *cache;
24 static cache_t username, groupname;
26 static void clear_cache(cache_t *cp)
32 void clear_username_cache(void)
34 clear_cache(&username);
35 clear_cache(&groupname);
38 #if 0 /* more generic, but we don't need that yet */
39 /* Returns -N-1 if not found. */
40 /* cp->cache[N] is allocated and must be filled in this case */
41 static int get_cached(cache_t *cp, unsigned id)
44 for (i = 0; i < cp->size; i++)
45 if (cp->cache[i].id == id)
48 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
49 cp->cache[i++].id = id;
54 typedef char* ug_func(char *name, long uid, int bufsize);
55 static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
58 for (i = 0; i < cp->size; i++)
59 if (cp->cache[i].id == id)
60 return cp->cache[i].name;
62 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
64 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
65 return cp->cache[i].name;
67 const char* get_cached_username(uid_t uid)
69 return get_cached(&username, uid, bb_getpwuid);
71 const char* get_cached_groupname(gid_t gid)
73 return get_cached(&groupname, gid, bb_getgrgid);
77 #define PROCPS_BUFSIZE 1024
79 static int read_to_buf(const char *filename, void *buf)
82 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
83 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
87 procps_status_t* alloc_procps_scan(int flags)
89 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
90 sp->dir = xopendir("/proc");
94 void free_procps_scan(procps_status_t* sp)
101 void BUG_comm_size(void);
102 procps_status_t* procps_scan(procps_status_t* sp, int flags)
104 struct dirent *entry;
105 char buf[PROCPS_BUFSIZE];
106 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
114 sp = alloc_procps_scan(flags);
117 entry = readdir(sp->dir);
119 free_procps_scan(sp);
122 pid = bb_strtou(entry->d_name, NULL, 10);
126 /* After this point we have to break, not continue
127 * ("continue" would mean that current /proc/NNN
128 * is not a valid process info) */
130 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
133 if (!(flags & ~PSSCAN_PID)) break;
135 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
137 if (flags & PSSCAN_UIDGID) {
138 if (stat(filename, &sb))
140 /* Need comment - is this effective or real UID/GID? */
145 if (flags & PSSCAN_STAT) {
147 unsigned long vsz, rss;
150 /* see proc(5) for some details on this */
151 strcpy(filename_tail, "/stat");
152 n = read_to_buf(filename, buf);
155 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
156 if (!cp || cp[1] != ' ')
159 if (sizeof(sp->comm) < 16)
161 sscanf(buf, "%*s (%15c", sp->comm);
163 "%c %u " /* state, ppid */
164 "%u %u %d %*s " /* pgid, sid, tty, tpgid */
165 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
166 "%lu %lu " /* utime, stime */
167 "%*s %*s %*s " /* cutime, cstime, priority */
169 "%*s %*s %*s " /* timeout, it_real_value, start_time */
172 /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
173 /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
174 /* "%lu %lu %lu" wchan, nswap, cnswap */
176 sp->state, &sp->ppid,
177 &sp->pgid, &sp->sid, &tty,
178 &sp->utime, &sp->stime,
185 sp->tty_str[0] = '?';
186 /* sp->tty_str[1] = '\0'; - done by memset */
187 if (tty) /* tty field of "0" means "no tty" */
188 snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
189 (tty >> 8) & 0xfff, /* major */
190 (tty & 0xff) | ((tty >> 12) & 0xfff00));
191 if (sp->vsz == 0 && sp->state[0] != 'Z')
197 else if (tasknice) /* > 0 */
202 sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
206 if (flags & PSSCAN_CMD) {
209 strcpy(filename_tail, "/cmdline");
210 n = read_to_buf(filename, buf);
213 if (buf[n-1] == '\n') {
220 if ((unsigned char)(buf[n]) < ' ')
223 sp->cmd = xstrdup(buf);
230 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
231 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
232 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
233 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
247 cputime_to_clock_t(utime),
248 cputime_to_clock_t(stime),
249 cputime_to_clock_t(cutime),
250 cputime_to_clock_t(cstime),
257 mm ? get_mm_rss(mm) : 0,
259 mm ? mm->start_code : 0,
260 mm ? mm->end_code : 0,
261 mm ? mm->start_stack : 0,
264 the rest is some obsolete cruft