[IMPROVE] check on incorrect 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
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         if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt)
210                 return NULL;
211
212         li = kmalloc(sizeof(*li), GFP_KERNEL);
213         if (li)
214                 goto free_path;
215
216         li->func = kmalloc(sizeof(struct func_inst_data *) * cnt, GFP_KERNEL);
217         if (li->cnt_func)
218                 goto free_li;
219
220         for (i = 0; i < cnt; ++i) {
221                 fi = create_func_inst_data(mb);
222                 if (fi == NULL)
223                         goto free_func;
224
225                 li->func[i] = fi;
226         }
227
228         li->path = path;
229         li->cnt_func = cnt;
230
231         return li;
232
233 free_func:
234         for (i -= 1; i >= 0; --i)
235                 destroy_func_inst_data(li->func[i]);
236         kfree(li->func);
237
238 free_li:
239         kfree(li);
240
241 free_path:
242         put_strung(path);
243
244         return NULL;
245 }
246
247 void destroy_lib_inst_data(struct lib_inst_data *li)
248 {
249         int i;
250
251         put_strung(li->path);
252
253         for (i = 0; i < li->cnt_func; ++i)
254                 destroy_func_inst_data(li->func[i]);
255
256         kfree(li->func);
257         kfree(li);
258 }
259
260
261
262
263
264 /* ============================================================================
265  * ==                               APP_INST                                 ==
266  * ============================================================================
267  */
268 struct app_inst_data *create_app_inst_data(struct msg_buf *mb)
269 {
270         struct app_inst_data *app_inst;
271         struct app_info_data *app_info;
272         struct func_inst_data *func;
273         struct lib_inst_data *lib;
274         u32 cnt_func, i_func = 0;
275         u32 cnt_lib, i_lib = 0;
276
277         app_info = create_app_info(mb);
278         if (app_info == NULL)
279                 return NULL;
280
281         if (get_u32(mb, &cnt_func))
282                 goto free_app_info;
283
284         if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt_func)
285                 goto free_app_info;
286
287         app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
288         if (app_inst == NULL)
289                 goto free_app_info;
290
291         app_inst->func = kmalloc(sizeof(struct func_inst_data *) * cnt_func,
292                                  GFP_KERNEL);
293         if (app_inst->func == NULL)
294                 goto free_app_inst;
295
296         for (i_func = 0; i_func < cnt_func; ++i_func) {
297                 func = create_func_inst_data(mb);
298                 if (func == NULL)
299                         goto free_func;
300
301                 app_inst->func[i_func] = func;
302         }
303
304         if (get_u32(mb, &cnt_lib))
305                 goto free_func;
306
307         if (remained_mb(mb) / MIN_SIZE_LIB_INST < cnt_lib)
308                 goto free_func;
309
310         app_inst->lib = kmalloc(sizeof(struct lib_inst_data *) * cnt_lib,
311                                 GFP_KERNEL);
312         if (app_inst->lib == NULL)
313                 goto free_func;
314
315         for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
316                 lib = create_lib_inst_data(mb);
317                 if (lib == NULL)
318                         goto free_lib;
319
320                 app_inst->lib[i_lib] = lib;
321         }
322
323         app_inst->cnt_func = cnt_func;
324         app_inst->cnt_lib = cnt_lib;
325
326         return app_inst;
327
328 free_lib:
329         for (i_lib -= 1; i_lib >= 0; --i_lib)
330                 destroy_lib_inst_data(app_inst->lib[i_lib]);
331         kfree(app_inst->lib);
332
333 free_func:
334         for (i_func -= 1; i_func >= 0; --i_func)
335                 destroy_func_inst_data(app_inst->func[i_func]);
336         kfree(app_inst->func);
337
338 free_app_inst:
339         kfree(app_inst);
340
341 free_app_info:
342         destroy_app_info(app_info);
343
344         return NULL;
345 }
346
347 void destroy_app_inst_data(struct app_inst_data *ai)
348 {
349         int i;
350
351         for (i = 0; i < ai->cnt_lib; ++i)
352                 destroy_lib_inst_data(ai->lib[i]);
353         kfree(ai->lib);
354
355         for (i = 0; i < ai->cnt_func; ++i)
356                 destroy_func_inst_data(ai->func[i]);
357         kfree(ai->func);
358
359         destroy_app_info(ai->app_info);
360         kfree(ai);
361 }
362
363
364
365
366
367 /* ============================================================================
368  * ==                                US_INST                                 ==
369  * ============================================================================
370  */
371 struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
372 {
373         struct us_inst_data *ui;
374         struct app_inst_data *ai;
375         u32 cnt, i = 0;
376
377         if (get_u32(mb, &cnt))
378                 return NULL;
379
380         if (remained_mb(mb) / MIN_SIZE_APP_INST < cnt)
381                 return NULL;
382
383         ui = kmalloc(sizeof(struct us_inst_data), GFP_KERNEL);
384         if (ui == NULL)
385                 return NULL;
386
387         ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
388                                GFP_KERNEL);
389         if (ui->app_inst == NULL)
390                 goto free_ui;
391
392         for (i = 0; i < cnt; ++i) {
393                 ai = create_app_inst_data(mb);
394                 if (ai == NULL)
395                         goto free_app_inst;
396
397                 ui->app_inst[i] = ai;
398         }
399
400         ui->cnt = cnt;
401
402         return ui;
403
404 free_app_inst:
405         for (i -= 1; i >= 0; --i)
406                 destroy_app_inst_data(ui->app_inst[i]);
407         kfree(ui->app_inst);
408
409 free_ui:
410         kfree(ui);
411
412         return NULL;
413 }
414
415 void destroy_us_inst_data(struct us_inst_data *ui)
416 {
417         int i;
418
419         for (i = 0; i < ui->cnt; ++i)
420                 destroy_app_inst_data(ui->app_inst[i]);
421
422         kfree(ui->app_inst);
423         kfree(ui);
424 }