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