Kill CONFIG_FEATURE_USE_DEVPS_PATCH and the devps patch. I'm not
[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
17 #include "libbb.h"
18
19 extern procps_status_t * procps_scan(int save_user_arg0)
20 {
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)
37                         error_msg_and_die("Can't open /proc");
38         }
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
53                 sprintf(status, "/proc/%d/stat", pid);
54                 if((fp = fopen(status, "r")) == NULL)
55                         continue;
56                 if(fstat(fileno(fp), &sb))
57                         continue;
58                 my_getpwuid(curstatus.user, sb.st_uid);
59                 name = fgets(buf, sizeof(buf), fp);
60                 fclose(fp);
61                 if(name == NULL)
62                         continue;
63                 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
64                 if(name == 0 || name[1] != ' ')
65                         continue;
66                 *name = 0;
67                 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
68                 n = sscanf(name+2,
69                 "%c %d "
70                 "%*s %*s %*s %*s "     /* pgrp, session, tty, tpgid */
71                 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
72 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
73                 "%lu %lu "
74 #else
75                 "%*s %*s "
76 #endif
77                 "%*s %*s %*s "         /* cutime, cstime, priority */
78                 "%ld "
79                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
80                 "%*s "                 /* vsize */
81                 "%ld",
82                 curstatus.state, &curstatus.ppid,
83 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
84                 &curstatus.utime, &curstatus.stime,
85 #endif
86                 &tasknice,
87                 &curstatus.rss);
88 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
89                 if(n != 6)
90 #else
91                 if(n != 4)
92 #endif
93                         continue;
94
95                 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
96                         curstatus.state[1] = 'W';
97                 else
98                         curstatus.state[1] = ' ';
99                 if (tasknice < 0)
100                         curstatus.state[2] = '<';
101                 else if (tasknice > 0)
102                         curstatus.state[2] = 'N';
103                 else
104                         curstatus.state[2] = ' ';
105
106                 curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
107
108                 sprintf(status, "/proc/%d/cmdline", pid);
109                 if(save_user_arg0) {
110                         if((fp = fopen(status, "r")) == NULL)
111                                 continue;
112                         if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
113                                 if(buf[n-1]=='\n')
114                                         buf[--n] = 0;
115                                 name = buf;
116                                 while(n) {
117                                         if(((unsigned char)*name) < ' ')
118                                                 *name = ' ';
119                                         name++;
120                                         n--;
121                                 }
122                                 *name = 0;
123                                 if(buf[0])
124                                         curstatus.cmd = strdup(buf);
125                                 /* if NULL it work true also */
126                         }
127                         fclose(fp);
128                 }
129                 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
130         }
131 }
132
133 /* END CODE */
134 /*
135 Local Variables:
136 c-file-style: "linux"
137 c-basic-offset: 4
138 tab-width: 4
139 End:
140 */