avoid signed<->unsigned warning
[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                         bb_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", pid);
54                 if(stat(status, &sb))
55                         continue;
56                 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
57
58                 sprintf(status, "/proc/%d/stat", pid);
59
60                 if((fp = fopen(status, "r")) == NULL)
61                         continue;
62                 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 */
75 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
76                 "%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,
86 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
87                 &curstatus.utime, &curstatus.stime,
88 #endif
89                 &tasknice,
90                 &curstatus.rss);
91 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
92                 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
109 #ifdef PAGE_SHIFT
110                 curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
111 #else
112                 curstatus.rss *= (getpagesize() >> 10);     /* 2**10 = 1kb */
113 #endif
114
115                 if(save_user_arg0) {
116                         sprintf(status, "/proc/%d/cmdline", pid);
117                         if((fp = fopen(status, "r")) == NULL)
118                                 continue;
119                         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) {
124                                         if(((unsigned char)*name) < ' ')
125                                                 *name = ' ';
126                                         name++;
127                                         n--;
128                                 }
129                                 *name = 0;
130                                 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
140 /* END CODE */
141 /*
142 Local Variables:
143 c-file-style: "linux"
144 c-basic-offset: 4
145 tab-width: 4
146 End:
147 */