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