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