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.
19 static user_map_t *username_cache;
20 static int username_cache_size;
22 void clear_username_cache(void)
25 username_cache = NULL;
26 username_cache_size = 0;
29 const char* get_cached_username(uid_t uid)
32 for (i = 0; i < username_cache_size; i++)
33 if (username_cache[i].uid == uid)
34 return username_cache[i].username;
35 i = username_cache_size++;
36 username_cache = xrealloc(username_cache, username_cache_size * sizeof(*username_cache));
37 username_cache[i].uid = uid;
38 bb_getpwuid(username_cache[i].username, uid, sizeof(username_cache[i].username));
39 return username_cache[i].username;
43 #define PROCPS_BUFSIZE 1024
45 static int read_to_buf(const char *filename, void *buf)
48 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
49 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
53 procps_status_t* alloc_procps_scan(int flags)
55 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
56 sp->dir = xopendir("/proc");
60 void free_procps_scan(procps_status_t* sp)
67 void BUG_comm_size(void);
68 procps_status_t* procps_scan(procps_status_t* sp, int flags)
71 char buf[PROCPS_BUFSIZE];
72 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
80 sp = alloc_procps_scan(flags);
83 entry = readdir(sp->dir);
88 if (safe_strtou(entry->d_name, &pid))
91 /* After this point we have to break, not continue
92 * ("continue" would mean that current /proc/NNN
93 * is not a valid process info) */
95 memset(&sp->rss, 0, sizeof(*sp) - offsetof(procps_status_t, rss));
98 if (!(flags & ~PSSCAN_PID)) break;
100 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
102 if (flags & PSSCAN_UIDGID) {
103 if (stat(filename, &sb))
105 /* Need comment - is this effective or read UID/GID? */
110 if (flags & PSSCAN_STAT) {
112 /* see proc(5) for some details on this */
113 strcpy(filename_tail, "/stat");
114 n = read_to_buf(filename, buf);
117 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
118 if (!cp || cp[1] != ' ')
121 if (sizeof(sp->comm) < 16)
123 sscanf(buf, "%*s (%15c", sp->comm);
125 "%c %u " /* state, ppid */
126 "%u %u %*s %*s " /* pgid, sid, tty, tpgid */
127 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
128 "%lu %lu " /* utime, stime */
129 "%*s %*s %*s " /* cutime, cstime, priority */
131 "%*s %*s %*s " /* timeout, it_real_value, start_time */
134 sp->state, &sp->ppid,
136 &sp->utime, &sp->stime,
142 if (sp->rss == 0 && sp->state[0] != 'Z')
148 else if (tasknice > 0)
154 sp->rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
156 sp->rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
160 if (flags & PSSCAN_CMD) {
163 strcpy(filename_tail, "/cmdline");
164 n = read_to_buf(filename, buf);
167 if (buf[n-1] == '\n') {
174 if ((unsigned char)(buf[n]) < ' ')
177 sp->cmd = strdup(buf);
184 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
185 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
186 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
187 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
202 cputime_to_clock_t(utime),
203 cputime_to_clock_t(stime),
204 cputime_to_clock_t(cutime),
205 cputime_to_clock_t(cstime),
212 mm ? get_mm_rss(mm) : 0,
214 mm ? mm->start_code : 0,
215 mm ? mm->end_code : 0,
216 mm ? mm->start_stack : 0,
219 the rest is some obsolete cruft