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>
7 * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 typedef struct unsigned_to_name_map_t {
17 char name[USERNAME_MAX_SIZE];
18 } unsigned_to_name_map_t;
20 typedef struct cache_t {
21 unsigned_to_name_map_t *cache;
25 static cache_t username, groupname;
27 static void clear_cache(cache_t *cp)
33 void clear_username_cache(void)
35 clear_cache(&username);
36 clear_cache(&groupname);
39 #if 0 /* more generic, but we don't need that yet */
40 /* Returns -N-1 if not found. */
41 /* cp->cache[N] is allocated and must be filled in this case */
42 static int get_cached(cache_t *cp, unsigned id)
45 for (i = 0; i < cp->size; i++)
46 if (cp->cache[i].id == id)
49 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
50 cp->cache[i++].id = id;
55 typedef char* ug_func(char *name, int bufsize, long uid);
56 static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
59 for (i = 0; i < cp->size; i++)
60 if (cp->cache[i].id == id)
61 return cp->cache[i].name;
63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
65 /* Never fails. Generates numeric string if name isn't found */
66 fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
67 return cp->cache[i].name;
69 const char* get_cached_username(uid_t uid)
71 return get_cached(&username, uid, bb_getpwuid);
73 const char* get_cached_groupname(gid_t gid)
75 return get_cached(&groupname, gid, bb_getgrgid);
79 #define PROCPS_BUFSIZE 1024
81 static int read_to_buf(const char *filename, void *buf)
84 /* open_read_close() would do two reads, checking for EOF.
85 * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
87 fd = open(filename, O_RDONLY);
89 ret = read(fd, buf, PROCPS_BUFSIZE-1);
92 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
96 procps_status_t *alloc_procps_scan(int flags)
98 unsigned n = getpagesize();
99 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
100 sp->dir = xopendir("/proc");
104 sp->shift_pages_to_bytes++;
106 sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10;
110 void free_procps_scan(procps_status_t* sp)
114 USE_SELINUX(free(sp->context);)
118 #if ENABLE_FEATURE_FAST_TOP
119 /* We cut a lot of corners here for speed */
120 static unsigned long fast_strtoul_10(char **endptr)
124 unsigned long n = *str - '0';
126 while ((c = *++str) != ' ')
127 n = n*10 + (c - '0');
129 *endptr = str + 1; /* We skip trailing space! */
132 static char *skip_fields(char *str, int count)
135 while (*str++ != ' ')
137 /* we found a space char, str points after it */
143 void BUG_comm_size(void);
144 procps_status_t *procps_scan(procps_status_t* sp, int flags)
146 struct dirent *entry;
147 char buf[PROCPS_BUFSIZE];
148 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
156 sp = alloc_procps_scan(flags);
159 entry = readdir(sp->dir);
161 free_procps_scan(sp);
164 pid = bb_strtou(entry->d_name, NULL, 10);
168 /* After this point we have to break, not continue
169 * ("continue" would mean that current /proc/NNN
170 * is not a valid process info) */
172 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
175 if (!(flags & ~PSSCAN_PID)) break;
178 if (flags & PSSCAN_CONTEXT) {
179 if (getpidcon(sp->pid, &sp->context) < 0)
184 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
186 if (flags & PSSCAN_UIDGID) {
187 if (stat(filename, &sb))
189 /* Need comment - is this effective or real UID/GID? */
194 if (flags & PSSCAN_STAT) {
197 #if !ENABLE_FEATURE_FAST_TOP
198 unsigned long vsz, rss;
201 /* see proc(5) for some details on this */
202 strcpy(filename_tail, "/stat");
203 n = read_to_buf(filename, buf);
206 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
207 /*if (!cp || cp[1] != ' ')
210 if (sizeof(sp->comm) < 16)
212 comm1 = strchr(buf, '(');
214 safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
216 #if !ENABLE_FEATURE_FAST_TOP
218 "%c %u " /* state, ppid */
219 "%u %u %d %*s " /* pgid, sid, tty, tpgid */
220 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
221 "%lu %lu " /* utime, stime */
222 "%*s %*s %*s " /* cutime, cstime, priority */
224 "%*s %*s %*s " /* timeout, it_real_value, start_time */
227 /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
228 /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
229 /* "%lu %lu %lu" wchan, nswap, cnswap */
231 sp->state, &sp->ppid,
232 &sp->pgid, &sp->sid, &tty,
233 &sp->utime, &sp->stime,
239 /* vsz is in bytes and we want kb */
241 /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
242 sp->rss = rss << sp->shift_pages_to_kb;
243 sp->tty_major = (tty >> 8) & 0xfff;
244 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
246 /* This costs ~100 bytes more but makes top faster by 20%
247 * If you run 10000 processes, this may be important for you */
248 sp->state[0] = cp[2];
250 sp->ppid = fast_strtoul_10(&cp);
251 sp->pgid = fast_strtoul_10(&cp);
252 sp->sid = fast_strtoul_10(&cp);
253 tty = fast_strtoul_10(&cp);
254 sp->tty_major = (tty >> 8) & 0xfff;
255 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
256 cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
257 sp->utime = fast_strtoul_10(&cp);
258 sp->stime = fast_strtoul_10(&cp);
259 cp = skip_fields(cp, 3); /* cutime, cstime, priority */
260 tasknice = fast_strtoul_10(&cp);
261 cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */
262 /* vsz is in bytes and we want kb */
263 sp->vsz = fast_strtoul_10(&cp) >> 10;
264 /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
265 sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
268 if (sp->vsz == 0 && sp->state[0] != 'Z')
274 else if (tasknice) /* > 0 */
281 #if 0 /* PSSCAN_CMD is not used */
282 if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
291 strcpy(filename_tail, "/cmdline");
292 /* TODO: to get rid of size limits, read into malloc buf,
293 * then realloc it down to real size. */
294 n = read_to_buf(filename, buf);
297 if (flags & PSSCAN_ARGV0)
298 sp->argv0 = xstrdup(buf);
299 if (flags & PSSCAN_CMD) {
302 if ((unsigned char)(buf[n]) < ' ')
305 sp->cmd = xstrdup(buf);
309 if (flags & PSSCAN_ARGV0) {
314 strcpy(filename_tail, "/cmdline");
315 n = read_to_buf(filename, buf);
318 if (flags & PSSCAN_ARGV0)
319 sp->argv0 = xstrdup(buf);
327 void read_cmdline(char *buf, int col, unsigned pid, const char *comm)
330 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
332 sprintf(filename, "/proc/%u/cmdline", pid);
333 sz = open_read_close(filename, buf, col);
337 if ((unsigned char)(buf[sz]) < ' ')
340 snprintf(buf, col, "[%s]", comm);
345 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
346 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
347 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
348 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
362 cputime_to_clock_t(utime),
363 cputime_to_clock_t(stime),
364 cputime_to_clock_t(cutime),
365 cputime_to_clock_t(cstime),
372 mm ? get_mm_rss(mm) : 0,
374 mm ? mm->start_code : 0,
375 mm ? mm->end_code : 0,
376 mm ? mm->start_stack : 0,
379 the rest is some obsolete cruft