[IMPROVE] create msg_parser
[kernel/swap-modules.git] / parser / msg_parser.c
1 #include <linux/slab.h>
2 #include "msg_parser.h"
3 #include "msg_buf.h"
4
5
6 static int str_to_u32(const char* str, u32 *val)
7 {
8         u32 result;
9         if(!str || !*str)
10                 return -EINVAL;
11
12         for (result = 0 ; *str; ++str) {
13                 if (*str < '0' || *str> '9')
14                         return -EINVAL;
15
16                 result = result * 10 + (*str - '0');
17         }
18
19         *val = result;
20
21         return 0;
22 }
23
24
25
26
27
28 /* ============================================================================
29  * ==                               APP_INFO                                 ==
30  * ============================================================================
31  */
32 struct app_info_data *create_app_info(struct msg_buf *mb)
33 {
34         int ret;
35         struct app_info_data *ai;
36         u32 app_type;
37         char *ta_id, *exec_path;
38
39         ret = get_u32(mb, &app_type);
40         if (ret)
41                 return NULL;
42
43         ret = get_string(mb, &ta_id);
44         if (ret)
45                 return NULL;
46
47         ret = get_string(mb, &exec_path);
48         if (ret)
49                 goto free_ta_id;
50
51         ai = kmalloc(sizeof(*ai), GFP_KERNEL);
52         if (ai == NULL)
53                 goto free_exec_path;
54
55         switch (app_type) {
56         case AT_TIZEN_NATIVE_APP:
57         case AT_COMMON_EXEC:
58                 ai->at_data = ta_id;
59                 break;
60         case AT_PID: {
61                 u32 pid;
62                 ret = str_to_u32(ta_id, &pid);
63                 if (ret)
64                         goto free_ai;
65
66                 ai->at_data = (void *)pid;
67                 break;
68         }
69         default:
70                 ret = -EINVAL;
71                 goto free_ai;
72         }
73
74         ai->app_type = (enum APP_TYPE)app_type;
75         ai->exec_path = exec_path;
76
77         return ai;
78
79 free_ai:
80         kfree(ai);
81
82 free_exec_path:
83         put_strung(exec_path);
84
85 free_ta_id:
86         put_strung(ta_id);
87
88         return NULL;
89 }
90
91 void destroy_app_info(struct app_info_data *ai)
92 {
93         switch (ai->app_type) {
94         case AT_TIZEN_NATIVE_APP:
95         case AT_COMMON_EXEC:
96                 put_strung(ai->at_data);
97                 break;
98
99         case AT_PID:
100                 break;
101
102         default:
103                 printk("### BUG()\n");
104                 break;
105         }
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                 return NULL;
127
128         if (get_u32(mb, &stp))
129                 return NULL;
130
131         if (get_u32(mb, &dmp))
132                 return NULL;
133
134         conf = kmalloc(sizeof(*conf), GFP_KERNEL);
135         if (conf == NULL)
136                 return NULL;
137
138         conf->use_features = uf;
139         conf->sys_trace_period = stp;
140         conf->data_msg_period = dmp;
141
142         return conf;
143 }
144
145 void destroy_conf_data(struct conf_data *conf)
146 {
147         kfree(conf);
148 }
149
150
151
152
153
154 /* ============================================================================
155  * ==                               FUNC_INST                                ==
156  * ============================================================================
157  */
158 struct func_inst_data *create_func_inst_data(struct msg_buf *mb)
159 {
160         struct func_inst_data *fi;
161         u64 addr;
162         char *args;
163
164         if (get_u64(mb, &addr))
165                 return NULL;
166
167         if (get_string(mb, &args))
168                 return NULL;
169
170         fi = kmalloc(sizeof(*fi), GFP_KERNEL);
171         if (fi == NULL) {
172                 put_strung(args);
173                 return NULL;
174         }
175
176         fi->addr = addr;
177         fi->args = args;
178
179         return fi;
180 }
181
182 void destroy_func_inst_data(struct func_inst_data *fi)
183 {
184         put_strung(fi->args);
185         kfree(fi);
186 }
187
188
189
190
191
192 /* ============================================================================
193  * ==                               LIB_INST                                 ==
194  * ============================================================================
195  */
196 struct lib_inst_data *create_lib_inst_data(struct msg_buf *mb)
197 {
198         struct lib_inst_data *li;
199         struct func_inst_data *fi;
200         char *path;
201         u32 cnt, i = 0;
202
203         if (get_string(mb, &path))
204                 return NULL;
205
206         if (get_u32(mb, &cnt))
207                 return NULL;
208
209         li = kmalloc(sizeof(*li), GFP_KERNEL);
210         if (li)
211                 goto free_path;
212
213         li->func = kmalloc(sizeof(struct func_inst_data *) * cnt, GFP_KERNEL);
214         if (li->cnt_func)
215                 goto free_li;
216
217         for (i = 0; i < cnt; ++i) {
218                 fi = create_func_inst_data(mb);
219                 if (fi == NULL)
220                         goto free_func;
221
222                 li->func[i] = fi;
223         }
224
225         li->path = path;
226         li->cnt_func = cnt;
227
228         return li;
229
230 free_func:
231         for (i -= 1; i >= 0; --i)
232                 destroy_func_inst_data(li->func[i]);
233         kfree(li->func);
234
235 free_li:
236         kfree(li);
237
238 free_path:
239         put_strung(path);
240
241         return NULL;
242 }
243
244 void destroy_lib_inst_data(struct lib_inst_data *li)
245 {
246         int i;
247
248         put_strung(li->path);
249
250         for (i = 0; i < li->cnt_func; ++i)
251                 destroy_func_inst_data(li->func[i]);
252
253         kfree(li->func);
254         kfree(li);
255 }
256
257
258
259
260
261 /* ============================================================================
262  * ==                               APP_INST                                 ==
263  * ============================================================================
264  */
265 struct app_inst_data *create_app_inst_data(struct msg_buf *mb)
266 {
267         struct app_inst_data *app_inst;
268         struct app_info_data *app_info;
269         struct func_inst_data *func;
270         struct lib_inst_data *lib;
271         u32 cnt_func, i_func = 0;
272         u32 cnt_lib, i_lib = 0;
273
274         app_info = create_app_info(mb);
275         if (app_info == NULL)
276                 return NULL;
277
278         if (get_u32(mb, &cnt_func))
279                 goto free_app_info;
280
281         app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
282         if (app_inst == NULL)
283                 goto free_app_info;
284
285         app_inst->func = kmalloc(sizeof(struct func_inst_data *) * cnt_func,
286                                  GFP_KERNEL);
287         if (app_inst->func == NULL)
288                 goto free_app_inst;
289
290         for (i_func = 0; i_func < cnt_func; ++i_func) {
291                 func = create_func_inst_data(mb);
292                 if (func == NULL)
293                         goto free_func;
294
295                 app_inst->func[i_func] = func;
296         }
297
298         if (get_u32(mb, &cnt_lib))
299                 goto free_func;
300
301         app_inst->lib = kmalloc(sizeof(struct lib_inst_data *) * cnt_lib,
302                                 GFP_KERNEL);
303         if (app_inst->lib == NULL)
304                 goto free_func;
305
306         for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
307                 lib = create_lib_inst_data(mb);
308                 if (lib == NULL)
309                         goto free_lib;
310
311                 app_inst->lib[i_lib] = lib;
312         }
313
314         app_inst->cnt_func = cnt_func;
315         app_inst->cnt_lib = cnt_lib;
316
317         return app_inst;
318
319 free_lib:
320         for (i_lib -= 1; i_lib >= 0; --i_lib)
321                 destroy_lib_inst_data(app_inst->lib[i_lib]);
322         kfree(app_inst->lib);
323
324 free_func:
325         for (i_func -= 1; i_func >= 0; --i_func)
326                 destroy_func_inst_data(app_inst->func[i_func]);
327         kfree(app_inst->func);
328
329 free_app_inst:
330         kfree(app_inst);
331
332 free_app_info:
333         destroy_app_info(app_info);
334
335         return NULL;
336 }
337
338 void destroy_app_inst_data(struct app_inst_data *ai)
339 {
340         int i;
341
342         for (i = 0; i < ai->cnt_lib; ++i)
343                 destroy_lib_inst_data(ai->lib[i]);
344         kfree(ai->lib);
345
346         for (i = 0; i < ai->cnt_func; ++i)
347                 destroy_func_inst_data(ai->func[i]);
348         kfree(ai->func);
349
350         destroy_app_info(ai->app_info);
351         kfree(ai);
352 }
353
354
355
356
357
358 /* ============================================================================
359  * ==                                US_INST                                 ==
360  * ============================================================================
361  */
362 struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
363 {
364         struct us_inst_data *ui;
365         struct app_inst_data *ai;
366         u32 cnt, i = 0;
367
368         if (get_u32(mb, &cnt))
369                 return NULL;
370
371         ui = kmalloc(sizeof(struct us_inst_data) * cnt, GFP_KERNEL);
372         if (ui == NULL)
373                 return NULL;
374
375         ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
376                                GFP_KERNEL);
377         if (ui->app_inst == NULL)
378                 goto free_ui;
379
380         for (i = 0; i < cnt; ++i) {
381                 ai = create_app_inst_data(mb);
382                 if (ai == NULL)
383                         goto free_app_inst;
384
385                 ui->app_inst[i] = ai;
386         }
387
388         ui->cnt = cnt;
389
390         return ui;
391
392 free_app_inst:
393         for (i -= 1; i >= 0; --i)
394                 destroy_app_inst_data(ui->app_inst[i]);
395         kfree(ui->app_inst);
396
397 free_ui:
398         kfree(ui);
399
400         return NULL;
401 }
402
403 void destroy_us_inst_data(struct us_inst_data *ui)
404 {
405         int i;
406
407         for (i = 0; i < ui->cnt; ++i)
408                 destroy_app_inst_data(ui->app_inst[i]);
409
410         kfree(ui->app_inst);
411         kfree(ui);
412 }