aboutsummaryrefslogtreecommitdiff
path: root/libbb/procps.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/procps.c')
-rw-r--r--libbb/procps.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/libbb/procps.c b/libbb/procps.c
index 1dea61518..e15ddd1e5 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -284,27 +284,25 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
284void BUG_comm_size(void); 284void BUG_comm_size(void);
285procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) 285procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
286{ 286{
287 struct dirent *entry;
288 char buf[PROCPS_BUFSIZE];
289 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
290 char *filename_tail;
291 long tasknice;
292 unsigned pid;
293 int n;
294 struct stat sb;
295
296 if (!sp) 287 if (!sp)
297 sp = alloc_procps_scan(); 288 sp = alloc_procps_scan();
298 289
299 for (;;) { 290 for (;;) {
291 struct dirent *entry;
292 char buf[PROCPS_BUFSIZE];
293 long tasknice;
294 unsigned pid;
295 int n;
296 char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
297 char *filename_tail;
298
300#if ENABLE_FEATURE_SHOW_THREADS 299#if ENABLE_FEATURE_SHOW_THREADS
301 if ((flags & PSSCAN_TASKS) && sp->task_dir) { 300 if (sp->task_dir) {
302 entry = readdir(sp->task_dir); 301 entry = readdir(sp->task_dir);
303 if (entry) 302 if (entry)
304 goto got_entry; 303 goto got_entry;
305 closedir(sp->task_dir); 304 closedir(sp->task_dir);
306 sp->task_dir = NULL; 305 sp->task_dir = NULL;
307 sp->main_thread_pid = 0;
308 } 306 }
309#endif 307#endif
310 entry = readdir(sp->dir); 308 entry = readdir(sp->dir);
@@ -321,9 +319,9 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
321 /* We found another /proc/PID. Do not use it, 319 /* We found another /proc/PID. Do not use it,
322 * there will be /proc/PID/task/PID (same PID!), 320 * there will be /proc/PID/task/PID (same PID!),
323 * so just go ahead and dive into /proc/PID/task. */ 321 * so just go ahead and dive into /proc/PID/task. */
324 char task_dir[sizeof("/proc/%u/task") + sizeof(int)*3]; 322 sprintf(filename, "/proc/%u/task", pid);
325 sprintf(task_dir, "/proc/%u/task", pid); 323 /* Note: if opendir fails, we just go to next /proc/XXX */
326 sp->task_dir = xopendir(task_dir); 324 sp->task_dir = opendir(filename);
327 sp->main_thread_pid = pid; 325 sp->main_thread_pid = pid;
328 continue; 326 continue;
329 } 327 }
@@ -347,9 +345,15 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
347 } 345 }
348#endif 346#endif
349 347
350 filename_tail = filename + sprintf(filename, "/proc/%u/", pid); 348#if ENABLE_FEATURE_SHOW_THREADS
349 if (sp->task_dir)
350 filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
351 else
352#endif
353 filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
351 354
352 if (flags & PSSCAN_UIDGID) { 355 if (flags & PSSCAN_UIDGID) {
356 struct stat sb;
353 if (stat(filename, &sb)) 357 if (stat(filename, &sb))
354 continue; /* process probably exited */ 358 continue; /* process probably exited */
355 /* Effective UID/GID, not real */ 359 /* Effective UID/GID, not real */