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