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
20 #define PAGE_MASK (~((getpagesize())-1))
23 extern procps_status_t * procps_scan(int save_user_arg0
25 , int use_selinux , security_id_t *sid
31 static procps_status_t ret_status;
37 procps_status_t curstatus;
43 dir = opendir("/proc");
45 bb_error_msg_and_die("Can't open /proc");
48 if((entry = readdir(dir)) == NULL) {
54 if (!(*name >= '0' && *name <= '9'))
57 memset(&curstatus, 0, sizeof(procps_status_t));
61 sprintf(status, "/proc/%d/stat", pid);
62 if((fp = fopen(status, "r")) == NULL)
67 if(fstat_secure(fileno(fp), &sb, sid))
72 if(fstat(fileno(fp), &sb))
74 my_getpwuid(curstatus.user, sb.st_uid);
75 name = fgets(buf, sizeof(buf), fp);
79 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
80 if(name == 0 || name[1] != ' ')
83 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
86 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
87 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
88 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
93 "%*s %*s %*s " /* cutime, cstime, priority */
95 "%*s %*s %*s " /* timeout, it_real_value, start_time */
98 curstatus.state, &curstatus.ppid,
99 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
100 &curstatus.utime, &curstatus.stime,
104 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
111 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
112 curstatus.state[1] = 'W';
114 curstatus.state[1] = ' ';
116 curstatus.state[2] = '<';
117 else if (tasknice > 0)
118 curstatus.state[2] = 'N';
120 curstatus.state[2] = ' ';
122 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
125 sprintf(status, "/proc/%d/cmdline", pid);
126 if((fp = fopen(status, "r")) == NULL)
128 if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
133 if(((unsigned char)*name) < ' ')
140 curstatus.cmd = strdup(buf);
141 /* if NULL it work true also */
145 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
152 c-file-style: "linux"