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, long uid, int bufsize);
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 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
66 return cp->cache[i].name;
68 const char* get_cached_username(uid_t uid)
70 return get_cached(&username, uid, bb_getpwuid);
72 const char* get_cached_groupname(gid_t gid)
74 return get_cached(&groupname, gid, bb_getgrgid);
78 #define PROCPS_BUFSIZE 1024
80 static int read_to_buf(const char *filename, void *buf)
83 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
84 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
88 procps_status_t* alloc_procps_scan(int flags)
90 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
91 sp->dir = xopendir("/proc");
95 void free_procps_scan(procps_status_t* sp)
99 USE_SELINUX(free(sp->context);)
103 void BUG_comm_size(void);
104 procps_status_t* procps_scan(procps_status_t* sp, int flags)
106 struct dirent *entry;
107 char buf[PROCPS_BUFSIZE];
108 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
116 sp = alloc_procps_scan(flags);
119 entry = readdir(sp->dir);
121 free_procps_scan(sp);
124 pid = bb_strtou(entry->d_name, NULL, 10);
128 /* After this point we have to break, not continue
129 * ("continue" would mean that current /proc/NNN
130 * is not a valid process info) */
132 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
135 if (!(flags & ~PSSCAN_PID)) break;
138 if (flags & PSSCAN_CONTEXT) {
139 if (getpidcon(sp->pid, &sp->context) < 0)
144 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
146 if (flags & PSSCAN_UIDGID) {
147 if (stat(filename, &sb))
149 /* Need comment - is this effective or real UID/GID? */
154 if (flags & PSSCAN_STAT) {
156 unsigned long vsz, rss;
159 /* see proc(5) for some details on this */
160 strcpy(filename_tail, "/stat");
161 n = read_to_buf(filename, buf);
164 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
165 if (!cp || cp[1] != ' ')
168 if (sizeof(sp->comm) < 16)
170 sscanf(buf, "%*s (%15c", sp->comm);
172 "%c %u " /* state, ppid */
173 "%u %u %d %*s " /* pgid, sid, tty, tpgid */
174 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
175 "%lu %lu " /* utime, stime */
176 "%*s %*s %*s " /* cutime, cstime, priority */
178 "%*s %*s %*s " /* timeout, it_real_value, start_time */
181 /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
182 /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
183 /* "%lu %lu %lu" wchan, nswap, cnswap */
185 sp->state, &sp->ppid,
186 &sp->pgid, &sp->sid, &tty,
187 &sp->utime, &sp->stime,
194 sp->tty_str[0] = '?';
195 /* sp->tty_str[1] = '\0'; - done by memset */
196 if (tty) /* tty field of "0" means "no tty" */
197 snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
198 (tty >> 8) & 0xfff, /* major */
199 (tty & 0xff) | ((tty >> 12) & 0xfff00));
200 if (sp->vsz == 0 && sp->state[0] != 'Z')
206 else if (tasknice) /* > 0 */
211 sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
215 if (flags & PSSCAN_CMD) {
218 strcpy(filename_tail, "/cmdline");
219 n = read_to_buf(filename, buf);
222 if (buf[n-1] == '\n') {
229 if ((unsigned char)(buf[n]) < ' ')
232 sp->cmd = xstrdup(buf);
239 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
240 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
241 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
242 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
256 cputime_to_clock_t(utime),
257 cputime_to_clock_t(stime),
258 cputime_to_clock_t(cutime),
259 cputime_to_clock_t(cstime),
266 mm ? get_mm_rss(mm) : 0,
268 mm ? mm->start_code : 0,
269 mm ? mm->end_code : 0,
270 mm ? mm->start_stack : 0,
273 the rest is some obsolete cruft