[FIX] wrong pack size of system memory total/used
[platform/core/system/swap-manager.git] / daemon / da_protocol_inst.c
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Contributors:
23  * - Samsung RnD Institute Russia
24  *
25  */
26
27 #include "debug.h"
28 #include "da_protocol.h"
29 #include "da_inst.h"
30 #include "da_protocol_check.h"
31
32 //----------------- hash
33 static uint32_t calc_lib_hash(struct us_lib_inst_t *lib)
34 {
35         uint32_t hash = 0;
36         char *p = lib->bin_path;
37         while (*p != 0) {
38                 hash <<= 1;
39                 hash += *p;
40                 p++;
41         }
42         return hash;
43 }
44
45 static uint32_t calc_app_hash(struct app_info_t *app)
46 {
47         uint32_t hash = 0;
48         char *p = app->exe_path;
49         while (*p != 0) {
50                 hash <<= 1;
51                 hash += *p;
52                 p++;
53         }
54         return hash;
55 }
56
57
58 //----------------------------------- parse -----------------------------------
59 int parse_us_inst_func(struct msg_buf_t *msg, struct probe_list_t **dest)
60 {
61         uint32_t size = 0;
62         struct us_func_inst_plane_t *func;
63
64         size = sizeof(*func) + strlen(msg->cur_pos + sizeof(func->func_addr) ) + 1;
65         func = malloc(size);
66         if (!parse_int64(msg, &(func->func_addr))) {
67                 LOGE("func addr parsing error\n");
68                 free(func);
69                 return 0;
70         }
71
72         if (!parse_string_no_alloc(msg, func->args) ||
73                 !check_us_inst_func_args(func->args))
74         {
75                 LOGE("args format parsing error\n");
76                 free(func);
77                 return 0;
78         }
79
80         *dest = new_probe();
81         if (*dest == NULL) {
82                 LOGE("alloc new_probe error\n");
83                 free(func);
84                 return 0;
85         }
86         (*dest)->size = size;
87         (*dest)->func = func;
88         return 1;
89 }
90
91 int parse_func_inst_list(struct msg_buf_t *msg,
92                                 struct data_list_t *dest)
93 {
94         uint32_t i = 0, num = 0;
95         struct probe_list_t *probe_el;
96
97         if (!parse_int32(msg, &num) ||
98                 !check_us_app_inst_func_count(num))
99         {
100                 LOGE("func num parsing error\n");
101                 return 0;
102         }
103         //parse user space function list
104
105         parse_deb("app_int_num = %d\n", num);
106         for (i = 0; i < num; i++) {
107                 parse_deb("app_int #%d\n", i);
108                 if (!parse_us_inst_func(msg, &probe_el)) {
109                         // TODO maybe need to free allocated memory up there
110                         LOGE("parse us inst func #%d failed\n", i + 1);
111                         return 0;
112                 }
113                 probe_list_append(dest, probe_el);
114         }
115         dest->func_num = num;
116         return 1;
117 }
118
119 int parse_inst_lib(struct msg_buf_t *msg, struct lib_list_t **dest)
120 {
121         *dest = new_lib();
122         if (*dest == NULL) {
123                 LOGE("lib alloc error\n");
124                 return 0;
125         };
126
127         if (!parse_string(msg, &((*dest)->lib->bin_path)) ||
128             !check_exec_path((*dest)->lib->bin_path))
129         {
130                 LOGE("bin path parsing error\n");
131                 return 0;
132         }
133
134         if (!parse_func_inst_list(msg, (struct data_list_t *) *dest)) {
135                 LOGE("funcs parsing error\n");
136                 return 0;
137         }
138
139         (*dest)->size +=  strlen((*dest)->lib->bin_path) + 1 + sizeof((*dest)->func_num);
140         (*dest)->hash = calc_lib_hash((*dest)->lib);
141         return 1;
142
143 }
144
145 int parse_lib_inst_list(struct msg_buf_t *msg,
146                         uint32_t *num,
147                         struct lib_list_t **lib_list)
148 {
149         uint32_t i = 0;
150         struct lib_list_t *lib = NULL;
151         if (!parse_int32(msg, num) ||
152                 !check_lib_inst_count(*num))
153         {
154                 LOGE("lib num parsing error\n");
155                 return 0;
156         }
157
158         for (i = 0; i < *num; i++) {
159                 if (!parse_inst_lib(msg, &lib)) {
160                         // TODO maybe need free allocated memory up there
161                         LOGE("parse is inst lib #%d failed\n", i + 1);
162                         return 0;
163                 }
164                 data_list_append((struct data_list_t **)lib_list,
165                                  (struct data_list_t *)lib);
166         }
167
168         return 1;
169 }
170
171 int parse_inst_app(struct msg_buf_t *msg, struct app_list_t **dest)
172 {
173         char *start, *end;
174         struct app_info_t *app_info = NULL;
175         *dest = new_app();
176
177         if (*dest == NULL) {
178                 LOGE("lib alloc error\n");
179                 return 0;
180         };
181
182         app_info = (*dest)->app;
183         start = msg->cur_pos;
184         if (!parse_int32(msg, &app_info->app_type) ||
185                 !check_app_type(app_info->app_type))
186         {
187                 LOGE("app type parsing error <0x%X>\n", app_info->app_type);
188                 return 0;
189         }
190
191         if (!parse_string(msg, &app_info->app_id) ||
192                 !check_app_id(app_info->app_type, app_info->app_id))
193         {
194                 LOGE("app id parsing error\n");
195                 return 0;
196         }
197         if (!parse_string(msg, &app_info->exe_path) ||
198                 !check_exec_path(app_info->exe_path))
199         {
200                 LOGE("exec path parsing error\n");
201                 return 0;
202         }
203         end = msg->cur_pos;
204
205         if (!parse_func_inst_list(msg, (struct data_list_t *)*dest)) {
206                 LOGE("funcs parsing error\n");
207                 return 0;
208         }
209
210         (*dest)->size += (end - start) + sizeof((*dest)->func_num);
211         (*dest)->hash = calc_app_hash(app_info);
212         return 1;
213 }
214
215 int parse_app_inst_list(struct msg_buf_t *msg,
216                         uint32_t *num,
217                         struct app_list_t  **app_list)
218 {
219         uint32_t i = 0;
220         struct app_list_t *app = NULL;
221         if (!parse_int32(msg, num) ||
222                 !check_lib_inst_count(*num))
223         {
224                 LOGE("app num parsing error\n");
225                 return 0;
226         }
227
228         parse_deb("app_int_num = %d\n", *num);
229         for (i = 0; i < *num; i++) {
230                 parse_deb("app_int #%d\n", i);
231                 if (!parse_inst_app(msg, &app)) {
232                         // TODO maybe need free allocated memory up there
233                         LOGE("parse is inst app #%d failed\n", i + 1);
234                         return 0;
235                 }
236                 data_list_append((struct data_list_t **)app_list,
237                                  (struct data_list_t *)app);
238         }
239
240         if (!parse_replay_event_seq(msg, &prof_session.replay_event_seq)) {
241                 LOGE("replay parsing error\n");
242                 return 0;
243         }
244
245         return 1;
246 }
247