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 /* open_read_close() would do two reads, checking for EOF.
84 * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
86 fd = open(filename, O_RDONLY);
88 ret = read(fd, buf, PROCPS_BUFSIZE-1);
91 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
95 procps_status_t* alloc_procps_scan(int flags)
97 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
98 sp->dir = xopendir("/proc");
102 void free_procps_scan(procps_status_t* sp)
106 USE_SELINUX(free(sp->context);)
110 void BUG_comm_size(void);
111 procps_status_t* procps_scan(procps_status_t* sp, int flags)
113 struct dirent *entry;
114 char buf[PROCPS_BUFSIZE];
115 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
123 sp = alloc_procps_scan(flags);
126 entry = readdir(sp->dir);
128 free_procps_scan(sp);
131 pid = bb_strtou(entry->d_name, NULL, 10);
135 /* After this point we have to break, not continue
136 * ("continue" would mean that current /proc/NNN
137 * is not a valid process info) */
139 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
142 if (!(flags & ~PSSCAN_PID)) break;
145 if (flags & PSSCAN_CONTEXT) {
146 if (getpidcon(sp->pid, &sp->context) < 0)
151 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
153 if (flags & PSSCAN_UIDGID) {
154 if (stat(filename, &sb))
156 /* Need comment - is this effective or real UID/GID? */
161 if (flags & PSSCAN_STAT) {
163 unsigned long vsz, rss;
166 /* see proc(5) for some details on this */
167 strcpy(filename_tail, "/stat");
168 n = read_to_buf(filename, buf);
171 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
172 if (!cp || cp[1] != ' ')
175 if (sizeof(sp->comm) < 16)
177 sscanf(buf, "%*s (%15c", sp->comm);
179 "%c %u " /* state, ppid */
180 "%u %u %d %*s " /* pgid, sid, tty, tpgid */
181 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
182 "%lu %lu " /* utime, stime */
183 "%*s %*s %*s " /* cutime, cstime, priority */
185 "%*s %*s %*s " /* timeout, it_real_value, start_time */
188 /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
189 /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */
190 /* "%lu %lu %lu" wchan, nswap, cnswap */
192 sp->state, &sp->ppid,
193 &sp->pgid, &sp->sid, &tty,
194 &sp->utime, &sp->stime,
201 sp->tty_str[0] = '?';
202 /* sp->tty_str[1] = '\0'; - done by memset */
203 if (tty) /* tty field of "0" means "no tty" */
204 snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
205 (tty >> 8) & 0xfff, /* major */
206 (tty & 0xff) | ((tty >> 12) & 0xfff00));
207 if (sp->vsz == 0 && sp->state[0] != 'Z')
213 else if (tasknice) /* > 0 */
218 sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
222 if (flags & PSSCAN_CMD) {
225 strcpy(filename_tail, "/cmdline");
226 n = read_to_buf(filename, buf);
229 if (buf[n-1] == '\n') {
236 if ((unsigned char)(buf[n]) < ' ')
239 sp->cmd = xstrdup(buf);
246 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
247 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
248 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
249 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
263 cputime_to_clock_t(utime),
264 cputime_to_clock_t(stime),
265 cputime_to_clock_t(cutime),
266 cputime_to_clock_t(cstime),
273 mm ? get_mm_rss(mm) : 0,
275 mm ? mm->start_code : 0,
276 mm ? mm->end_code : 0,
277 mm ? mm->start_stack : 0,
280 the rest is some obsolete cruft