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)
28 fd = open(filename, O_RDONLY);
31 ret = read(fd, buf, PROCPS_BUFSIZE-1);
32 ((char *)buf)[ret > 0 ? ret : 0] = 0;
38 procps_status_t * procps_scan(int save_user_arg0)
42 static procps_status_t ret_status;
47 char buf[PROCPS_BUFSIZE];
48 procps_status_t curstatus;
54 dir = xopendir("/proc");
64 if (!(*name >= '0' && *name <= '9'))
67 memset(&curstatus, 0, sizeof(procps_status_t));
71 status_tail = status + sprintf(status, "/proc/%d", pid);
72 if (stat(status, &sb))
74 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
76 /* see proc(5) for some details on this */
77 strcpy(status_tail, "/stat");
78 n = read_to_buf(status, buf);
81 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
82 if (name == 0 || name[1] != ' ')
85 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
88 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
89 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
90 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
91 "%lu %lu " /* utime, stime */
93 "%*s %*s " /* utime, stime */
95 "%*s %*s %*s " /* cutime, cstime, priority */
97 "%*s %*s %*s " /* timeout, it_real_value, start_time */
100 curstatus.state, &curstatus.ppid,
101 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
102 &curstatus.utime, &curstatus.stime,
106 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
113 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
114 curstatus.state[1] = 'W';
116 curstatus.state[1] = ' ';
118 curstatus.state[2] = '<';
119 else if (tasknice > 0)
120 curstatus.state[2] = 'N';
122 curstatus.state[2] = ' ';
125 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
127 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
130 if (save_user_arg0) {
131 strcpy(status_tail, "/cmdline");
132 n = read_to_buf(status, buf);
138 if (((unsigned char)*name) < ' ')
145 curstatus.cmd = strdup(buf);
146 /* if NULL it work true also */
149 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));