Fix off by one error. (I know I had a reason for doing that, but I have _no_
[oweals/busybox.git] / libbb / procps.c
1 /* 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
11 #include <dirent.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <asm/page.h>
16 #include <fcntl.h>
17
18 #include "libbb.h"
19
20
21 static int read_to_buf(char *filename, void *buf, int bufsize)
22 {
23         int fd;
24
25         fd = open(filename, O_RDONLY);
26         if(fd < 0)
27                 return -1;
28         bufsize = read(fd, buf, bufsize);
29         close(fd);
30         return bufsize;
31 }
32
33
34 extern procps_status_t * procps_scan(int save_user_arg0)
35 {
36         static DIR *dir;
37         struct dirent *entry;
38         static procps_status_t ret_status;
39         char *name;
40         int n;
41         char status[32];
42         char *status_tail;
43         char buf[1024];
44         procps_status_t curstatus;
45         int pid;
46         long tasknice;
47         struct stat sb;
48
49         if (!dir) {
50                 dir = opendir("/proc");
51                 if(!dir)
52                         bb_error_msg_and_die("Can't open /proc");
53         }
54         for(;;) {
55                 if((entry = readdir(dir)) == NULL) {
56                         closedir(dir);
57                         dir = 0;
58                         return 0;
59                 }
60                 name = entry->d_name;
61                 if (!(*name >= '0' && *name <= '9'))
62                         continue;
63
64                 memset(&curstatus, 0, sizeof(procps_status_t));
65                 pid = atoi(name);
66                 curstatus.pid = pid;
67
68                 status_tail = status + sprintf(status, "/proc/%d", pid);
69                 if(stat(status, &sb))
70                         continue;
71                 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
72
73                 strcpy(status_tail, "/stat");
74                 n = read_to_buf(status, buf, sizeof(buf));
75                 if(n < 0)
76                         continue;
77                 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
78                 if(name == 0 || name[1] != ' ')
79                         continue;
80                 *name = 0;
81                 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
82                 n = sscanf(name+2,
83                 "%c %d "
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
87                 "%lu %lu "
88 #else
89                 "%*s %*s "
90 #endif
91                 "%*s %*s %*s "         /* cutime, cstime, priority */
92                 "%ld "
93                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
94                 "%*s "                 /* vsize */
95                 "%ld",
96                 curstatus.state, &curstatus.ppid,
97 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
98                 &curstatus.utime, &curstatus.stime,
99 #endif
100                 &tasknice,
101                 &curstatus.rss);
102 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
103                 if(n != 6)
104 #else
105                 if(n != 4)
106 #endif
107                         continue;
108
109                 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
110                         curstatus.state[1] = 'W';
111                 else
112                         curstatus.state[1] = ' ';
113                 if (tasknice < 0)
114                         curstatus.state[2] = '<';
115                 else if (tasknice > 0)
116                         curstatus.state[2] = 'N';
117                 else
118                         curstatus.state[2] = ' ';
119
120 #ifdef PAGE_SHIFT
121                 curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
122 #else
123                 curstatus.rss *= (getpagesize() >> 10);     /* 2**10 = 1kb */
124 #endif
125
126                 if(save_user_arg0) {
127                         strcpy(status_tail, "/cmdline");
128                         n = read_to_buf(status, buf, sizeof(buf));
129                         if(n > 0) {
130                                 if(buf[n-1]=='\n')
131                                         buf[--n] = 0;
132                                 name = buf;
133                                 while(n) {
134                                         if(((unsigned char)*name) < ' ')
135                                                 *name = ' ';
136                                         name++;
137                                         n--;
138                                 }
139                                 *name = 0;
140                                 if(buf[0])
141                                         curstatus.cmd = strdup(buf);
142                                 /* if NULL it work true also */
143                         }
144                 }
145                 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
146         }
147 }
148
149 /* END CODE */
150 /*
151 Local Variables:
152 c-file-style: "linux"
153 c-basic-offset: 4
154 tab-width: 4
155 End:
156 */