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>
7 * GNU Library General Public License Version 2, or any later version
21 #define PROCPS_BUFSIZE 1024
23 static int read_to_buf(const char *filename, void *buf)
28 fd = open(filename, O_RDONLY);
31 ret = read(fd, buf, PROCPS_BUFSIZE);
37 procps_status_t * procps_scan(int save_user_arg0)
41 static procps_status_t ret_status;
46 char buf[PROCPS_BUFSIZE];
47 procps_status_t curstatus;
53 dir = opendir("/proc");
55 bb_error_msg_and_die("Can't open /proc");
58 if((entry = readdir(dir)) == NULL) {
64 if (!(*name >= '0' && *name <= '9'))
67 memset(&curstatus, 0, sizeof(procps_status_t));
71 status_tail = status + sprintf(status, "/proc/%d", pid);
74 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
76 strcpy(status_tail, "/stat");
77 n = read_to_buf(status, buf);
80 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
81 if(name == 0 || name[1] != ' ')
84 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
87 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
88 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
89 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
94 "%*s %*s %*s " /* cutime, cstime, priority */
96 "%*s %*s %*s " /* timeout, it_real_value, start_time */
99 curstatus.state, &curstatus.ppid,
100 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
101 &curstatus.utime, &curstatus.stime,
105 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
112 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
113 curstatus.state[1] = 'W';
115 curstatus.state[1] = ' ';
117 curstatus.state[2] = '<';
118 else if (tasknice > 0)
119 curstatus.state[2] = 'N';
121 curstatus.state[2] = ' ';
124 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
126 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
130 strcpy(status_tail, "/cmdline");
131 n = read_to_buf(status, buf);
137 if(((unsigned char)*name) < ' ')
144 curstatus.cmd = strdup(buf);
145 /* if NULL it work true also */
148 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
155 c-file-style: "linux"