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