Merge branch 'kernel' of 106.109.8.71:/srv/git/dbi into kernel
[kernel/swap-modules.git] / parser / msg_parser.c
1 #include <linux/slab.h>
2 #include "msg_parser.h"
3 #include "msg_buf.h"
4 #include "parser_defs.h"
5
6
7 static int str_to_u32(const char* str, u32 *val)
8 {
9         u32 result;
10         if(!str || !*str)
11                 return -EINVAL;
12
13         for (result = 0 ; *str; ++str) {
14                 if (*str < '0' || *str> '9')
15                         return -EINVAL;
16
17                 result = result * 10 + (*str - '0');
18         }
19
20         *val = result;
21
22         return 0;
23 }
24
25
26
27
28
29 /* ============================================================================
30  * ==                               APP_INFO                                 ==
31  * ============================================================================
32  */
33 struct app_info_data *create_app_info(struct msg_buf *mb)
34 {
35         int ret;
36         struct app_info_data *ai;
37         u32 app_type;
38         char *ta_id, *exec_path;
39
40         ret = get_u32(mb, &app_type);
41         if (ret) {
42                 print_err("failed to read target application type\n");
43                 return NULL;
44         }
45
46         ret = get_string(mb, &ta_id);
47         if (ret) {
48                 print_err("failed to read target application ID\n");
49                 return NULL;
50         }
51
52         ret = get_string(mb, &exec_path);
53         if (ret) {
54                 print_err("failed to read executable path\n");
55                 goto free_ta_id;
56         }
57
58         ai = kmalloc(sizeof(*ai), GFP_KERNEL);
59         if (ai == NULL) {
60                 print_err("out of memory\n");
61                 goto free_exec_path;
62         }
63
64         switch (app_type) {
65         case AT_TIZEN_NATIVE_APP:
66         case AT_COMMON_EXEC:
67                 ai->at_data = ta_id;
68                 break;
69         case AT_PID: {
70                 u32 pid;
71                 ret = str_to_u32(ta_id, &pid);
72                 if (ret) {
73                         print_err("converting string to PID, str='%s'\n", ta_id);
74                         goto free_ai;
75                 }
76
77                 ai->at_data = (void *)pid;
78                 break;
79         }
80         default:
81                 print_err("wrong application type(%u)\n", app_type);
82                 ret = -EINVAL;
83                 goto free_ai;
84         }
85
86         ai->app_type = (enum APP_TYPE)app_type;
87         ai->exec_path = exec_path;
88
89         return ai;
90
91 free_ai:
92         kfree(ai);
93
94 free_exec_path:
95         put_strung(exec_path);
96
97 free_ta_id:
98         put_strung(ta_id);
99
100         return NULL;
101 }
102
103 void destroy_app_info(struct app_info_data *ai)
104 {
105         switch (ai->app_type) {
106         case AT_TIZEN_NATIVE_APP:
107         case AT_COMMON_EXEC:
108                 put_strung(ai->at_data);
109                 break;
110
111         case AT_PID:
112                 break;
113
114         default:
115                 print_err("wrong application type(%u)\n", ai->app_type);
116                 break;
117         }
118
119         put_strung(ai->exec_path);
120         kfree(ai);
121 }
122
123
124
125
126
127 /* ============================================================================
128  * ==                                CONFIG                                  ==
129  * ============================================================================
130  */
131 struct conf_data *create_conf_data(struct msg_buf *mb)
132 {
133         struct conf_data *conf;
134         u64 uf;
135         u32 stp, dmp;
136
137         if (get_u64(mb, &uf)) {
138                 print_err("failed to read use_features\n");
139                 return NULL;
140         }
141
142         if (get_u32(mb, &stp)) {
143                 print_err("failed to read sys trace period\n");
144                 return NULL;
145         }
146
147         if (get_u32(mb, &dmp)) {
148                 print_err("failed to read data message period\n");
149                 return NULL;
150         }
151
152         conf = kmalloc(sizeof(*conf), GFP_KERNEL);
153         if (conf == NULL) {
154                 print_err("out of memory\n");
155                 return NULL;
156         }
157
158         conf->use_features = uf;
159         conf->sys_trace_period = stp;
160         conf->data_msg_period = dmp;
161
162         return conf;
163 }
164
165 void destroy_conf_data(struct conf_data *conf)
166 {
167         kfree(conf);
168 }
169
170
171
172
173
174 /* ============================================================================
175  * ==                               FUNC_INST                                ==
176  * ============================================================================
177  */
178 struct func_inst_data *create_func_inst_data(struct msg_buf *mb)
179 {
180         struct func_inst_data *fi;
181         u64 addr;
182         char *args;
183
184         if (get_u64(mb, &addr)) {
185                 print_err("failed to read data function address\n");
186                 return NULL;
187         }
188
189         if (get_string(mb, &args)) {
190                 print_err("failed to read data function arguments\n");
191                 return NULL;
192         }
193
194         fi = kmalloc(sizeof(*fi), GFP_KERNEL);
195         if (fi == NULL) {
196                 print_err("out of memory\n");
197                 put_strung(args);
198                 return NULL;
199         }
200
201         fi->addr = addr;
202         fi->args = args;
203
204         return fi;
205 }
206
207 void destroy_func_inst_data(struct func_inst_data *fi)
208 {
209         put_strung(fi->args);
210         kfree(fi);
211 }
212
213
214
215
216
217 /* ============================================================================
218  * ==                               LIB_INST                                 ==
219  * ============================================================================
220  */
221 struct lib_inst_data *create_lib_inst_data(struct msg_buf *mb)
222 {
223         struct lib_inst_data *li;
224         struct func_inst_data *fi;
225         char *path;
226         u32 cnt, j, i = 0;
227
228         if (get_string(mb, &path)) {
229                 print_err("failed to read path of binary\n");
230                 return NULL;
231         }
232
233         if (get_u32(mb, &cnt)) {
234                 print_err("failed to read count of functions\n");
235                 return NULL;
236         }
237
238         if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt) {
239                 print_err("to match count of functions(%u)\n", cnt);
240                 return NULL;
241         }
242
243         li = kmalloc(sizeof(*li), GFP_KERNEL);
244         if (li == NULL)
245         if (li == NULL) {
246                 print_err("out of memory\n");
247                 goto free_path;
248         }
249
250         li->func = kmalloc(sizeof(struct func_inst_data *) * cnt, GFP_KERNEL);
251         if (li->func == NULL)
252         if (li->func == NULL) {
253                 print_err("out of memory\n");
254                 goto free_li;
255         }
256
257         for (i = 0; i < cnt; ++i) {
258                 fi = create_func_inst_data(mb);
259                 if (fi == NULL)
260                         goto free_func;
261
262                 li->func[i] = fi;
263         }
264
265         li->path = path;
266         li->cnt_func = cnt;
267
268         return li;
269
270 free_func:
271         for (j = 0; j < i; ++j)
272                 destroy_func_inst_data(li->func[j]);
273         kfree(li->func);
274
275 free_li:
276         kfree(li);
277
278 free_path:
279         put_strung(path);
280
281         return NULL;
282 }
283
284 void destroy_lib_inst_data(struct lib_inst_data *li)
285 {
286         int i;
287
288         put_strung(li->path);
289
290         for (i = 0; i < li->cnt_func; ++i)
291                 destroy_func_inst_data(li->func[i]);
292
293         kfree(li->func);
294         kfree(li);
295 }
296
297
298
299
300
301 /* ============================================================================
302  * ==                               APP_INST                                 ==
303  * ============================================================================
304  */
305 struct app_inst_data *create_app_inst_data(struct msg_buf *mb)
306 {
307         struct app_inst_data *app_inst;
308         struct app_info_data *app_info;
309         struct func_inst_data *func;
310         struct lib_inst_data *lib;
311         u32 cnt_func, i_func = 0, cnt_lib, i_lib = 0, i;
312
313         app_info = create_app_info(mb);
314         if (app_info == NULL)
315                 return NULL;
316
317         if (get_u32(mb, &cnt_func)) {
318                 print_err("failed to read count of functions\n");
319                 goto free_app_info;
320         }
321
322         if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt_func) {
323                 print_err("to match count of functions(%u)\n", cnt_func);
324                 goto free_app_info;
325         }
326
327         app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
328         if (app_inst == NULL) {
329                 print_err("out of memory\n");
330                 goto free_app_info;
331         }
332
333         app_inst->func = kmalloc(sizeof(struct func_inst_data *) * cnt_func,
334                                  GFP_KERNEL);
335         if (app_inst->func == NULL) {
336                 print_err("out of memory\n");
337                 goto free_app_inst;
338         }
339
340         for (i_func = 0; i_func < cnt_func; ++i_func) {
341                 func = create_func_inst_data(mb);
342                 if (func == NULL)
343                         goto free_func;
344
345                 app_inst->func[i_func] = func;
346         }
347
348         if (get_u32(mb, &cnt_lib)) {
349                 print_err("failed to read count of libraries\n");
350                 goto free_func;
351         }
352
353         if (remained_mb(mb) / MIN_SIZE_LIB_INST < cnt_lib) {
354                 print_err("to match count of libraries(%u)\n", cnt_lib);
355                 goto free_func;
356         }
357
358         app_inst->lib = kmalloc(sizeof(struct lib_inst_data *) * cnt_lib,
359                                 GFP_KERNEL);
360         if (app_inst->lib == NULL) {
361                 print_err("out of memory\n");
362                 goto free_func;
363         }
364
365         for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
366                 lib = create_lib_inst_data(mb);
367                 if (lib == NULL)
368                         goto free_lib;
369
370                 app_inst->lib[i_lib] = lib;
371         }
372
373         app_inst->app_info = app_info;
374         app_inst->cnt_func = cnt_func;
375         app_inst->cnt_lib = cnt_lib;
376
377         return app_inst;
378
379 free_lib:
380         for (i = 0; i < i_lib; ++i)
381                 destroy_lib_inst_data(app_inst->lib[i]);
382         kfree(app_inst->lib);
383
384 free_func:
385         for (i = 0; i < i_func; ++i)
386                 destroy_func_inst_data(app_inst->func[i]);
387         kfree(app_inst->func);
388
389 free_app_inst:
390         kfree(app_inst);
391
392 free_app_info:
393         destroy_app_info(app_info);
394
395         return NULL;
396 }
397
398 void destroy_app_inst_data(struct app_inst_data *ai)
399 {
400         int i;
401
402         for (i = 0; i < ai->cnt_lib; ++i)
403                 destroy_lib_inst_data(ai->lib[i]);
404         kfree(ai->lib);
405
406         for (i = 0; i < ai->cnt_func; ++i)
407                 destroy_func_inst_data(ai->func[i]);
408         kfree(ai->func);
409
410         destroy_app_info(ai->app_info);
411         kfree(ai);
412 }
413
414
415
416
417
418 /* ============================================================================
419  * ==                                US_INST                                 ==
420  * ============================================================================
421  */
422 struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
423 {
424         struct us_inst_data *ui;
425         struct app_inst_data *ai;
426         u32 cnt, j, i = 0;
427
428         if (get_u32(mb, &cnt)) {
429                 print_err("failed to read count of applications\n");
430                 return NULL;
431         }
432
433         if (remained_mb(mb) / MIN_SIZE_APP_INST < cnt) {
434                 print_err("to match count of applications(%u)\n", cnt);
435                 return NULL;
436         }
437
438         ui = kmalloc(sizeof(struct us_inst_data), GFP_KERNEL);
439         if (ui == NULL) {
440                 print_err("out of memory\n");
441                 return NULL;
442         }
443
444         ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
445                                GFP_KERNEL);
446         if (ui->app_inst == NULL) {
447                 print_err("out of memory\n");
448                 goto free_ui;
449         }
450
451         for (i = 0; i < cnt; ++i) {
452                 ai = create_app_inst_data(mb);
453                 if (ai == NULL)
454                         goto free_app_inst;
455
456                 ui->app_inst[i] = ai;
457         }
458
459         ui->cnt = cnt;
460
461         return ui;
462
463 free_app_inst:
464         for (j = 0; j < i; ++j)
465                 destroy_app_inst_data(ui->app_inst[j]);
466         kfree(ui->app_inst);
467
468 free_ui:
469         kfree(ui);
470
471         return NULL;
472 }
473
474 void destroy_us_inst_data(struct us_inst_data *ui)
475 {
476         int i;
477
478         for (i = 0; i < ui->cnt; ++i)
479                 destroy_app_inst_data(ui->app_inst[i]);
480
481         kfree(ui->app_inst);
482         kfree(ui);
483 }