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