Add new implementation for pkg 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                 return -1;
728         }
729
730         ret = pkgmgrinfo_updateinfo_set_version(update_info, (char *)version);
731         if (ret != PMINFO_R_OK) {
732                 g_dbus_method_invocation_return_value(invocation,
733                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
734                 return -1;
735         }
736
737         ret = pkgmgrinfo_updateinfo_set_type(update_info, converted_type);
738         if (ret != PMINFO_R_OK) {
739                 g_dbus_method_invocation_return_value(invocation,
740                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
741                 return -1;
742         }
743
744         reqkey = __generate_reqkey("register_pkg_update_info");
745         if (reqkey == NULL) {
746                 g_dbus_method_invocation_return_value(invocation,
747                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
748                 return -1;
749         }
750
751         if (_push_queue(target_uid, caller_uid, reqkey,
752                         REQUEST_TYPE_REGISTER_PKG_UPDATE_INFO, "default",
753                         NULL, buf, update_info))
754                 g_dbus_method_invocation_return_value(invocation,
755                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
756
757         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
758                                 (gpointer)invocation))
759                 ERR("reqkey already exists");
760
761         return 0;
762 }
763
764 static int __handle_request_unregister_pkg_update_info(
765                 uid_t caller_uid, GDBusMethodInvocation *invocation,
766                 GVariant *parameters)
767 {
768         uid_t target_uid = (uid_t)-1;
769         char *pkgid = NULL;
770         char *reqkey;
771         pkgmgrinfo_pkginfo_h pkg_info;
772         bool is_global_pkg;
773         int ret;
774
775         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
776         if (target_uid == (uid_t)-1 || pkgid == NULL) {
777                 g_dbus_method_invocation_return_value(invocation,
778                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
779                 return -1;
780         }
781
782         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &pkg_info);
783         if (ret != PMINFO_R_OK) {
784                 g_dbus_method_invocation_return_value(invocation,
785                                 g_variant_new("(i)", PKGMGR_R_EIO));
786                 return -1;
787         }
788         ret = pkgmgrinfo_pkginfo_is_global(pkg_info, &is_global_pkg);
789         if (ret != PMINFO_R_OK) {
790                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
791                 g_dbus_method_invocation_return_value(invocation,
792                                 g_variant_new("(i)", PKGMGR_R_EIO));
793                 return -1;
794         }
795         pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_info);
796
797         /* return error if given pkg is global pkg but caller is not admin */
798         if (caller_uid >= REGULAR_USER && is_global_pkg
799                         && !__is_admin_user(caller_uid)) {
800                 g_dbus_method_invocation_return_value(invocation,
801                                 g_variant_new("(i)", PKGMGR_R_EPRIV));
802                 return -1;
803         }
804
805         reqkey = __generate_reqkey("unregister_pkg_update_info");
806         if (reqkey == NULL) {
807                 g_dbus_method_invocation_return_value(invocation,
808                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
809                 return -1;
810         }
811
812         if (_push_queue(target_uid, caller_uid, reqkey,
813                         REQUEST_TYPE_UNREGISTER_PKG_UPDATE_INFO, "default",
814                         pkgid, NULL, NULL))
815                 g_dbus_method_invocation_return_value(invocation,
816                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
817
818         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
819                                 (gpointer)invocation))
820                 ERR("reqkey already exists");
821
822
823         return 0;
824
825 }
826
827 static int __handle_request_unregister_all_pkg_update_info(
828                 uid_t caller_uid, GDBusMethodInvocation *invocation,
829                 GVariant *parameters)
830 {
831         uid_t target_uid = (uid_t)-1;
832         char *reqkey;
833
834         g_variant_get(parameters, "(u)", &target_uid);
835         if (target_uid == (uid_t)-1) {
836                 g_dbus_method_invocation_return_value(invocation,
837                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
838                 return -1;
839         }
840
841         reqkey = __generate_reqkey("unregister_all_pkg_update_info");
842         if (reqkey == NULL) {
843                 g_dbus_method_invocation_return_value(invocation,
844                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
845                 return -1;
846         }
847
848         if (_push_queue(target_uid, caller_uid, reqkey,
849                         REQUEST_TYPE_UNREGISTER_ALL_PKG_UPDATE_INFO, "default",
850                         NULL, NULL, NULL))
851                 g_dbus_method_invocation_return_value(invocation,
852                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
853
854         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
855                                 (gpointer)invocation))
856                 ERR("reqkey already exists");
857
858         return 0;
859 }
860
861 static int __handle_request_disable_pkgs(uid_t caller_uid,
862                 GDBusMethodInvocation *invocation, GVariant *parameters)
863 {
864         uid_t target_uid = (uid_t)-1;
865         char *pkgtype;
866         char *pkgid;
867         char *reqkey;
868         GVariantIter *iter;
869
870         g_variant_get(parameters, "(uas)", &target_uid, &iter);
871         if (target_uid == (uid_t)-1 || iter == NULL) {
872                 g_dbus_method_invocation_return_value(invocation,
873                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
874                 return -1;
875         }
876
877         reqkey = __generate_reqkey("disable_pkgs");
878         if (reqkey == NULL) {
879                 g_dbus_method_invocation_return_value(invocation,
880                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
881                 return -1;
882         }
883
884         while (g_variant_iter_next(iter, "&s", &pkgid)) {
885                 pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
886                 if (pkgtype == NULL) {
887                         g_dbus_method_invocation_return_value(invocation,
888                                         g_variant_new("(is)",
889                                                 PKGMGR_R_ENOPKG, ""));
890                         free(reqkey);
891                         return -1;
892                 }
893                 if (_push_queue(target_uid, caller_uid, reqkey,
894                                         REQUEST_TYPE_DISABLE_PKG,
895                                         pkgtype, pkgid, NULL, NULL)) {
896                         g_dbus_method_invocation_return_value(invocation,
897                                         g_variant_new("(is)",
898                                                 PKGMGR_R_ESYSTEM, ""));
899                         free(pkgtype);
900                         free(reqkey);
901                         return -1;
902                 }
903                 free(pkgtype);
904         }
905
906         g_dbus_method_invocation_return_value(invocation,
907                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
908         free(reqkey);
909
910         return 0;
911 }
912
913 static int __handle_request_enable_apps(uid_t caller_uid,
914                 GDBusMethodInvocation *invocation, GVariant *parameters)
915 {
916         uid_t target_uid = (uid_t)-1;
917         char *appid;
918         char *reqkey;
919         char buf[MAX_PKG_ARGS_LEN];
920         GVariantIter *iter;
921
922         g_variant_get(parameters, "(uas)", &target_uid, &iter);
923         if (target_uid == (uid_t)-1 || iter == NULL) {
924                 g_dbus_method_invocation_return_value(invocation,
925                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
926                 return -1;
927         }
928
929         reqkey = __generate_reqkey("enable_apps");
930         if (reqkey == NULL) {
931                 g_dbus_method_invocation_return_value(invocation,
932                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
933                 return -1;
934         }
935
936         while (g_variant_iter_next(iter, "&s", &appid)) {
937                 if (_push_queue(target_uid, caller_uid, reqkey,
938                                         REQUEST_TYPE_ENABLE_APP,
939                                         "default", appid, buf, NULL)) {
940                         g_dbus_method_invocation_return_value(invocation,
941                                         g_variant_new("(is)",
942                                                 PKGMGR_R_ESYSTEM, ""));
943                         free(reqkey);
944                         return -1;
945                 }
946         }
947
948         g_dbus_method_invocation_return_value(invocation,
949                 g_variant_new("(is)", PKGMGR_R_OK, reqkey));
950
951         free(reqkey);
952         return 0;
953 }
954
955 static int __handle_request_disable_apps(uid_t caller_uid,
956                 GDBusMethodInvocation *invocation, GVariant *parameters)
957 {
958         uid_t target_uid = (uid_t)-1;
959         char *appid;
960         char *reqkey;
961         char buf[MAX_PKG_ARGS_LEN];
962         GVariantIter *iter;
963
964         g_variant_get(parameters, "(uas)", &target_uid, &iter);
965         if (target_uid == (uid_t)-1 || iter == NULL) {
966                 g_dbus_method_invocation_return_value(invocation,
967                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
968                 return -1;
969         }
970
971         reqkey = __generate_reqkey("disable_apps");
972         if (reqkey == NULL) {
973                 g_dbus_method_invocation_return_value(invocation,
974                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
975                 return -1;
976         }
977
978         while (g_variant_iter_next(iter, "&s", &appid)) {
979                 if (_push_queue(target_uid, caller_uid, reqkey,
980                                         REQUEST_TYPE_DISABLE_APP,
981                                         "default", appid, buf, NULL)) {
982                         g_dbus_method_invocation_return_value(invocation,
983                                         g_variant_new("(is)",
984                                                 PKGMGR_R_ESYSTEM, ""));
985                         free(reqkey);
986                         return -1;
987                 }
988         }
989
990         g_dbus_method_invocation_return_value(invocation,
991                 g_variant_new("(is)", PKGMGR_R_OK, reqkey));
992
993         free(reqkey);
994         return 0;
995
996 }
997
998 static int __handle_request_enable_app(uid_t caller_uid,
999                 GDBusMethodInvocation *invocation, GVariant *parameters)
1000 {
1001         uid_t target_uid = (uid_t)-1;
1002         char *appid = NULL;
1003         char *reqkey = NULL;
1004         int ret = -1;
1005
1006         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1007         if (target_uid == (uid_t)-1 || appid == NULL) {
1008                 g_dbus_method_invocation_return_value(invocation,
1009                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1010                 return -1;
1011         }
1012
1013         reqkey = __generate_reqkey(appid);
1014         if (reqkey == NULL) {
1015                 g_dbus_method_invocation_return_value(invocation,
1016                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1017                 ret = -1;
1018                 goto catch;
1019         }
1020
1021         if (_push_queue(target_uid, caller_uid, reqkey,
1022                                 REQUEST_TYPE_ENABLE_APP, "default",
1023                                 appid, NULL, NULL)) {
1024                 g_dbus_method_invocation_return_value(invocation,
1025                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1026                 ret = -1;
1027                 goto catch;
1028         }
1029
1030         g_dbus_method_invocation_return_value(invocation,
1031                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1032
1033         ret = 0;
1034
1035 catch:
1036         if (reqkey)
1037                 free(reqkey);
1038
1039         return ret;
1040 }
1041
1042 static int __handle_request_disable_app(uid_t caller_uid,
1043                 GDBusMethodInvocation *invocation, GVariant *parameters)
1044 {
1045         uid_t target_uid = (uid_t)-1;
1046         char *appid = NULL;
1047         char *reqkey = NULL;
1048         int ret = -1;
1049
1050         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1051         if (target_uid == (uid_t)-1 || appid == NULL) {
1052                 g_dbus_method_invocation_return_value(invocation,
1053                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1054                 return -1;
1055         }
1056
1057         reqkey = __generate_reqkey(appid);
1058         if (reqkey == NULL) {
1059                 g_dbus_method_invocation_return_value(invocation,
1060                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1061                 ret = -1;
1062                 goto catch;
1063         }
1064
1065         if (_push_queue(target_uid, caller_uid, reqkey,
1066                                 REQUEST_TYPE_DISABLE_APP, "default",
1067                                 appid, NULL, NULL)) {
1068                 g_dbus_method_invocation_return_value(invocation,
1069                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1070                 ret = -1;
1071                 goto catch;
1072         }
1073
1074         g_dbus_method_invocation_return_value(invocation,
1075                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1076
1077         ret = 0;
1078
1079 catch:
1080         if (reqkey)
1081                 free(reqkey);
1082
1083         return ret;
1084 }
1085
1086 static int __handle_request_enable_global_app_for_uid(uid_t caller_uid,
1087                 GDBusMethodInvocation *invocation, GVariant *parameters)
1088 {
1089         uid_t target_uid = (uid_t)-1;
1090         char *appid = NULL;
1091         char *reqkey = NULL;
1092         int ret = -1;
1093
1094         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1095         if (target_uid == (uid_t)-1 || appid == NULL) {
1096                 g_dbus_method_invocation_return_value(invocation,
1097                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1098                 return -1;
1099         }
1100
1101         reqkey = __generate_reqkey(appid);
1102         if (reqkey == NULL) {
1103                 g_dbus_method_invocation_return_value(invocation,
1104                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1105                 ret = -1;
1106                 goto catch;
1107         }
1108
1109         if (_push_queue(target_uid, caller_uid, reqkey,
1110                                 REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID,
1111                                 "default", appid, NULL, NULL)) {
1112                 g_dbus_method_invocation_return_value(invocation,
1113                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1114                 ret = -1;
1115                 goto catch;
1116         }
1117
1118         g_dbus_method_invocation_return_value(invocation,
1119                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1120
1121         ret = 0;
1122
1123 catch:
1124         if (reqkey)
1125                 free(reqkey);
1126
1127         return ret;
1128 }
1129
1130 static int __handle_request_disable_global_app_for_uid(uid_t caller_uid,
1131                 GDBusMethodInvocation *invocation, GVariant *parameters)
1132 {
1133         uid_t target_uid = (uid_t)-1;
1134         char *appid = NULL;
1135         char *reqkey = NULL;
1136         int ret = -1;
1137
1138         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1139         if (target_uid == (uid_t)-1 || appid == NULL) {
1140                 g_dbus_method_invocation_return_value(invocation,
1141                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1142                 return -1;
1143         }
1144
1145         reqkey = __generate_reqkey(appid);
1146         if (reqkey == NULL) {
1147                 g_dbus_method_invocation_return_value(invocation,
1148                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1149                 ret = -1;
1150                 goto catch;
1151         }
1152
1153         if (_push_queue(target_uid, caller_uid, reqkey,
1154                                 REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID,
1155                                 "default", appid, NULL, NULL)) {
1156                 g_dbus_method_invocation_return_value(invocation,
1157                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1158                 ret = -1;
1159                 goto catch;
1160         }
1161
1162         g_dbus_method_invocation_return_value(invocation,
1163                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1164
1165         ret = 0;
1166
1167 catch:
1168         if (reqkey)
1169                 free(reqkey);
1170
1171         return ret;
1172 }
1173
1174 static int __handle_request_getsize(uid_t caller_uid,
1175                 GDBusMethodInvocation *invocation, GVariant *parameters)
1176 {
1177         uid_t target_uid = (uid_t)-1;
1178         char *pkgid = NULL;
1179         int get_type = -1;
1180         char *reqkey;
1181         char buf[4];
1182
1183         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &get_type);
1184         if (target_uid == (uid_t)-1 || pkgid == NULL || get_type == -1) {
1185                 g_dbus_method_invocation_return_value(invocation,
1186                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1187                 return -1;
1188         }
1189
1190         reqkey = __generate_reqkey(pkgid);
1191         if (reqkey == NULL) {
1192                 g_dbus_method_invocation_return_value(invocation,
1193                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1194                 return -1;
1195         }
1196
1197         snprintf(buf, sizeof(buf), "%d", get_type);
1198         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_GETSIZE,
1199                                 "pkgtool", pkgid, buf, NULL)) {
1200                 g_dbus_method_invocation_return_value(invocation,
1201                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1202                 free(reqkey);
1203                 return -1;
1204         }
1205
1206         g_dbus_method_invocation_return_value(invocation,
1207                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
1208         free(reqkey);
1209
1210         return 0;
1211 }
1212
1213 static int __handle_request_getsize_sync(uid_t caller_uid,
1214                 GDBusMethodInvocation *invocation, GVariant *parameters)
1215 {
1216         uid_t target_uid = (uid_t)-1;
1217         char *pkgid = NULL;
1218         int get_type = -1;
1219         char *reqkey;
1220         char buf[4];
1221
1222         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &get_type);
1223         if (target_uid == (uid_t)-1 || pkgid == NULL || get_type == -1) {
1224                 g_dbus_method_invocation_return_value(invocation,
1225                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
1226                 return -1;
1227         }
1228
1229         reqkey = __generate_reqkey(pkgid);
1230         if (reqkey == NULL) {
1231                 g_dbus_method_invocation_return_value(invocation,
1232                                 g_variant_new("(is)", PKGMGR_R_ENOMEM, ""));
1233                 return -1;
1234         }
1235
1236         snprintf(buf, sizeof(buf), "%d", get_type);
1237         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_GETSIZE_SYNC,
1238                                 "pkgtool", pkgid, buf, NULL)) {
1239                 g_dbus_method_invocation_return_value(invocation,
1240                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
1241                 free(reqkey);
1242                 return -1;
1243         }
1244
1245         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1246                                 (gpointer)invocation))
1247                 ERR("reqkey already exists");
1248
1249         return 0;
1250 }
1251
1252 static int __handle_request_cleardata(uid_t caller_uid,
1253                 GDBusMethodInvocation *invocation, GVariant *parameters)
1254 {
1255         uid_t target_uid = (uid_t)-1;
1256         char *pkgid = NULL;
1257         char *pkgtype;
1258
1259         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1260         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1261                 g_dbus_method_invocation_return_value(invocation,
1262                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1263                 return -1;
1264         }
1265
1266         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1267         if (pkgtype == NULL) {
1268                 g_dbus_method_invocation_return_value(invocation,
1269                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
1270                 return -1;
1271         }
1272
1273         if (_push_queue(target_uid, caller_uid, NULL, REQUEST_TYPE_CLEARDATA,
1274                                 pkgtype, pkgid, NULL, NULL)) {
1275                 g_dbus_method_invocation_return_value(invocation,
1276                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1277                 free(pkgtype);
1278                 return -1;
1279         }
1280
1281         g_dbus_method_invocation_return_value(invocation,
1282                         g_variant_new("(i)", PKGMGR_R_OK));
1283         free(pkgtype);
1284
1285         return 0;
1286 }
1287
1288 static int __handle_request_clearcache(uid_t caller_uid,
1289                 GDBusMethodInvocation *invocation, GVariant *parameters)
1290 {
1291         uid_t target_uid = (uid_t)-1;
1292         char *pkgid = NULL;
1293         char *pkgtype;
1294
1295         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1296         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1297                 g_dbus_method_invocation_return_value(invocation,
1298                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1299                 return -1;
1300         }
1301
1302         if (strcmp(PKG_CLEAR_ALL_CACHE, pkgid) == 0)
1303                 pkgtype = strdup("pkgtool");
1304         else
1305                 pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1306         if (pkgtype == NULL) {
1307                 g_dbus_method_invocation_return_value(invocation,
1308                                 g_variant_new("(is)", PKGMGR_R_ENOPKG, ""));
1309                 return -1;
1310         }
1311
1312         if (_push_queue(target_uid, caller_uid, NULL, REQUEST_TYPE_CLEARCACHE,
1313                                 pkgtype,  pkgid, NULL, NULL)) {
1314                 g_dbus_method_invocation_return_value(invocation,
1315                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1316                 free(pkgtype);
1317                 return -1;
1318         }
1319
1320         g_dbus_method_invocation_return_value(invocation,
1321                         g_variant_new("(i)", PKGMGR_R_OK));
1322         free(pkgtype);
1323
1324         return 0;
1325 }
1326
1327 static int __handle_request_kill(uid_t caller_uid,
1328                 GDBusMethodInvocation *invocation, GVariant *parameters)
1329 {
1330         uid_t target_uid = (uid_t)-1;
1331         char *pkgid = NULL;
1332         char *reqkey = NULL;
1333
1334         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1335         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1336                 g_dbus_method_invocation_return_value(invocation,
1337                                 g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
1338                 return -1;
1339         }
1340
1341         reqkey = __generate_reqkey(pkgid);
1342         if (reqkey == NULL) {
1343                 g_dbus_method_invocation_return_value(invocation,
1344                                 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1345                 return -1;
1346         }
1347
1348         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_KILL,
1349                                 "default", pkgid, NULL, NULL)) {
1350                 g_dbus_method_invocation_return_value(invocation,
1351                                 g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
1352                 free(reqkey);
1353                 return -1;
1354         }
1355
1356         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1357                                 (gpointer)invocation))
1358                 ERR("reqkey already exists");
1359
1360         return 0;
1361 }
1362
1363 static int __handle_request_check(uid_t caller_uid,
1364                 GDBusMethodInvocation *invocation, GVariant *parameters)
1365 {
1366         uid_t target_uid = (uid_t)-1;
1367         char *pkgid = NULL;
1368         char *reqkey = NULL;
1369
1370         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1371         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1372                 g_dbus_method_invocation_return_value(invocation,
1373                                 g_variant_new("(ii)", PKGMGR_R_ECOMM, 0));
1374                 return -1;
1375         }
1376
1377         reqkey = __generate_reqkey(pkgid);
1378         if (reqkey == NULL) {
1379                 g_dbus_method_invocation_return_value(invocation,
1380                                 g_variant_new("(ii)", PKGMGR_R_ENOMEM, 0));
1381                 return -1;
1382         }
1383
1384         if (_push_queue(target_uid, caller_uid, reqkey, REQUEST_TYPE_CHECK,
1385                                 "default", pkgid, NULL, NULL)) {
1386                 g_dbus_method_invocation_return_value(invocation,
1387                                 g_variant_new("(ii)", PKGMGR_R_ESYSTEM, 0));
1388                 free(reqkey);
1389                 return -1;
1390         }
1391
1392         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1393                                 (gpointer)invocation))
1394                 ERR("reqkey already exists");
1395
1396         return 0;
1397 }
1398
1399 static int __handle_request_generate_license_request(uid_t caller_uid,
1400                 GDBusMethodInvocation *invocation, GVariant *parameters)
1401 {
1402         char *reqkey;
1403         char *resp_data = NULL;
1404
1405         g_variant_get(parameters, "(&s)", &resp_data);
1406         if (resp_data == NULL) {
1407                 g_dbus_method_invocation_return_value(invocation,
1408                                 g_variant_new("(iss)", PKGMGR_R_ECOMM, "", ""));
1409                 return -1;
1410         }
1411
1412         reqkey = __generate_reqkey("drm");
1413         if (reqkey == NULL) {
1414                 g_dbus_method_invocation_return_value(invocation,
1415                                 g_variant_new("(iss)", PKGMGR_R_ENOMEM, "",
1416                                         ""));
1417                 return -1;
1418         }
1419
1420         if (_push_queue(caller_uid, caller_uid, reqkey,
1421                                 REQUEST_TYPE_GENERATE_LICENSE_REQUEST,
1422                                 "default", NULL, resp_data, NULL)) {
1423                 g_dbus_method_invocation_return_value(invocation,
1424                                 g_variant_new("(iss)", PKGMGR_R_ESYSTEM, "",
1425                                         ""));
1426                 free(reqkey);
1427                 return -1;
1428         }
1429
1430         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1431                                 (gpointer)invocation))
1432                 ERR("reqkey already exists");
1433
1434         return 0;
1435 }
1436
1437 static int __handle_request_register_license(uid_t caller_uid,
1438                 GDBusMethodInvocation *invocation, GVariant *parameters)
1439 {
1440         char *reqkey;
1441         char *resp_data = NULL;
1442
1443         g_variant_get(parameters, "(&s)", &resp_data);
1444         if (resp_data == NULL) {
1445                 g_dbus_method_invocation_return_value(invocation,
1446                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1447                 return -1;
1448         }
1449
1450         reqkey = __generate_reqkey("drm");
1451         if (reqkey == NULL) {
1452                 g_dbus_method_invocation_return_value(invocation,
1453                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1454                 return -1;
1455         }
1456
1457         if (_push_queue(caller_uid, caller_uid, reqkey,
1458                                 REQUEST_TYPE_REGISTER_LICENSE,
1459                                 "default", NULL, resp_data, NULL)) {
1460                 g_dbus_method_invocation_return_value(invocation,
1461                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1462                 free(reqkey);
1463                 return -1;
1464         }
1465
1466         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1467                                 (gpointer)invocation))
1468                 ERR("reqkey already exists");
1469
1470         return 0;
1471 }
1472
1473 static int __handle_request_decrypt_package(uid_t caller_uid,
1474                 GDBusMethodInvocation *invocation, GVariant *parameters)
1475 {
1476         char *reqkey;
1477         char *drm_file_path = NULL;
1478         char *decrypted_file_path = NULL;
1479
1480         g_variant_get(parameters, "(&s&s)", &drm_file_path,
1481                         &decrypted_file_path);
1482         if (drm_file_path == NULL || decrypted_file_path == NULL) {
1483                 g_dbus_method_invocation_return_value(invocation,
1484                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1485                 return -1;
1486         }
1487
1488         reqkey = __generate_reqkey("drm");
1489         if (reqkey == NULL) {
1490                 g_dbus_method_invocation_return_value(invocation,
1491                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1492                 return -1;
1493         }
1494
1495         if (_push_queue(caller_uid, caller_uid, reqkey,
1496                                 REQUEST_TYPE_DECRYPT_PACKAGE,
1497                                 "default", drm_file_path,
1498                                 decrypted_file_path, NULL)) {
1499                 g_dbus_method_invocation_return_value(invocation,
1500                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1501                 free(reqkey);
1502                 return -1;
1503         }
1504
1505         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1506                                 (gpointer)invocation))
1507                 ERR("reqkey already exists");
1508
1509         return 0;
1510 }
1511
1512 static int __update_app_splash_screen(uid_t caller_uid,
1513                 GDBusMethodInvocation *invocation, GVariant *parameters,
1514                 int req_type)
1515 {
1516         uid_t target_uid = (uid_t)-1;
1517         char *appid = NULL;
1518         char *reqkey;
1519
1520         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1521         if (target_uid == (uid_t)-1 || appid == NULL) {
1522                 ERR("target_uid: %d, appid: %s", target_uid, appid);
1523                 g_dbus_method_invocation_return_value(invocation,
1524                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1525                 return -1;
1526         }
1527
1528         reqkey = __generate_reqkey(appid);
1529         if (reqkey == NULL) {
1530                 ERR("Failed to generate request key");
1531                 g_dbus_method_invocation_return_value(invocation,
1532                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1533                 return -1;
1534         }
1535
1536         if (_push_queue(target_uid, caller_uid, reqkey, req_type, "default",
1537                                 appid, NULL, NULL)) {
1538                 ERR("Failed to push request");
1539                 g_dbus_method_invocation_return_value(invocation,
1540                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1541                 free(reqkey);
1542                 return -1;
1543         }
1544
1545         g_dbus_method_invocation_return_value(invocation,
1546                         g_variant_new("(i)", PKGMGR_R_OK));
1547
1548         if (reqkey)
1549                 free(reqkey);
1550
1551         return 0;
1552 }
1553
1554 static int __handle_request_enable_app_splash_screen(uid_t caller_uid,
1555                 GDBusMethodInvocation *invocation, GVariant *parameters)
1556 {
1557         return __update_app_splash_screen(caller_uid, invocation, parameters,
1558                         REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN);
1559 }
1560
1561 static int __handle_request_disable_app_splash_screen(uid_t caller_uid,
1562                 GDBusMethodInvocation *invocation, GVariant *parameters)
1563 {
1564         return __update_app_splash_screen(caller_uid, invocation, parameters,
1565                         REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN);
1566 }
1567
1568 static int __handle_request_set_restriction_mode(uid_t caller_uid,
1569                 GDBusMethodInvocation *invocation, GVariant *parameters)
1570 {
1571         uid_t target_uid = (uid_t)-1;
1572         char *pkgid = NULL;
1573         char *reqkey;
1574         int mode = -1;
1575         char buf[4];
1576
1577         g_variant_get(parameters, "(usi)", &target_uid, &pkgid, &mode);
1578         if (target_uid == (uid_t)-1 || pkgid == NULL || mode < 0) {
1579                 g_dbus_method_invocation_return_value(invocation,
1580                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1581                 return -1;
1582         }
1583
1584         reqkey = __generate_reqkey("restriction");
1585         if (reqkey == NULL) {
1586                 g_dbus_method_invocation_return_value(invocation,
1587                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1588                 return -1;
1589         }
1590
1591         snprintf(buf, sizeof(buf), "%d", mode);
1592         if (_push_queue(target_uid, caller_uid, reqkey,
1593                                 REQUEST_TYPE_SET_RESTRICTION_MODE,
1594                                 "default", pkgid, buf, NULL)) {
1595                 g_dbus_method_invocation_return_value(invocation,
1596                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1597                 free(reqkey);
1598                 return -1;
1599         }
1600
1601         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1602                                 (gpointer)invocation))
1603                 ERR("reqkey already exists");
1604
1605         return 0;
1606 }
1607
1608 static int __handle_request_unset_restriction_mode(uid_t caller_uid,
1609                 GDBusMethodInvocation *invocation, GVariant *parameters)
1610 {
1611         uid_t target_uid = (uid_t)-1;
1612         char *pkgid = NULL;
1613         char *reqkey;
1614         int mode = -1;
1615         char buf[4];
1616
1617         g_variant_get(parameters, "(usi)", &target_uid, &pkgid, &mode);
1618         if (target_uid == (uid_t)-1 || pkgid == NULL || mode < 0) {
1619                 g_dbus_method_invocation_return_value(invocation,
1620                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1621                 return -1;
1622         }
1623
1624         reqkey = __generate_reqkey("restriction");
1625         if (reqkey == NULL) {
1626                 g_dbus_method_invocation_return_value(invocation,
1627                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1628                 return -1;
1629         }
1630
1631         snprintf(buf, sizeof(buf), "%d", mode);
1632         if (_push_queue(target_uid, caller_uid, reqkey,
1633                                 REQUEST_TYPE_UNSET_RESTRICTION_MODE,
1634                                 "default", pkgid, buf, NULL)) {
1635                 g_dbus_method_invocation_return_value(invocation,
1636                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1637                 free(reqkey);
1638                 return -1;
1639         }
1640
1641         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1642                                 (gpointer)invocation))
1643                 ERR("reqkey already exists");
1644
1645         return 0;
1646 }
1647
1648 static int __handle_request_get_restriction_mode(uid_t caller_uid,
1649                 GDBusMethodInvocation *invocation, GVariant *parameters)
1650 {
1651         uid_t target_uid = (uid_t)-1;
1652         char *pkgid = NULL;
1653         char *reqkey;
1654
1655         g_variant_get(parameters, "(us)", &target_uid, &pkgid);
1656         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1657                 g_dbus_method_invocation_return_value(invocation,
1658                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1659                 return -1;
1660         }
1661
1662         reqkey = __generate_reqkey("restriction");
1663         if (reqkey == NULL) {
1664                 g_dbus_method_invocation_return_value(invocation,
1665                                 g_variant_new("(ii)", -1, PKGMGR_R_ENOMEM));
1666                 return -1;
1667         }
1668
1669         if (_push_queue(target_uid, caller_uid, reqkey,
1670                                 REQUEST_TYPE_GET_RESTRICTION_MODE,
1671                                 "default", pkgid, NULL, NULL)) {
1672                 g_dbus_method_invocation_return_value(invocation,
1673                                 g_variant_new("(ii)", -1, PKGMGR_R_ESYSTEM));
1674                 free(reqkey);
1675                 return -1;
1676         }
1677
1678         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1679                                 (gpointer)invocation))
1680                 ERR("reqkey already exists");
1681
1682         return 0;
1683 }
1684
1685 static int __handle_request_set_app_label(uid_t uid,
1686                 GDBusMethodInvocation *invocation, GVariant *parameters)
1687 {
1688         uid_t target_uid = (uid_t)-1;
1689         char *appid = NULL;
1690         char *label = NULL;
1691         char *reqkey;
1692
1693         g_variant_get(parameters, "(uss)", &target_uid, &appid, &label);
1694         if (target_uid == (uid_t)-1 || appid == NULL || label == NULL) {
1695                 g_dbus_method_invocation_return_value(invocation,
1696                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1697                 return -1;
1698         }
1699
1700         reqkey = __generate_reqkey("appid");
1701         if (reqkey == NULL) {
1702                 g_dbus_method_invocation_return_value(invocation,
1703                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1704                 return -1;
1705         }
1706
1707         if (_push_queue(target_uid, uid, reqkey,
1708                                 REQUEST_TYPE_SET_APP_LABEL,
1709                                 "default", appid, label, NULL)) {
1710                 g_dbus_method_invocation_return_value(invocation,
1711                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1712                 free(reqkey);
1713                 return -1;
1714         }
1715
1716         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1717                                 (gpointer)invocation))
1718                 ERR("reqkey already exists");
1719
1720         return 0;
1721 }
1722
1723 static int __handle_request_migrate_external_image(uid_t uid,
1724                 GDBusMethodInvocation *invocation, GVariant *parameters)
1725 {
1726         uid_t target_uid = (uid_t)-1;
1727         char *pkgid = NULL;
1728         char *pkgtype;
1729         char *reqkey;
1730
1731         g_variant_get(parameters, "(us)", &target_uid, &pkgid);
1732         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1733                 g_dbus_method_invocation_return_value(invocation,
1734                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1735                 return -1;
1736         }
1737
1738         pkgtype = _get_pkgtype_from_pkgid(pkgid, target_uid);
1739         if (pkgtype == NULL) {
1740                 g_dbus_method_invocation_return_value(invocation,
1741                                 g_variant_new("(i)", PKGMGR_R_ENOPKG));
1742                 return -1;
1743         }
1744
1745         reqkey = __generate_reqkey(pkgid);
1746         if (reqkey == NULL) {
1747                 g_dbus_method_invocation_return_value(invocation,
1748                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1749                 free(pkgtype);
1750                 return -1;
1751         }
1752
1753         if (_push_queue(target_uid, uid, reqkey,
1754                                 REQUEST_TYPE_MIGRATE_EXTERNAL_IMAGE,
1755                                 pkgtype, pkgid, NULL, NULL)) {
1756                 g_dbus_method_invocation_return_value(invocation,
1757                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1758                 free(reqkey);
1759                 free(pkgtype);
1760                 return -1;
1761         }
1762
1763         g_dbus_method_invocation_return_value(invocation,
1764                         g_variant_new("(i)", PKGMGR_R_OK));
1765
1766         free(reqkey);
1767         free(pkgtype);
1768
1769         return 0;
1770 }
1771
1772 static uid_t __get_caller_uid(GDBusConnection *connection, const char *name)
1773 {
1774         GError *err = NULL;
1775         GVariant *result;
1776         uid_t uid;
1777
1778         result = g_dbus_connection_call_sync(connection,
1779                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
1780                         "org.freedesktop.DBus", "GetConnectionUnixUser",
1781                         g_variant_new("(s)", name), NULL,
1782                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1783         if (result == NULL) {
1784                 ERR("failed to get caller uid: %s", err->message);
1785                 g_error_free(err);
1786                 return (uid_t)-1;
1787         }
1788
1789         g_variant_get(result, "(u)", &uid);
1790         g_variant_unref(result);
1791
1792         return uid;
1793 }
1794
1795 static void __handle_method_call(GDBusConnection *connection,
1796                 const gchar *sender, const gchar *object_path,
1797                 const gchar *interface_name, const gchar *method_name,
1798                 GVariant *parameters, GDBusMethodInvocation *invocation,
1799                 gpointer user_data)
1800 {
1801         int ret;
1802         uid_t uid;
1803
1804         uid = __get_caller_uid(connection,
1805                 g_dbus_method_invocation_get_sender(invocation));
1806         if (uid == (uid_t)-1)
1807                 return;
1808
1809         if (__check_caller_permission(uid, invocation, parameters))
1810                 return;
1811
1812         if (g_strcmp0(method_name, "install") == 0)
1813                 ret = __handle_request_install(uid, invocation, parameters);
1814         else if (g_strcmp0(method_name, "mount_install") == 0)
1815                 ret = __handle_request_mount_install(uid, invocation,
1816                                 parameters);
1817         else if (g_strcmp0(method_name, "reinstall") == 0)
1818                 ret = __handle_request_reinstall(uid, invocation, parameters);
1819         else if (g_strcmp0(method_name, "uninstall") == 0)
1820                 ret = __handle_request_uninstall(uid, invocation, parameters);
1821         else if (g_strcmp0(method_name, "cleardata") == 0)
1822                 ret = __handle_request_cleardata(uid, invocation, parameters);
1823         else if (g_strcmp0(method_name, "move") == 0)
1824                 ret = __handle_request_move(uid, invocation, parameters);
1825         else if (g_strcmp0(method_name, "enable_pkgs") == 0)
1826                 ret = __handle_request_enable_pkgs(uid, invocation, parameters);
1827         else if (g_strcmp0(method_name, "disable_pkgs") == 0)
1828                 ret = __handle_request_disable_pkgs(uid, invocation, parameters);
1829         else if (g_strcmp0(method_name, "register_pkg_update_info") == 0)
1830                 ret = __handle_request_register_pkg_update_info(uid, invocation, parameters);
1831         else if (g_strcmp0(method_name, "unregister_pkg_update_info") == 0)
1832                 ret = __handle_request_unregister_pkg_update_info(uid, invocation, parameters);
1833         else if (g_strcmp0(method_name, "unregister_all_pkg_update_info") == 0)
1834                 ret = __handle_request_unregister_all_pkg_update_info(uid, invocation, parameters);
1835         else if (g_strcmp0(method_name, "getsize") == 0)
1836                 ret = __handle_request_getsize(uid, invocation, parameters);
1837         else if (g_strcmp0(method_name, "getsize_sync") == 0)
1838                 ret = __handle_request_getsize_sync(uid, invocation, parameters);
1839         else if (g_strcmp0(method_name, "clearcache") == 0)
1840                 ret = __handle_request_clearcache(uid, invocation, parameters);
1841         else if (g_strcmp0(method_name, "enable_app") == 0)
1842                 ret = __handle_request_enable_app(uid, invocation, parameters);
1843         else if (g_strcmp0(method_name, "disable_app") == 0)
1844                 ret = __handle_request_disable_app(uid, invocation, parameters);
1845         else if (g_strcmp0(method_name, "enable_apps") == 0)
1846                 ret = __handle_request_enable_apps(uid, invocation, parameters);
1847         else if (g_strcmp0(method_name, "disable_apps") == 0)
1848                 ret = __handle_request_disable_apps(uid, invocation, parameters);
1849         else if (g_strcmp0(method_name, "enable_global_app_for_uid") == 0)
1850                 ret = __handle_request_enable_global_app_for_uid(uid,
1851                                 invocation, parameters);
1852         else if (g_strcmp0(method_name, "disable_global_app_for_uid") == 0)
1853                 ret = __handle_request_disable_global_app_for_uid(uid,
1854                                 invocation, parameters);
1855         else if (g_strcmp0(method_name, "kill") == 0)
1856                 ret = __handle_request_kill(uid, invocation, parameters);
1857         else if (g_strcmp0(method_name, "check") == 0)
1858                 ret = __handle_request_check(uid, invocation, parameters);
1859         else if (g_strcmp0(method_name, "generate_license_request") == 0)
1860                 ret = __handle_request_generate_license_request(uid, invocation,
1861                                 parameters);
1862         else if (g_strcmp0(method_name, "register_license") == 0)
1863                 ret = __handle_request_register_license(uid, invocation,
1864                                 parameters);
1865         else if (g_strcmp0(method_name, "decrypt_package") == 0)
1866                 ret = __handle_request_decrypt_package(uid, invocation,
1867                                 parameters);
1868         else if (g_strcmp0(method_name, "disable_app_splash_screen") == 0)
1869                 ret = __handle_request_disable_app_splash_screen(uid,
1870                                 invocation, parameters);
1871         else if (g_strcmp0(method_name, "enable_app_splash_screen") == 0)
1872                 ret = __handle_request_enable_app_splash_screen(uid,
1873                                 invocation, parameters);
1874         else if (g_strcmp0(method_name, "set_restriction_mode") == 0)
1875                 ret = __handle_request_set_restriction_mode(uid, invocation,
1876                                 parameters);
1877         else if (g_strcmp0(method_name, "unset_restriction_mode") == 0)
1878                 ret = __handle_request_unset_restriction_mode(uid, invocation,
1879                                 parameters);
1880         else if (g_strcmp0(method_name, "get_restriction_mode") == 0)
1881                 ret = __handle_request_get_restriction_mode(uid, invocation,
1882                                 parameters);
1883         else if (g_strcmp0(method_name, "set_app_label") == 0)
1884                 ret = __handle_request_set_app_label(uid, invocation, parameters);
1885         else if (g_strcmp0(method_name, "migrate_external_image") == 0)
1886                 ret = __handle_request_migrate_external_image(uid, invocation,
1887                                 parameters);
1888         else
1889                 ret = -1;
1890
1891         if (ret == 0)
1892                 g_idle_add(queue_job, NULL);
1893 }
1894
1895 int _return_value_to_caller(const char *req_key, GVariant *result)
1896 {
1897         GDBusMethodInvocation *invocation;
1898
1899         invocation = (GDBusMethodInvocation *)g_hash_table_lookup(req_table,
1900                         (gpointer)req_key);
1901         if (invocation == NULL) {
1902                 ERR("no such request id");
1903                 return -1;
1904         }
1905
1906         g_dbus_method_invocation_return_value(invocation, result);
1907         g_hash_table_remove(req_table, (gpointer)req_key);
1908
1909         return 0;
1910 }
1911
1912 static const GDBusInterfaceVTable interface_vtable = {
1913         __handle_method_call,
1914         NULL,
1915         NULL,
1916 };
1917
1918 static void __on_bus_acquired(GDBusConnection *connection, const gchar *name,
1919                 gpointer user_data)
1920 {
1921         GError *err = NULL;
1922
1923         DBG("on bus acquired");
1924
1925         reg_id = g_dbus_connection_register_object(connection,
1926                         PKGMGR_DBUS_OBJECT_PATH,
1927                         instropection_data->interfaces[0],
1928                         &interface_vtable, NULL, NULL, &err);
1929
1930         if (reg_id == 0) {
1931                 ERR("failed to register object: %s", err->message);
1932                 g_error_free(err);
1933         }
1934 }
1935
1936 static void __on_name_acquired(GDBusConnection *connection, const gchar *name,
1937                 gpointer user_data)
1938 {
1939         DBG("on name acquired: %s", name);
1940 }
1941
1942 static void __on_name_lost(GDBusConnection *connection, const gchar *name,
1943                 gpointer user_data)
1944 {
1945         DBG("on name lost: %s", name);
1946 }
1947
1948 int _init_request_handler(void)
1949 {
1950         instropection_data = g_dbus_node_info_new_for_xml(instropection_xml,
1951                         NULL);
1952
1953         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, PKGMGR_DBUS_SERVICE,
1954                         G_BUS_NAME_OWNER_FLAGS_NONE, __on_bus_acquired,
1955                         __on_name_acquired, __on_name_lost, NULL, NULL);
1956
1957         req_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1958                         free, NULL);
1959         if (req_table == NULL)
1960                 return -1;
1961
1962         return 0;
1963 }
1964
1965 void _fini_request_handler(void)
1966 {
1967         g_hash_table_destroy(req_table);
1968         g_bus_unown_name(owner_id);
1969         g_dbus_node_info_unref(instropection_data);
1970 }