Refactoring updateinfo related codes
[platform/core/appfw/pkgmgr-server.git] / src / request.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3 #include <sys/time.h>
4
5 #include <glib.h>
6 #include <gio/gio.h>
7
8 #include "queue.h"
9 #include "pkgmgr-info.h"
10 #include "pkgmgr-server.h"
11 #include "package-manager.h"
12
13 #define PKGMGR_DBUS_SERVICE "org.tizen.pkgmgr"
14 #define PKGMGR_DBUS_OBJECT_PATH "/org/tizen/pkgmgr"
15
16 static const char instropection_xml[] =
17         "<node>"
18         "  <interface name='org.tizen.pkgmgr'>"
19         "    <method name='install'>"
20         "      <arg type='u' name='uid' direction='in'/>"
21         "      <arg type='s' name='pkgtype' direction='in'/>"
22         "      <arg type='s' name='pkgpath' direction='in'/>"
23         "      <arg type='as' name='args' direction='in'/>"
24         "      <arg type='i' name='ret' direction='out'/>"
25         "      <arg type='s' name='reqkey' direction='out'/>"
26         "    </method>"
27         "    <method name='reinstall'>"
28         "      <arg type='u' name='uid' direction='in'/>"
29         "      <arg type='s' name='pkgid' direction='in'/>"
30         "      <arg type='i' name='ret' direction='out'/>"
31         "      <arg type='s' name='reqkey' direction='out'/>"
32         "    </method>"
33         "    <method name='mount_install'>"
34         "      <arg type='u' name='uid' direction='in'/>"
35         "      <arg type='s' name='pkgtype' direction='in'/>"
36         "      <arg type='s' name='pkgpath' direction='in'/>"
37         "      <arg type='as' name='args' direction='in'/>"
38         "      <arg type='i' name='ret' direction='out'/>"
39         "      <arg type='s' name='reqkey' direction='out'/>"
40         "    </method>"
41         "    <method name='uninstall'>"
42         "      <arg type='u' name='uid' direction='in'/>"
43         "      <arg type='s' name='pkgid' direction='in'/>"
44         "      <arg type='i' name='ret' direction='out'/>"
45         "      <arg type='s' name='reqkey' direction='out'/>"
46         "    </method>"
47         "    <method name='move'>"
48         "      <arg type='u' name='uid' direction='in'/>"
49         "      <arg type='s' name='pkgid' direction='in'/>"
50         "      <arg type='i' name='movetype' direction='in'/>"
51         "      <arg type='i' name='ret' direction='out'/>"
52         "      <arg type='s' name='reqkey' direction='out'/>"
53         "    </method>"
54         "    <method name='enable_pkgs'>"
55         "      <arg type='u' name='uid' direction='in'/>"
56         "      <arg type='as' name='pkgids' direction='in'/>"
57         "      <arg type='i' name='ret' direction='out'/>"
58         "      <arg type='s' name='reqkey' direction='out'/>"
59         "    </method>"
60         "    <method name='disable_pkgs'>"
61         "      <arg type='u' name='uid' direction='in'/>"
62         "      <arg type='as' name='pkgids' direction='in'/>"
63         "      <arg type='i' name='ret' direction='out'/>"
64         "      <arg type='s' name='reqkey' direction='out'/>"
65         "    </method>"
66         "    <method name='register_pkg_update_info'>"
67         "      <arg type='u' name='uid' direction='in'/>"
68         "      <arg type='s' name='pkgid' direction='in'/>"
69         "      <arg type='s' name='version' direction='in'/>"
70         "      <arg type='i' name='type' direction='in'/>"
71         "      <arg type='i' name='ret' direction='out'/>"
72         "    </method>"
73         "    <method name='unregister_pkg_update_info'>"
74         "      <arg type='u' name='uid' direction='in'/>"
75         "      <arg type='s' name='pkgid' direction='in'/>"
76         "      <arg type='i' name='ret' direction='out'/>"
77         "    </method>"
78         "    <method name='unregister_all_pkg_update_info'>"
79         "      <arg type='u' name='uid' direction='in'/>"
80         "      <arg type='i' name='ret' direction='out'/>"
81         "    </method>"
82         "    <method name='enable_app'>"
83         "      <arg type='u' name='uid' direction='in'/>"
84         "      <arg type='s' name='appid' direction='in'/>"
85         "      <arg type='i' name='ret' direction='out'/>"
86         "      <arg type='s' name='reqkey' direction='out'/>"
87         "    </method>"
88         "    <method name='disable_app'>"
89         "      <arg type='u' name='uid' direction='in'/>"
90         "      <arg type='s' name='appid' direction='in'/>"
91         "      <arg type='i' name='ret' direction='out'/>"
92         "      <arg type='s' name='reqkey' direction='out'/>"
93         "    </method>"
94         "    <method name='enable_apps'>"
95         "      <arg type='u' name='uid' direction='in'/>"
96         "      <arg type='as' name='appids' direction='in'/>"
97         "      <arg type='i' name='ret' direction='out'/>"
98         "      <arg type='s' name='reqkey' direction='out'/>"
99         "    </method>"
100         "    <method name='disable_apps'>"
101         "      <arg type='u' name='uid' direction='in'/>"
102         "      <arg type='as' name='appids' direction='in'/>"
103         "      <arg type='i' name='ret' direction='out'/>"
104         "      <arg type='s' name='reqkey' direction='out'/>"
105         "    </method>"
106         "    <method name='enable_global_app_for_uid'>"
107         "      <arg type='u' name='uid' direction='in'/>"
108         "      <arg type='s' name='appid' direction='in'/>"
109         "      <arg type='i' name='ret' direction='out'/>"
110         "      <arg type='s' name='reqkey' direction='out'/>"
111         "    </method>"
112         "    <method name='disable_global_app_for_uid'>"
113         "      <arg type='u' name='uid' direction='in'/>"
114         "      <arg type='s' name='appid' direction='in'/>"
115         "      <arg type='i' name='ret' direction='out'/>"
116         "      <arg type='s' name='reqkey' direction='out'/>"
117         "    </method>"
118         "    <method name='getsize'>"
119         "      <arg type='u' name='uid' direction='in'/>"
120         "      <arg type='s' name='pkgid' direction='in'/>"
121         "      <arg type='i' name='get_type' direction='in'/>"
122         "      <arg type='i' name='ret' direction='out'/>"
123         "      <arg type='s' name='reqkey' direction='out'/>"
124         "    </method>"
125         "    <method name='getsize_sync'>"
126         "      <arg type='u' name='uid' direction='in'/>"
127         "      <arg type='s' name='pkgid' direction='in'/>"
128         "      <arg type='i' name='get_type' direction='in'/>"
129         "      <arg type='i' name='ret' direction='out'/>"
130         "      <arg type='x' name='size_info' direction='out'/>"
131         "    </method>"
132         "    <method name='cleardata'>"
133         "      <arg type='u' name='uid' direction='in'/>"
134         "      <arg type='s' name='pkgid' direction='in'/>"
135         "      <arg type='i' name='ret' direction='out'/>"
136         "    </method>"
137         "    <method name='clearcache'>"
138         "      <arg type='u' name='uid' direction='in'/>"
139         "      <arg type='s' name='pkgid' direction='in'/>"
140         "      <arg type='i' name='ret' direction='out'/>"
141         "    </method>"
142         "    <method name='kill'>"
143         "      <arg type='u' name='uid' direction='in'/>"
144         "      <arg type='s' name='pkgid' direction='in'/>"
145         "      <arg type='i' name='ret' direction='out'/>"
146         "      <arg type='i' name='pid' direction='out'/>"
147         "    </method>"
148         "    <method name='check'>"
149         "      <arg type='u' name='uid' direction='in'/>"
150         "      <arg type='s' name='pkgid' direction='in'/>"
151         "      <arg type='i' name='ret' direction='out'/>"
152         "      <arg type='i' name='pid' direction='out'/>"
153         "    </method>"
154         "    <method name='generate_license_request'>"
155         "      <arg type='s' name='resp_data' direction='in'/>"
156         "      <arg type='i' name='ret' direction='out'/>"
157         "      <arg type='s' name='req_data' direction='out'/>"
158         "      <arg type='s' name='license_url' direction='out'/>"
159         "    </method>"
160         "    <method name='register_license'>"
161         "      <arg type='s' name='resp_data' direction='in'/>"
162         "      <arg type='i' name='ret' direction='out'/>"
163         "    </method>"
164         "    <method name='decrypt_package'>"
165         "      <arg type='s' name='drm_file_path' direction='in'/>"
166         "      <arg type='s' name='decrypted_file_path' direction='in'/>"
167         "      <arg type='i' name='ret' direction='out'/>"
168         "    </method>"
169         "    <method name='enable_app_splash_screen'>"
170         "      <arg type='u' name='uid' direction='in'/>"
171         "      <arg type='s' name='appid' direction='in'/>"
172         "      <arg type='i' name='ret' direction='out'/>"
173         "    </method>"
174         "    <method name='disable_app_splash_screen'>"
175         "      <arg type='u' name='uid' direction='in'/>"
176         "      <arg type='s' name='appid' direction='in'/>"
177         "      <arg type='i' name='ret' direction='out'/>"
178         "    </method>"
179         "    <method name='set_restriction_mode'>"
180         "      <arg type='u' name='uid' direction='in'/>"
181         "      <arg type='s' name='pkgid' direction='in'/>"
182         "      <arg type='i' name='mode' direction='in'/>"
183         "      <arg type='i' name='ret' direction='out'/>"
184         "    </method>"
185         "    <method name='unset_restriction_mode'>"
186         "      <arg type='u' name='uid' direction='in'/>"
187         "      <arg type='s' name='pkgid' direction='in'/>"
188         "      <arg type='i' name='mode' direction='in'/>"
189         "      <arg type='i' name='ret' direction='out'/>"
190         "    </method>"
191         "    <method name='get_restriction_mode'>"
192         "      <arg type='u' name='uid' direction='in'/>"
193         "      <arg type='s' name='pkgid' direction='in'/>"
194         "      <arg type='i' name='result' direction='out'/>"
195         "      <arg type='i' name='ret' direction='out'/>"
196         "    </method>"
197         "    <method name='set_app_label'>"
198         "      <arg type='u' name='uid' direction='in'/>"
199         "      <arg type='s' name='appid' direction='in'/>"
200         "      <arg type='s' name='label' direction='in'/>"
201         "      <arg type='i' name='ret' direction='out'/>"
202         "    </method>"
203         "    <method name='migrate_external_image'>"
204         "      <arg type='u' name='uid' direction='in'/>"
205         "      <arg type='s' name='pkgid' direction='in'/>"
206         "      <arg type='i' name='ret' direction='out'/>"
207         "    </method>"
208         "  </interface>"
209         "</node>";
210 static GDBusNodeInfo *instropection_data;
211 static guint reg_id;
212 static guint owner_id;
213 static GHashTable *req_table;
214
215 static char *__generate_reqkey(const char *pkgid)
216 {
217         struct timeval tv;
218         long curtime;
219         char timestr[MAX_PKG_ARGS_LEN];
220         char *str_req_key;
221         int size;
222
223         gettimeofday(&tv, NULL);
224         curtime = tv.tv_sec * 1000000 + tv.tv_usec;
225         snprintf(timestr, sizeof(timestr), "%ld", curtime);
226
227         size = strlen(pkgid) + strlen(timestr) + 2;
228         str_req_key = (char *)calloc(size, sizeof(char));
229         if (str_req_key == NULL) {
230                 ERR("calloc failed");
231                 return NULL;
232         }
233         snprintf(str_req_key, size, "%s_%s", pkgid, timestr);
234
235         return str_req_key;
236 }
237
238 static int __check_caller_permission(uid_t uid,
239                 GDBusMethodInvocation *invocation, GVariant *parameters)
240 {
241         GVariant *v;
242         uid_t target_uid;
243         int is_admin;
244
245         if (uid < REGULAR_USER)
246                 return 0;
247
248         v = g_variant_get_child_value(parameters, 0);
249         if (v == NULL) {
250                 g_dbus_method_invocation_return_error_literal(invocation,
251                                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
252                                 "Internal error.");
253                 return -1;
254         }
255
256         target_uid = g_variant_get_uint32(v);
257         g_variant_unref(v);
258         if (uid == target_uid)
259                 return 0;
260
261         is_admin = __is_admin_user(uid);
262         if (is_admin == -1) {
263                 g_dbus_method_invocation_return_error_literal(invocation,
264                                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
265                                 "Internal error.");
266                 return -1;
267         } else if (is_admin == 0) {
268                 g_dbus_method_invocation_return_error_literal(invocation,
269                                 G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
270                                 "Non-admin user cannot request operation to "
271                                 "other users.");
272                 return -1;
273         }
274
275         return 0;
276 }
277
278 static int __handle_request_install(uid_t caller_uid,
279                 GDBusMethodInvocation *invocation, GVariant *parameters)
280 {
281         uid_t target_uid = (uid_t)-1;
282         char *arg_pkgtype = NULL;
283         const char *pkgtype;
284         const char *pkgpath = NULL;
285         char *args = NULL;
286         char *reqkey = NULL;
287         gchar **tmp_args = NULL;
288         gsize args_count;
289         int ret = -1;
290         GVariant *value;
291         int i = 0;
292         int len = 0;
293         size_t s = 0;
294
295         g_variant_get(parameters, "(u&s&s@as)", &target_uid, &arg_pkgtype,
296                         &pkgpath, &value);
297         tmp_args = (gchar **)g_variant_get_strv(value, &args_count);
298
299         for (i = 0; i < args_count; i++)
300                 len = len + strlen(tmp_args[i]) + 1;
301
302         args = (char *)calloc(len, sizeof(char));
303         if (args == NULL) {
304                 ERR("calloc failed");
305                 g_dbus_method_invocation_return_value(invocation,
306                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
307                 ret = -1;
308                 goto catch;
309         }
310
311         for (i = 0; i < args_count; i++) {
312                 strncat(args, tmp_args[i], len - s - 1);
313                 s += strlen(tmp_args[i]);
314                 if (i != args_count - 1) {
315                         strncat(args, " ", len - s - 1);
316                         s += strlen(" ");
317                 }
318         }
319
320         if (target_uid == (uid_t)-1 || pkgpath == NULL) {
321                 g_dbus_method_invocation_return_value(invocation,
322                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
323                 ret = -1;
324                 goto catch;
325         }
326
327         pkgpath = _get_adjusted_pkgpath(pkgpath, caller_uid);
328         pkgtype = _get_pkgtype_from_file(pkgpath);
329         if (!pkgtype && arg_pkgtype && strlen(arg_pkgtype))
330                 pkgtype = (const char *)arg_pkgtype;
331         if (pkgtype == NULL) {
332                 g_dbus_method_invocation_return_value(invocation,
333                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
334                 ret = -1;
335                 goto catch;
336         }
337
338         reqkey = __generate_reqkey(pkgpath);
339         if (reqkey == NULL) {
340                 g_dbus_method_invocation_return_value(invocation,
341                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
342                 ret = -1;
343                 goto catch;
344         }
345
346         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_INSTALL,
347                                 pkgtype, pkgpath, args, NULL)) {
348                 g_dbus_method_invocation_return_value(invocation,
349                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
350                 ret = -1;
351                 goto catch;
352         }
353
354         g_dbus_method_invocation_return_value(invocation,
355                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
356
357         ret = 0;
358
359 catch:
360         if (reqkey)
361                 free(reqkey);
362
363         if (args)
364                 free(args);
365
366         if (tmp_args)
367                 g_free(tmp_args);
368
369         return ret;
370 }
371
372 static int __handle_request_mount_install(uid_t caller_uid,
373         GDBusMethodInvocation *invocation, GVariant *parameters)
374 {
375         uid_t target_uid = (uid_t)-1;
376         char *arg_pkgtype = NULL;
377         const char *pkgtype;
378         const char *pkgpath = NULL;
379         char *args = NULL;
380         char *reqkey = NULL;
381         gchar **tmp_args = NULL;
382         gsize args_count;
383         int ret = -1;
384         GVariant *value;
385         int i = 0;
386         int len = 0;
387         size_t s = 0;
388
389         g_variant_get(parameters, "(u&s&s@as)", &target_uid, &arg_pkgtype,
390                         &pkgpath, &value);
391         tmp_args = (gchar **)g_variant_get_strv(value, &args_count);
392
393         for (i = 0; i < args_count; i++)
394                 len = len + strlen(tmp_args[i]) + 1;
395
396         args = (char *)calloc(len, sizeof(char));
397         if (args == NULL) {
398                 ERR("calloc failed");
399                 g_dbus_method_invocation_return_value(invocation,
400                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
401                 ret = -1;
402                 goto catch;
403         }
404
405         for (i = 0; i < args_count; i++) {
406                 strncat(args, tmp_args[i], len - s - 1);
407                 s += strlen(tmp_args[i]);
408                 if (i != args_count - 1) {
409                         strncat(args, " ", len - s - 1);
410                         s += strlen(" ");
411                 }
412         }
413
414         if (target_uid == (uid_t)-1 || pkgpath == NULL) {
415                 g_dbus_method_invocation_return_value(invocation,
416                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
417                 ret = -1;
418                 goto catch;
419         }
420
421         pkgpath = _get_adjusted_pkgpath(pkgpath, caller_uid);
422         pkgtype = _get_pkgtype_from_file(pkgpath);
423         if (!pkgtype && arg_pkgtype && strlen(arg_pkgtype))
424                 pkgtype = (const char *)arg_pkgtype;
425         if (pkgtype == NULL) {
426                 g_dbus_method_invocation_return_value(invocation,
427                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
428                 ret = -1;
429                 goto catch;
430         }
431
432         reqkey = __generate_reqkey(pkgpath);
433         if (reqkey == NULL) {
434                 g_dbus_method_invocation_return_value(invocation,
435                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
436                 ret = -1;
437                 goto catch;
438         }
439
440         if (_push_queue(target_uid, caller_uid, reqkey,
441                                 REQUEST_TYPE_MOUNT_INSTALL,
442                                 pkgtype, pkgpath, args, NULL)) {
443                 g_dbus_method_invocation_return_value(invocation,
444                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
445                 ret = -1;
446                 goto catch;
447         }
448
449         g_dbus_method_invocation_return_value(invocation,
450                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
451         ret = 0;
452
453 catch:
454         if (reqkey)
455                 free(reqkey);
456
457         if (args)
458                 free(args);
459
460         if (tmp_args)
461                 g_free(tmp_args);
462
463         return ret;
464 }
465
466 static int __handle_request_reinstall(uid_t caller_uid,
467                 GDBusMethodInvocation *invocation, GVariant *parameters)
468 {
469         uid_t target_uid = (uid_t)-1;
470         char *pkgtype;
471         char *pkgid = NULL;
472         char *reqkey;
473
474         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
475         if (target_uid == (uid_t)-1 || pkgid == NULL) {
476                 g_dbus_method_invocation_return_value(invocation,
477                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
478                 return -1;
479         }
480
481         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
482         if (pkgtype == NULL) {
483                 g_dbus_method_invocation_return_value(invocation,
484                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
485                 return -1;
486         }
487
488         reqkey = __generate_reqkey(pkgid);
489         if (reqkey == NULL) {
490                 g_dbus_method_invocation_return_value(invocation,
491                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
492                 free(pkgtype);
493                 return -1;
494         }
495         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_REINSTALL,
496                                 pkgtype, pkgid, NULL, NULL)) {
497                 g_dbus_method_invocation_return_value(invocation,
498                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
499                 free(reqkey);
500                 free(pkgtype);
501                 return -1;
502         }
503
504         g_dbus_method_invocation_return_value(invocation,
505                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
506         free(reqkey);
507         free(pkgtype);
508
509         return 0;
510 }
511
512 static int __handle_request_uninstall(uid_t caller_uid,
513                 GDBusMethodInvocation *invocation, GVariant *parameters)
514 {
515         uid_t target_uid = (uid_t)-1;
516         char *pkgtype;
517         char *pkgid = NULL;
518         char *reqkey;
519
520         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
521         if (target_uid == (uid_t)-1 || pkgid == NULL) {
522                 g_dbus_method_invocation_return_value(invocation,
523                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
524                 return -1;
525         }
526
527         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
528         if (pkgtype == NULL) {
529                 g_dbus_method_invocation_return_value(invocation,
530                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
531                 return -1;
532         }
533
534         reqkey = __generate_reqkey(pkgid);
535         if (reqkey == NULL) {
536                 g_dbus_method_invocation_return_value(invocation,
537                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
538                 free(pkgtype);
539                 return -1;
540         }
541         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_UNINSTALL,
542                                 pkgtype, pkgid, NULL, NULL)) {
543                 g_dbus_method_invocation_return_value(invocation,
544                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
545                 free(reqkey);
546                 free(pkgtype);
547                 return -1;
548         }
549
550         g_dbus_method_invocation_return_value(invocation,
551                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
552         free(reqkey);
553         free(pkgtype);
554
555         return 0;
556 }
557
558 static int __handle_request_move(uid_t caller_uid,
559                 GDBusMethodInvocation *invocation, GVariant *parameters)
560 {
561         uid_t target_uid = (uid_t)-1;
562         char *pkgtype;
563         char *pkgid = NULL;
564         char *reqkey;
565         int move_type = -1;
566         char buf[4] = { '\0' };
567
568         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &move_type);
569         if (target_uid == (uid_t)-1 || pkgid == NULL) {
570                 g_dbus_method_invocation_return_value(invocation,
571                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
572                 return -1;
573         }
574
575         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
576         if (pkgtype == NULL) {
577                 g_dbus_method_invocation_return_value(invocation,
578                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
579                 return -1;
580         }
581
582         reqkey = __generate_reqkey(pkgid);
583         if (reqkey == NULL) {
584                 g_dbus_method_invocation_return_value(invocation,
585                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
586                 free(pkgtype);
587                 return -1;
588         }
589
590         snprintf(buf, sizeof(buf), "%d", move_type);
591         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_MOVE,
592                                 pkgtype, pkgid, buf, NULL)) {
593                 g_dbus_method_invocation_return_value(invocation,
594                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
595                 free(reqkey);
596                 free(pkgtype);
597                 return -1;
598         }
599
600         g_dbus_method_invocation_return_value(invocation,
601                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
602         free(reqkey);
603         free(pkgtype);
604
605         return 0;
606 }
607
608 static int __handle_request_enable_pkgs(uid_t caller_uid,
609                 GDBusMethodInvocation *invocation, GVariant *parameters)
610 {
611         uid_t target_uid = (uid_t)-1;
612         char *pkgtype;
613         char *pkgid;
614         char *reqkey;
615         GVariantIter *iter;
616
617         g_variant_get(parameters, "(uas)", &target_uid, &iter);
618         if (target_uid == (uid_t)-1 || iter == NULL) {
619                 g_dbus_method_invocation_return_value(invocation,
620                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
621                 return -1;
622         }
623
624         reqkey = __generate_reqkey("enable_pkgs");
625         if (reqkey == NULL) {
626                 g_dbus_method_invocation_return_value(invocation,
627                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
628                 return -1;
629         }
630
631         while (g_variant_iter_next(iter, "&s", &pkgid)) {
632                 pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
633                 if (pkgtype == NULL) {
634                         g_dbus_method_invocation_return_value(invocation,
635                                         g_variant_new("(is)",
636                                                 PKGMGR_R_ENOPKG, ""));
637                         free(reqkey);
638                         return -1;
639                 }
640                 if (_push_queue(target_uid, caller_uid, reqkey,
641                                         REQUEST_TYPE_ENABLE_PKG,
642                                         pkgtype, pkgid, NULL, NULL)) {
643                         g_dbus_method_invocation_return_value(invocation,
644                                         g_variant_new("(is)",
645                                                 PKGMGR_R_ESYSTEM, ""));
646                         free(pkgtype);
647                         free(reqkey);
648                         return -1;
649                 }
650                 free(pkgtype);
651         }
652
653         g_dbus_method_invocation_return_value(invocation,
654                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
655         free(reqkey);
656
657         return 0;
658 }
659
660 static int __handle_request_register_pkg_update_info(
661                 uid_t caller_uid, GDBusMethodInvocation *invocation,
662                 GVariant *parameters)
663 {
664         char *reqkey;
665         char buf[MAX_PKG_ARGS_LEN];
666         int ret;
667         pkgmgrinfo_pkginfo_h pkg_info;
668         bool is_global_pkg;
669         pkgmgrinfo_updateinfo_h update_info;
670         pkgmgrinfo_updateinfo_update_type converted_type;
671         uid_t target_uid = (uid_t)-1;
672         gchar *pkgid = NULL;
673         gchar *version = NULL;
674
675         g_variant_get(parameters, "(ussi)", &target_uid, &pkgid, &version, &converted_type);
676
677         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &pkg_info);
678         if (ret != PMINFO_R_OK) {
679                 g_dbus_method_invocation_return_value(invocation,
680                                 g_variant_new("(i)", PKGMGR_R_EIO));
681                 return -1;
682         }
683         ret = pkgmgrinfo_pkginfo_is_global(pkg_info, &is_global_pkg);
684         if (ret != PMINFO_R_OK) {
685                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
686                 g_dbus_method_invocation_return_value(invocation,
687                                 g_variant_new("(i)", PKGMGR_R_EIO));
688                 return -1;
689         }
690         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
691
692         /* return error if given pkg is global pkg but caller is not admin */
693         if (caller_uid >= REGULAR_USER && is_global_pkg
694                         && !__is_admin_user(caller_uid)) {
695                 g_dbus_method_invocation_return_value(invocation,
696                                 g_variant_new("(i)", PKGMGR_R_EPRIV));
697                 return -1;
698         }
699
700         ret = pkgmgrinfo_updateinfo_create(&update_info);
701         if (ret != PMINFO_R_OK) {
702                 g_dbus_method_invocation_return_value(invocation,
703                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
704                 return -1;
705         }
706
707         ret = pkgmgrinfo_updateinfo_set_pkgid(update_info, (char *)pkgid);
708         if (ret != PMINFO_R_OK) {
709                 g_dbus_method_invocation_return_value(invocation,
710                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
711                 pkgmgrinfo_updateinfo_destroy(update_info);
712                 return -1;
713         }
714
715         ret = pkgmgrinfo_updateinfo_set_version(update_info, (char *)version);
716         if (ret != PMINFO_R_OK) {
717                 g_dbus_method_invocation_return_value(invocation,
718                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
719                 pkgmgrinfo_updateinfo_destroy(update_info);
720                 return -1;
721         }
722
723         ret = pkgmgrinfo_updateinfo_set_type(update_info, converted_type);
724         if (ret != PMINFO_R_OK) {
725                 g_dbus_method_invocation_return_value(invocation,
726                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
727                 pkgmgrinfo_updateinfo_destroy(update_info);
728                 return -1;
729         }
730
731         reqkey = __generate_reqkey("register_pkg_update_info");
732         if (reqkey == NULL) {
733                 pkgmgrinfo_updateinfo_destroy(update_info);
734                 g_dbus_method_invocation_return_value(invocation,
735                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
736                 return -1;
737         }
738
739         if (_push_queue(target_uid, caller_uid, reqkey,
740                         REQUEST_TYPE_REGISTER_PKG_UPDATE_INFO, "default",
741                         NULL, buf, update_info)) {
742                 pkgmgrinfo_updateinfo_destroy(update_info);
743                 g_dbus_method_invocation_return_value(invocation,
744                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
745                 free(reqkey);
746                 return -1;
747         }
748         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
749                                 (gpointer)invocation))
750                 ERR("reqkey already exists");
751
752         return 0;
753 }
754
755 static int __handle_request_unregister_pkg_update_info(
756                 uid_t caller_uid, GDBusMethodInvocation *invocation,
757                 GVariant *parameters)
758 {
759         uid_t target_uid = (uid_t)-1;
760         char *pkgid = NULL;
761         char *reqkey;
762         pkgmgrinfo_pkginfo_h pkg_info;
763         bool is_global_pkg;
764         int ret;
765
766         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
767         if (target_uid == (uid_t)-1 || pkgid == NULL) {
768                 g_dbus_method_invocation_return_value(invocation,
769                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
770                 return -1;
771         }
772
773         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &pkg_info);
774         if (ret != PMINFO_R_OK) {
775                 g_dbus_method_invocation_return_value(invocation,
776                                 g_variant_new("(i)", PKGMGR_R_EIO));
777                 return -1;
778         }
779         ret = pkgmgrinfo_pkginfo_is_global(pkg_info, &is_global_pkg);
780         if (ret != PMINFO_R_OK) {
781                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
782                 g_dbus_method_invocation_return_value(invocation,
783                                 g_variant_new("(i)", PKGMGR_R_EIO));
784                 return -1;
785         }
786         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
787
788         /* return error if given pkg is global pkg but caller is not admin */
789         if (caller_uid >= REGULAR_USER && is_global_pkg
790                         && !__is_admin_user(caller_uid)) {
791                 g_dbus_method_invocation_return_value(invocation,
792                                 g_variant_new("(i)", PKGMGR_R_EPRIV));
793                 return -1;
794         }
795
796         reqkey = __generate_reqkey("unregister_pkg_update_info");
797         if (reqkey == NULL) {
798                 g_dbus_method_invocation_return_value(invocation,
799                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
800                 return -1;
801         }
802
803         if (_push_queue(target_uid, caller_uid, reqkey,
804                         REQUEST_TYPE_UNREGISTER_PKG_UPDATE_INFO, "default",
805                         pkgid, NULL, NULL)) {
806                 g_dbus_method_invocation_return_value(invocation,
807                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
808                 free(reqkey);
809                 return -1;
810         }
811
812         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
813                                 (gpointer)invocation))
814                 ERR("reqkey already exists");
815
816
817         return 0;
818
819 }
820
821 static int __handle_request_unregister_all_pkg_update_info(
822                 uid_t caller_uid, GDBusMethodInvocation *invocation,
823                 GVariant *parameters)
824 {
825         uid_t target_uid = (uid_t)-1;
826         char *reqkey;
827
828         g_variant_get(parameters, "(u)", &target_uid);
829         if (target_uid == (uid_t)-1) {
830                 g_dbus_method_invocation_return_value(invocation,
831                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
832                 return -1;
833         }
834
835         reqkey = __generate_reqkey("unregister_all_pkg_update_info");
836         if (reqkey == NULL) {
837                 g_dbus_method_invocation_return_value(invocation,
838                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
839                 return -1;
840         }
841
842         if (_push_queue(target_uid, caller_uid, reqkey,
843                         REQUEST_TYPE_UNREGISTER_ALL_PKG_UPDATE_INFO, "default",
844                         NULL, NULL, NULL)) {
845                 g_dbus_method_invocation_return_value(invocation,
846                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
847                 free(reqkey);
848                 return -1;
849         }
850
851         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
852                                 (gpointer)invocation))
853                 ERR("reqkey already exists");
854
855         return 0;
856 }
857
858 static int __handle_request_disable_pkgs(uid_t caller_uid,
859                 GDBusMethodInvocation *invocation, GVariant *parameters)
860 {
861         uid_t target_uid = (uid_t)-1;
862         char *pkgtype;
863         char *pkgid;
864         char *reqkey;
865         GVariantIter *iter;
866
867         g_variant_get(parameters, "(uas)", &target_uid, &iter);
868         if (target_uid == (uid_t)-1 || iter == NULL) {
869                 g_dbus_method_invocation_return_value(invocation,
870                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
871                 return -1;
872         }
873
874         reqkey = __generate_reqkey("disable_pkgs");
875         if (reqkey == NULL) {
876                 g_dbus_method_invocation_return_value(invocation,
877                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
878                 return -1;
879         }
880
881         while (g_variant_iter_next(iter, "&s", &pkgid)) {
882                 pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
883                 if (pkgtype == NULL) {
884                         g_dbus_method_invocation_return_value(invocation,
885                                         g_variant_new("(is)",
886                                                 PKGMGR_R_ENOPKG, ""));
887                         free(reqkey);
888                         return -1;
889                 }
890                 if (_push_queue(target_uid, caller_uid, reqkey,
891                                         REQUEST_TYPE_DISABLE_PKG,
892                                         pkgtype, pkgid, NULL, NULL)) {
893                         g_dbus_method_invocation_return_value(invocation,
894                                         g_variant_new("(is)",
895                                                 PKGMGR_R_ESYSTEM, ""));
896                         free(pkgtype);
897                         free(reqkey);
898                         return -1;
899                 }
900                 free(pkgtype);
901         }
902
903         g_dbus_method_invocation_return_value(invocation,
904                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
905         free(reqkey);
906
907         return 0;
908 }
909
910 static int __handle_request_enable_apps(uid_t caller_uid,
911                 GDBusMethodInvocation *invocation, GVariant *parameters)
912 {
913         uid_t target_uid = (uid_t)-1;
914         char *appid;
915         char *reqkey;
916         char buf[MAX_PKG_ARGS_LEN];
917         GVariantIter *iter;
918
919         g_variant_get(parameters, "(uas)", &target_uid, &iter);
920         if (target_uid == (uid_t)-1 || iter == NULL) {
921                 g_dbus_method_invocation_return_value(invocation,
922                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
923                 return -1;
924         }
925
926         reqkey = __generate_reqkey("enable_apps");
927         if (reqkey == NULL) {
928                 g_dbus_method_invocation_return_value(invocation,
929                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
930                 return -1;
931         }
932
933         while (g_variant_iter_next(iter, "&s", &appid)) {
934                 if (_push_queue(target_uid, caller_uid, reqkey,
935                                         REQUEST_TYPE_ENABLE_APP,
936                                         "default", appid, buf, NULL)) {
937                         g_dbus_method_invocation_return_value(invocation,
938                                         g_variant_new("(is)",
939                                                 PKGMGR_R_ESYSTEM, ""));
940                         free(reqkey);
941                         return -1;
942                 }
943         }
944
945         g_dbus_method_invocation_return_value(invocation,
946                 g_variant_new("(is)", PKGMGR_R_OK, reqkey));
947
948         free(reqkey);
949         return 0;
950 }
951
952 static int __handle_request_disable_apps(uid_t caller_uid,
953                 GDBusMethodInvocation *invocation, GVariant *parameters)
954 {
955         uid_t target_uid = (uid_t)-1;
956         char *appid;
957         char *reqkey;
958         char buf[MAX_PKG_ARGS_LEN];
959         GVariantIter *iter;
960
961         g_variant_get(parameters, "(uas)", &target_uid, &iter);
962         if (target_uid == (uid_t)-1 || iter == NULL) {
963                 g_dbus_method_invocation_return_value(invocation,
964                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
965                 return -1;
966         }
967
968         reqkey = __generate_reqkey("disable_apps");
969         if (reqkey == NULL) {
970                 g_dbus_method_invocation_return_value(invocation,
971                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
972                 return -1;
973         }
974
975         while (g_variant_iter_next(iter, "&s", &appid)) {
976                 if (_push_queue(target_uid, caller_uid, reqkey,
977                                         REQUEST_TYPE_DISABLE_APP,
978                                         "default", appid, buf, NULL)) {
979                         g_dbus_method_invocation_return_value(invocation,
980                                         g_variant_new("(is)",
981                                                 PKGMGR_R_ESYSTEM, ""));
982                         free(reqkey);
983                         return -1;
984                 }
985         }
986
987         g_dbus_method_invocation_return_value(invocation,
988                 g_variant_new("(is)", PKGMGR_R_OK, reqkey));
989
990         free(reqkey);
991         return 0;
992
993 }
994
995 static int __handle_request_enable_app(uid_t caller_uid,
996                 GDBusMethodInvocation *invocation, GVariant *parameters)
997 {
998         uid_t target_uid = (uid_t)-1;
999         char *appid = NULL;
1000         char *reqkey = NULL;
1001         int ret = -1;
1002
1003         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1004         if (target_uid == (uid_t)-1 || appid == NULL) {
1005                 g_dbus_method_invocation_return_value(invocation,
1006                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1007                 return -1;
1008         }
1009
1010         reqkey = __generate_reqkey(appid);
1011         if (reqkey == NULL) {
1012                 g_dbus_method_invocation_return_value(invocation,
1013                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1014                 ret = -1;
1015                 goto catch;
1016         }
1017
1018         if (_push_queue(target_uid, caller_uid, reqkey,
1019                                 REQUEST_TYPE_ENABLE_APP, "default",
1020                                 appid, NULL, NULL)) {
1021                 g_dbus_method_invocation_return_value(invocation,
1022                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1023                 ret = -1;
1024                 goto catch;
1025         }
1026
1027         g_dbus_method_invocation_return_value(invocation,
1028                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1029
1030         ret = 0;
1031
1032 catch:
1033         if (reqkey)
1034                 free(reqkey);
1035
1036         return ret;
1037 }
1038
1039 static int __handle_request_disable_app(uid_t caller_uid,
1040                 GDBusMethodInvocation *invocation, GVariant *parameters)
1041 {
1042         uid_t target_uid = (uid_t)-1;
1043         char *appid = NULL;
1044         char *reqkey = NULL;
1045         int ret = -1;
1046
1047         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1048         if (target_uid == (uid_t)-1 || appid == NULL) {
1049                 g_dbus_method_invocation_return_value(invocation,
1050                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1051                 return -1;
1052         }
1053
1054         reqkey = __generate_reqkey(appid);
1055         if (reqkey == NULL) {
1056                 g_dbus_method_invocation_return_value(invocation,
1057                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1058                 ret = -1;
1059                 goto catch;
1060         }
1061
1062         if (_push_queue(target_uid, caller_uid, reqkey,
1063                                 REQUEST_TYPE_DISABLE_APP, "default",
1064                                 appid, NULL, NULL)) {
1065                 g_dbus_method_invocation_return_value(invocation,
1066                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1067                 ret = -1;
1068                 goto catch;
1069         }
1070
1071         g_dbus_method_invocation_return_value(invocation,
1072                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1073
1074         ret = 0;
1075
1076 catch:
1077         if (reqkey)
1078                 free(reqkey);
1079
1080         return ret;
1081 }
1082
1083 static int __handle_request_enable_global_app_for_uid(uid_t caller_uid,
1084                 GDBusMethodInvocation *invocation, GVariant *parameters)
1085 {
1086         uid_t target_uid = (uid_t)-1;
1087         char *appid = NULL;
1088         char *reqkey = NULL;
1089         int ret = -1;
1090
1091         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1092         if (target_uid == (uid_t)-1 || appid == NULL) {
1093                 g_dbus_method_invocation_return_value(invocation,
1094                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1095                 return -1;
1096         }
1097
1098         reqkey = __generate_reqkey(appid);
1099         if (reqkey == NULL) {
1100                 g_dbus_method_invocation_return_value(invocation,
1101                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1102                 ret = -1;
1103                 goto catch;
1104         }
1105
1106         if (_push_queue(target_uid, caller_uid, reqkey,
1107                                 REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID,
1108                                 "default", appid, NULL, NULL)) {
1109                 g_dbus_method_invocation_return_value(invocation,
1110                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1111                 ret = -1;
1112                 goto catch;
1113         }
1114
1115         g_dbus_method_invocation_return_value(invocation,
1116                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1117
1118         ret = 0;
1119
1120 catch:
1121         if (reqkey)
1122                 free(reqkey);
1123
1124         return ret;
1125 }
1126
1127 static int __handle_request_disable_global_app_for_uid(uid_t caller_uid,
1128                 GDBusMethodInvocation *invocation, GVariant *parameters)
1129 {
1130         uid_t target_uid = (uid_t)-1;
1131         char *appid = NULL;
1132         char *reqkey = NULL;
1133         int ret = -1;
1134
1135         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1136         if (target_uid == (uid_t)-1 || appid == NULL) {
1137                 g_dbus_method_invocation_return_value(invocation,
1138                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1139                 return -1;
1140         }
1141
1142         reqkey = __generate_reqkey(appid);
1143         if (reqkey == NULL) {
1144                 g_dbus_method_invocation_return_value(invocation,
1145                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1146                 ret = -1;
1147                 goto catch;
1148         }
1149
1150         if (_push_queue(target_uid, caller_uid, reqkey,
1151                                 REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID,
1152                                 "default", appid, NULL, NULL)) {
1153                 g_dbus_method_invocation_return_value(invocation,
1154                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1155                 ret = -1;
1156                 goto catch;
1157         }
1158
1159         g_dbus_method_invocation_return_value(invocation,
1160                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1161
1162         ret = 0;
1163
1164 catch:
1165         if (reqkey)
1166                 free(reqkey);
1167
1168         return ret;
1169 }
1170
1171 static int __handle_request_getsize(uid_t caller_uid,
1172                 GDBusMethodInvocation *invocation, GVariant *parameters)
1173 {
1174         uid_t target_uid = (uid_t)-1;
1175         char *pkgid = NULL;
1176         int get_type = -1;
1177         char *reqkey;
1178         char buf[4];
1179
1180         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &get_type);
1181         if (target_uid == (uid_t)-1 || pkgid == NULL || get_type == -1) {
1182                 g_dbus_method_invocation_return_value(invocation,
1183                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1184                 return -1;
1185         }
1186
1187         reqkey = __generate_reqkey(pkgid);
1188         if (reqkey == NULL) {
1189                 g_dbus_method_invocation_return_value(invocation,
1190                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1191                 return -1;
1192         }
1193
1194         snprintf(buf, sizeof(buf), "%d", get_type);
1195         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_GETSIZE,
1196                                 "pkgtool", pkgid, buf, NULL)) {
1197                 g_dbus_method_invocation_return_value(invocation,
1198                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1199                 free(reqkey);
1200                 return -1;
1201         }
1202
1203         g_dbus_method_invocation_return_value(invocation,
1204                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1205         free(reqkey);
1206
1207         return 0;
1208 }
1209
1210 static int __handle_request_getsize_sync(uid_t caller_uid,
1211                 GDBusMethodInvocation *invocation, GVariant *parameters)
1212 {
1213         uid_t target_uid = (uid_t)-1;
1214         char *pkgid = NULL;
1215         int get_type = -1;
1216         char *reqkey;
1217         char buf[4];
1218
1219         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &get_type);
1220         if (target_uid == (uid_t)-1 || pkgid == NULL || get_type == -1) {
1221                 g_dbus_method_invocation_return_value(invocation,
1222                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1223                 return -1;
1224         }
1225
1226         reqkey = __generate_reqkey(pkgid);
1227         if (reqkey == NULL) {
1228                 g_dbus_method_invocation_return_value(invocation,
1229                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1230                 return -1;
1231         }
1232
1233         snprintf(buf, sizeof(buf), "%d", get_type);
1234         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_GETSIZE_SYNC,
1235                                 "pkgtool", pkgid, buf, NULL)) {
1236                 g_dbus_method_invocation_return_value(invocation,
1237                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1238                 free(reqkey);
1239                 return -1;
1240         }
1241
1242         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1243                                 (gpointer)invocation))
1244                 ERR("reqkey already exists");
1245
1246         return 0;
1247 }
1248
1249 static int __handle_request_cleardata(uid_t caller_uid,
1250                 GDBusMethodInvocation *invocation, GVariant *parameters)
1251 {
1252         uid_t target_uid = (uid_t)-1;
1253         char *pkgid = NULL;
1254         char *pkgtype;
1255
1256         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1257         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1258                 g_dbus_method_invocation_return_value(invocation,
1259                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1260                 return -1;
1261         }
1262
1263         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1264         if (pkgtype == NULL) {
1265                 g_dbus_method_invocation_return_value(invocation,
1266                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
1267                 return -1;
1268         }
1269
1270         if (_push_queue(target_uid, caller_uid, NULL, REQUEST_TYPE_CLEARDATA,
1271                                 pkgtype, pkgid, NULL, NULL)) {
1272                 g_dbus_method_invocation_return_value(invocation,
1273                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1274                 free(pkgtype);
1275                 return -1;
1276         }
1277
1278         g_dbus_method_invocation_return_value(invocation,
1279                         g_variant_new("(i)", PKGMGR_R_OK));
1280         free(pkgtype);
1281
1282         return 0;
1283 }
1284
1285 static int __handle_request_clearcache(uid_t caller_uid,
1286                 GDBusMethodInvocation *invocation, GVariant *parameters)
1287 {
1288         uid_t target_uid = (uid_t)-1;
1289         char *pkgid = NULL;
1290         char *pkgtype;
1291
1292         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1293         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1294                 g_dbus_method_invocation_return_value(invocation,
1295                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1296                 return -1;
1297         }
1298
1299         if (strcmp(PKG_CLEAR_ALL_CACHE, pkgid) == 0)
1300                 pkgtype = strdup("pkgtool");
1301         else
1302                 pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1303         if (pkgtype == NULL) {
1304                 g_dbus_method_invocation_return_value(invocation,
1305                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
1306                 return -1;
1307         }
1308
1309         if (_push_queue(target_uid, caller_uid, NULL, REQUEST_TYPE_CLEARCACHE,
1310                                 pkgtype,  pkgid, NULL, NULL)) {
1311                 g_dbus_method_invocation_return_value(invocation,
1312                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1313                 free(pkgtype);
1314                 return -1;
1315         }
1316
1317         g_dbus_method_invocation_return_value(invocation,
1318                         g_variant_new("(i)", PKGMGR_R_OK));
1319         free(pkgtype);
1320
1321         return 0;
1322 }
1323
1324 static int __handle_request_kill(uid_t caller_uid,
1325                 GDBusMethodInvocation *invocation, GVariant *parameters)
1326 {
1327         uid_t target_uid = (uid_t)-1;
1328         char *pkgid = NULL;
1329         char *reqkey = NULL;
1330
1331         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1332         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1333                 g_dbus_method_invocation_return_value(invocation,
1334                                 g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
1335                 return -1;
1336         }
1337
1338         reqkey = __generate_reqkey(pkgid);
1339         if (reqkey == NULL) {
1340                 g_dbus_method_invocation_return_value(invocation,
1341                                 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1342                 return -1;
1343         }
1344
1345         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_KILL,
1346                                 "default", pkgid, NULL, NULL)) {
1347                 g_dbus_method_invocation_return_value(invocation,
1348                                 g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
1349                 free(reqkey);
1350                 return -1;
1351         }
1352
1353         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1354                                 (gpointer)invocation))
1355                 ERR("reqkey already exists");
1356
1357         return 0;
1358 }
1359
1360 static int __handle_request_check(uid_t caller_uid,
1361                 GDBusMethodInvocation *invocation, GVariant *parameters)
1362 {
1363         uid_t target_uid = (uid_t)-1;
1364         char *pkgid = NULL;
1365         char *reqkey = NULL;
1366
1367         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1368         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1369                 g_dbus_method_invocation_return_value(invocation,
1370                                 g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
1371                 return -1;
1372         }
1373
1374         reqkey = __generate_reqkey(pkgid);
1375         if (reqkey == NULL) {
1376                 g_dbus_method_invocation_return_value(invocation,
1377                                 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1378                 return -1;
1379         }
1380
1381         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_CHECK,
1382                                 "default", pkgid, NULL, NULL)) {
1383                 g_dbus_method_invocation_return_value(invocation,
1384                                 g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
1385                 free(reqkey);
1386                 return -1;
1387         }
1388
1389         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1390                                 (gpointer)invocation))
1391                 ERR("reqkey already exists");
1392
1393         return 0;
1394 }
1395
1396 static int __handle_request_generate_license_request(uid_t caller_uid,
1397                 GDBusMethodInvocation *invocation, GVariant *parameters)
1398 {
1399         char *reqkey;
1400         char *resp_data = NULL;
1401
1402         g_variant_get(parameters, "(&s)", &resp_data);
1403         if (resp_data == NULL) {
1404                 g_dbus_method_invocation_return_value(invocation,
1405                                 g_variant_new("(iss)", PKGMGR_R_ECOMM, "", ""));
1406                 return -1;
1407         }
1408
1409         reqkey = __generate_reqkey("drm");
1410         if (reqkey == NULL) {
1411                 g_dbus_method_invocation_return_value(invocation,
1412                                 g_variant_new("(iss)", PKGMGR_R_ENOMEM, "",
1413                                         ""));
1414                 return -1;
1415         }
1416
1417         if (_push_queue(caller_uid, caller_uid, reqkey,
1418                                 REQUEST_TYPE_GENERATE_LICENSE_REQUEST,
1419                                 "default", NULL, resp_data, NULL)) {
1420                 g_dbus_method_invocation_return_value(invocation,
1421                                 g_variant_new("(iss)", PKGMGR_R_ESYSTEM, "",
1422                                         ""));
1423                 free(reqkey);
1424                 return -1;
1425         }
1426
1427         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1428                                 (gpointer)invocation))
1429                 ERR("reqkey already exists");
1430
1431         return 0;
1432 }
1433
1434 static int __handle_request_register_license(uid_t caller_uid,
1435                 GDBusMethodInvocation *invocation, GVariant *parameters)
1436 {
1437         char *reqkey;
1438         char *resp_data = NULL;
1439
1440         g_variant_get(parameters, "(&s)", &resp_data);
1441         if (resp_data == NULL) {
1442                 g_dbus_method_invocation_return_value(invocation,
1443                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1444                 return -1;
1445         }
1446
1447         reqkey = __generate_reqkey("drm");
1448         if (reqkey == NULL) {
1449                 g_dbus_method_invocation_return_value(invocation,
1450                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1451                 return -1;
1452         }
1453
1454         if (_push_queue(caller_uid, caller_uid, reqkey,
1455                                 REQUEST_TYPE_REGISTER_LICENSE,
1456                                 "default", NULL, resp_data, NULL)) {
1457                 g_dbus_method_invocation_return_value(invocation,
1458                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1459                 free(reqkey);
1460                 return -1;
1461         }
1462
1463         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1464                                 (gpointer)invocation))
1465                 ERR("reqkey already exists");
1466
1467         return 0;
1468 }
1469
1470 static int __handle_request_decrypt_package(uid_t caller_uid,
1471                 GDBusMethodInvocation *invocation, GVariant *parameters)
1472 {
1473         char *reqkey;
1474         char *drm_file_path = NULL;
1475         char *decrypted_file_path = NULL;
1476
1477         g_variant_get(parameters, "(&s&s)", &drm_file_path,
1478                         &decrypted_file_path);
1479         if (drm_file_path == NULL || decrypted_file_path == NULL) {
1480                 g_dbus_method_invocation_return_value(invocation,
1481                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1482                 return -1;
1483         }
1484
1485         reqkey = __generate_reqkey("drm");
1486         if (reqkey == NULL) {
1487                 g_dbus_method_invocation_return_value(invocation,
1488                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1489                 return -1;
1490         }
1491
1492         if (_push_queue(caller_uid, caller_uid, reqkey,
1493                                 REQUEST_TYPE_DECRYPT_PACKAGE,
1494                                 "default", drm_file_path,
1495                                 decrypted_file_path, NULL)) {
1496                 g_dbus_method_invocation_return_value(invocation,
1497                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1498                 free(reqkey);
1499                 return -1;
1500         }
1501
1502         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1503                                 (gpointer)invocation))
1504                 ERR("reqkey already exists");
1505
1506         return 0;
1507 }
1508
1509 static int __update_app_splash_screen(uid_t caller_uid,
1510                 GDBusMethodInvocation *invocation, GVariant *parameters,
1511                 int req_type)
1512 {
1513         uid_t target_uid = (uid_t)-1;
1514         char *appid = NULL;
1515         char *reqkey;
1516
1517         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1518         if (target_uid == (uid_t)-1 || appid == NULL) {
1519                 ERR("target_uid: %d, appid: %s", target_uid, appid);
1520                 g_dbus_method_invocation_return_value(invocation,
1521                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1522                 return -1;
1523         }
1524
1525         reqkey = __generate_reqkey(appid);
1526         if (reqkey == NULL) {
1527                 ERR("Failed to generate request key");
1528                 g_dbus_method_invocation_return_value(invocation,
1529                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1530                 return -1;
1531         }
1532
1533         if (_push_queue(target_uid, caller_uid, reqkey, req_type, "default",
1534                                 appid, NULL, NULL)) {
1535                 ERR("Failed to push request");
1536                 g_dbus_method_invocation_return_value(invocation,
1537                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1538                 free(reqkey);
1539                 return -1;
1540         }
1541
1542         g_dbus_method_invocation_return_value(invocation,
1543                         g_variant_new("(i)", PKGMGR_R_OK));
1544
1545         if (reqkey)
1546                 free(reqkey);
1547
1548         return 0;
1549 }
1550
1551 static int __handle_request_enable_app_splash_screen(uid_t caller_uid,
1552                 GDBusMethodInvocation *invocation, GVariant *parameters)
1553 {
1554         return __update_app_splash_screen(caller_uid, invocation, parameters,
1555                         REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN);
1556 }
1557
1558 static int __handle_request_disable_app_splash_screen(uid_t caller_uid,
1559                 GDBusMethodInvocation *invocation, GVariant *parameters)
1560 {
1561         return __update_app_splash_screen(caller_uid, invocation, parameters,
1562                         REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN);
1563 }
1564
1565 static int __handle_request_set_restriction_mode(uid_t caller_uid,
1566                 GDBusMethodInvocation *invocation, GVariant *parameters)
1567 {
1568         uid_t target_uid = (uid_t)-1;
1569         char *pkgid = NULL;
1570         char *reqkey;
1571         int mode = -1;
1572         char buf[4];
1573
1574         g_variant_get(parameters, "(usi)", &target_uid, &pkgid, &mode);
1575         if (target_uid == (uid_t)-1 || pkgid == NULL || mode < 0) {
1576                 g_dbus_method_invocation_return_value(invocation,
1577                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1578                 return -1;
1579         }
1580
1581         reqkey = __generate_reqkey("restriction");
1582         if (reqkey == NULL) {
1583                 g_dbus_method_invocation_return_value(invocation,
1584                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1585                 return -1;
1586         }
1587
1588         snprintf(buf, sizeof(buf), "%d", mode);
1589         if (_push_queue(target_uid, caller_uid, reqkey,
1590                                 REQUEST_TYPE_SET_RESTRICTION_MODE,
1591                                 "default", pkgid, buf, NULL)) {
1592                 g_dbus_method_invocation_return_value(invocation,
1593                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1594                 free(reqkey);
1595                 return -1;
1596         }
1597
1598         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1599                                 (gpointer)invocation))
1600                 ERR("reqkey already exists");
1601
1602         return 0;
1603 }
1604
1605 static int __handle_request_unset_restriction_mode(uid_t caller_uid,
1606                 GDBusMethodInvocation *invocation, GVariant *parameters)
1607 {
1608         uid_t target_uid = (uid_t)-1;
1609         char *pkgid = NULL;
1610         char *reqkey;
1611         int mode = -1;
1612         char buf[4];
1613
1614         g_variant_get(parameters, "(usi)", &target_uid, &pkgid, &mode);
1615         if (target_uid == (uid_t)-1 || pkgid == NULL || mode < 0) {
1616                 g_dbus_method_invocation_return_value(invocation,
1617                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1618                 return -1;
1619         }
1620
1621         reqkey = __generate_reqkey("restriction");
1622         if (reqkey == NULL) {
1623                 g_dbus_method_invocation_return_value(invocation,
1624                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1625                 return -1;
1626         }
1627
1628         snprintf(buf, sizeof(buf), "%d", mode);
1629         if (_push_queue(target_uid, caller_uid, reqkey,
1630                                 REQUEST_TYPE_UNSET_RESTRICTION_MODE,
1631                                 "default", pkgid, buf, NULL)) {
1632                 g_dbus_method_invocation_return_value(invocation,
1633                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1634                 free(reqkey);
1635                 return -1;
1636         }
1637
1638         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1639                                 (gpointer)invocation))
1640                 ERR("reqkey already exists");
1641
1642         return 0;
1643 }
1644
1645 static int __handle_request_get_restriction_mode(uid_t caller_uid,
1646                 GDBusMethodInvocation *invocation, GVariant *parameters)
1647 {
1648         uid_t target_uid = (uid_t)-1;
1649         char *pkgid = NULL;
1650         char *reqkey;
1651
1652         g_variant_get(parameters, "(us)", &target_uid, &pkgid);
1653         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1654                 g_dbus_method_invocation_return_value(invocation,
1655                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1656                 return -1;
1657         }
1658
1659         reqkey = __generate_reqkey("restriction");
1660         if (reqkey == NULL) {
1661                 g_dbus_method_invocation_return_value(invocation,
1662                                 g_variant_new("(ii)", -1, PKGMGR_R_ENOMEM));
1663                 return -1;
1664         }
1665
1666         if (_push_queue(target_uid, caller_uid, reqkey,
1667                                 REQUEST_TYPE_GET_RESTRICTION_MODE,
1668                                 "default", pkgid, NULL, NULL)) {
1669                 g_dbus_method_invocation_return_value(invocation,
1670                                 g_variant_new("(ii)", -1, PKGMGR_R_ESYSTEM));
1671                 free(reqkey);
1672                 return -1;
1673         }
1674
1675         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1676                                 (gpointer)invocation))
1677                 ERR("reqkey already exists");
1678
1679         return 0;
1680 }
1681
1682 static int __handle_request_set_app_label(uid_t uid,
1683                 GDBusMethodInvocation *invocation, GVariant *parameters)
1684 {
1685         uid_t target_uid = (uid_t)-1;
1686         char *appid = NULL;
1687         char *label = NULL;
1688         char *reqkey;
1689
1690         g_variant_get(parameters, "(uss)", &target_uid, &appid, &label);
1691         if (target_uid == (uid_t)-1 || appid == NULL || label == NULL) {
1692                 g_dbus_method_invocation_return_value(invocation,
1693                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1694                 return -1;
1695         }
1696
1697         reqkey = __generate_reqkey("appid");
1698         if (reqkey == NULL) {
1699                 g_dbus_method_invocation_return_value(invocation,
1700                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1701                 return -1;
1702         }
1703
1704         if (_push_queue(target_uid, uid, reqkey,
1705                                 REQUEST_TYPE_SET_APP_LABEL,
1706                                 "default", appid, label, NULL)) {
1707                 g_dbus_method_invocation_return_value(invocation,
1708                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1709                 free(reqkey);
1710                 return -1;
1711         }
1712
1713         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1714                                 (gpointer)invocation))
1715                 ERR("reqkey already exists");
1716
1717         return 0;
1718 }
1719
1720 static int __handle_request_migrate_external_image(uid_t uid,
1721                 GDBusMethodInvocation *invocation, GVariant *parameters)
1722 {
1723         uid_t target_uid = (uid_t)-1;
1724         char *pkgid = NULL;
1725         char *pkgtype;
1726         char *reqkey;
1727
1728         g_variant_get(parameters, "(us)", &target_uid, &pkgid);
1729         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1730                 g_dbus_method_invocation_return_value(invocation,
1731                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1732                 return -1;
1733         }
1734
1735         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1736         if (pkgtype == NULL) {
1737                 g_dbus_method_invocation_return_value(invocation,
1738                                 g_variant_new("(i)", PKGMGR_R_ENOPKG));
1739                 return -1;
1740         }
1741
1742         reqkey = __generate_reqkey(pkgid);
1743         if (reqkey == NULL) {
1744                 g_dbus_method_invocation_return_value(invocation,
1745                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1746                 free(pkgtype);
1747                 return -1;
1748         }
1749
1750         if (_push_queue(target_uid, uid, reqkey,
1751                                 REQUEST_TYPE_MIGRATE_EXTERNAL_IMAGE,
1752                                 pkgtype, pkgid, NULL, NULL)) {
1753                 g_dbus_method_invocation_return_value(invocation,
1754                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1755                 free(reqkey);
1756                 free(pkgtype);
1757                 return -1;
1758         }
1759
1760         g_dbus_method_invocation_return_value(invocation,
1761                         g_variant_new("(i)", PKGMGR_R_OK));
1762
1763         free(reqkey);
1764         free(pkgtype);
1765
1766         return 0;
1767 }
1768
1769 static uid_t __get_caller_uid(GDBusConnection *connection, const char *name)
1770 {
1771         GError *err = NULL;
1772         GVariant *result;
1773         uid_t uid;
1774
1775         result = g_dbus_connection_call_sync(connection,
1776                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
1777                         "org.freedesktop.DBus", "GetConnectionUnixUser",
1778                         g_variant_new("(s)", name), NULL,
1779                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1780         if (result == NULL) {
1781                 ERR("failed to get caller uid: %s", err->message);
1782                 g_error_free(err);
1783                 return (uid_t)-1;
1784         }
1785
1786         g_variant_get(result, "(u)", &uid);
1787         g_variant_unref(result);
1788
1789         return uid;
1790 }
1791
1792 static void __handle_method_call(GDBusConnection *connection,
1793                 const gchar *sender, const gchar *object_path,
1794                 const gchar *interface_name, const gchar *method_name,
1795                 GVariant *parameters, GDBusMethodInvocation *invocation,
1796                 gpointer user_data)
1797 {
1798         int ret;
1799         uid_t uid;
1800
1801         uid = __get_caller_uid(connection,
1802                 g_dbus_method_invocation_get_sender(invocation));
1803         if (uid == (uid_t)-1)
1804                 return;
1805
1806         if (__check_caller_permission(uid, invocation, parameters))
1807                 return;
1808
1809         if (g_strcmp0(method_name, "install") == 0)
1810                 ret = __handle_request_install(uid, invocation, parameters);
1811         else if (g_strcmp0(method_name, "mount_install") == 0)
1812                 ret = __handle_request_mount_install(uid, invocation,
1813                                 parameters);
1814         else if (g_strcmp0(method_name, "reinstall") == 0)
1815                 ret = __handle_request_reinstall(uid, invocation, parameters);
1816         else if (g_strcmp0(method_name, "uninstall") == 0)
1817                 ret = __handle_request_uninstall(uid, invocation, parameters);
1818         else if (g_strcmp0(method_name, "cleardata") == 0)
1819                 ret = __handle_request_cleardata(uid, invocation, parameters);
1820         else if (g_strcmp0(method_name, "move") == 0)
1821                 ret = __handle_request_move(uid, invocation, parameters);
1822         else if (g_strcmp0(method_name, "enable_pkgs") == 0)
1823                 ret = __handle_request_enable_pkgs(uid, invocation, parameters);
1824         else if (g_strcmp0(method_name, "disable_pkgs") == 0)
1825                 ret = __handle_request_disable_pkgs(uid, invocation, parameters);
1826         else if (g_strcmp0(method_name, "register_pkg_update_info") == 0)
1827                 ret = __handle_request_register_pkg_update_info(uid, invocation, parameters);
1828         else if (g_strcmp0(method_name, "unregister_pkg_update_info") == 0)
1829                 ret = __handle_request_unregister_pkg_update_info(uid, invocation, parameters);
1830         else if (g_strcmp0(method_name, "unregister_all_pkg_update_info") == 0)
1831                 ret = __handle_request_unregister_all_pkg_update_info(uid, invocation, parameters);
1832         else if (g_strcmp0(method_name, "getsize") == 0)
1833                 ret = __handle_request_getsize(uid, invocation, parameters);
1834         else if (g_strcmp0(method_name, "getsize_sync") == 0)
1835                 ret = __handle_request_getsize_sync(uid, invocation, parameters);
1836         else if (g_strcmp0(method_name, "clearcache") == 0)
1837                 ret = __handle_request_clearcache(uid, invocation, parameters);
1838         else if (g_strcmp0(method_name, "enable_app") == 0)
1839                 ret = __handle_request_enable_app(uid, invocation, parameters);
1840         else if (g_strcmp0(method_name, "disable_app") == 0)
1841                 ret = __handle_request_disable_app(uid, invocation, parameters);
1842         else if (g_strcmp0(method_name, "enable_apps") == 0)
1843                 ret = __handle_request_enable_apps(uid, invocation, parameters);
1844         else if (g_strcmp0(method_name, "disable_apps") == 0)
1845                 ret = __handle_request_disable_apps(uid, invocation, parameters);
1846         else if (g_strcmp0(method_name, "enable_global_app_for_uid") == 0)
1847                 ret = __handle_request_enable_global_app_for_uid(uid,
1848                                 invocation, parameters);
1849         else if (g_strcmp0(method_name, "disable_global_app_for_uid") == 0)
1850                 ret = __handle_request_disable_global_app_for_uid(uid,
1851                                 invocation, parameters);
1852         else if (g_strcmp0(method_name, "kill") == 0)
1853                 ret = __handle_request_kill(uid, invocation, parameters);
1854         else if (g_strcmp0(method_name, "check") == 0)
1855                 ret = __handle_request_check(uid, invocation, parameters);
1856         else if (g_strcmp0(method_name, "generate_license_request") == 0)
1857                 ret = __handle_request_generate_license_request(uid, invocation,
1858                                 parameters);
1859         else if (g_strcmp0(method_name, "register_license") == 0)
1860                 ret = __handle_request_register_license(uid, invocation,
1861                                 parameters);
1862         else if (g_strcmp0(method_name, "decrypt_package") == 0)
1863                 ret = __handle_request_decrypt_package(uid, invocation,
1864                                 parameters);
1865         else if (g_strcmp0(method_name, "disable_app_splash_screen") == 0)
1866                 ret = __handle_request_disable_app_splash_screen(uid,
1867                                 invocation, parameters);
1868         else if (g_strcmp0(method_name, "enable_app_splash_screen") == 0)
1869                 ret = __handle_request_enable_app_splash_screen(uid,
1870                                 invocation, parameters);
1871         else if (g_strcmp0(method_name, "set_restriction_mode") == 0)
1872                 ret = __handle_request_set_restriction_mode(uid, invocation,
1873                                 parameters);
1874         else if (g_strcmp0(method_name, "unset_restriction_mode") == 0)
1875                 ret = __handle_request_unset_restriction_mode(uid, invocation,
1876                                 parameters);
1877         else if (g_strcmp0(method_name, "get_restriction_mode") == 0)
1878                 ret = __handle_request_get_restriction_mode(uid, invocation,
1879                                 parameters);
1880         else if (g_strcmp0(method_name, "set_app_label") == 0)
1881                 ret = __handle_request_set_app_label(uid, invocation, parameters);
1882         else if (g_strcmp0(method_name, "migrate_external_image") == 0)
1883                 ret = __handle_request_migrate_external_image(uid, invocation,
1884                                 parameters);
1885         else
1886                 ret = -1;
1887
1888         if (ret == 0)
1889                 g_idle_add(queue_job, NULL);
1890 }
1891
1892 int _return_value_to_caller(const char *req_key, GVariant *result)
1893 {
1894         GDBusMethodInvocation *invocation;
1895
1896         invocation = (GDBusMethodInvocation *)g_hash_table_lookup(req_table,
1897                         (gpointer)req_key);
1898         if (invocation == NULL) {
1899                 ERR("no such request id");
1900                 return -1;
1901         }
1902
1903         g_dbus_method_invocation_return_value(invocation, result);
1904         g_hash_table_remove(req_table, (gpointer)req_key);
1905
1906         return 0;
1907 }
1908
1909 static const GDBusInterfaceVTable interface_vtable = {
1910         __handle_method_call,
1911         NULL,
1912         NULL,
1913 };
1914
1915 static void __on_bus_acquired(GDBusConnection *connection, const gchar *name,
1916                 gpointer user_data)
1917 {
1918         GError *err = NULL;
1919
1920         DBG("on bus acquired");
1921
1922         reg_id = g_dbus_connection_register_object(connection,
1923                         PKGMGR_DBUS_OBJECT_PATH,
1924                         instropection_data->interfaces[0],
1925                         &interface_vtable, NULL, NULL, &err);
1926
1927         if (reg_id == 0) {
1928                 ERR("failed to register object: %s", err->message);
1929                 g_error_free(err);
1930         }
1931 }
1932
1933 static void __on_name_acquired(GDBusConnection *connection, const gchar *name,
1934                 gpointer user_data)
1935 {
1936         DBG("on name acquired: %s", name);
1937 }
1938
1939 static void __on_name_lost(GDBusConnection *connection, const gchar *name,
1940                 gpointer user_data)
1941 {
1942         DBG("on name lost: %s", name);
1943 }
1944
1945 int _init_request_handler(void)
1946 {
1947         instropection_data = g_dbus_node_info_new_for_xml(instropection_xml,
1948                         NULL);
1949
1950         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, PKGMGR_DBUS_SERVICE,
1951                         G_BUS_NAME_OWNER_FLAGS_NONE, __on_bus_acquired,
1952                         __on_name_acquired, __on_name_lost, NULL, NULL);
1953
1954         req_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1955                         free, NULL);
1956         if (req_table == NULL)
1957                 return -1;
1958
1959         return 0;
1960 }
1961
1962 void _fini_request_handler(void)
1963 {
1964         g_hash_table_destroy(req_table);
1965         g_bus_unown_name(owner_id);
1966         g_dbus_node_info_unref(instropection_data);
1967 }