Add support for mount install
[platform/core/appfw/pkgmgr-server.git] / src / request.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3 #include <sys/time.h>
4
5 #include <glib.h>
6 #include <gio/gio.h>
7 #include <gum/gum-user.h>
8 #include <gum/common/gum-user-types.h>
9
10 #include "pm-queue.h"
11 #include "pkgmgr-server.h"
12 #include "package-manager.h"
13
14 #define PKGMGR_DBUS_SERVICE "org.tizen.pkgmgr"
15 #define PKGMGR_DBUS_OBJECT_PATH "/org/tizen/pkgmgr"
16
17 static const char instropection_xml[] =
18         "<node>"
19         "  <interface name='org.tizen.pkgmgr'>"
20         "    <method name='install'>"
21         "      <arg type='u' name='uid' direction='in'/>"
22         "      <arg type='s' name='pkgtype' direction='in'/>"
23         "      <arg type='s' name='pkgpath' direction='in'/>"
24         "      <arg type='as' name='args' direction='in'/>"
25         "      <arg type='i' name='ret' direction='out'/>"
26         "      <arg type='s' name='reqkey' direction='out'/>"
27         "    </method>"
28         "    <method name='reinstall'>"
29         "      <arg type='u' name='uid' direction='in'/>"
30         "      <arg type='s' name='pkgtype' direction='in'/>"
31         "      <arg type='s' name='pkgid' direction='in'/>"
32         "      <arg type='i' name='ret' direction='out'/>"
33         "      <arg type='s' name='reqkey' direction='out'/>"
34         "    </method>"
35         "    <method name='mount_install'>"
36         "      <arg type='u' name='uid' direction='in'/>"
37         "      <arg type='s' name='pkgtype' direction='in'/>"
38         "      <arg type='s' name='pkgpath' direction='in'/>"
39         "      <arg type='as' name='args' direction='in'/>"
40         "      <arg type='i' name='ret' direction='out'/>"
41         "      <arg type='s' name='reqkey' direction='out'/>"
42         "    </method>"
43         "    <method name='uninstall'>"
44         "      <arg type='u' name='uid' direction='in'/>"
45         "      <arg type='s' name='pkgtype' direction='in'/>"
46         "      <arg type='s' name='pkgid' direction='in'/>"
47         "      <arg type='i' name='ret' direction='out'/>"
48         "      <arg type='s' name='reqkey' direction='out'/>"
49         "    </method>"
50         "    <method name='move'>"
51         "      <arg type='u' name='uid' direction='in'/>"
52         "      <arg type='s' name='pkgtype' direction='in'/>"
53         "      <arg type='s' name='pkgid' direction='in'/>"
54         "      <arg type='i' name='movetype' direction='in'/>"
55         "      <arg type='i' name='ret' direction='out'/>"
56         "    </method>"
57         "    <method name='enable_pkg'>"
58         "      <arg type='u' name='uid' direction='in'/>"
59         "      <arg type='s' name='pkgtype' direction='in'/>"
60         "      <arg type='s' name='pkgid' direction='in'/>"
61         "      <arg type='i' name='ret' direction='out'/>"
62         "    </method>"
63         "    <method name='disable_pkg'>"
64         "      <arg type='u' name='uid' direction='in'/>"
65         "      <arg type='s' name='pkgtype' direction='in'/>"
66         "      <arg type='s' name='pkgid' direction='in'/>"
67         "      <arg type='i' name='ret' direction='out'/>"
68         "    </method>"
69         "    <method name='enable_app'>"
70         "      <arg type='u' name='uid' direction='in'/>"
71         "      <arg type='s' name='appid' direction='in'/>"
72         "      <arg type='i' name='ret' direction='out'/>"
73         "      <arg type='s' name='reqkey' direction='out'/>"
74         "    </method>"
75         "    <method name='disable_app'>"
76         "      <arg type='u' name='uid' direction='in'/>"
77         "      <arg type='s' name='appid' direction='in'/>"
78         "      <arg type='i' name='ret' direction='out'/>"
79         "      <arg type='s' name='reqkey' direction='out'/>"
80         "    </method>"
81         "    <method name='enable_global_app_for_uid'>"
82         "      <arg type='u' name='uid' direction='in'/>"
83         "      <arg type='s' name='appid' direction='in'/>"
84         "      <arg type='i' name='ret' direction='out'/>"
85         "      <arg type='s' name='reqkey' direction='out'/>"
86         "    </method>"
87         "    <method name='disable_global_app_for_uid'>"
88         "      <arg type='u' name='uid' direction='in'/>"
89         "      <arg type='s' name='appid' direction='in'/>"
90         "      <arg type='i' name='ret' direction='out'/>"
91         "      <arg type='s' name='reqkey' direction='out'/>"
92         "    </method>"
93         "    <method name='getsize'>"
94         "      <arg type='u' name='uid' direction='in'/>"
95         "      <arg type='s' name='pkgid' direction='in'/>"
96         "      <arg type='i' name='get_type' direction='in'/>"
97         "      <arg type='i' name='ret' direction='out'/>"
98         "      <arg type='s' name='reqkey' direction='out'/>"
99         "    </method>"
100         "    <method name='cleardata'>"
101         "      <arg type='u' name='uid' direction='in'/>"
102         "      <arg type='s' name='pkgtype' direction='in'/>"
103         "      <arg type='s' name='pkgid' direction='in'/>"
104         "      <arg type='i' name='ret' direction='out'/>"
105         "    </method>"
106         "    <method name='clearcache'>"
107         "      <arg type='u' name='uid' direction='in'/>"
108         "      <arg type='s' name='pkgid' direction='in'/>"
109         "      <arg type='i' name='ret' direction='out'/>"
110         "    </method>"
111         "    <method name='kill'>"
112         "      <arg type='u' name='uid' direction='in'/>"
113         "      <arg type='s' name='pkgid' direction='in'/>"
114         "      <arg type='i' name='ret' direction='out'/>"
115         "    </method>"
116         "    <method name='check'>"
117         "      <arg type='u' name='uid' direction='in'/>"
118         "      <arg type='s' name='pkgid' direction='in'/>"
119         "      <arg type='i' name='ret' direction='out'/>"
120         "    </method>"
121         "    <method name='generate_license_request'>"
122         "      <arg type='s' name='resp_data' direction='in'/>"
123         "      <arg type='i' name='ret' direction='out'/>"
124         "      <arg type='s' name='req_data' direction='out'/>"
125         "      <arg type='s' name='license_url' direction='out'/>"
126         "    </method>"
127         "    <method name='register_license'>"
128         "      <arg type='s' name='resp_data' direction='in'/>"
129         "      <arg type='i' name='ret' direction='out'/>"
130         "    </method>"
131         "    <method name='decrypt_package'>"
132         "      <arg type='s' name='drm_file_path' direction='in'/>"
133         "      <arg type='s' name='decrypted_file_path' direction='in'/>"
134         "      <arg type='i' name='ret' direction='out'/>"
135         "    </method>"
136         "    <method name='add_blacklist'>"
137         "      <arg type='u' name='uid' direction='in'/>"
138         "      <arg type='s' name='pkgid' direction='in'/>"
139         "      <arg type='i' name='ret' direction='out'/>"
140         "    </method>"
141         "    <method name='remove_blacklist'>"
142         "      <arg type='u' name='uid' direction='in'/>"
143         "      <arg type='s' name='pkgid' direction='in'/>"
144         "      <arg type='i' name='ret' direction='out'/>"
145         "    </method>"
146         "    <method name='check_blacklist'>"
147         "      <arg type='u' name='uid' direction='in'/>"
148         "      <arg type='s' name='pkgid' direction='in'/>"
149         "      <arg type='i' name='result' direction='out'/>"
150         "      <arg type='i' name='ret' direction='out'/>"
151         "    </method>"
152         "    <method name='enable_app_splash_screen'>"
153         "      <arg type='u' name='uid' direction='in'/>"
154         "      <arg type='s' name='appid' direction='in'/>"
155         "      <arg type='i' name='ret' direction='out'/>"
156         "    </method>"
157         "    <method name='disable_app_splash_screen'>"
158         "      <arg type='u' name='uid' direction='in'/>"
159         "      <arg type='s' name='appid' direction='in'/>"
160         "      <arg type='i' name='ret' direction='out'/>"
161         "    </method>"
162         "  </interface>"
163         "</node>";
164 static GDBusNodeInfo *instropection_data;
165 static guint reg_id;
166 static guint owner_id;
167 static GHashTable *req_table;
168
169 static char *__generate_reqkey(const char *pkgid)
170 {
171         struct timeval tv;
172         long curtime;
173         char timestr[MAX_PKG_ARGS_LEN];
174         char *str_req_key;
175         int size;
176
177         gettimeofday(&tv, NULL);
178         curtime = tv.tv_sec * 1000000 + tv.tv_usec;
179         snprintf(timestr, sizeof(timestr), "%ld", curtime);
180
181         size = strlen(pkgid) + strlen(timestr) + 2;
182         str_req_key = (char *)calloc(size, sizeof(char));
183         if (str_req_key == NULL) {
184                 DBG("calloc failed");
185                 return NULL;
186         }
187         snprintf(str_req_key, size, "%s_%s", pkgid, timestr);
188
189         return str_req_key;
190 }
191
192 static int __is_admin_user(uid_t uid)
193 {
194         GumUser *guser;
195         GumUserType ut = GUM_USERTYPE_NONE;
196
197         guser = gum_user_get_sync(uid, FALSE);
198         if (guser == NULL) {
199                 ERR("cannot get user information from gumd");
200                 return -1;
201         }
202
203         g_object_get(G_OBJECT(guser), "usertype", &ut, NULL);
204         if (ut == GUM_USERTYPE_NONE) {
205                 ERR("cannot get user type");
206                 g_object_unref(guser);
207                 return -1;
208         } else if (ut != GUM_USERTYPE_ADMIN) {
209                 g_object_unref(guser);
210                 return 0;
211         }
212
213         g_object_unref(guser);
214
215         return 1;
216 }
217
218 static int __check_caller_permission(uid_t uid,
219                 GDBusMethodInvocation *invocation, GVariant *parameters)
220 {
221         GVariant *v;
222         uid_t target_uid;
223         int is_admin;
224
225         v = g_variant_get_child_value(parameters, 0);
226         if (v == NULL) {
227                 g_dbus_method_invocation_return_error_literal(invocation,
228                                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
229                                 "Internal error.");
230                 return -1;
231         }
232
233         target_uid = g_variant_get_uint32(v);
234         g_variant_unref(v);
235         if (uid == target_uid)
236                 return 0;
237
238         is_admin = __is_admin_user(uid);
239         if (is_admin == -1) {
240                 g_dbus_method_invocation_return_error_literal(invocation,
241                                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
242                                 "Internal error.");
243                 return -1;
244         } else if (is_admin == 0) {
245                 g_dbus_method_invocation_return_error_literal(invocation,
246                                 G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED,
247                                 "Non-admin user cannot request operation to "
248                                 "other users.");
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 static int __handle_request_install(uid_t uid,
256                 GDBusMethodInvocation *invocation, GVariant *parameters)
257 {
258         uid_t target_uid = (uid_t)-1;
259         char *pkgtype = NULL;
260         char *pkgpath = NULL;
261         char *args = NULL;
262         char *reqkey = NULL;
263         gchar **tmp_args = NULL;
264         gsize args_count;
265         int ret = -1;
266         GVariant *value;
267         int i = 0;
268         int len = 0;
269
270         g_variant_get(parameters, "(u&s&s@as)", &target_uid, &pkgtype, &pkgpath, &value);
271         tmp_args = (gchar **)g_variant_get_strv(value, &args_count);
272
273         for (i = 0; i < args_count; i++)
274                 len = len + strlen(tmp_args[i]) + 1;
275
276         args = (char *)calloc(len, sizeof(char));
277         if (args == NULL) {
278                 ERR("calloc failed");
279                 ret =  -1;
280                 goto catch;
281         }
282
283         for (i = 0; i < args_count; i++) {
284                 strncat(args, tmp_args[i], strlen(tmp_args[i]));
285                 if (i != args_count - 1)
286                         strncat(args, " ", strlen(" "));
287         }
288
289         if (target_uid == (uid_t)-1 || pkgtype == NULL) {
290                 g_dbus_method_invocation_return_value(invocation,
291                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
292                 ret = -1;
293                 goto catch;
294         }
295
296         if (pkgpath == NULL) {
297                 g_dbus_method_invocation_return_value(invocation,
298                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
299                 ret = -1;
300                 goto catch;
301         }
302
303         reqkey = __generate_reqkey(pkgpath);
304         if (reqkey == NULL) {
305                 ret = -1;
306                 goto catch;
307         }
308
309         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_INSTALL, pkgtype,
310                                 pkgpath, args)) {
311                 g_dbus_method_invocation_return_value(invocation,
312                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
313                 ret = -1;
314                 goto catch;
315         }
316
317         g_dbus_method_invocation_return_value(invocation,
318                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
319
320         ret = 0;
321
322 catch:
323         if (reqkey)
324                 free(reqkey);
325
326         if (args)
327                 free(args);
328
329         return ret;
330 }
331
332 static int __handle_request_mount_install(uid_t uid,
333         GDBusMethodInvocation *invocation, GVariant *parameters)
334 {
335         uid_t target_uid = (uid_t)-1;
336         char *pkgtype = NULL;
337         char *pkgpath = NULL;
338         char *args = NULL;
339         char *reqkey = NULL;
340         gchar **tmp_args = NULL;
341         gsize args_count;
342         int ret = -1;
343         GVariant *value;
344         int i = 0;
345         int len = 0;
346
347         g_variant_get(parameters, "(u&s&s@as)", &target_uid, &pkgtype, &pkgpath, &value);
348         tmp_args = (gchar **)g_variant_get_strv(value, &args_count);
349
350         for (i = 0; i < args_count; i++)
351                 len = len + strlen(tmp_args[i]) + 1;
352
353         args = (char *)calloc(len, sizeof(char));
354         if (args == NULL) {
355                 ERR("calloc failed");
356                 ret =  -1;
357                 goto catch;
358         }
359
360         for (i = 0; i < args_count; i++) {
361                 strncat(args, tmp_args[i], strlen(tmp_args[i]));
362                 if (i != args_count - 1)
363                         strncat(args, " ", strlen(" "));
364         }
365
366         if (target_uid == (uid_t)-1 || pkgtype == NULL) {
367                 g_dbus_method_invocation_return_value(invocation,
368                                                       g_variant_new("(is)",
369                                                                     PKGMGR_R_ECOMM, ""));
370                 ret = -1;
371                 goto catch;
372         }
373
374         if (pkgpath == NULL) {
375                 g_dbus_method_invocation_return_value(invocation,
376                                                       g_variant_new("(is)",
377                                                                     PKGMGR_R_ECOMM, ""));
378                 ret = -1;
379                 goto catch;
380         }
381
382         reqkey = __generate_reqkey(pkgpath);
383         if (reqkey == NULL) {
384                 ret = -1;
385                 goto catch;
386         }
387
388         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_MOUNT_INSTALL,
389                        pkgtype, pkgpath, args)) {
390         g_dbus_method_invocation_return_value(invocation,
391                                               g_variant_new("(is)",
392                                                             PKGMGR_R_ESYSTEM, ""));
393                 ret = -1;
394                 goto catch;
395         }
396
397         g_dbus_method_invocation_return_value(invocation,
398                                               g_variant_new("(is)",
399                                                             PKGMGR_R_OK,
400                                                             reqkey));
401         ret = 0;
402
403         catch:
404         if (reqkey)
405                 free(reqkey);
406
407         if (args)
408                 free(args);
409
410         return ret;
411 }
412
413 static int __handle_request_reinstall(uid_t uid,
414                 GDBusMethodInvocation *invocation, GVariant *parameters)
415 {
416         uid_t target_uid = (uid_t)-1;
417         char *pkgtype = NULL;
418         char *pkgid = NULL;
419         char *reqkey;
420
421         g_variant_get(parameters, "(u&s&s)", &target_uid, &pkgtype, &pkgid);
422         if (target_uid == (uid_t)-1 || pkgtype == NULL || pkgid == NULL) {
423                 g_dbus_method_invocation_return_value(invocation,
424                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
425                 return -1;
426         }
427
428         reqkey = __generate_reqkey(pkgid);
429         if (reqkey == NULL)
430                 return -1;
431         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_REINSTALL, pkgtype,
432                                 pkgid, "")) {
433                 g_dbus_method_invocation_return_value(invocation,
434                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
435                 free(reqkey);
436                 return -1;
437         }
438
439         g_dbus_method_invocation_return_value(invocation,
440                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
441         free(reqkey);
442
443         return 0;
444 }
445
446 static int __handle_request_uninstall(uid_t uid,
447                 GDBusMethodInvocation *invocation, GVariant *parameters)
448 {
449         uid_t target_uid = (uid_t)-1;
450         char *pkgtype = NULL;
451         char *pkgid = NULL;
452         char *reqkey;
453
454         g_variant_get(parameters, "(u&s&s)", &target_uid, &pkgtype, &pkgid);
455         if (target_uid == (uid_t)-1 || pkgtype == NULL || pkgid == NULL) {
456                 g_dbus_method_invocation_return_value(invocation,
457                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
458                 return -1;
459         }
460
461         reqkey = __generate_reqkey(pkgid);
462         if (reqkey == NULL)
463                 return -1;
464         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_UNINSTALL, pkgtype,
465                                 pkgid, "")) {
466                 g_dbus_method_invocation_return_value(invocation,
467                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
468                 free(reqkey);
469                 return -1;
470         }
471
472         g_dbus_method_invocation_return_value(invocation,
473                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
474         free(reqkey);
475
476         return 0;
477 }
478
479 static int __handle_request_move(uid_t uid,
480                 GDBusMethodInvocation *invocation, GVariant *parameters)
481 {
482         uid_t target_uid = (uid_t)-1;
483         char *pkgtype = NULL;
484         char *pkgid = NULL;
485         char *reqkey;
486         int move_type = -1;
487         char buf[4] = { '\0' };
488
489         g_variant_get(parameters, "(u&s&si)", &target_uid, &pkgtype, &pkgid, &move_type);
490         if (target_uid == (uid_t)-1 || pkgtype == NULL || pkgid == NULL) {
491                 g_dbus_method_invocation_return_value(invocation,
492                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
493                 return -1;
494         }
495
496         reqkey = __generate_reqkey(pkgid);
497         if (reqkey == NULL)
498                 return -1;
499
500         snprintf(buf, sizeof(buf), "%d", move_type);
501         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_MOVE, pkgtype,
502                                 pkgid, buf)) {
503                 g_dbus_method_invocation_return_value(invocation,
504                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
505                 free(reqkey);
506                 return -1;
507         }
508
509         g_dbus_method_invocation_return_value(invocation,
510                         g_variant_new("(i)", PKGMGR_R_OK));
511         free(reqkey);
512
513         return 0;
514 }
515
516 static int __handle_request_enable_pkg(uid_t uid,
517                 GDBusMethodInvocation *invocation, GVariant *parameters)
518 {
519         uid_t target_uid = (uid_t)-1;
520         char *pkgtype = NULL;
521         char *pkgid = NULL;
522
523         g_variant_get(parameters, "(u&s&s)", &target_uid, &pkgtype, &pkgid);
524         if (target_uid == (uid_t)-1 || pkgid == NULL) {
525                 g_dbus_method_invocation_return_value(invocation,
526                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
527                 return -1;
528         }
529
530         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_ENABLE_PKG, pkgtype,
531                                 pkgid, "")) {
532                 g_dbus_method_invocation_return_value(invocation,
533                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
534                 return -1;
535         }
536
537         g_dbus_method_invocation_return_value(invocation,
538                         g_variant_new("(i)", PKGMGR_R_OK));
539
540         return 0;
541 }
542
543 static int __handle_request_disable_pkg(uid_t uid,
544                 GDBusMethodInvocation *invocation, GVariant *parameters)
545 {
546         uid_t target_uid = (uid_t)-1;
547         char *pkgtype = NULL;
548         char *pkgid = NULL;
549
550         g_variant_get(parameters, "(u&s&s)", &target_uid, &pkgtype, &pkgid);
551         if (target_uid == (uid_t)-1 || pkgid == NULL) {
552                 g_dbus_method_invocation_return_value(invocation,
553                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
554                 return -1;
555         }
556
557         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_DISABLE_PKG, pkgtype,
558                                 pkgid, "")) {
559                 g_dbus_method_invocation_return_value(invocation,
560                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
561                 return -1;
562         }
563
564         g_dbus_method_invocation_return_value(invocation,
565                         g_variant_new("(i)", PKGMGR_R_OK));
566
567         return 0;
568 }
569
570 static int __handle_request_enable_app(uid_t uid,
571                 GDBusMethodInvocation *invocation, GVariant *parameters)
572 {
573         uid_t target_uid = (uid_t)-1;
574         char *appid = NULL;
575         char *reqkey = NULL;
576         int ret = -1;
577
578         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
579         if (target_uid == (uid_t)-1 || appid == NULL) {
580                 g_dbus_method_invocation_return_value(invocation,
581                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
582                 return -1;
583         }
584
585         reqkey = __generate_reqkey(appid);
586         if (reqkey == NULL) {
587                 ret = -1;
588                 goto catch;
589         }
590
591         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_ENABLE_APP, "default",
592                                 appid, "")) {
593                 g_dbus_method_invocation_return_value(invocation,
594                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
595                 ret = -1;
596                 goto catch;
597         }
598
599         g_dbus_method_invocation_return_value(invocation,
600                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
601
602         ret = 0;
603
604 catch:
605         if(reqkey)
606                 free(reqkey);
607
608         return ret;
609 }
610
611 static int __handle_request_disable_app(uid_t uid,
612                 GDBusMethodInvocation *invocation, GVariant *parameters)
613 {
614         uid_t target_uid = (uid_t)-1;
615         char *appid = NULL;
616         char *reqkey = NULL;
617         int ret = -1;
618
619         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
620         if (target_uid == (uid_t)-1 || appid == NULL) {
621                 g_dbus_method_invocation_return_value(invocation,
622                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
623                 return -1;
624         }
625
626         reqkey = __generate_reqkey(appid);
627         if (reqkey == NULL) {
628                 ret = -1;
629                 goto catch;
630         }
631
632         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_DISABLE_APP, "default",
633                                 appid, "")) {
634                 g_dbus_method_invocation_return_value(invocation,
635                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
636                 ret = -1;
637                 goto catch;
638         }
639
640         g_dbus_method_invocation_return_value(invocation,
641                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
642
643         ret = 0;
644
645 catch:
646         if (reqkey)
647                 free(reqkey);
648
649         return ret;
650 }
651
652 static int __handle_request_enable_global_app_for_uid(uid_t uid,
653                 GDBusMethodInvocation *invocation, GVariant *parameters)
654 {
655         uid_t target_uid = (uid_t)-1;
656         char *appid = NULL;
657         char *reqkey = NULL;
658         int ret = -1;
659
660         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
661         if (target_uid == (uid_t)-1 || appid == NULL) {
662                 g_dbus_method_invocation_return_value(invocation,
663                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
664                 return -1;
665         }
666
667         reqkey = __generate_reqkey(appid);
668         if (reqkey == NULL) {
669                 ret = -1;
670                 goto catch;
671         }
672
673         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_ENABLE_GLOBAL_APP_FOR_UID, "default",
674                                 appid, "")) {
675                 g_dbus_method_invocation_return_value(invocation,
676                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
677                 ret = -1;
678                 goto catch;
679         }
680
681         g_dbus_method_invocation_return_value(invocation,
682                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
683
684         ret = 0;
685
686 catch:
687         if (reqkey)
688                 free(reqkey);
689
690         return ret;
691 }
692
693 static int __handle_request_disable_global_app_for_uid(uid_t uid,
694                 GDBusMethodInvocation *invocation, GVariant *parameters)
695 {
696         uid_t target_uid = (uid_t)-1;
697         char *appid = NULL;
698         char *reqkey = NULL;
699         int ret = -1;
700
701         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
702         if (target_uid == (uid_t)-1 || appid == NULL) {
703                 g_dbus_method_invocation_return_value(invocation,
704                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
705                 return -1;
706         }
707
708         reqkey = __generate_reqkey(appid);
709         if (reqkey == NULL) {
710                 ret = -1;
711                 goto catch;
712         }
713
714         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_DISABLE_GLOBAL_APP_FOR_UID, "default",
715                                 appid, "")) {
716                 g_dbus_method_invocation_return_value(invocation,
717                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
718                 ret = -1;
719                 goto catch;
720         }
721
722         g_dbus_method_invocation_return_value(invocation,
723                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
724
725         ret = 0;
726
727 catch:
728         if (reqkey)
729                 free(reqkey);
730
731         return ret;
732 }
733
734 static int __handle_request_getsize(uid_t uid,
735                 GDBusMethodInvocation *invocation, GVariant *parameters)
736 {
737         uid_t target_uid = (uid_t)-1;
738         char *pkgid = NULL;
739         int get_type = -1;
740         char *reqkey;
741         char buf[4];
742
743         g_variant_get(parameters, "(u&si)", &target_uid, &pkgid, &get_type);
744         if (target_uid == (uid_t)-1 || pkgid == NULL || get_type == -1) {
745                 g_dbus_method_invocation_return_value(invocation,
746                                 g_variant_new("(is)", PKGMGR_R_ECOMM, ""));
747                 return -1;
748         }
749
750         reqkey = __generate_reqkey(pkgid);
751         if (reqkey == NULL)
752                 return -1;
753
754         snprintf(buf, sizeof(buf), "%d", get_type);
755         if (_pm_queue_push(target_uid, reqkey, PKGMGR_REQUEST_TYPE_GETSIZE, "pkgtool",
756                                 pkgid, buf)) {
757                 g_dbus_method_invocation_return_value(invocation,
758                                 g_variant_new("(is)", PKGMGR_R_ESYSTEM, ""));
759                 free(reqkey);
760                 return -1;
761         }
762
763         g_dbus_method_invocation_return_value(invocation,
764                         g_variant_new("(is)", PKGMGR_R_OK, reqkey));
765         free(reqkey);
766
767         return 0;
768 }
769
770 static int __handle_request_cleardata(uid_t uid,
771                 GDBusMethodInvocation *invocation, GVariant *parameters)
772 {
773         uid_t target_uid = (uid_t)-1;
774         char *pkgtype = NULL;
775         char *pkgid = NULL;
776
777         g_variant_get(parameters, "(u&s&s)", &target_uid, &pkgtype, &pkgid);
778         if (target_uid == (uid_t)-1 || pkgtype == NULL || pkgid == NULL) {
779                 g_dbus_method_invocation_return_value(invocation,
780                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
781                 return -1;
782         }
783
784         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_CLEARDATA, "pkgtool",
785                                 pkgid, "")) {
786                 g_dbus_method_invocation_return_value(invocation,
787                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
788                 return -1;
789         }
790
791         g_dbus_method_invocation_return_value(invocation,
792                         g_variant_new("(i)", PKGMGR_R_OK));
793
794         return 0;
795 }
796
797 static int __handle_request_clearcache(uid_t uid,
798                 GDBusMethodInvocation *invocation, GVariant *parameters)
799 {
800         uid_t target_uid = (uid_t)-1;
801         char *pkgid = NULL;
802
803         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
804         if (target_uid == (uid_t)-1 || pkgid == NULL) {
805                 g_dbus_method_invocation_return_value(invocation,
806                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
807                 return -1;
808         }
809
810         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_CLEARCACHE,
811                                 "pkgtool",  pkgid, "")) {
812                 g_dbus_method_invocation_return_value(invocation,
813                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
814                 return -1;
815         }
816
817         g_dbus_method_invocation_return_value(invocation,
818                         g_variant_new("(i)", PKGMGR_R_OK));
819
820         return 0;
821 }
822
823 static int __handle_request_kill(uid_t uid,
824                 GDBusMethodInvocation *invocation, GVariant *parameters)
825 {
826         uid_t target_uid = (uid_t)-1;
827         char *pkgid = NULL;
828
829         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
830         if (target_uid == (uid_t)-1 || pkgid == NULL) {
831                 g_dbus_method_invocation_return_value(invocation,
832                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
833                 return -1;
834         }
835
836         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_KILL, "default",
837                                 pkgid, "")) {
838                 g_dbus_method_invocation_return_value(invocation,
839                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
840                 return -1;
841         }
842
843         g_dbus_method_invocation_return_value(invocation,
844                         g_variant_new("(i)", PKGMGR_R_OK));
845
846         return 0;
847 }
848
849 static int __handle_request_check(uid_t uid,
850                 GDBusMethodInvocation *invocation, GVariant *parameters)
851 {
852         uid_t target_uid = (uid_t)-1;
853         char *pkgid = NULL;
854
855         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
856         if (target_uid == (uid_t)-1 || pkgid == NULL) {
857                 g_dbus_method_invocation_return_value(invocation,
858                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
859                 return -1;
860         }
861
862         if (_pm_queue_push(target_uid, "", PKGMGR_REQUEST_TYPE_CHECK, "default",
863                                 pkgid, "")) {
864                 g_dbus_method_invocation_return_value(invocation,
865                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
866                 return -1;
867         }
868
869         g_dbus_method_invocation_return_value(invocation,
870                         g_variant_new("(i)", PKGMGR_R_OK));
871
872         return 0;
873 }
874
875 static int __handle_request_generate_license_request(uid_t uid,
876                 GDBusMethodInvocation *invocation, GVariant *parameters)
877 {
878         char *reqkey;
879         char *resp_data = NULL;
880
881         g_variant_get(parameters, "(&s)", &resp_data);
882         if (resp_data == NULL) {
883                 g_dbus_method_invocation_return_value(invocation,
884                                 g_variant_new("(iss)", PKGMGR_R_ECOMM, "", ""));
885                 return -1;
886         }
887
888         reqkey = __generate_reqkey("drm");
889         if (reqkey == NULL) {
890                 g_dbus_method_invocation_return_value(invocation,
891                                 g_variant_new("(iss)", PKGMGR_R_ENOMEM, "",
892                                         ""));
893                 return -1;
894         }
895
896         if (_pm_queue_push(uid, reqkey,
897                                 PKGMGR_REQUEST_TYPE_GENERATE_LICENSE_REQUEST,
898                                 "default", "", resp_data)) {
899                 g_dbus_method_invocation_return_value(invocation,
900                                 g_variant_new("(iss)", PKGMGR_R_ESYSTEM, "",
901                                         ""));
902                 free(reqkey);
903                 return -1;
904         }
905
906         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
907                                 (gpointer)invocation))
908                 ERR("reqkey already exists");
909
910         return 0;
911 }
912
913 static int __handle_request_register_license(uid_t uid,
914                 GDBusMethodInvocation *invocation, GVariant *parameters)
915 {
916         char *reqkey;
917         char *resp_data = NULL;
918
919         g_variant_get(parameters, "(&s)", &resp_data);
920         if (resp_data == NULL) {
921                 g_dbus_method_invocation_return_value(invocation,
922                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
923                 return -1;
924         }
925
926         reqkey = __generate_reqkey("drm");
927         if (reqkey == NULL) {
928                 g_dbus_method_invocation_return_value(invocation,
929                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
930                 return -1;
931         }
932
933         if (_pm_queue_push(uid, reqkey, PKGMGR_REQUEST_TYPE_REGISTER_LICENSE,
934                                 "default", "", resp_data)) {
935                 g_dbus_method_invocation_return_value(invocation,
936                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
937                 free(reqkey);
938                 return -1;
939         }
940
941         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
942                                 (gpointer)invocation))
943                 ERR("reqkey already exists");
944
945         return 0;
946 }
947
948 static int __handle_request_decrypt_package(uid_t uid,
949                 GDBusMethodInvocation *invocation, GVariant *parameters)
950 {
951         char *reqkey;
952         char *drm_file_path = NULL;
953         char *decrypted_file_path = NULL;
954
955         g_variant_get(parameters, "(&s&s)", &drm_file_path,
956                         &decrypted_file_path);
957         if (drm_file_path == NULL || decrypted_file_path == NULL) {
958                 g_dbus_method_invocation_return_value(invocation,
959                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
960                 return -1;
961         }
962
963         reqkey = __generate_reqkey("drm");
964         if (reqkey == NULL) {
965                 g_dbus_method_invocation_return_value(invocation,
966                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
967                 return -1;
968         }
969
970         if (_pm_queue_push(uid, reqkey, PKGMGR_REQUEST_TYPE_DECRYPT_PACKAGE,
971                                 "default", drm_file_path, decrypted_file_path)) {
972                 g_dbus_method_invocation_return_value(invocation,
973                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
974                 free(reqkey);
975                 return -1;
976         }
977
978         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
979                                 (gpointer)invocation))
980                 ERR("reqkey already exists");
981
982         return 0;
983 }
984
985 static int __handle_request_add_blacklist(uid_t uid,
986                 GDBusMethodInvocation *invocation, GVariant *parameters)
987 {
988         uid_t target_uid = (uid_t)-1;
989         char *reqkey;
990         char *pkgid = NULL;
991
992         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
993         if (target_uid == (uid_t)-1 || pkgid == NULL) {
994                 g_dbus_method_invocation_return_value(invocation,
995                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
996                 return -1;
997         }
998
999         reqkey = __generate_reqkey("blacklist");
1000         if (reqkey == NULL) {
1001                 g_dbus_method_invocation_return_value(invocation,
1002                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1003                 return -1;
1004         }
1005
1006         if (_pm_queue_push(target_uid, reqkey,
1007                                 PKGMGR_REQUEST_TYPE_ADD_BLACKLIST,
1008                                 "default",  pkgid, "")) {
1009                 g_dbus_method_invocation_return_value(invocation,
1010                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1011                 free(reqkey);
1012                 return -1;
1013         }
1014
1015         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1016                                 (gpointer)invocation))
1017                 ERR("reqkey already exists");
1018
1019         return 0;
1020 }
1021
1022 static int __handle_request_remove_blacklist(uid_t uid,
1023                 GDBusMethodInvocation *invocation, GVariant *parameters)
1024 {
1025         uid_t target_uid = (uid_t)-1;
1026         char *reqkey;
1027         char *pkgid = NULL;
1028
1029         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1030         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1031                 g_dbus_method_invocation_return_value(invocation,
1032                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1033                 return -1;
1034         }
1035
1036         reqkey = __generate_reqkey("blacklist");
1037         if (reqkey == NULL) {
1038                 g_dbus_method_invocation_return_value(invocation,
1039                                 g_variant_new("(i)", PKGMGR_R_ENOMEM));
1040                 return -1;
1041         }
1042
1043         if (_pm_queue_push(target_uid, reqkey,
1044                                 PKGMGR_REQUEST_TYPE_REMOVE_BLACKLIST,
1045                                 "default", pkgid, "")) {
1046                 g_dbus_method_invocation_return_value(invocation,
1047                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1048                 free(reqkey);
1049                 return -1;
1050         }
1051
1052         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1053                                 (gpointer)invocation))
1054                 ERR("reqkey already exists");
1055
1056         return 0;
1057 }
1058
1059 static int __handle_request_check_blacklist(uid_t uid,
1060                 GDBusMethodInvocation *invocation, GVariant *parameters)
1061 {
1062         uid_t target_uid = (uid_t)-1;
1063         char *reqkey;
1064         char *pkgid = NULL;
1065
1066         g_variant_get(parameters, "(u&s)", &target_uid, &pkgid);
1067         if (target_uid == (uid_t)-1 || pkgid == NULL) {
1068                 g_dbus_method_invocation_return_value(invocation,
1069                                 g_variant_new("(ii)", PKGMGR_R_ECOMM, -1));
1070                 return -1;
1071         }
1072
1073         reqkey = __generate_reqkey("blacklist");
1074         if (reqkey == NULL) {
1075                 g_dbus_method_invocation_return_value(invocation,
1076                                 g_variant_new("(ii)", PKGMGR_R_ENOMEM, -1));
1077                 return -1;
1078         }
1079
1080         if (_pm_queue_push(target_uid, reqkey,
1081                                 PKGMGR_REQUEST_TYPE_CHECK_BLACKLIST,
1082                                 "default", pkgid, "")) {
1083                 g_dbus_method_invocation_return_value(invocation,
1084                                 g_variant_new("(ii)", PKGMGR_R_ESYSTEM, -1));
1085                 free(reqkey);
1086                 return -1;
1087         }
1088
1089         if (!g_hash_table_insert(req_table, (gpointer)reqkey,
1090                                 (gpointer)invocation))
1091                 ERR("reqkey already exists");
1092
1093         return 0;
1094 }
1095
1096
1097 static int __update_app_splash_screen(uid_t uid,
1098                 GDBusMethodInvocation *invocation, GVariant *parameters,
1099                 int req_type)
1100 {
1101         uid_t target_uid = (uid_t)-1;
1102         char *appid = NULL;
1103         char *reqkey;
1104
1105         g_variant_get(parameters, "(u&s)", &target_uid, &appid);
1106         if (target_uid == (uid_t)-1 || appid == NULL) {
1107                 ERR("target_uid: %d, appid: %s", target_uid, appid);
1108                 g_dbus_method_invocation_return_value(invocation,
1109                                 g_variant_new("(i)", PKGMGR_R_ECOMM));
1110                 return -1;
1111         }
1112
1113         reqkey = __generate_reqkey(appid);
1114         if (reqkey == NULL) {
1115                 ERR("Failed to generate request key");
1116                 return -1;
1117         }
1118
1119         if (_pm_queue_push(target_uid, reqkey, req_type, "default",
1120                                 appid, "")) {
1121                 ERR("Failed to push request");
1122                 g_dbus_method_invocation_return_value(invocation,
1123                                 g_variant_new("(i)", PKGMGR_R_ESYSTEM));
1124                 free(reqkey);
1125                 return -1;
1126         }
1127
1128         g_dbus_method_invocation_return_value(invocation,
1129                         g_variant_new("(i)", PKGMGR_R_OK));
1130
1131         if (reqkey)
1132                 free(reqkey);
1133
1134         return 0;
1135 }
1136
1137 static int __handle_request_enable_app_splash_screen(uid_t uid,
1138                 GDBusMethodInvocation *invocation, GVariant *parameters)
1139 {
1140         return __update_app_splash_screen(uid, invocation, parameters,
1141                         PKGMGR_REQUEST_TYPE_ENABLE_APP_SPLASH_SCREEN);
1142 }
1143
1144 static int __handle_request_disable_app_splash_screen(uid_t uid,
1145                 GDBusMethodInvocation *invocation, GVariant *parameters)
1146 {
1147         return __update_app_splash_screen(uid, invocation, parameters,
1148                         PKGMGR_REQUEST_TYPE_DISABLE_APP_SPLASH_SCREEN);
1149 }
1150
1151 static uid_t __get_caller_uid(GDBusConnection *connection, const char *name)
1152 {
1153         GError *err = NULL;
1154         GVariant *result;
1155         uid_t uid;
1156
1157         result = g_dbus_connection_call_sync(connection,
1158                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
1159                         "org.freedesktop.DBus", "GetConnectionUnixUser",
1160                         g_variant_new("(s)", name), NULL,
1161                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1162         if (result == NULL) {
1163                 ERR("failed to get caller uid: %s", err->message);
1164                 g_error_free(err);
1165                 return (uid_t)-1;
1166         }
1167
1168         g_variant_get(result, "(u)", &uid);
1169
1170         return uid;
1171 }
1172
1173 static void __handle_method_call(GDBusConnection *connection,
1174                 const gchar *sender, const gchar *object_path,
1175                 const gchar *interface_name, const gchar *method_name,
1176                 GVariant *parameters, GDBusMethodInvocation *invocation,
1177                 gpointer user_data)
1178 {
1179         int ret;
1180         uid_t uid;
1181
1182         uid = __get_caller_uid(connection,
1183                 g_dbus_method_invocation_get_sender(invocation));
1184         if (uid == (uid_t)-1)
1185                 return;
1186
1187         if (__check_caller_permission(uid, invocation, parameters))
1188                 return;
1189
1190         if (g_strcmp0(method_name, "install") == 0)
1191                 ret = __handle_request_install(uid, invocation, parameters);
1192         else if (g_strcmp0(method_name, "mount_install") == 0)
1193                 ret = __handle_request_mount_install(uid, invocation, parameters);
1194         else if (g_strcmp0(method_name, "reinstall") == 0)
1195                 ret = __handle_request_reinstall(uid, invocation, parameters);
1196         else if (g_strcmp0(method_name, "uninstall") == 0)
1197                 ret = __handle_request_uninstall(uid, invocation, parameters);
1198         else if (g_strcmp0(method_name, "cleardata") == 0)
1199                 ret = __handle_request_cleardata(uid, invocation, parameters);
1200         else if (g_strcmp0(method_name, "move") == 0)
1201                 ret = __handle_request_move(uid, invocation, parameters);
1202         else if (g_strcmp0(method_name, "enable") == 0)
1203                 ret = __handle_request_enable_pkg(uid, invocation, parameters);
1204         else if (g_strcmp0(method_name, "disable") == 0)
1205                 ret = __handle_request_disable_pkg(uid, invocation, parameters);
1206         else if (g_strcmp0(method_name, "getsize") == 0)
1207                 ret = __handle_request_getsize(uid, invocation, parameters);
1208         else if (g_strcmp0(method_name, "clearcache") == 0)
1209                 ret = __handle_request_clearcache(uid, invocation, parameters);
1210         else if (g_strcmp0(method_name, "enable_app") == 0)
1211                 ret = __handle_request_enable_app(uid, invocation, parameters);
1212         else if (g_strcmp0(method_name, "disable_app") == 0)
1213                 ret = __handle_request_disable_app(uid, invocation, parameters);
1214         else if (g_strcmp0(method_name, "enable_global_app_for_uid") == 0)
1215                 ret = __handle_request_enable_global_app_for_uid(uid, invocation, parameters);
1216         else if (g_strcmp0(method_name, "disable_global_app_for_uid") == 0)
1217                 ret = __handle_request_disable_global_app_for_uid(uid, invocation, parameters);
1218         else if (g_strcmp0(method_name, "kill") == 0)
1219                 ret = __handle_request_kill(uid, invocation, parameters);
1220         else if (g_strcmp0(method_name, "check") == 0)
1221                 ret = __handle_request_check(uid, invocation, parameters);
1222         else if (g_strcmp0(method_name, "generate_license_request") == 0)
1223                 ret = __handle_request_generate_license_request(uid, invocation,
1224                                 parameters);
1225         else if (g_strcmp0(method_name, "register_license") == 0)
1226                 ret = __handle_request_register_license(uid, invocation,
1227                                 parameters);
1228         else if (g_strcmp0(method_name, "decrypt_package") == 0)
1229                 ret = __handle_request_decrypt_package(uid, invocation,
1230                                 parameters);
1231         else if (g_strcmp0(method_name, "add_blacklist") == 0)
1232                 ret = __handle_request_add_blacklist(uid, invocation,
1233                                 parameters);
1234         else if (g_strcmp0(method_name, "remove_blacklist") == 0)
1235                 ret = __handle_request_remove_blacklist(uid, invocation,
1236                                 parameters);
1237         else if (g_strcmp0(method_name, "check_blacklist") == 0)
1238                 ret = __handle_request_check_blacklist(uid, invocation,
1239                                 parameters);
1240         else if (g_strcmp0(method_name, "disable_app_splash_screen") == 0)
1241                 ret = __handle_request_disable_app_splash_screen(uid,
1242                                 invocation, parameters);
1243         else if (g_strcmp0(method_name, "enable_app_splash_screen") == 0)
1244                 ret = __handle_request_enable_app_splash_screen(uid,
1245                                 invocation, parameters);
1246         else
1247                 ret = -1;
1248
1249         if (ret == 0)
1250                 g_idle_add(queue_job, NULL);
1251 }
1252
1253 int __return_value_to_caller(const char *req_key, GVariant *result)
1254 {
1255         GDBusMethodInvocation *invocation;
1256
1257         invocation = (GDBusMethodInvocation *)g_hash_table_lookup(req_table,
1258                         (gpointer)req_key);
1259         if (invocation == NULL) {
1260                 ERR("no such request id");
1261                 return -1;
1262         }
1263
1264         g_dbus_method_invocation_return_value(invocation, result);
1265         g_hash_table_remove(req_table, (gpointer)req_key);
1266
1267         return 0;
1268 }
1269
1270 static const GDBusInterfaceVTable interface_vtable =
1271 {
1272         __handle_method_call,
1273         NULL,
1274         NULL,
1275 };
1276
1277 static void __on_bus_acquired(GDBusConnection *connection, const gchar *name,
1278                 gpointer user_data)
1279 {
1280         GError *err = NULL;
1281
1282         DBG("on bus acquired");
1283
1284         reg_id = g_dbus_connection_register_object(connection,
1285                         PKGMGR_DBUS_OBJECT_PATH,
1286                         instropection_data->interfaces[0],
1287                         &interface_vtable, NULL, NULL, &err);
1288
1289         if (reg_id == 0) {
1290                 ERR("failed to register object: %s", err->message);
1291                 g_error_free(err);
1292         }
1293 }
1294
1295 static void __on_name_acquired(GDBusConnection *connection, const gchar *name,
1296                 gpointer user_data)
1297 {
1298         DBG("on name acquired: %s", name);
1299 }
1300
1301 static void __on_name_lost(GDBusConnection *connection, const gchar *name,
1302                 gpointer user_data)
1303 {
1304         DBG("on name lost: %s", name);
1305 }
1306
1307 int __init_request_handler(void)
1308 {
1309         instropection_data = g_dbus_node_info_new_for_xml(instropection_xml, NULL);
1310
1311         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, PKGMGR_DBUS_SERVICE,
1312                         G_BUS_NAME_OWNER_FLAGS_NONE, __on_bus_acquired,
1313                         __on_name_acquired, __on_name_lost, NULL, NULL);
1314
1315         req_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1316                         free, NULL);
1317         if (req_table == NULL)
1318                 return -1;
1319
1320         return 0;
1321 }
1322
1323 void __fini_request_handler(void)
1324 {
1325         g_hash_table_destroy(req_table);
1326         g_bus_unown_name(owner_id);
1327         g_dbus_node_info_unref(instropection_data);
1328 }