Tito writes:
[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 #ifdef CONFIG_SELINUX
21         , int use_selinux , security_id_t *sid
22 #endif
23         )
24 {
25         static DIR *dir;
26         struct dirent *entry;
27         static procps_status_t ret_status;
28         char *name;
29         int n;
30         char status[32];
31         char buf[1024];
32         FILE *fp;
33         procps_status_t curstatus;
34         int pid;
35         long tasknice;
36         struct stat sb;
37
38         if (!dir) {
39                 dir = opendir("/proc");
40                 if(!dir)
41                         bb_error_msg_and_die("Can't open /proc");
42         }
43         for(;;) {
44                 if((entry = readdir(dir)) == NULL) {
45                         closedir(dir);
46                         dir = 0;
47                         return 0;
48                 }
49                 name = entry->d_name;
50                 if (!(*name >= '0' && *name <= '9'))
51                         continue;
52
53                 memset(&curstatus, 0, sizeof(procps_status_t));
54                 pid = atoi(name);
55                 curstatus.pid = pid;
56
57                 sprintf(status, "/proc/%d", pid);
58                 if(stat(status, &sb))
59                         continue;
60                 my_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
61
62                 sprintf(status, "/proc/%d/stat", pid);
63                 if((fp = fopen(status, "r")) == NULL)
64                         continue;
65 #ifdef CONFIG_SELINUX
66                 if(use_selinux)
67                 {
68                         if(fstat_secure(fileno(fp), &sb, sid))
69                                 continue;
70                 }
71                 else
72 #endif
73                 name = fgets(buf, sizeof(buf), fp);
74                 fclose(fp);
75                 if(name == NULL)
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 FEATURE_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 FEATURE_CPU_USAGE_PERCENTAGE
98                 &curstatus.utime, &curstatus.stime,
99 #endif
100                 &tasknice,
101                 &curstatus.rss);
102 #ifdef FEATURE_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                         sprintf(status, "/proc/%d/cmdline", pid);
128                         if((fp = fopen(status, "r")) == NULL)
129                                 continue;
130                         if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
131                                 if(buf[n-1]=='\n')
132                                         buf[--n] = 0;
133                                 name = buf;
134                                 while(n) {
135                                         if(((unsigned char)*name) < ' ')
136                                                 *name = ' ';
137                                         name++;
138                                         n--;
139                                 }
140                                 *name = 0;
141                                 if(buf[0])
142                                         curstatus.cmd = strdup(buf);
143                                 /* if NULL it work true also */
144                         }
145                         fclose(fp);
146                 }
147                 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
148         }
149 }
150
151 /* END CODE */
152 /*
153 Local Variables:
154 c-file-style: "linux"
155 c-basic-offset: 4
156 tab-width: 4
157 End:
158 */