• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/corennnnn


Commit MetaInfo

Revision06e23259722a7d69e392d9e01eb9da0c57007bd2 (tree)
Zeit2015-12-22 12:24:42
AutorChih-Wei Huang <cwhuang@linu...>
CommiterChih-Wei Huang

Log Message

libcutils: refine probe_module

Make the code be more elegant and fix the realloc bug.

Ändern Zusammenfassung

Diff

--- a/libcutils/probe_module.c
+++ b/libcutils/probe_module.c
@@ -27,7 +27,6 @@
2727 #include <cutils/log.h>
2828
2929 #define LDM_DEFAULT_MOD_PATH "/system/lib/modules/"
30-#define LDM_INIT_DEP_NUM 10
3130
3231 extern int init_module(void *, unsigned long, const char *);
3332 extern int delete_module(const char *, unsigned int);
@@ -57,17 +56,10 @@ static void dump_dep(char **dep)
5756 ALOGD("DUMP DEP: %s\n", dep[d]);
5857 }
5958
60-static char * strip_path(const char * const str)
59+static char *strip_path(char *str)
6160 {
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;
7163 }
7264
7365 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)
111103 static int is_target_module(char *line, const char *target)
112104 {
113105 char *token;
114- char *name;
115- size_t name_len;
106+ char name[PATH_MAX];
116107 const char *suffix = ".ko";
117108 const char *delimiter = ":";
118109 int ret = 0;
@@ -129,24 +120,13 @@ static int is_target_module(char *line, const char *target)
129120 *token = '\0';
130121
131122 /* 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");
142124
143- ret = !match_name(strip_path(line), name, name_len);
125+ ret = !match_name(strip_path(line), name, strlen(name));
144126
145127 /* restore [single] token, keep line unchanged until we parse it later */
146128 *token = *delimiter;
147129
148- free(name);
149-
150130 return ret;
151131
152132 }
@@ -159,45 +139,21 @@ static int is_target_module(char *line, const char *target)
159139 */
160140 static char** setup_dep(char *line)
161141 {
162- char *tmp;
142+ char *tmp = line;
163143 char *brk;
164- int dep_num = LDM_INIT_DEP_NUM;
165- char **new;
166144 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;
175146
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++;
197149
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));
198156 }
199- /* terminate array with a null pointer */
200- dep[i] = NULL;
201157
202158 return dep;
203159 }
@@ -241,10 +197,10 @@ static int insmod(const char *path_name, const char *args)
241197 static int insmod_s(char *dep[], const char *args, int strip, const char *base)
242198 {
243199 char *name;
244- char *path_name;
245200 int cnt;
246201 size_t len;
247202 int ret = 0;
203+ char path_name[PATH_MAX];
248204 char def_mod_path[PATH_MAX];
249205 const char *base_dir;
250206
@@ -257,30 +213,15 @@ static int insmod_s(char *dep[], const char *args, int strip, const char *base)
257213 for (cnt = 0; dep[cnt]; cnt++)
258214 ;
259215
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));
272217
273- snprintf(path_name, len, "%s%s", base_dir, name);
218+ while (!ret && cnt--) {
274219
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];
279221
280- free(path_name);
222+ strcpy(path_name + len, name);
281223
282- if (ret)
283- break;
224+ ret = insmod(path_name, cnt ? "" : args);
284225 }
285226
286227 return ret;