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 static int read_to_buf(char *filename, void *buf, int bufsize)
25 fd = open(filename, O_RDONLY);
28 bufsize = read(fd, buf, bufsize);
34 extern procps_status_t * procps_scan(int save_user_arg0)
38 static procps_status_t ret_status;
44 procps_status_t curstatus;
50 dir = opendir("/proc");
52 bb_error_msg_and_die("Can't open /proc");
55 if((entry = readdir(dir)) == NULL) {
61 if (!(*name >= '0' && *name <= '9'))
64 memset(&curstatus, 0, sizeof(procps_status_t));
68 status_tail = status + sprintf(status, "/proc/%d", pid);
71 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
73 strcpy(status_tail, "/stat");
74 n = read_to_buf(status, buf, sizeof(buf));
77 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
78 if(name == 0 || name[1] != ' ')
81 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
84 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
85 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
86 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
91 "%*s %*s %*s " /* cutime, cstime, priority */
93 "%*s %*s %*s " /* timeout, it_real_value, start_time */
96 curstatus.state, &curstatus.ppid,
97 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
98 &curstatus.utime, &curstatus.stime,
102 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
109 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
110 curstatus.state[1] = 'W';
112 curstatus.state[1] = ' ';
114 curstatus.state[2] = '<';
115 else if (tasknice > 0)
116 curstatus.state[2] = 'N';
118 curstatus.state[2] = ' ';
121 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
123 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
127 strcpy(status_tail, "/cmdline");
128 n = read_to_buf(status, buf, sizeof(buf));
134 if(((unsigned char)*name) < ' ')
141 curstatus.cmd = strdup(buf);
142 /* if NULL it work true also */
145 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
152 c-file-style: "linux"