blob: 3e863b0deaaaa0a0e3613d3ad6b1337e5b19d9bf [file] [log] [blame]
Eric Andersen44608e92002-10-22 12:21:15 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
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
8 *
9 */
10
Eric Andersen44608e92002-10-22 12:21:15 +000011#include <dirent.h>
12#include <string.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <asm/page.h>
16
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000017#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000018
Rob Landley60158cb2005-05-03 06:25:50 +000019extern procps_status_t * procps_scan(int save_user_arg0)
Eric Andersen44608e92002-10-22 12:21:15 +000020{
21 static DIR *dir;
22 struct dirent *entry;
23 static procps_status_t ret_status;
24 char *name;
25 int n;
26 char status[32];
27 char buf[1024];
28 FILE *fp;
29 procps_status_t curstatus;
30 int pid;
31 long tasknice;
32 struct stat sb;
33
34 if (!dir) {
35 dir = opendir("/proc");
36 if(!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 bb_error_msg_and_die("Can't open /proc");
Eric Andersen44608e92002-10-22 12:21:15 +000038 }
39 for(;;) {
40 if((entry = readdir(dir)) == NULL) {
41 closedir(dir);
42 dir = 0;
43 return 0;
44 }
45 name = entry->d_name;
46 if (!(*name >= '0' && *name <= '9'))
47 continue;
48
49 memset(&curstatus, 0, sizeof(procps_status_t));
50 pid = atoi(name);
51 curstatus.pid = pid;
52
Eric Andersen242ab832004-01-27 20:17:39 +000053 sprintf(status, "/proc/%d", pid);
54 if(stat(status, &sb))
55 continue;
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000056 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
Eric Andersen242ab832004-01-27 20:17:39 +000057
Eric Andersen44608e92002-10-22 12:21:15 +000058 sprintf(status, "/proc/%d/stat", pid);
Rob Landley60158cb2005-05-03 06:25:50 +000059
Eric Andersen44608e92002-10-22 12:21:15 +000060 if((fp = fopen(status, "r")) == NULL)
61 continue;
Eric Andersen44608e92002-10-22 12:21:15 +000062 name = fgets(buf, sizeof(buf), fp);
63 fclose(fp);
64 if(name == NULL)
65 continue;
66 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
67 if(name == 0 || name[1] != ' ')
68 continue;
69 *name = 0;
70 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
71 n = sscanf(name+2,
72 "%c %d "
73 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
74 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000075#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +000076 "%lu %lu "
77#else
78 "%*s %*s "
79#endif
80 "%*s %*s %*s " /* cutime, cstime, priority */
81 "%ld "
82 "%*s %*s %*s " /* timeout, it_real_value, start_time */
83 "%*s " /* vsize */
84 "%ld",
85 curstatus.state, &curstatus.ppid,
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000086#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +000087 &curstatus.utime, &curstatus.stime,
88#endif
89 &tasknice,
90 &curstatus.rss);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000091#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +000092 if(n != 6)
93#else
94 if(n != 4)
95#endif
96 continue;
97
98 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
99 curstatus.state[1] = 'W';
100 else
101 curstatus.state[1] = ' ';
102 if (tasknice < 0)
103 curstatus.state[2] = '<';
104 else if (tasknice > 0)
105 curstatus.state[2] = 'N';
106 else
107 curstatus.state[2] = ' ';
108
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000109#ifdef PAGE_SHIFT
Eric Andersen44608e92002-10-22 12:21:15 +0000110 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000111#else
112 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
113#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000114
Eric Andersen44608e92002-10-22 12:21:15 +0000115 if(save_user_arg0) {
Eric Andersenfab3e122003-05-26 18:07:30 +0000116 sprintf(status, "/proc/%d/cmdline", pid);
Eric Andersen44608e92002-10-22 12:21:15 +0000117 if((fp = fopen(status, "r")) == NULL)
118 continue;
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000119 if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
120 if(buf[n-1]=='\n')
121 buf[--n] = 0;
122 name = buf;
123 while(n) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000124 if(((unsigned char)*name) < ' ')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000125 *name = ' ';
126 name++;
127 n--;
128 }
129 *name = 0;
Eric Andersen44608e92002-10-22 12:21:15 +0000130 if(buf[0])
131 curstatus.cmd = strdup(buf);
132 /* if NULL it work true also */
133 }
134 fclose(fp);
135 }
136 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
137 }
138}
139
Eric Andersen44608e92002-10-22 12:21:15 +0000140/* END CODE */
141/*
142Local Variables:
143c-file-style: "linux"
144c-basic-offset: 4
145tab-width: 4
146End:
147*/