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