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