blob: f56b71b210f97c39f6cc22410944c5e4b26b9a1e [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>
Denis Vlasenko0696b8a2007-06-06 07:40:16 +00007 * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
Denis Vlasenko49a128a2007-07-17 21:42:59 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen44608e92002-10-22 12:21:15 +000010 */
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
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010014typedef struct id_to_name_map_t {
15 uid_t id;
Denis Vlasenko2405ad62007-01-19 21:24:17 +000016 char name[USERNAME_MAX_SIZE];
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010017} id_to_name_map_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000018
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000019typedef struct cache_t {
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010020 id_to_name_map_t *cache;
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000021 int size;
22} cache_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000023
Denys Vlasenko89a55972020-12-30 01:04:56 +010024static cache_t *cache_user_group;
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000025
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000026void FAST_FUNC clear_username_cache(void)
Denis Vlasenko459e4d62006-11-05 00:43:51 +000027{
Denys Vlasenko89a55972020-12-30 01:04:56 +010028 if (cache_user_group) {
29 free(cache_user_group[0].cache);
30 free(cache_user_group[1].cache);
31 free(cache_user_group);
32 cache_user_group = NULL;
33 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +000034}
35
Denys Vlasenko89a55972020-12-30 01:04:56 +010036static char* get_cached(int user_group, uid_t id,
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010037 char* FAST_FUNC x2x_utoa(uid_t id))
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000038{
Denys Vlasenko89a55972020-12-30 01:04:56 +010039 cache_t *cp;
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000040 int i;
Denys Vlasenko89a55972020-12-30 01:04:56 +010041
42 if (!cache_user_group)
43 cache_user_group = xzalloc(sizeof(cache_user_group[0]) * 2);
44
45 cp = &cache_user_group[user_group];
46
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000047 for (i = 0; i < cp->size; i++)
48 if (cp->cache[i].id == id)
49 return cp->cache[i].name;
Denis Vlasenko27842282008-08-04 13:20:36 +000050 i = cp->size++;
51 cp->cache = xrealloc_vector(cp->cache, 2, i);
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000052 cp->cache[i].id = id;
Denis Vlasenko3734b942007-07-27 11:20:10 +000053 /* Never fails. Generates numeric string if name isn't found */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000054 safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name));
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000055 return cp->cache[i].name;
56}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000057const char* FAST_FUNC get_cached_username(uid_t uid)
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000058{
Denys Vlasenko89a55972020-12-30 01:04:56 +010059 return get_cached(0, uid, uid2uname_utoa);
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000060}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000061const char* FAST_FUNC get_cached_groupname(gid_t gid)
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000062{
Denys Vlasenko89a55972020-12-30 01:04:56 +010063 return get_cached(1, gid, gid2group_utoa);
Denis Vlasenko459e4d62006-11-05 00:43:51 +000064}
65
66
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000067#define PROCPS_BUFSIZE 1024
68
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000069static int read_to_buf(const char *filename, void *buf)
Rob Landleyb2804552006-02-13 22:04:27 +000070{
Denis Vlasenko5a654472007-06-10 17:11:59 +000071 int fd;
72 /* open_read_close() would do two reads, checking for EOF.
73 * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */
74 ssize_t ret = -1;
75 fd = open(filename, O_RDONLY);
76 if (fd >= 0) {
77 ret = read(fd, buf, PROCPS_BUFSIZE-1);
78 close(fd);
79 }
Denis Vlasenkoea620772006-10-14 02:23:43 +000080 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000081 return ret;
Rob Landleyb2804552006-02-13 22:04:27 +000082}
83
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000084static procps_status_t* FAST_FUNC alloc_procps_scan(void)
Eric Andersen44608e92002-10-22 12:21:15 +000085{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000086 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
Denys Vlasenkoc7b858f2020-12-14 18:49:23 +010087 unsigned n = bb_getpagesize();
Denis Vlasenko512499c2007-08-26 18:23:13 +000088 while (1) {
89 n >>= 1;
90 if (!n) break;
91 sp->shift_pages_to_bytes++;
92 }
93 sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10;
Denys Vlasenkoc7b858f2020-12-14 18:49:23 +010094 sp->dir = xopendir("/proc");
Denis Vlasenko459e4d62006-11-05 00:43:51 +000095 return sp;
96}
Denis Vlasenko35fb5122006-11-01 09:16:49 +000097
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000098void FAST_FUNC free_procps_scan(procps_status_t* sp)
Denis Vlasenko459e4d62006-11-05 00:43:51 +000099{
100 closedir(sp->dir);
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200101#if ENABLE_FEATURE_SHOW_THREADS
102 if (sp->task_dir)
103 closedir(sp->task_dir);
104#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000105 free(sp->argv0);
Alexander Shishkine766f622009-07-29 01:35:13 +0200106 free(sp->exe);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000107 IF_SELINUX(free(sp->context);)
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000108 free(sp);
109}
110
Dan Fandrich63adf832010-09-19 17:13:21 +0200111#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
Denys Vlasenkoaad76962018-12-30 20:24:59 +0100112static unsigned long long fast_strtoull_16(char **endptr)
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000113{
114 unsigned char c;
115 char *str = *endptr;
Denys Vlasenkoaad76962018-12-30 20:24:59 +0100116 unsigned long long n = 0;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000117
Denys Vlasenko62c006d2012-02-28 11:16:21 +0100118 /* Need to stop on both ' ' and '\n' */
Denys Vlasenko0b170e62012-02-28 03:36:49 +0100119 while ((c = *str++) > ' ') {
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000120 c = ((c|0x20) - '0');
121 if (c > 9)
Denys Vlasenko62c006d2012-02-28 11:16:21 +0100122 /* c = c + '0' - 'a' + 10: */
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000123 c = c - ('a' - '0' - 10);
124 n = n*16 + c;
125 }
126 *endptr = str; /* We skip trailing space! */
127 return n;
128}
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000129#endif
130
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200131#if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000132/* We cut a lot of corners here for speed */
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000133static unsigned long fast_strtoul_10(char **endptr)
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000134{
Denys Vlasenko62c006d2012-02-28 11:16:21 +0100135 unsigned char c;
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000136 char *str = *endptr;
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000137 unsigned long n = *str - '0';
138
Denys Vlasenko62c006d2012-02-28 11:16:21 +0100139 /* Need to stop on both ' ' and '\n' */
140 while ((c = *++str) > ' ')
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000141 n = n*10 + (c - '0');
142
143 *endptr = str + 1; /* We skip trailing space! */
144 return n;
145}
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200146
Denys Vlasenkof42c7b82011-01-07 13:51:16 +0100147# if ENABLE_FEATURE_FAST_TOP
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200148static long fast_strtol_10(char **endptr)
149{
150 if (**endptr != '-')
151 return fast_strtoul_10(endptr);
152
153 (*endptr)++;
154 return - (long)fast_strtoul_10(endptr);
155}
Denys Vlasenkof42c7b82011-01-07 13:51:16 +0100156# endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200157
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000158static char *skip_fields(char *str, int count)
159{
160 do {
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000161 while (*str++ != ' ')
162 continue;
163 /* we found a space char, str points after it */
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000164 } while (--count);
165 return str;
166}
167#endif
168
Denys Vlasenko77a51a22020-12-29 16:53:11 +0100169#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
Martin Lewisac79db62020-06-11 15:45:57 -0500170static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix)
171{
172 char *tp = is_prefixed_with(buf, prefix);
173 if (tp) {
174 tp = skip_whitespace(tp);
175 }
176 return tp;
177}
178
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200179int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100180 void (*cb)(struct smaprec *, void *), void *data)
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200181{
182 FILE *file;
183 struct smaprec currec;
184 char filename[sizeof("/proc/%u/smaps") + sizeof(int)*3];
185 char buf[PROCPS_BUFSIZE];
186#if !ENABLE_PMAP
187 void (*cb)(struct smaprec *, void *) = NULL;
188 void *data = NULL;
189#endif
190
191 sprintf(filename, "/proc/%u/smaps", (int)pid);
192
193 file = fopen_for_read(filename);
194 if (!file)
195 return 1;
196
197 memset(&currec, 0, sizeof(currec));
198 while (fgets(buf, PROCPS_BUFSIZE, file)) {
199 // Each mapping datum has this form:
Denys Vlasenko9ec3cd42012-02-02 11:29:43 +0100200 // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200201 // Size: nnn kB
202 // Rss: nnn kB
203 // .....
204
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100205 char *tp, *p;
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200206
207#define SCAN(S, X) \
Martin Lewisac79db62020-06-11 15:45:57 -0500208 if ((tp = skip_whitespace_if_prefixed_with(buf, S)) != NULL) { \
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200209 total->X += currec.X = fast_strtoul_10(&tp); \
210 continue; \
211 }
212 if (cb) {
213 SCAN("Pss:" , smap_pss );
214 SCAN("Swap:" , smap_swap );
215 }
216 SCAN("Private_Dirty:", private_dirty);
217 SCAN("Private_Clean:", private_clean);
218 SCAN("Shared_Dirty:" , shared_dirty );
219 SCAN("Shared_Clean:" , shared_clean );
220#undef SCAN
221 tp = strchr(buf, '-');
222 if (tp) {
223 // We reached next mapping - the line of this form:
Denys Vlasenko9ec3cd42012-02-02 11:29:43 +0100224 // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200225
226 if (cb) {
227 /* If we have a previous record, there's nothing more
228 * for it, call the callback and clear currec
229 */
230 if (currec.smap_size)
231 cb(&currec, data);
232 free(currec.smap_name);
233 }
234 memset(&currec, 0, sizeof(currec));
235
236 *tp = ' ';
237 tp = buf;
Denys Vlasenkoaad76962018-12-30 20:24:59 +0100238 currec.smap_start = fast_strtoull_16(&tp);
239 currec.smap_size = (fast_strtoull_16(&tp) - currec.smap_start) >> 10;
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200240
241 strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
242
Denys Vlasenko9ec3cd42012-02-02 11:29:43 +0100243 // skipping "rw-s FILEOFS M:m INODE "
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200244 tp = skip_whitespace(skip_fields(tp, 4));
245 // filter out /dev/something (something != zero)
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100246 if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200247 if (currec.smap_mode[1] == 'w') {
248 currec.mapped_rw = currec.smap_size;
249 total->mapped_rw += currec.smap_size;
250 } else if (currec.smap_mode[1] == '-') {
251 currec.mapped_ro = currec.smap_size;
252 total->mapped_ro += currec.smap_size;
253 }
254 }
255
256 if (strcmp(tp, "[stack]\n") == 0)
257 total->stack += currec.smap_size;
258 if (cb) {
259 p = skip_non_whitespace(tp);
260 if (p == tp) {
261 currec.smap_name = xstrdup(" [ anon ]");
262 } else {
263 *p = '\0';
264 currec.smap_name = xstrdup(tp);
265 }
266 }
267 total->smap_size += currec.smap_size;
268 }
269 }
270 fclose(file);
271
272 if (cb) {
273 if (currec.smap_size)
274 cb(&currec, data);
275 free(currec.smap_name);
276 }
277
278 return 0;
279}
280#endif
281
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000282procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000283{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000284 if (!sp)
Denis Vlasenko68404f12008-03-17 09:00:54 +0000285 sp = alloc_procps_scan();
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000286
Mike Frysinger58dda842006-07-12 20:04:00 +0000287 for (;;) {
Denys Vlasenko23901092011-08-28 11:57:36 +0200288 struct dirent *entry;
289 char buf[PROCPS_BUFSIZE];
290 long tasknice;
291 unsigned pid;
292 int n;
293 char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
294 char *filename_tail;
295
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200296#if ENABLE_FEATURE_SHOW_THREADS
Denys Vlasenko23901092011-08-28 11:57:36 +0200297 if (sp->task_dir) {
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200298 entry = readdir(sp->task_dir);
299 if (entry)
300 goto got_entry;
301 closedir(sp->task_dir);
302 sp->task_dir = NULL;
303 }
304#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000305 entry = readdir(sp->dir);
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000306 if (entry == NULL) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000307 free_procps_scan(sp);
308 return NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000309 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200310 IF_FEATURE_SHOW_THREADS(got_entry:)
Denis Vlasenkod686a042006-11-27 14:43:21 +0000311 pid = bb_strtou(entry->d_name, NULL, 10);
312 if (errno)
Eric Andersen44608e92002-10-22 12:21:15 +0000313 continue;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200314#if ENABLE_FEATURE_SHOW_THREADS
315 if ((flags & PSSCAN_TASKS) && !sp->task_dir) {
316 /* We found another /proc/PID. Do not use it,
317 * there will be /proc/PID/task/PID (same PID!),
318 * so just go ahead and dive into /proc/PID/task. */
Denys Vlasenko23901092011-08-28 11:57:36 +0200319 sprintf(filename, "/proc/%u/task", pid);
320 /* Note: if opendir fails, we just go to next /proc/XXX */
321 sp->task_dir = opendir(filename);
Lauri Kasanen6578f2c2010-12-05 05:22:29 +0100322 sp->main_thread_pid = pid;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200323 continue;
324 }
325#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000326
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200327 /* After this point we can:
328 * "break": stop parsing, return the data
329 * "continue": try next /proc/XXX
330 */
Eric Andersen44608e92002-10-22 12:21:15 +0000331
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000332 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
Eric Andersen242ab832004-01-27 20:17:39 +0000333
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000334 sp->pid = pid;
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200335 if (!(flags & ~PSSCAN_PID))
336 break; /* we needed only pid, we got it */
Eric Andersen44608e92002-10-22 12:21:15 +0000337
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000338#if ENABLE_SELINUX
339 if (flags & PSSCAN_CONTEXT) {
340 if (getpidcon(sp->pid, &sp->context) < 0)
341 sp->context = NULL;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000342 }
343#endif
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000344
Denys Vlasenko23901092011-08-28 11:57:36 +0200345#if ENABLE_FEATURE_SHOW_THREADS
346 if (sp->task_dir)
347 filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
348 else
349#endif
350 filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000351
352 if (flags & PSSCAN_UIDGID) {
Denys Vlasenko23901092011-08-28 11:57:36 +0200353 struct stat sb;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000354 if (stat(filename, &sb))
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200355 continue; /* process probably exited */
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200356 /* Effective UID/GID, not real */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000357 sp->uid = sb.st_uid;
358 sp->gid = sb.st_gid;
359 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000360
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200361 /* These are all retrieved from proc/NN/stat in one go: */
362 if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
363 | PSSCAN_COMM | PSSCAN_STATE
364 | PSSCAN_VSZ | PSSCAN_RSS
365 | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
366 | PSSCAN_TTY | PSSCAN_NICE
367 | PSSCAN_CPU)
368 ) {
Denys Vlasenkob6871c12017-02-02 13:04:30 +0100369 int s_idx;
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000370 char *cp, *comm1;
371 int tty;
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000372#if !ENABLE_FEATURE_FAST_TOP
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000373 unsigned long vsz, rss;
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000374#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000375 /* see proc(5) for some details on this */
Alexander Shishkine766f622009-07-29 01:35:13 +0200376 strcpy(filename_tail, "stat");
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000377 n = read_to_buf(filename, buf);
378 if (n < 0)
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200379 continue; /* process probably exited */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000380 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000381 /*if (!cp || cp[1] != ' ')
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200382 continue;*/
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000383 cp[0] = '\0';
Denys Vlasenkoab3964d2015-10-13 14:50:20 +0200384 BUILD_BUG_ON(sizeof(sp->comm) < 16);
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000385 comm1 = strchr(buf, '(');
386 /*if (comm1)*/
387 safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000388
389#if !ENABLE_FEATURE_FAST_TOP
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000390 n = sscanf(cp+2,
391 "%c %u " /* state, ppid */
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000392 "%u %u %d %*s " /* pgid, sid, tty, tpgid */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000393 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
394 "%lu %lu " /* utime, stime */
395 "%*s %*s %*s " /* cutime, cstime, priority */
396 "%ld " /* nice */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000397 "%*s %*s " /* timeout, it_real_value */
398 "%lu " /* start_time */
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000399 "%lu " /* vsize */
400 "%lu " /* rss */
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200401# if ENABLE_FEATURE_TOP_SMP_PROCESS
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000402 "%*s %*s %*s %*s %*s %*s " /*rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
403 "%*s %*s %*s %*s " /*signal, blocked, sigignore, sigcatch */
404 "%*s %*s %*s %*s " /*wchan, nswap, cnswap, exit_signal */
405 "%d" /*cpu last seen on*/
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200406# endif
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000407 ,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000408 sp->state, &sp->ppid,
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000409 &sp->pgid, &sp->sid, &tty,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000410 &sp->utime, &sp->stime,
411 &tasknice,
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000412 &sp->start_time,
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000413 &vsz,
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000414 &rss
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200415# if ENABLE_FEATURE_TOP_SMP_PROCESS
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000416 , &sp->last_seen_on_cpu
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200417# endif
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000418 );
419
420 if (n < 11)
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200421 continue; /* bogus data, get next /proc/XXX */
422# if ENABLE_FEATURE_TOP_SMP_PROCESS
Denys Vlasenko50157f32012-09-06 12:43:22 +0200423 if (n == 11)
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000424 sp->last_seen_on_cpu = 0;
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200425# endif
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000426
Denis Vlasenko512499c2007-08-26 18:23:13 +0000427 /* vsz is in bytes and we want kb */
428 sp->vsz = vsz >> 10;
429 /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
430 sp->rss = rss << sp->shift_pages_to_kb;
Denis Vlasenko31789a82007-06-19 14:43:17 +0000431 sp->tty_major = (tty >> 8) & 0xfff;
Denis Vlasenko88976d02007-06-21 07:09:18 +0000432 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000433#else
434/* This costs ~100 bytes more but makes top faster by 20%
435 * If you run 10000 processes, this may be important for you */
Denis Vlasenko31789a82007-06-19 14:43:17 +0000436 sp->state[0] = cp[2];
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000437 cp += 4;
438 sp->ppid = fast_strtoul_10(&cp);
439 sp->pgid = fast_strtoul_10(&cp);
440 sp->sid = fast_strtoul_10(&cp);
441 tty = fast_strtoul_10(&cp);
Denis Vlasenko31789a82007-06-19 14:43:17 +0000442 sp->tty_major = (tty >> 8) & 0xfff;
443 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000444 cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
Denis Vlasenko42ee26d2007-06-30 08:01:04 +0000445 sp->utime = fast_strtoul_10(&cp);
446 sp->stime = fast_strtoul_10(&cp);
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000447 cp = skip_fields(cp, 3); /* cutime, cstime, priority */
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200448 tasknice = fast_strtol_10(&cp);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000449 cp = skip_fields(cp, 2); /* timeout, it_real_value */
450 sp->start_time = fast_strtoul_10(&cp);
Denis Vlasenko512499c2007-08-26 18:23:13 +0000451 /* vsz is in bytes and we want kb */
452 sp->vsz = fast_strtoul_10(&cp) >> 10;
453 /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
454 sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200455# if ENABLE_FEATURE_TOP_SMP_PROCESS
Denis Vlasenko17e7f042008-09-25 10:48:06 +0000456 /* (6): rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
457 /* (4): signal, blocked, sigignore, sigcatch */
458 /* (4): wchan, nswap, cnswap, exit_signal */
459 cp = skip_fields(cp, 14);
460//FIXME: is it safe to assume this field exists?
461 sp->last_seen_on_cpu = fast_strtoul_10(&cp);
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200462# endif
Denys Vlasenkof42c7b82011-01-07 13:51:16 +0100463#endif /* FEATURE_FAST_TOP */
Denis Vlasenko3afac4c2007-06-19 13:46:24 +0000464
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200465#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
466 sp->niceness = tasknice;
467#endif
Denys Vlasenkob6871c12017-02-02 13:04:30 +0100468 sp->state[1] = ' ';
469 sp->state[2] = ' ';
470 s_idx = 1;
471 if (sp->vsz == 0 && sp->state[0] != 'Z') {
472 /* not sure what the purpose of this flag */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000473 sp->state[1] = 'W';
Denys Vlasenkob6871c12017-02-02 13:04:30 +0100474 s_idx = 2;
475 }
476 if (tasknice != 0) {
477 if (tasknice < 0)
478 sp->state[s_idx] = '<';
479 else /* > 0 */
480 sp->state[s_idx] = 'N';
481 }
Eric Andersen44608e92002-10-22 12:21:15 +0000482 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000483
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000484#if ENABLE_FEATURE_TOPMEM
Alexander Shishkin0834a6d2010-08-28 23:20:34 +0200485 if (flags & PSSCAN_SMAPS)
486 procps_read_smaps(pid, &sp->smaps, NULL, NULL);
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000487#endif /* TOPMEM */
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200488#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
489 if (flags & PSSCAN_RUIDGID) {
490 FILE *file;
Denis Vlasenkoff6e8e22007-09-08 16:51:19 +0000491
Alexander Shishkine766f622009-07-29 01:35:13 +0200492 strcpy(filename_tail, "status");
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200493 file = fopen_for_read(filename);
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200494 if (file) {
495 while (fgets(buf, sizeof(buf), file)) {
496 char *tp;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200497#define SCAN_TWO(str, name, statement) \
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100498 if ((tp = is_prefixed_with(buf, str)) != NULL) { \
499 tp = skip_whitespace(tp); \
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200500 sscanf(tp, "%u", &sp->name); \
501 statement; \
502 }
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200503 SCAN_TWO("Uid:", ruid, continue);
504 SCAN_TWO("Gid:", rgid, break);
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200505#undef SCAN_TWO
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200506 }
507 fclose(file);
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200508 }
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200509 }
510#endif /* PS_ADDITIONAL_COLUMNS */
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200511 if (flags & PSSCAN_EXE) {
512 strcpy(filename_tail, "exe");
513 free(sp->exe);
514 sp->exe = xmalloc_readlink(filename);
515 }
516 /* Note: if /proc/PID/cmdline is empty,
517 * code below "breaks". Therefore it must be
518 * the last code to parse /proc/PID/xxx data
519 * (we used to have /proc/PID/exe parsing after it
520 * and were getting stale sp->exe).
521 */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000522#if 0 /* PSSCAN_CMD is not used */
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000523 if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
Denis Vlasenkoab8c9372007-09-28 22:13:55 +0000524 free(sp->argv0);
525 sp->argv0 = NULL;
526 free(sp->cmd);
527 sp->cmd = NULL;
Alexander Shishkine766f622009-07-29 01:35:13 +0200528 strcpy(filename_tail, "cmdline");
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000529 /* TODO: to get rid of size limits, read into malloc buf,
530 * then realloc it down to real size. */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000531 n = read_to_buf(filename, buf);
532 if (n <= 0)
533 break;
Denis Vlasenkof7d07b12007-06-30 08:03:26 +0000534 if (flags & PSSCAN_ARGV0)
535 sp->argv0 = xstrdup(buf);
536 if (flags & PSSCAN_CMD) {
537 do {
538 n--;
539 if ((unsigned char)(buf[n]) < ' ')
540 buf[n] = ' ';
541 } while (n);
542 sp->cmd = xstrdup(buf);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000543 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000544 }
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000545#else
Denis Vlasenko304a97a2007-09-29 22:49:26 +0000546 if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) {
Denis Vlasenkoab8c9372007-09-28 22:13:55 +0000547 free(sp->argv0);
548 sp->argv0 = NULL;
Alexander Shishkine766f622009-07-29 01:35:13 +0200549 strcpy(filename_tail, "cmdline");
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000550 n = read_to_buf(filename, buf);
551 if (n <= 0)
552 break;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000553 if (flags & PSSCAN_ARGVN) {
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000554 sp->argv_len = n;
Ron Yorstond840c5d2015-07-19 23:05:20 +0200555 sp->argv0 = xmemdup(buf, n + 1);
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000556 /* sp->argv0[n] = '\0'; - buf has it */
557 } else {
558 sp->argv_len = 0;
559 sp->argv0 = xstrdup(buf);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000560 }
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000561 }
562#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000563 break;
Denys Vlasenkoed4ff0e2009-09-22 23:06:07 +0200564 } /* for (;;) */
565
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000566 return sp;
Eric Andersen44608e92002-10-22 12:21:15 +0000567}
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000568
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000569void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000570{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200571 int sz;
Denys Vlasenko5331e382011-06-18 15:51:16 +0200572 char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000573
574 sprintf(filename, "/proc/%u/cmdline", pid);
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200575 sz = open_read_close(filename, buf, col - 1);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000576 if (sz > 0) {
Denys Vlasenko5331e382011-06-18 15:51:16 +0200577 const char *base;
578 int comm_len;
579
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000580 buf[sz] = '\0';
Denys Vlasenkoae357bd2009-09-11 23:29:45 +0200581 while (--sz >= 0 && buf[sz] == '\0')
582 continue;
Denys Vlasenko32711c42012-10-02 11:31:37 +0200583 /* Prevent basename("process foo/bar") = "bar" */
584 strchrnul(buf, ' ')[0] = '\0';
Denys Vlasenko5331e382011-06-18 15:51:16 +0200585 base = bb_basename(buf); /* before we replace argv0's NUL with space */
586 while (sz >= 0) {
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000587 if ((unsigned char)(buf[sz]) < ' ')
588 buf[sz] = ' ';
Denys Vlasenko5331e382011-06-18 15:51:16 +0200589 sz--;
590 }
Denys Vlasenko049b0072015-10-24 03:45:57 +0200591 if (base[0] == '-') /* "-sh" (login shell)? */
592 base++;
Denys Vlasenko5331e382011-06-18 15:51:16 +0200593
594 /* If comm differs from argv0, prepend "{comm} ".
595 * It allows to see thread names set by prctl(PR_SET_NAME).
596 */
Denys Vlasenko049b0072015-10-24 03:45:57 +0200597 if (!comm)
598 return;
Denys Vlasenko5331e382011-06-18 15:51:16 +0200599 comm_len = strlen(comm);
600 /* Why compare up to comm_len, not COMM_LEN-1?
601 * Well, some processes rewrite argv, and use _spaces_ there
602 * while rewriting. (KDE is observed to do it).
603 * I prefer to still treat argv0 "process foo bar"
604 * as 'equal' to comm "process".
605 */
606 if (strncmp(base, comm, comm_len) != 0) {
607 comm_len += 3;
608 if (col > comm_len)
609 memmove(buf + comm_len, buf, col - comm_len);
610 snprintf(buf, col, "{%s}", comm);
611 if (col <= comm_len)
612 return;
613 buf[comm_len - 1] = ' ';
614 buf[col - 1] = '\0';
615 }
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000616 } else {
Denys Vlasenko049b0072015-10-24 03:45:57 +0200617 snprintf(buf, col, "[%s]", comm ? comm : "?");
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000618 }
619}
620
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000621/* from kernel:
622 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000623 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000624%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
625%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000626 task->pid,
627 tcomm,
628 state,
629 ppid,
630 pgid,
631 sid,
632 tty_nr,
633 tty_pgrp,
634 task->flags,
635 min_flt,
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000636 cmin_flt,
637 maj_flt,
638 cmaj_flt,
639 cputime_to_clock_t(utime),
640 cputime_to_clock_t(stime),
641 cputime_to_clock_t(cutime),
642 cputime_to_clock_t(cstime),
643 priority,
644 nice,
645 num_threads,
646 // 0,
647 start_time,
648 vsize,
649 mm ? get_mm_rss(mm) : 0,
650 rsslim,
651 mm ? mm->start_code : 0,
652 mm ? mm->end_code : 0,
653 mm ? mm->start_stack : 0,
654 esp,
655 eip,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000656the rest is some obsolete cruft
657*/