diff options
author | WANG Cong <xiyou.wangcong@gmail.com> | 2007-10-17 02:31:29 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-10-17 11:43:06 -0400 |
commit | f32143a2fedfa299d790ca96bff9b1d2e2d6996b (patch) | |
tree | 351a003cb63efe43a6e53c20483f2caeeef5737e /Documentation/vm | |
parent | 24950898ffd161f2ea67c60f398146457bca8bf0 (diff) |
Documentation/vm/slabinfo.c: clean up this code
This patch does the following cleanups for Documentation/vm/slabinfo.c:
- Fix two memory leaks;
- Constify some char pointers;
- Use snprintf instead of sprintf in case of buffer overflow;
- Fix some indentations;
- Other little improvements.
Acked-by: Christoph Lameter <clameter@sgi.com>
Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Documentation/vm')
-rw-r--r-- | Documentation/vm/slabinfo.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/Documentation/vm/slabinfo.c b/Documentation/vm/slabinfo.c index 1af7bd5a2183..7047696c47a1 100644 --- a/Documentation/vm/slabinfo.c +++ b/Documentation/vm/slabinfo.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
12 | #include <sys/types.h> | 12 | #include <sys/types.h> |
13 | #include <dirent.h> | 13 | #include <dirent.h> |
14 | #include <strings.h> | ||
14 | #include <string.h> | 15 | #include <string.h> |
15 | #include <unistd.h> | 16 | #include <unistd.h> |
16 | #include <stdarg.h> | 17 | #include <stdarg.h> |
@@ -84,7 +85,7 @@ void fatal(const char *x, ...) | |||
84 | va_start(ap, x); | 85 | va_start(ap, x); |
85 | vfprintf(stderr, x, ap); | 86 | vfprintf(stderr, x, ap); |
86 | va_end(ap); | 87 | va_end(ap); |
87 | exit(1); | 88 | exit(EXIT_FAILURE); |
88 | } | 89 | } |
89 | 90 | ||
90 | void usage(void) | 91 | void usage(void) |
@@ -119,14 +120,14 @@ void usage(void) | |||
119 | ); | 120 | ); |
120 | } | 121 | } |
121 | 122 | ||
122 | unsigned long read_obj(char *name) | 123 | unsigned long read_obj(const char *name) |
123 | { | 124 | { |
124 | FILE *f = fopen(name, "r"); | 125 | FILE *f = fopen(name, "r"); |
125 | 126 | ||
126 | if (!f) | 127 | if (!f) |
127 | buffer[0] = 0; | 128 | buffer[0] = 0; |
128 | else { | 129 | else { |
129 | if (!fgets(buffer,sizeof(buffer), f)) | 130 | if (!fgets(buffer, sizeof(buffer), f)) |
130 | buffer[0] = 0; | 131 | buffer[0] = 0; |
131 | fclose(f); | 132 | fclose(f); |
132 | if (buffer[strlen(buffer)] == '\n') | 133 | if (buffer[strlen(buffer)] == '\n') |
@@ -139,7 +140,7 @@ unsigned long read_obj(char *name) | |||
139 | /* | 140 | /* |
140 | * Get the contents of an attribute | 141 | * Get the contents of an attribute |
141 | */ | 142 | */ |
142 | unsigned long get_obj(char *name) | 143 | unsigned long get_obj(const char *name) |
143 | { | 144 | { |
144 | if (!read_obj(name)) | 145 | if (!read_obj(name)) |
145 | return 0; | 146 | return 0; |
@@ -147,7 +148,7 @@ unsigned long get_obj(char *name) | |||
147 | return atol(buffer); | 148 | return atol(buffer); |
148 | } | 149 | } |
149 | 150 | ||
150 | unsigned long get_obj_and_str(char *name, char **x) | 151 | unsigned long get_obj_and_str(const char *name, char **x) |
151 | { | 152 | { |
152 | unsigned long result = 0; | 153 | unsigned long result = 0; |
153 | char *p; | 154 | char *p; |
@@ -166,12 +167,12 @@ unsigned long get_obj_and_str(char *name, char **x) | |||
166 | return result; | 167 | return result; |
167 | } | 168 | } |
168 | 169 | ||
169 | void set_obj(struct slabinfo *s, char *name, int n) | 170 | void set_obj(struct slabinfo *s, const char *name, int n) |
170 | { | 171 | { |
171 | char x[100]; | 172 | char x[100]; |
172 | FILE *f; | 173 | FILE *f; |
173 | 174 | ||
174 | sprintf(x, "%s/%s", s->name, name); | 175 | snprintf(x, 100, "%s/%s", s->name, name); |
175 | f = fopen(x, "w"); | 176 | f = fopen(x, "w"); |
176 | if (!f) | 177 | if (!f) |
177 | fatal("Cannot write to %s\n", x); | 178 | fatal("Cannot write to %s\n", x); |
@@ -180,13 +181,13 @@ void set_obj(struct slabinfo *s, char *name, int n) | |||
180 | fclose(f); | 181 | fclose(f); |
181 | } | 182 | } |
182 | 183 | ||
183 | unsigned long read_slab_obj(struct slabinfo *s, char *name) | 184 | unsigned long read_slab_obj(struct slabinfo *s, const char *name) |
184 | { | 185 | { |
185 | char x[100]; | 186 | char x[100]; |
186 | FILE *f; | 187 | FILE *f; |
187 | int l; | 188 | size_t l; |
188 | 189 | ||
189 | sprintf(x, "%s/%s", s->name, name); | 190 | snprintf(x, 100, "%s/%s", s->name, name); |
190 | f = fopen(x, "r"); | 191 | f = fopen(x, "r"); |
191 | if (!f) { | 192 | if (!f) { |
192 | buffer[0] = 0; | 193 | buffer[0] = 0; |
@@ -453,7 +454,7 @@ void slabcache(struct slabinfo *s) | |||
453 | return; | 454 | return; |
454 | 455 | ||
455 | store_size(size_str, slab_size(s)); | 456 | store_size(size_str, slab_size(s)); |
456 | sprintf(dist_str,"%lu/%lu/%d", s->slabs, s->partial, s->cpu_slabs); | 457 | snprintf(dist_str, 40, "%lu/%lu/%d", s->slabs, s->partial, s->cpu_slabs); |
457 | 458 | ||
458 | if (!line++) | 459 | if (!line++) |
459 | first_line(); | 460 | first_line(); |
@@ -1062,6 +1063,7 @@ void read_slab_dir(void) | |||
1062 | slab->partial = get_obj("partial"); | 1063 | slab->partial = get_obj("partial"); |
1063 | slab->partial = get_obj_and_str("partial", &t); | 1064 | slab->partial = get_obj_and_str("partial", &t); |
1064 | decode_numa_list(slab->numa_partial, t); | 1065 | decode_numa_list(slab->numa_partial, t); |
1066 | free(t); | ||
1065 | slab->poison = get_obj("poison"); | 1067 | slab->poison = get_obj("poison"); |
1066 | slab->reclaim_account = get_obj("reclaim_account"); | 1068 | slab->reclaim_account = get_obj("reclaim_account"); |
1067 | slab->red_zone = get_obj("red_zone"); | 1069 | slab->red_zone = get_obj("red_zone"); |
@@ -1069,6 +1071,7 @@ void read_slab_dir(void) | |||
1069 | slab->slab_size = get_obj("slab_size"); | 1071 | slab->slab_size = get_obj("slab_size"); |
1070 | slab->slabs = get_obj_and_str("slabs", &t); | 1072 | slab->slabs = get_obj_and_str("slabs", &t); |
1071 | decode_numa_list(slab->numa, t); | 1073 | decode_numa_list(slab->numa, t); |
1074 | free(t); | ||
1072 | slab->store_user = get_obj("store_user"); | 1075 | slab->store_user = get_obj("store_user"); |
1073 | slab->trace = get_obj("trace"); | 1076 | slab->trace = get_obj("trace"); |
1074 | chdir(".."); | 1077 | chdir(".."); |
@@ -1148,7 +1151,7 @@ int main(int argc, char *argv[]) | |||
1148 | 1151 | ||
1149 | while ((c = getopt_long(argc, argv, "ad::efhil1noprstvzTS", | 1152 | while ((c = getopt_long(argc, argv, "ad::efhil1noprstvzTS", |
1150 | opts, NULL)) != -1) | 1153 | opts, NULL)) != -1) |
1151 | switch(c) { | 1154 | switch (c) { |
1152 | case '1': | 1155 | case '1': |
1153 | show_single_ref = 1; | 1156 | show_single_ref = 1; |
1154 | break; | 1157 | break; |