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 /* see proc(5) for some details on this */
148 strcpy(filename_tail, "/stat");
149 n = read_to_buf(filename, buf);
152 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
153 if (!cp || cp[1] != ' ')
156 if (sizeof(sp->comm) < 16)
158 sscanf(buf, "%*s (%15c", sp->comm);
160 "%c %u " /* state, ppid */
161 "%u %u %*s %*s " /* pgid, sid, tty, tpgid */
162 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
163 "%lu %lu " /* utime, stime */
164 "%*s %*s %*s " /* cutime, cstime, priority */
166 "%*s %*s %*s " /* timeout, it_real_value, start_time */
168 sp->state, &sp->ppid,
170 &sp->utime, &sp->stime,
176 if (sp->vsz == 0 && sp->state[0] != 'Z')
182 else if (tasknice > 0)
187 sp->vsz >>= 10; /* vsize is in bytes and we want kb */
190 if (flags & PSSCAN_CMD) {
193 strcpy(filename_tail, "/cmdline");
194 n = read_to_buf(filename, buf);
197 if (buf[n-1] == '\n') {
204 if ((unsigned char)(buf[n]) < ' ')
207 sp->cmd = xstrdup(buf);
214 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
215 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
216 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
217 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
231 cputime_to_clock_t(utime),
232 cputime_to_clock_t(stime),
233 cputime_to_clock_t(cutime),
234 cputime_to_clock_t(cstime),
241 mm ? get_mm_rss(mm) : 0,
243 mm ? mm->start_code : 0,
244 mm ? mm->end_code : 0,
245 mm ? mm->start_stack : 0,
248 the rest is some obsolete cruft