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