[FIX] verification of dedicated memory
[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, j, 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 == NULL)
214                 goto free_path;
215
216         li->func = kmalloc(sizeof(struct func_inst_data *) * cnt, GFP_KERNEL);
217         if (li->func == NULL)
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 (j = 0; j < i; ++j)
235                 destroy_func_inst_data(li->func[j]);
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, cnt_lib, i_lib = 0, i;
275
276         app_info = create_app_info(mb);
277         if (app_info == NULL)
278                 return NULL;
279
280         if (get_u32(mb, &cnt_func))
281                 goto free_app_info;
282
283         if (remained_mb(mb) / MIN_SIZE_FUNC_INST < cnt_func)
284                 goto free_app_info;
285
286         app_inst = kmalloc(sizeof(*app_inst), GFP_KERNEL);
287         if (app_inst == NULL)
288                 goto free_app_info;
289
290         app_inst->func = kmalloc(sizeof(struct func_inst_data *) * cnt_func,
291                                  GFP_KERNEL);
292         if (app_inst->func == NULL)
293                 goto free_app_inst;
294
295         for (i_func = 0; i_func < cnt_func; ++i_func) {
296                 func = create_func_inst_data(mb);
297                 if (func == NULL)
298                         goto free_func;
299
300                 app_inst->func[i_func] = func;
301         }
302
303         if (get_u32(mb, &cnt_lib))
304                 goto free_func;
305
306         if (remained_mb(mb) / MIN_SIZE_LIB_INST < cnt_lib)
307                 goto free_func;
308
309         app_inst->lib = kmalloc(sizeof(struct lib_inst_data *) * cnt_lib,
310                                 GFP_KERNEL);
311         if (app_inst->lib == NULL)
312                 goto free_func;
313
314         for (i_lib = 0; i_lib < cnt_lib; ++i_lib) {
315                 lib = create_lib_inst_data(mb);
316                 if (lib == NULL)
317                         goto free_lib;
318
319                 app_inst->lib[i_lib] = lib;
320         }
321
322         app_inst->cnt_func = cnt_func;
323         app_inst->cnt_lib = cnt_lib;
324
325         return app_inst;
326
327 free_lib:
328         for (i = 0; i < i_lib; ++i)
329                 destroy_lib_inst_data(app_inst->lib[i]);
330         kfree(app_inst->lib);
331
332 free_func:
333         for (i = 0; i < i_func; ++i)
334                 destroy_func_inst_data(app_inst->func[i]);
335         kfree(app_inst->func);
336
337 free_app_inst:
338         kfree(app_inst);
339
340 free_app_info:
341         destroy_app_info(app_info);
342
343         return NULL;
344 }
345
346 void destroy_app_inst_data(struct app_inst_data *ai)
347 {
348         int i;
349
350         for (i = 0; i < ai->cnt_lib; ++i)
351                 destroy_lib_inst_data(ai->lib[i]);
352         kfree(ai->lib);
353
354         for (i = 0; i < ai->cnt_func; ++i)
355                 destroy_func_inst_data(ai->func[i]);
356         kfree(ai->func);
357
358         destroy_app_info(ai->app_info);
359         kfree(ai);
360 }
361
362
363
364
365
366 /* ============================================================================
367  * ==                                US_INST                                 ==
368  * ============================================================================
369  */
370 struct us_inst_data *create_us_inst_data(struct msg_buf *mb)
371 {
372         struct us_inst_data *ui;
373         struct app_inst_data *ai;
374         u32 cnt, j, i = 0;
375
376         if (get_u32(mb, &cnt))
377                 return NULL;
378
379         if (remained_mb(mb) / MIN_SIZE_APP_INST < cnt)
380                 return NULL;
381
382         ui = kmalloc(sizeof(struct us_inst_data), GFP_KERNEL);
383         if (ui == NULL)
384                 return NULL;
385
386         ui->app_inst = kmalloc(sizeof(struct app_inst_data *) * cnt,
387                                GFP_KERNEL);
388         if (ui->app_inst == NULL)
389                 goto free_ui;
390
391         for (i = 0; i < cnt; ++i) {
392                 ai = create_app_inst_data(mb);
393                 if (ai == NULL)
394                         goto free_app_inst;
395
396                 ui->app_inst[i] = ai;
397         }
398
399         ui->cnt = cnt;
400
401         return ui;
402
403 free_app_inst:
404         for (j = 0; j < i; ++j)
405                 destroy_app_inst_data(ui->app_inst[j]);
406         kfree(ui->app_inst);
407
408 free_ui:
409         kfree(ui);
410
411         return NULL;
412 }
413
414 void destroy_us_inst_data(struct us_inst_data *ui)
415 {
416         int i;
417
418         for (i = 0; i < ui->cnt; ++i)
419                 destroy_app_inst_data(ui->app_inst[i]);
420
421         kfree(ui->app_inst);
422         kfree(ui);
423 }