blob: ee4f5e53f916a783f1c49ae25dd90efc197bd3dc [file] [log] [blame]
Eric Andersen44608e92002-10-22 12:21:15 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright 1998 by Albert Cahalan; all rights reserved.
6 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen44608e92002-10-22 12:21:15 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen44608e92002-10-22 12:21:15 +00009 */
10
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000011#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000012
Rob Landleyb2804552006-02-13 22:04:27 +000013
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000014typedef struct unsigned_to_name_map_t {
15 unsigned id;
16 char name[12];
17} unsigned_to_name_map_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000018
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000019typedef struct cache_t {
20 unsigned_to_name_map_t *cache;
21 int size;
22} cache_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000023
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000024static cache_t username, groupname;
25
26static void clear_cache(cache_t *cp)
27{
28 free(cp->cache);
29 cp->cache = NULL;
30 cp->size = 0;
31}
Denis Vlasenko459e4d62006-11-05 00:43:51 +000032void clear_username_cache(void)
33{
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000034 clear_cache(&username);
35 clear_cache(&groupname);
Denis Vlasenko459e4d62006-11-05 00:43:51 +000036}
37
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000038#if 0 /* more generic, but we don't need that yet */
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000039/* Returns -N-1 if not found. */
40/* cp->cache[N] is allocated and must be filled in this case */
41static int get_cached(cache_t *cp, unsigned id)
Denis Vlasenko459e4d62006-11-05 00:43:51 +000042{
43 int i;
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000044 for (i = 0; i < cp->size; i++)
45 if (cp->cache[i].id == id)
46 return i;
47 i = cp->size++;
48 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
49 cp->cache[i++].id = id;
50 return -i;
51}
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000052#endif
53
54typedef char* ug_func(char *name, long uid, int bufsize);
55static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
56{
57 int i;
58 for (i = 0; i < cp->size; i++)
59 if (cp->cache[i].id == id)
60 return cp->cache[i].name;
61 i = cp->size++;
62 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
63 cp->cache[i].id = id;
64 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
65 return cp->cache[i].name;
66}
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000067const char* get_cached_username(uid_t uid)
68{
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000069 return get_cached(&username, uid, bb_getpwuid);
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000070}
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000071const char* get_cached_groupname(gid_t gid)
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000072{
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000073 return get_cached(&groupname, gid, bb_getgrgid);
Denis Vlasenko459e4d62006-11-05 00:43:51 +000074}
75
76
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000077#define PROCPS_BUFSIZE 1024
78
79static int read_to_buf(const char *filename, void *buf)
Rob Landleyb2804552006-02-13 22:04:27 +000080{
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000081 ssize_t ret;
Denis Vlasenkoea620772006-10-14 02:23:43 +000082 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
83 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000084 return ret;
Rob Landleyb2804552006-02-13 22:04:27 +000085}
86
Denis Vlasenko459e4d62006-11-05 00:43:51 +000087procps_status_t* alloc_procps_scan(int flags)
Eric Andersen44608e92002-10-22 12:21:15 +000088{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000089 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
90 sp->dir = xopendir("/proc");
91 return sp;
92}
Denis Vlasenko35fb5122006-11-01 09:16:49 +000093
Denis Vlasenko459e4d62006-11-05 00:43:51 +000094void free_procps_scan(procps_status_t* sp)
95{
96 closedir(sp->dir);
97 free(sp->cmd);
98 free(sp);
99}
100
101void BUG_comm_size(void);
102procps_status_t* procps_scan(procps_status_t* sp, int flags)
103{
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000104 struct dirent *entry;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +0000105 char buf[PROCPS_BUFSIZE];
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000106 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
107 char *filename_tail;
Eric Andersen44608e92002-10-22 12:21:15 +0000108 long tasknice;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000109 unsigned pid;
Denis Vlasenkodd539f72006-11-01 20:20:37 +0000110 int n;
Eric Andersen44608e92002-10-22 12:21:15 +0000111 struct stat sb;
112
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000113 if (!sp)
114 sp = alloc_procps_scan(flags);
115
Mike Frysinger58dda842006-07-12 20:04:00 +0000116 for (;;) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000117 entry = readdir(sp->dir);
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000118 if (entry == NULL) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000119 free_procps_scan(sp);
120 return NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000121 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000122 if (safe_strtou(entry->d_name, &pid))
Eric Andersen44608e92002-10-22 12:21:15 +0000123 continue;
124
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000125 /* After this point we have to break, not continue
126 * ("continue" would mean that current /proc/NNN
127 * is not a valid process info) */
Eric Andersen44608e92002-10-22 12:21:15 +0000128
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000129 memset(&sp->rss, 0, sizeof(*sp) - offsetof(procps_status_t, rss));
Eric Andersen242ab832004-01-27 20:17:39 +0000130
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000131 sp->pid = pid;
132 if (!(flags & ~PSSCAN_PID)) break;
Eric Andersen44608e92002-10-22 12:21:15 +0000133
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000134 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
135
136 if (flags & PSSCAN_UIDGID) {
137 if (stat(filename, &sb))
138 break;
139 /* Need comment - is this effective or read UID/GID? */
140 sp->uid = sb.st_uid;
141 sp->gid = sb.st_gid;
142 }
143
144 if (flags & PSSCAN_STAT) {
145 char *cp;
146 /* see proc(5) for some details on this */
147 strcpy(filename_tail, "/stat");
148 n = read_to_buf(filename, buf);
149 if (n < 0)
150 break;
151 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
152 if (!cp || cp[1] != ' ')
153 break;
154 cp[0] = '\0';
155 if (sizeof(sp->comm) < 16)
156 BUG_comm_size();
157 sscanf(buf, "%*s (%15c", sp->comm);
158 n = sscanf(cp+2,
159 "%c %u " /* state, ppid */
160 "%u %u %*s %*s " /* pgid, sid, tty, tpgid */
161 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
162 "%lu %lu " /* utime, stime */
163 "%*s %*s %*s " /* cutime, cstime, priority */
164 "%ld " /* nice */
165 "%*s %*s %*s " /* timeout, it_real_value, start_time */
166 "%*s " /* vsize */
167 "%lu", /* rss */
168 sp->state, &sp->ppid,
169 &sp->pgid, &sp->sid,
170 &sp->utime, &sp->stime,
171 &tasknice,
172 &sp->rss);
173 if (n != 8)
174 break;
175
176 if (sp->rss == 0 && sp->state[0] != 'Z')
177 sp->state[1] = 'W';
178 else
179 sp->state[1] = ' ';
180 if (tasknice < 0)
181 sp->state[2] = '<';
182 else if (tasknice > 0)
183 sp->state[2] = 'N';
184 else
185 sp->state[2] = ' ';
Eric Andersen44608e92002-10-22 12:21:15 +0000186
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000187#ifdef PAGE_SHIFT
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000188 sp->rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000189#else
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000190 sp->rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000191#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000192 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000193
194 if (flags & PSSCAN_CMD) {
195 free(sp->cmd);
196 sp->cmd = NULL;
197 strcpy(filename_tail, "/cmdline");
198 n = read_to_buf(filename, buf);
199 if (n <= 0)
200 break;
201 if (buf[n-1] == '\n') {
202 if (!--n)
203 break;
204 buf[n] = '\0';
205 }
206 do {
207 n--;
208 if ((unsigned char)(buf[n]) < ' ')
209 buf[n] = ' ';
210 } while (n);
211 sp->cmd = strdup(buf);
212 }
213 break;
Eric Andersen44608e92002-10-22 12:21:15 +0000214 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000215 return sp;
Eric Andersen44608e92002-10-22 12:21:15 +0000216}
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000217/* from kernel:
218 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000219 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000220%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
221%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000222 task->pid,
223 tcomm,
224 state,
225 ppid,
226 pgid,
227 sid,
228 tty_nr,
229 tty_pgrp,
230 task->flags,
231 min_flt,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000232
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000233 cmin_flt,
234 maj_flt,
235 cmaj_flt,
236 cputime_to_clock_t(utime),
237 cputime_to_clock_t(stime),
238 cputime_to_clock_t(cutime),
239 cputime_to_clock_t(cstime),
240 priority,
241 nice,
242 num_threads,
243 // 0,
244 start_time,
245 vsize,
246 mm ? get_mm_rss(mm) : 0,
247 rsslim,
248 mm ? mm->start_code : 0,
249 mm ? mm->end_code : 0,
250 mm ? mm->start_stack : 0,
251 esp,
252 eip,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000253the rest is some obsolete cruft
254*/