system/corennnnn
Revision | 636157cc20f4a0be4bf2be95d5c64b74ff16f211 (tree) |
---|---|
Time | 2016-07-27 03:54:20 |
Author | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Jaap Jan Meijer |
libcutils: refine probe_module
Make the code be more elegant and fix the realloc bug.
@@ -27,7 +27,6 @@ | ||
27 | 27 | #include <cutils/log.h> |
28 | 28 | |
29 | 29 | #define LDM_DEFAULT_MOD_PATH "/system/lib/modules/" |
30 | -#define LDM_INIT_DEP_NUM 10 | |
31 | 30 | |
32 | 31 | extern int init_module(void *, unsigned long, const char *); |
33 | 32 | extern int delete_module(const char *, unsigned int); |
@@ -57,17 +56,10 @@ static void dump_dep(char **dep) | ||
57 | 56 | ALOGD("DUMP DEP: %s\n", dep[d]); |
58 | 57 | } |
59 | 58 | |
60 | -static char * strip_path(const char * const str) | |
59 | +static char *strip_path(char *str) | |
61 | 60 | { |
62 | - char *ptr; | |
63 | - int i; | |
64 | - | |
65 | - /* initialize pos to terminator */ | |
66 | - for (i = strlen(str); i > 0; i--) | |
67 | - if (str[i - 1] == '/') | |
68 | - break; | |
69 | - | |
70 | - return (char *)&str[i]; | |
61 | + char *ptr = strrchr(str, '/'); | |
62 | + return ptr ? ptr + 1 : str; | |
71 | 63 | } |
72 | 64 | |
73 | 65 | static void hyphen_to_underscore(char *str) |
@@ -111,8 +103,7 @@ static int match_name(const char *s1, const char *s2, const size_t size) | ||
111 | 103 | static int is_target_module(char *line, const char *target) |
112 | 104 | { |
113 | 105 | char *token; |
114 | - char *name; | |
115 | - size_t name_len; | |
106 | + char name[PATH_MAX]; | |
116 | 107 | const char *suffix = ".ko"; |
117 | 108 | const char *delimiter = ":"; |
118 | 109 | int ret = 0; |
@@ -129,24 +120,13 @@ static int is_target_module(char *line, const char *target) | ||
129 | 120 | *token = '\0'; |
130 | 121 | |
131 | 122 | /* use "module.ko" in comparision */ |
132 | - name_len = strlen(suffix) + strlen(target) + 1; | |
133 | - | |
134 | - name = malloc(sizeof(char) * name_len); | |
135 | - | |
136 | - if (!name) { | |
137 | - ALOGE("cannot alloc ram for comparision\n"); | |
138 | - return 0; | |
139 | - } | |
140 | - | |
141 | - snprintf(name, name_len, "%s%s", target, suffix); | |
123 | + strcat(strcpy(name, target), ".ko"); | |
142 | 124 | |
143 | - ret = !match_name(strip_path(line), name, name_len); | |
125 | + ret = !match_name(strip_path(line), name, strlen(name)); | |
144 | 126 | |
145 | 127 | /* restore [single] token, keep line unchanged until we parse it later */ |
146 | 128 | *token = *delimiter; |
147 | 129 | |
148 | - free(name); | |
149 | - | |
150 | 130 | return ret; |
151 | 131 | |
152 | 132 | } |
@@ -159,45 +139,21 @@ static int is_target_module(char *line, const char *target) | ||
159 | 139 | */ |
160 | 140 | static char** setup_dep(char *line) |
161 | 141 | { |
162 | - char *tmp; | |
142 | + char *tmp = line; | |
163 | 143 | char *brk; |
164 | - int dep_num = LDM_INIT_DEP_NUM; | |
165 | - char **new; | |
166 | 144 | int i; |
167 | - char **dep = NULL; | |
168 | - | |
169 | - dep = malloc(sizeof(char *) * dep_num); | |
170 | - | |
171 | - if (!dep) { | |
172 | - ALOGE("cannot alloc dep array\n"); | |
173 | - return dep; | |
174 | - } | |
145 | + char **dep; | |
175 | 146 | |
176 | - for (i = 0, tmp = strtok_r(line, ": ", &brk); | |
177 | - tmp; | |
178 | - tmp = strtok_r(NULL, ": ", &brk), i++) { | |
179 | - | |
180 | - /* check if we need enlarge dep array */ | |
181 | - if (!(i < dep_num - 1)) { | |
182 | - | |
183 | - dep_num += LDM_INIT_DEP_NUM; | |
184 | - | |
185 | - new = realloc(dep, dep_num); | |
186 | - | |
187 | - if (!new) { | |
188 | - ALOGE("failed to enlarge dep buffer\n"); | |
189 | - free(dep); | |
190 | - return NULL; | |
191 | - } | |
192 | - else | |
193 | - dep = new; | |
194 | - } | |
195 | - | |
196 | - dep[i] = tmp; | |
147 | + for (i = 2; (tmp = strchr(tmp, ' ')); i++) | |
148 | + tmp++; | |
197 | 149 | |
150 | + dep = malloc(sizeof(char *) * i); | |
151 | + if (dep) { | |
152 | + i = 0; | |
153 | + do { | |
154 | + tmp = strtok_r(i ? NULL : line, ": ", &brk); | |
155 | + } while ((dep[i++] = tmp)); | |
198 | 156 | } |
199 | - /* terminate array with a null pointer */ | |
200 | - dep[i] = NULL; | |
201 | 157 | |
202 | 158 | return dep; |
203 | 159 | } |
@@ -241,10 +197,10 @@ static int insmod(const char *path_name, const char *args) | ||
241 | 197 | static int insmod_s(char *dep[], const char *args, int strip, const char *base) |
242 | 198 | { |
243 | 199 | char *name; |
244 | - char *path_name; | |
245 | 200 | int cnt; |
246 | 201 | size_t len; |
247 | 202 | int ret = 0; |
203 | + char path_name[PATH_MAX]; | |
248 | 204 | char def_mod_path[PATH_MAX]; |
249 | 205 | const char *base_dir; |
250 | 206 |
@@ -257,30 +213,15 @@ static int insmod_s(char *dep[], const char *args, int strip, const char *base) | ||
257 | 213 | for (cnt = 0; dep[cnt]; cnt++) |
258 | 214 | ; |
259 | 215 | |
260 | - while (cnt--) { | |
261 | - | |
262 | - name = strip ? strip_path(dep[cnt]) : dep[cnt]; | |
263 | - | |
264 | - len = strlen(base_dir) + strlen(name) + 1; | |
265 | - | |
266 | - path_name = malloc(sizeof(char) * len); | |
267 | - | |
268 | - if (!path_name) { | |
269 | - ALOGE("alloc module [%s] path failed\n", path_name); | |
270 | - return -1; | |
271 | - } | |
216 | + len = strlen(strcpy(path_name, base_dir)); | |
272 | 217 | |
273 | - snprintf(path_name, len, "%s%s", base_dir, name); | |
218 | + while (!ret && cnt--) { | |
274 | 219 | |
275 | - if (cnt) | |
276 | - ret = insmod(path_name, ""); | |
277 | - else | |
278 | - ret = insmod(path_name, args); | |
220 | + name = strip ? strip_path(dep[cnt]) : dep[cnt]; | |
279 | 221 | |
280 | - free(path_name); | |
222 | + strcpy(path_name + len, name); | |
281 | 223 | |
282 | - if (ret) | |
283 | - break; | |
224 | + ret = insmod(path_name, cnt ? "" : args); | |
284 | 225 | } |
285 | 226 | |
286 | 227 | return ret; |