c15b3e0a4bfd3666a23f018add49f96574fe2711
[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 #ifndef PAGE_MASK
20 #define PAGE_MASK               (~((getpagesize())-1))
21 #endif
22
23 extern procps_status_t * procps_scan(int save_user_arg0
24 #ifdef CONFIG_SELINUX
25         , int use_selinux , security_id_t *sid
26 #endif
27         )
28 {
29         static DIR *dir;
30         struct dirent *entry;
31         static procps_status_t ret_status;
32         char *name;
33         int n;
34         char status[32];
35         char buf[1024];
36         FILE *fp;
37         procps_status_t curstatus;
38         int pid;
39         long tasknice;
40         struct stat sb;
41
42         if (!dir) {
43                 dir = opendir("/proc");
44                 if(!dir)
45                         bb_error_msg_and_die("Can't open /proc");
46         }
47         for(;;) {
48                 if((entry = readdir(dir)) == NULL) {
49                         closedir(dir);
50                         dir = 0;
51                         return 0;
52                 }
53                 name = entry->d_name;
54                 if (!(*name >= '0' && *name <= '9'))
55                         continue;
56
57                 memset(&curstatus, 0, sizeof(procps_status_t));
58                 pid = atoi(name);
59                 curstatus.pid = pid;
60
61                 sprintf(status, "/proc/%d/stat", pid);
62                 if((fp = fopen(status, "r")) == NULL)
63                         continue;
64 #ifdef CONFIG_SELINUX
65                 if(use_selinux)
66                 {
67                         if(fstat_secure(fileno(fp), &sb, sid))
68                                 continue;
69                 }
70                 else
71 #endif
72                 if(fstat(fileno(fp), &sb))
73                         continue;
74                 my_getpwuid(curstatus.user, sb.st_uid);
75                 name = fgets(buf, sizeof(buf), fp);
76                 fclose(fp);
77                 if(name == NULL)
78                         continue;
79                 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
80                 if(name == 0 || name[1] != ' ')
81                         continue;
82                 *name = 0;
83                 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
84                 n = sscanf(name+2,
85                 "%c %d "
86                 "%*s %*s %*s %*s "     /* pgrp, session, tty, tpgid */
87                 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
88 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
89                 "%lu %lu "
90 #else
91                 "%*s %*s "
92 #endif
93                 "%*s %*s %*s "         /* cutime, cstime, priority */
94                 "%ld "
95                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
96                 "%*s "                 /* vsize */
97                 "%ld",
98                 curstatus.state, &curstatus.ppid,
99 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
100                 &curstatus.utime, &curstatus.stime,
101 #endif
102                 &tasknice,
103                 &curstatus.rss);
104 #ifdef FEATURE_CPU_USAGE_PERCENTAGE
105                 if(n != 6)
106 #else
107                 if(n != 4)
108 #endif
109                         continue;
110
111                 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
112                         curstatus.state[1] = 'W';
113                 else
114                         curstatus.state[1] = ' ';
115                 if (tasknice < 0)
116                         curstatus.state[2] = '<';
117                 else if (tasknice > 0)
118                         curstatus.state[2] = 'N';
119                 else
120                         curstatus.state[2] = ' ';
121
122                 curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
123
124                 if(save_user_arg0) {
125                         sprintf(status, "/proc/%d/cmdline", pid);
126                         if((fp = fopen(status, "r")) == NULL)
127                                 continue;
128                         if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
129                                 if(buf[n-1]=='\n')
130                                         buf[--n] = 0;
131                                 name = buf;
132                                 while(n) {
133                                         if(((unsigned char)*name) < ' ')
134                                                 *name = ' ';
135                                         name++;
136                                         n--;
137                                 }
138                                 *name = 0;
139                                 if(buf[0])
140                                         curstatus.cmd = strdup(buf);
141                                 /* if NULL it work true also */
142                         }
143                         fclose(fp);
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 */