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