diff options
Diffstat (limited to 'libbb/procps.c')
-rw-r--r-- | libbb/procps.c | 51 |
1 files changed, 20 insertions, 31 deletions
diff --git a/libbb/procps.c b/libbb/procps.c index 4f9705bc1..8c9cac125 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -21,40 +21,29 @@ typedef struct cache_t { | |||
21 | int size; | 21 | int size; |
22 | } cache_t; | 22 | } cache_t; |
23 | 23 | ||
24 | static cache_t username, groupname; | 24 | static cache_t *cache_user_group; |
25 | 25 | ||
26 | static void clear_cache(cache_t *cp) | ||
27 | { | ||
28 | free(cp->cache); | ||
29 | cp->cache = NULL; | ||
30 | cp->size = 0; | ||
31 | } | ||
32 | void FAST_FUNC clear_username_cache(void) | 26 | void FAST_FUNC clear_username_cache(void) |
33 | { | 27 | { |
34 | clear_cache(&username); | 28 | if (cache_user_group) { |
35 | clear_cache(&groupname); | 29 | free(cache_user_group[0].cache); |
36 | } | 30 | free(cache_user_group[1].cache); |
37 | 31 | free(cache_user_group); | |
38 | #if 0 /* more generic, but we don't need that yet */ | 32 | cache_user_group = NULL; |
39 | /* Returns -N-1 if not found. */ | 33 | } |
40 | /* cp->cache[N] is allocated and must be filled in this case */ | ||
41 | static int get_cached(cache_t *cp, uid_t id) | ||
42 | { | ||
43 | int i; | ||
44 | for (i = 0; i < cp->size; i++) | ||
45 | if (cp->cache[i].id == id) | ||
46 | return i; | ||
47 | i = cp->size++; | ||
48 | cp->cache = xrealloc_vector(cp->cache, 2, i); | ||
49 | cp->cache[i++].id = id; | ||
50 | return -i; | ||
51 | } | 34 | } |
52 | #endif | ||
53 | 35 | ||
54 | static char* get_cached(cache_t *cp, uid_t id, | 36 | static char* get_cached(int user_group, uid_t id, |
55 | char* FAST_FUNC x2x_utoa(uid_t id)) | 37 | char* FAST_FUNC x2x_utoa(uid_t id)) |
56 | { | 38 | { |
39 | cache_t *cp; | ||
57 | int i; | 40 | int i; |
41 | |||
42 | if (!cache_user_group) | ||
43 | cache_user_group = xzalloc(sizeof(cache_user_group[0]) * 2); | ||
44 | |||
45 | cp = &cache_user_group[user_group]; | ||
46 | |||
58 | for (i = 0; i < cp->size; i++) | 47 | for (i = 0; i < cp->size; i++) |
59 | if (cp->cache[i].id == id) | 48 | if (cp->cache[i].id == id) |
60 | return cp->cache[i].name; | 49 | return cp->cache[i].name; |
@@ -67,11 +56,11 @@ static char* get_cached(cache_t *cp, uid_t id, | |||
67 | } | 56 | } |
68 | const char* FAST_FUNC get_cached_username(uid_t uid) | 57 | const char* FAST_FUNC get_cached_username(uid_t uid) |
69 | { | 58 | { |
70 | return get_cached(&username, uid, uid2uname_utoa); | 59 | return get_cached(0, uid, uid2uname_utoa); |
71 | } | 60 | } |
72 | const char* FAST_FUNC get_cached_groupname(gid_t gid) | 61 | const char* FAST_FUNC get_cached_groupname(gid_t gid) |
73 | { | 62 | { |
74 | return get_cached(&groupname, gid, gid2group_utoa); | 63 | return get_cached(1, gid, gid2group_utoa); |
75 | } | 64 | } |
76 | 65 | ||
77 | #if !ENABLE_PLATFORM_MINGW32 | 66 | #if !ENABLE_PLATFORM_MINGW32 |
@@ -95,15 +84,15 @@ static int read_to_buf(const char *filename, void *buf) | |||
95 | 84 | ||
96 | static procps_status_t* FAST_FUNC alloc_procps_scan(void) | 85 | static procps_status_t* FAST_FUNC alloc_procps_scan(void) |
97 | { | 86 | { |
98 | unsigned n = getpagesize(); | ||
99 | procps_status_t* sp = xzalloc(sizeof(procps_status_t)); | 87 | procps_status_t* sp = xzalloc(sizeof(procps_status_t)); |
100 | sp->dir = xopendir("/proc"); | 88 | unsigned n = bb_getpagesize(); |
101 | while (1) { | 89 | while (1) { |
102 | n >>= 1; | 90 | n >>= 1; |
103 | if (!n) break; | 91 | if (!n) break; |
104 | sp->shift_pages_to_bytes++; | 92 | sp->shift_pages_to_bytes++; |
105 | } | 93 | } |
106 | sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10; | 94 | sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10; |
95 | sp->dir = xopendir("/proc"); | ||
107 | return sp; | 96 | return sp; |
108 | } | 97 | } |
109 | 98 | ||
@@ -178,6 +167,7 @@ static char *skip_fields(char *str, int count) | |||
178 | } | 167 | } |
179 | #endif | 168 | #endif |
180 | 169 | ||
170 | #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP | ||
181 | static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix) | 171 | static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix) |
182 | { | 172 | { |
183 | char *tp = is_prefixed_with(buf, prefix); | 173 | char *tp = is_prefixed_with(buf, prefix); |
@@ -187,7 +177,6 @@ static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix) | |||
187 | return tp; | 177 | return tp; |
188 | } | 178 | } |
189 | 179 | ||
190 | #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP | ||
191 | int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total, | 180 | int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total, |
192 | void (*cb)(struct smaprec *, void *), void *data) | 181 | void (*cb)(struct smaprec *, void *), void *data) |
193 | { | 182 | { |