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