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>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/param.h>
21 #define PROCPS_BUFSIZE 1024
23 static int read_to_buf(const char *filename, void *buf)
26 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
27 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
32 procps_status_t * procps_scan(int save_user_arg0)
35 static procps_status_t ret_status;
42 char buf[PROCPS_BUFSIZE];
43 procps_status_t curstatus;
49 dir = xopendir("/proc");
59 if (!(*name >= '0' && *name <= '9'))
62 memset(&curstatus, 0, sizeof(procps_status_t));
66 status_tail = status + sprintf(status, "/proc/%d", pid);
67 if (stat(status, &sb))
69 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
71 /* see proc(5) for some details on this */
72 strcpy(status_tail, "/stat");
73 n = read_to_buf(status, buf);
76 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
77 if (name == 0 || name[1] != ' ')
80 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
83 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
84 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
85 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
86 "%lu %lu " /* utime, stime */
88 "%*s %*s " /* utime, stime */
90 "%*s %*s %*s " /* cutime, cstime, priority */
92 "%*s %*s %*s " /* timeout, it_real_value, start_time */
95 curstatus.state, &curstatus.ppid,
96 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
97 &curstatus.utime, &curstatus.stime,
101 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
108 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
109 curstatus.state[1] = 'W';
111 curstatus.state[1] = ' ';
113 curstatus.state[2] = '<';
114 else if (tasknice > 0)
115 curstatus.state[2] = 'N';
117 curstatus.state[2] = ' ';
120 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
122 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
125 if (save_user_arg0) {
126 strcpy(status_tail, "/cmdline");
127 n = read_to_buf(status, buf);
133 if (((unsigned char)*name) < ' ')
140 curstatus.cmd = strdup(buf);
141 /* if NULL it work true also */
144 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));