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