blob: 2ebe66e9db16ddab671d9cfafbab678133d246bb [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>
Rob Landleyb2804552006-02-13 22:04:27 +000016#include <fcntl.h>
Eric Andersen44608e92002-10-22 12:21:15 +000017
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000018#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000019
Rob Landleyb2804552006-02-13 22:04:27 +000020
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000021#define PROCPS_BUFSIZE 1024
22
23static int read_to_buf(const char *filename, void *buf)
Rob Landleyb2804552006-02-13 22:04:27 +000024{
25 int fd;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000026 ssize_t ret;
Rob Landleyb2804552006-02-13 22:04:27 +000027
28 fd = open(filename, O_RDONLY);
29 if(fd < 0)
30 return -1;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000031 ret = read(fd, buf, PROCPS_BUFSIZE);
Rob Landleyb2804552006-02-13 22:04:27 +000032 close(fd);
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000033 return ret;
Rob Landleyb2804552006-02-13 22:04:27 +000034}
35
36
Rob Landley60158cb2005-05-03 06:25:50 +000037extern procps_status_t * procps_scan(int save_user_arg0)
Eric Andersen44608e92002-10-22 12:21:15 +000038{
39 static DIR *dir;
40 struct dirent *entry;
41 static procps_status_t ret_status;
42 char *name;
43 int n;
44 char status[32];
Rob Landleyb2804552006-02-13 22:04:27 +000045 char *status_tail;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000046 char buf[PROCPS_BUFSIZE];
Eric Andersen44608e92002-10-22 12:21:15 +000047 procps_status_t curstatus;
48 int pid;
49 long tasknice;
50 struct stat sb;
51
52 if (!dir) {
53 dir = opendir("/proc");
54 if(!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 bb_error_msg_and_die("Can't open /proc");
Eric Andersen44608e92002-10-22 12:21:15 +000056 }
57 for(;;) {
58 if((entry = readdir(dir)) == NULL) {
59 closedir(dir);
60 dir = 0;
61 return 0;
62 }
63 name = entry->d_name;
64 if (!(*name >= '0' && *name <= '9'))
65 continue;
66
67 memset(&curstatus, 0, sizeof(procps_status_t));
68 pid = atoi(name);
69 curstatus.pid = pid;
70
Rob Landleyb2804552006-02-13 22:04:27 +000071 status_tail = status + sprintf(status, "/proc/%d", pid);
Eric Andersen242ab832004-01-27 20:17:39 +000072 if(stat(status, &sb))
73 continue;
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000074 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
Eric Andersen242ab832004-01-27 20:17:39 +000075
Rob Landleyb2804552006-02-13 22:04:27 +000076 strcpy(status_tail, "/stat");
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000077 n = read_to_buf(status, buf);
Rob Landleyb2804552006-02-13 22:04:27 +000078 if(n < 0)
Eric Andersen44608e92002-10-22 12:21:15 +000079 continue;
80 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
81 if(name == 0 || name[1] != ' ')
82 continue;
83 *name = 0;
84 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
85 n = sscanf(name+2,
86 "%c %d "
87 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
88 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000089#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +000090 "%lu %lu "
91#else
92 "%*s %*s "
93#endif
94 "%*s %*s %*s " /* cutime, cstime, priority */
95 "%ld "
96 "%*s %*s %*s " /* timeout, it_real_value, start_time */
97 "%*s " /* vsize */
98 "%ld",
99 curstatus.state, &curstatus.ppid,
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000100#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +0000101 &curstatus.utime, &curstatus.stime,
102#endif
103 &tasknice,
104 &curstatus.rss);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000105#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +0000106 if(n != 6)
107#else
108 if(n != 4)
109#endif
110 continue;
111
112 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
113 curstatus.state[1] = 'W';
114 else
115 curstatus.state[1] = ' ';
116 if (tasknice < 0)
117 curstatus.state[2] = '<';
118 else if (tasknice > 0)
119 curstatus.state[2] = 'N';
120 else
121 curstatus.state[2] = ' ';
122
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000123#ifdef PAGE_SHIFT
Eric Andersen44608e92002-10-22 12:21:15 +0000124 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000125#else
126 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
127#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000128
Eric Andersen44608e92002-10-22 12:21:15 +0000129 if(save_user_arg0) {
Rob Landleyb2804552006-02-13 22:04:27 +0000130 strcpy(status_tail, "/cmdline");
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +0000131 n = read_to_buf(status, buf);
Rob Landleyb2804552006-02-13 22:04:27 +0000132 if(n > 0) {
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000133 if(buf[n-1]=='\n')
134 buf[--n] = 0;
135 name = buf;
136 while(n) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000137 if(((unsigned char)*name) < ' ')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000138 *name = ' ';
139 name++;
140 n--;
141 }
142 *name = 0;
Eric Andersen44608e92002-10-22 12:21:15 +0000143 if(buf[0])
144 curstatus.cmd = strdup(buf);
145 /* if NULL it work true also */
146 }
Eric Andersen44608e92002-10-22 12:21:15 +0000147 }
148 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
149 }
150}
151
Eric Andersen44608e92002-10-22 12:21:15 +0000152/* END CODE */
153/*
154Local Variables:
155c-file-style: "linux"
156c-basic-offset: 4
157tab-width: 4
158End:
159*/