ps: fix RSS parsing (rss field in /proc/PID/stat is in pages, not bytes)
[oweals/busybox.git] / libbb / procps.c
1 /* 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>
7  * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "libbb.h"
13
14
15 typedef struct unsigned_to_name_map_t {
16         unsigned id;
17         char name[USERNAME_MAX_SIZE];
18 } unsigned_to_name_map_t;
19
20 typedef struct cache_t {
21         unsigned_to_name_map_t *cache;
22         int size;
23 } cache_t;
24
25 static cache_t username, groupname;
26
27 static void clear_cache(cache_t *cp)
28 {
29         free(cp->cache);
30         cp->cache = NULL;
31         cp->size = 0;
32 }
33 void clear_username_cache(void)
34 {
35         clear_cache(&username);
36         clear_cache(&groupname);
37 }
38
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)
43 {
44         int i;
45         for (i = 0; i < cp->size; i++)
46                 if (cp->cache[i].id == id)
47                         return i;
48         i = cp->size++;
49         cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
50         cp->cache[i++].id = id;
51         return -i;
52 }
53 #endif
54
55 typedef char* ug_func(char *name, int bufsize, long uid);
56 static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
57 {
58         int i;
59         for (i = 0; i < cp->size; i++)
60                 if (cp->cache[i].id == id)
61                         return cp->cache[i].name;
62         i = cp->size++;
63         cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
64         cp->cache[i].id = id;
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;
68 }
69 const char* get_cached_username(uid_t uid)
70 {
71         return get_cached(&username, uid, bb_getpwuid);
72 }
73 const char* get_cached_groupname(gid_t gid)
74 {
75         return get_cached(&groupname, gid, bb_getgrgid);
76 }
77
78
79 #define PROCPS_BUFSIZE 1024
80
81 static int read_to_buf(const char *filename, void *buf)
82 {
83         int fd;
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 */
86         ssize_t ret = -1;
87         fd = open(filename, O_RDONLY);
88         if (fd >= 0) {
89                 ret = read(fd, buf, PROCPS_BUFSIZE-1);
90                 close(fd);
91         }
92         ((char *)buf)[ret > 0 ? ret : 0] = '\0';
93         return ret;
94 }
95
96 procps_status_t *alloc_procps_scan(int flags)
97 {
98         unsigned n = getpagesize();
99         procps_status_t* sp = xzalloc(sizeof(procps_status_t));
100         sp->dir = xopendir("/proc");
101         while (1) {
102                 n >>= 1;
103                 if (!n) break;
104                 sp->shift_pages_to_bytes++;
105         }
106         sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10;
107         return sp;
108 }
109
110 void free_procps_scan(procps_status_t* sp)
111 {
112         closedir(sp->dir);
113         free(sp->argv0);
114         USE_SELINUX(free(sp->context);)
115         free(sp);
116 }
117
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)
121 {
122         char c;
123         char *str = *endptr;
124         unsigned long n = *str - '0';
125
126         while ((c = *++str) != ' ')
127                 n = n*10 + (c - '0');
128
129         *endptr = str + 1; /* We skip trailing space! */
130         return n;
131 }
132 static char *skip_fields(char *str, int count)
133 {
134         do {
135                 while (*str++ != ' ')
136                         continue;
137                 /* we found a space char, str points after it */
138         } while (--count);
139         return str;
140 }
141 #endif
142
143 void BUG_comm_size(void);
144 procps_status_t *procps_scan(procps_status_t* sp, int flags)
145 {
146         struct dirent *entry;
147         char buf[PROCPS_BUFSIZE];
148         char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
149         char *filename_tail;
150         long tasknice;
151         unsigned pid;
152         int n;
153         struct stat sb;
154
155         if (!sp)
156                 sp = alloc_procps_scan(flags);
157
158         for (;;) {
159                 entry = readdir(sp->dir);
160                 if (entry == NULL) {
161                         free_procps_scan(sp);
162                         return NULL;
163                 }
164                 pid = bb_strtou(entry->d_name, NULL, 10);
165                 if (errno)
166                         continue;
167
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) */
171
172                 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
173
174                 sp->pid = pid;
175                 if (!(flags & ~PSSCAN_PID)) break;
176
177 #if ENABLE_SELINUX
178                 if (flags & PSSCAN_CONTEXT) {
179                         if (getpidcon(sp->pid, &sp->context) < 0)
180                                 sp->context = NULL;
181                 }
182 #endif
183
184                 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
185
186                 if (flags & PSSCAN_UIDGID) {
187                         if (stat(filename, &sb))
188                                 break;
189                         /* Need comment - is this effective or real UID/GID? */
190                         sp->uid = sb.st_uid;
191                         sp->gid = sb.st_gid;
192                 }
193
194                 if (flags & PSSCAN_STAT) {
195                         char *cp, *comm1;
196                         int tty;
197 #if !ENABLE_FEATURE_FAST_TOP
198                         unsigned long vsz, rss;
199 #endif
200
201                         /* see proc(5) for some details on this */
202                         strcpy(filename_tail, "/stat");
203                         n = read_to_buf(filename, buf);
204                         if (n < 0)
205                                 break;
206                         cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
207                         /*if (!cp || cp[1] != ' ')
208                                 break;*/
209                         cp[0] = '\0';
210                         if (sizeof(sp->comm) < 16)
211                                 BUG_comm_size();
212                         comm1 = strchr(buf, '(');
213                         /*if (comm1)*/
214                                 safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
215
216 #if !ENABLE_FEATURE_FAST_TOP
217                         n = sscanf(cp+2,
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 */
223                                 "%ld "                 /* nice */
224                                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
225                                 "%lu "                 /* vsize */
226                                 "%lu "                 /* rss */
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 */
230                                 ,
231                                 sp->state, &sp->ppid,
232                                 &sp->pgid, &sp->sid, &tty,
233                                 &sp->utime, &sp->stime,
234                                 &tasknice,
235                                 &vsz,
236                                 &rss);
237                         if (n != 10)
238                                 break;
239                         /* vsz is in bytes and we want kb */
240                         sp->vsz = vsz >> 10;
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);
245 #else
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];
249                         cp += 4;
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;
266 #endif
267
268                         if (sp->vsz == 0 && sp->state[0] != 'Z')
269                                 sp->state[1] = 'W';
270                         else
271                                 sp->state[1] = ' ';
272                         if (tasknice < 0)
273                                 sp->state[2] = '<';
274                         else if (tasknice) /* > 0 */
275                                 sp->state[2] = 'N';
276                         else
277                                 sp->state[2] = ' ';
278
279                 }
280
281 #if 0 /* PSSCAN_CMD is not used */
282                 if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
283                         if (sp->argv0) {
284                                 free(sp->argv0);
285                                 sp->argv0 = NULL;
286                         }
287                         if (sp->cmd) {
288                                 free(sp->cmd);
289                                 sp->cmd = NULL;
290                         }
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);
295                         if (n <= 0)
296                                 break;
297                         if (flags & PSSCAN_ARGV0)
298                                 sp->argv0 = xstrdup(buf);
299                         if (flags & PSSCAN_CMD) {
300                                 do {
301                                         n--;
302                                         if ((unsigned char)(buf[n]) < ' ')
303                                                 buf[n] = ' ';
304                                 } while (n);
305                                 sp->cmd = xstrdup(buf);
306                         }
307                 }
308 #else
309                 if (flags & PSSCAN_ARGV0) {
310                         if (sp->argv0) {
311                                 free(sp->argv0);
312                                 sp->argv0 = NULL;
313                         }
314                         strcpy(filename_tail, "/cmdline");
315                         n = read_to_buf(filename, buf);
316                         if (n <= 0)
317                                 break;
318                         if (flags & PSSCAN_ARGV0)
319                                 sp->argv0 = xstrdup(buf);
320                 }
321 #endif
322                 break;
323         }
324         return sp;
325 }
326
327 void read_cmdline(char *buf, int col, unsigned pid, const char *comm)
328 {
329         ssize_t sz;
330         char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
331
332         sprintf(filename, "/proc/%u/cmdline", pid);
333         sz = open_read_close(filename, buf, col);
334         if (sz > 0) {
335                 buf[sz] = '\0';
336                 while (--sz >= 0)
337                         if ((unsigned char)(buf[sz]) < ' ')
338                                 buf[sz] = ' ';
339         } else {
340                 snprintf(buf, col, "[%s]", comm);
341         }
342 }
343
344 /* from kernel:
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",
349                 task->pid,
350                 tcomm,
351                 state,
352                 ppid,
353                 pgid,
354                 sid,
355                 tty_nr,
356                 tty_pgrp,
357                 task->flags,
358                 min_flt,
359                 cmin_flt,
360                 maj_flt,
361                 cmaj_flt,
362                 cputime_to_clock_t(utime),
363                 cputime_to_clock_t(stime),
364                 cputime_to_clock_t(cutime),
365                 cputime_to_clock_t(cstime),
366                 priority,
367                 nice,
368                 num_threads,
369                 // 0,
370                 start_time,
371                 vsize,
372                 mm ? get_mm_rss(mm) : 0,
373                 rsslim,
374                 mm ? mm->start_code : 0,
375                 mm ? mm->end_code : 0,
376                 mm ? mm->start_stack : 0,
377                 esp,
378                 eip,
379 the rest is some obsolete cruft
380 */