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