add enalbe/disable function for entire packages.
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_server.c
1 /*
2  * app2sd-server
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <gio/gio.h>
21
22 #include "app2sd_internals.h"
23
24 GMainLoop *app2sd_mainloop = NULL;
25
26 gboolean __exit_app2sd_server(void *data)
27 {
28         _D("exit app2sd_server");
29
30         g_main_loop_quit(app2sd_mainloop);
31
32         return FALSE;
33 }
34
35 static int __app2sd_get_sender_unixinfo(GDBusConnection *conn,
36                 const char *sender_name, const char *type)
37 {
38         GDBusMessage *msg = NULL;
39         GDBusMessage *reply = NULL;
40         GError *err = NULL;
41         GVariant *body;
42         int ret = -1;
43         unsigned int value;
44
45         msg = g_dbus_message_new_method_call("org.freedesktop.DBus",
46                 "/org/freedesktop/DBus", "org.freedesktop.DBus", type);
47         if (!msg) {
48                 _E("Can't allocate new method call");
49                 goto out;
50         }
51
52         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
53         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
54                 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
55
56         if (!reply) {
57                 if (err != NULL) {
58                         _E("Failed to get info [%s]", err->message);
59                         g_error_free(err);
60                 }
61                 goto out;
62         }
63
64         body = g_dbus_message_get_body(reply);
65         g_variant_get(body, "(u)", &value);
66         ret = (int)value;
67
68 out:
69         if (msg)
70                 g_object_unref(msg);
71         if (reply)
72                 g_object_unref(reply);
73
74         return ret;
75 }
76
77 /*
78 static int __app2sd_get_sender_pid(GDBusConnection *conn,
79                 const char *sender_name)
80 {
81         int pid = 0;
82
83         pid = __app2sd_get_sender_unixinfo(conn, sender_name,
84                 "GetConnectionUnixProcessID");
85         if (pid < 0) {
86                 _E("failed to get pid");
87                 pid = 0;
88         }
89
90         _D("sender_name(%s), pid(%d)", sender_name, pid);
91
92         return pid;
93 }
94 */
95
96 static int __app2sd_get_sender_uid(GDBusConnection *conn,
97                 const char *sender_name)
98 {
99         int uid = -1;
100
101         uid = __app2sd_get_sender_unixinfo(conn, sender_name,
102                 "GetConnectionUnixUser");
103         if (uid < 0)
104                 _E("failed to get uid");
105
106         _D("sender_name(%s), uid(%d)", sender_name, uid);
107
108         return uid;
109 }
110
111 static GDBusNodeInfo *introspection_data;
112 static const gchar introspection_xml[] =
113 "<node>"
114 "       <interface name='org.tizen.app2sd'>"
115 "               <method name='PreAppInstall'>"
116 "                       <arg type='s' name='pkgid' direction='in'/>"
117 "                       <arg type='i' name='size' direction='in'/>"
118 "                       <arg type='a(si)' name='dir_list' direction='in'/>"
119 "                       <arg type='i' name='uid' direction='in'/>"
120 "                       <arg type='i' name='result' direction='out'/>"
121 "               </method>"
122 "               <method name='PostAppInstall'>"
123 "                       <arg type='s' name='pkgid' direction='in'/>"
124 "                       <arg type='i' name='install_status' direction='in'/>"
125 "                       <arg type='i' name='uid' direction='in'/>"
126 "                       <arg type='i' name='result' direction='out'/>"
127 "               </method>"
128 "               <method name='PreAppUpgrade'>"
129 "                       <arg type='s' name='pkgid' direction='in'/>"
130 "                       <arg type='i' name='size' direction='in'/>"
131 "                       <arg type='a(si)' name='dir_list' direction='in'/>"
132 "                       <arg type='i' name='uid' direction='in'/>"
133 "                       <arg type='i' name='result' direction='out'/>"
134 "               </method>"
135 "               <method name='PostAppUpgrade'>"
136 "                       <arg type='s' name='pkgid' direction='in'/>"
137 "                       <arg type='i' name='install_status' direction='in'/>"
138 "                       <arg type='i' name='uid' direction='in'/>"
139 "                       <arg type='i' name='result' direction='out'/>"
140 "               </method>"
141 "               <method name='PreAppUninstall'>"
142 "                       <arg type='s' name='pkgid' direction='in'/>"
143 "                       <arg type='i' name='uid' direction='in'/>"
144 "                       <arg type='i' name='result' direction='out'/>"
145 "               </method>"
146 "               <method name='PostAppUninstall'>"
147 "                       <arg type='s' name='pkgid' direction='in'/>"
148 "                       <arg type='i' name='uid' direction='in'/>"
149 "                       <arg type='i' name='result' direction='out'/>"
150 "               </method>"
151 "               <method name='OndemandSetupInit'>"
152 "                       <arg type='s' name='pkgid' direction='in'/>"
153 "                       <arg type='i' name='uid' direction='in'/>"
154 "                       <arg type='i' name='result' direction='out'/>"
155 "               </method>"
156 "               <method name='OndemandSetupExit'>"
157 "                       <arg type='s' name='pkgid' direction='in'/>"
158 "                       <arg type='i' name='uid' direction='in'/>"
159 "                       <arg type='i' name='result' direction='out'/>"
160 "               </method>"
161 "               <method name='MoveInstalledApp'>"
162 "                       <arg type='s' name='pkgid' direction='in'/>"
163 "                       <arg type='i' name='move_type' direction='in'/>"
164 "                       <arg type='a(si)' name='dir_list' direction='in'/>"
165 "                       <arg type='i' name='uid' direction='in'/>"
166 "                       <arg type='i' name='result' direction='out'/>"
167 "               </method>"
168 "               <method name='ForceClean'>"
169 "                       <arg type='s' name='pkgid' direction='in'/>"
170 "                       <arg type='i' name='uid' direction='in'/>"
171 "                       <arg type='i' name='result' direction='out'/>"
172 "               </method>"
173 "               <method name='EnableFullPkg'>"
174 "                       <arg type='i' name='result' direction='out'/>"
175 "               </method>"
176 "               <method name='DisableFullPkg'>"
177 "                       <arg type='i' name='result' direction='out'/>"
178 "               </method>"
179 "       </interface>"
180 "</node>";
181
182 static void _app2sd_server_return_method_error(GDBusMethodInvocation *invocation, int result)
183 {
184         GVariant *param = NULL;
185
186         param = g_variant_new("(i)", result);
187         g_dbus_method_invocation_return_value(invocation, param);
188 }
189
190 static void _app2sd_server_pre_app_install(GDBusConnection *connection, const gchar *sender,
191         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
192 {
193         GVariant *param = NULL;
194         int result = APP2EXT_SUCCESS;
195         int size;
196         char *pkgid = NULL;
197         GVariantIter *iter;
198         gchar *str = NULL;
199         int type;
200         int ret = 0;
201         uid_t target_uid = -1;
202         GList *dir_list = NULL;
203         GList *list = NULL;
204         app2ext_dir_details *dir_detail = NULL;
205
206         g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter, &target_uid);
207
208         _D("pkgid(%s), size(%d),sender_uid(%d), target_uid(%d)",
209                 pkgid, size, sender_uid, target_uid);
210
211         if (sender_uid != 0 && sender_uid != target_uid) {
212                 _E("Not permitted user!");
213                 g_variant_iter_free(iter);
214                 _app2sd_server_return_method_error(invocation,
215                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
216                 return;
217         }
218
219         while (g_variant_iter_loop(iter, "(si)", &str, &type)) {
220                 if (str) {
221                         _D("str(%s), type(%d)", str, type);
222
223                         /* generate dir_list */
224                         dir_detail = (app2ext_dir_details *)calloc(1, sizeof(app2ext_dir_details));
225                         if (dir_detail == NULL) {
226                                 _E("memory allocation failed");
227                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
228                                 break;
229                         }
230
231                         dir_detail->name = strdup((char *)str);
232                         if (dir_detail->name == NULL) {
233                                 _E("out of memory");
234                                 free(dir_detail);
235                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
236                                 break;
237                         }
238
239                         dir_detail->type = type;
240                         list = g_list_append(list, dir_detail);
241                 }
242         }
243         g_variant_iter_free(iter);
244
245         dir_list = g_list_first(list);
246         ret = app2sd_usr_pre_app_install(pkgid, dir_list, size, target_uid);
247         if (ret) {
248                 _E("error(%d)", ret);
249                 result = ret;
250         }
251
252         param = g_variant_new("(i)", result);
253         g_dbus_method_invocation_return_value(invocation, param);
254 }
255
256 static void _app2sd_server_post_app_install(GDBusConnection *connection, const gchar *sender,
257         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
258 {
259         GVariant *param = NULL;
260         int result = APP2EXT_SUCCESS;
261         char *pkgid = NULL;
262         int install_status = 0;
263         int target_uid = -1;
264         int ret = 0;
265
266         g_variant_get(parameters, "(&sii)", &pkgid, &install_status, &target_uid);
267
268         _D("pkgid(%s), install_status(%d), sender_uid(%d), target_uid(%d)",
269                 pkgid, install_status, sender_uid, target_uid);
270
271         if (sender_uid != 0 && sender_uid != target_uid) {
272                 _E("Not permitted user!");
273                 _app2sd_server_return_method_error(invocation,
274                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
275                 return;
276         }
277
278         ret = app2sd_usr_post_app_install(pkgid, install_status, target_uid);
279         if (ret) {
280                 _E("error(%d)", ret);
281                 result = ret;
282         }
283
284         param = g_variant_new("(i)", result);
285         g_dbus_method_invocation_return_value(invocation, param);
286 }
287
288 static void _app2sd_server_pre_app_upgrade(GDBusConnection *connection, const gchar *sender,
289         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
290 {
291         GVariant *param = NULL;
292         int result = APP2EXT_SUCCESS;
293         int size;
294         char *pkgid = NULL;
295         GVariantIter *iter;
296         gchar *str = NULL;
297         int type;
298         uid_t target_uid = -1;
299         int ret = 0;
300         GList *dir_list = NULL;
301         GList *list = NULL;
302         app2ext_dir_details *dir_detail = NULL;
303
304         g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter, &target_uid);
305
306         _D("pkgid(%s), size(%d), sender_uid(%d), target_uid(%d)",
307                 pkgid, size, sender_uid, target_uid);
308
309         if (sender_uid != 0 && sender_uid != target_uid) {
310                 _E("Not permitted user!");
311                 g_variant_iter_free(iter);
312                 _app2sd_server_return_method_error(invocation,
313                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
314                 return;
315         }
316
317         while (g_variant_iter_loop(iter, "(si)", &str, &type)) {
318                 if (str) {
319                         _D("str(%s), type(%d)", str, type);
320
321                         /* generate dir_list */
322                         dir_detail = (app2ext_dir_details *)calloc(1, sizeof(app2ext_dir_details));
323                         if (dir_detail == NULL) {
324                                 _E("memory allocation failed");
325                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
326                                 break;
327                         }
328
329                         dir_detail->name = strdup((char *)str);
330                         if (dir_detail->name == NULL) {
331                                 _E("out of memory");
332                                 free(dir_detail);
333                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
334                                 break;
335                         }
336
337                         dir_detail->type = type;
338                         list = g_list_append(list, dir_detail);
339                 }
340         }
341         g_variant_iter_free(iter);
342
343         dir_list = g_list_first(list);
344         ret = app2sd_usr_pre_app_upgrade(pkgid, dir_list, size, target_uid);
345         if (ret) {
346                 _E("error(%d)", ret);
347                 result = ret;
348         }
349
350         param = g_variant_new("(i)", result);
351         g_dbus_method_invocation_return_value(invocation, param);
352 }
353
354 static void _app2sd_server_post_app_upgrade(GDBusConnection *connection, const gchar *sender,
355         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
356 {
357         GVariant *param = NULL;
358         int result = APP2EXT_SUCCESS;
359         char *pkgid = NULL;
360         int install_status = 0;
361         uid_t target_uid = -1;
362         int ret = 0;
363
364         g_variant_get(parameters, "(&sii)", &pkgid, &install_status, &target_uid);
365
366         _D("pkgid(%s), install_status(%d), sender_uid(%d), target_uid(%d)",
367                 pkgid, install_status, sender_uid, target_uid);
368
369         if (sender_uid != 0 && sender_uid != target_uid) {
370                 _E("Not permitted user!");
371                 _app2sd_server_return_method_error(invocation,
372                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
373                 return;
374         }
375
376         ret = app2sd_usr_post_app_upgrade(pkgid, install_status, target_uid);
377         if (ret) {
378                 _E("error(%d)", ret);
379                 result = ret;
380         }
381
382         param = g_variant_new("(i)", result);
383         g_dbus_method_invocation_return_value(invocation, param);
384 }
385
386 static void _app2sd_server_pre_app_uninstall(GDBusConnection *connection, const gchar *sender,
387         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
388 {
389         GVariant *param = NULL;
390         int result = APP2EXT_SUCCESS;
391         char *pkgid = NULL;
392         uid_t target_uid = -1;
393         int ret = 0;
394
395         g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
396
397         _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
398                 pkgid, sender_uid, target_uid);
399
400         if (sender_uid != 0 && sender_uid != target_uid) {
401                 _E("Not permitted user!");
402                 _app2sd_server_return_method_error(invocation,
403                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
404                 return;
405         }
406
407         ret = app2sd_usr_pre_app_uninstall(pkgid, target_uid);
408         if (ret) {
409                 _E("error(%d)", ret);
410                 result = ret;
411         }
412
413         param = g_variant_new("(i)", result);
414         g_dbus_method_invocation_return_value(invocation, param);
415 }
416
417 static void _app2sd_server_post_app_uninstall(GDBusConnection *connection, const gchar *sender,
418         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
419 {
420         GVariant *param = NULL;
421         int result = APP2EXT_SUCCESS;
422         char *pkgid = NULL;
423         uid_t target_uid = -1;
424         int ret = 0;
425
426         g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
427
428         _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
429                 pkgid, sender_uid, target_uid);
430
431         if (sender_uid != 0 && sender_uid != target_uid) {
432                 _E("Not permitted user!");
433                 _app2sd_server_return_method_error(invocation,
434                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
435                 return;
436         }
437
438         ret = app2sd_usr_post_app_uninstall(pkgid, target_uid);
439         if (ret) {
440                 _E("error(%d)", ret);
441                 result = ret;
442         }
443
444         param = g_variant_new("(i)", result);
445         g_dbus_method_invocation_return_value(invocation, param);
446 }
447
448 static void _app2sd_server_ondemand_setup_init(GDBusConnection *connection, const gchar *sender,
449         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
450 {
451         GVariant *param = NULL;
452         int result = APP2EXT_SUCCESS;
453         char *pkgid = NULL;
454         uid_t target_uid = -1;
455         int ret = 0;
456
457         g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
458
459         _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
460                 pkgid, sender_uid, target_uid);
461
462         if (sender_uid != 0 && sender_uid != target_uid) {
463                 _E("Not permitted user!");
464                 _app2sd_server_return_method_error(invocation,
465                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
466                 return;
467         }
468
469         ret = app2sd_usr_on_demand_setup_init(pkgid, target_uid);
470         if (ret) {
471                 _E("error(%d)", ret);
472                 result = ret;
473         }
474
475         param = g_variant_new("(i)", result);
476         g_dbus_method_invocation_return_value(invocation, param);
477 }
478
479 static void _app2sd_server_ondemand_setup_exit(GDBusConnection *connection, const gchar *sender,
480         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
481 {
482         GVariant *param = NULL;
483         int result = APP2EXT_SUCCESS;
484         char *pkgid = NULL;
485         uid_t target_uid = -1;
486         int ret = 0;
487
488         g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
489
490         _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
491                 pkgid, sender_uid, target_uid);
492
493         if (sender_uid != 0 && sender_uid != target_uid) {
494                 _E("Not permitted user!");
495                 _app2sd_server_return_method_error(invocation,
496                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
497                 return;
498         }
499
500         ret = app2sd_usr_on_demand_setup_exit(pkgid, target_uid);
501         if (ret) {
502                 _E("error(%d)", ret);
503                 result = ret;
504         }
505
506         param = g_variant_new("(i)", result);
507         g_dbus_method_invocation_return_value(invocation, param);
508 }
509
510 static void _app2sd_server_move_installed_app(GDBusConnection *connection, const gchar *sender,
511         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
512 {
513         GVariant *param = NULL;
514         int result = APP2EXT_SUCCESS;
515         int move_type;
516         char *pkgid = NULL;
517         GVariantIter *iter;
518         gchar *str = NULL;
519         int type;
520         int ret = 0;
521         uid_t target_uid = -1;
522         GList *dir_list = NULL;
523         GList *list = NULL;
524         app2ext_dir_details *dir_detail = NULL;
525
526         g_variant_get(parameters, "(&sia(si)i)", &pkgid, &move_type, &iter, &target_uid);
527
528         _D("pkgid(%s), move_type(%d),sender_uid(%d), target_uid(%d)",
529                 pkgid, move_type, sender_uid, target_uid);
530
531         if (sender_uid != 0 && sender_uid != target_uid) {
532                 _E("Not permitted user!");
533                 g_variant_iter_free(iter);
534                 _app2sd_server_return_method_error(invocation,
535                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
536                 return;
537         }
538
539         while (g_variant_iter_loop(iter, "(si)", &str, &type)) {
540                 if (str) {
541                         _D("str(%s), type(%d)", str, type);
542
543                         /* generate dir_list */
544                         dir_detail = (app2ext_dir_details *)calloc(1, sizeof(app2ext_dir_details));
545                         if (dir_detail == NULL) {
546                                 _E("memory allocation failed");
547                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
548                                 break;
549                         }
550
551                         dir_detail->name = strdup((char *)str);
552                         if (dir_detail->name == NULL) {
553                                 _E("out of memory");
554                                 free(dir_detail);
555                                 result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
556                                 break;
557                         }
558
559                         dir_detail->type = type;
560                         list = g_list_append(list, dir_detail);
561                 }
562         }
563         g_variant_iter_free(iter);
564
565         dir_list = g_list_first(list);
566         ret = app2sd_usr_move_installed_app(pkgid, dir_list, move_type, target_uid);
567         if (ret) {
568                 _E("usr_move error(%d)", ret);
569                 result = ret;
570         }
571
572         param = g_variant_new("(i)", result);
573         g_dbus_method_invocation_return_value(invocation, param);
574 }
575
576 static void _app2sd_server_force_clean(GDBusConnection *connection, const gchar *sender,
577         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
578 {
579         GVariant *param = NULL;
580         int result = APP2EXT_SUCCESS;
581         char *pkgid = NULL;
582         uid_t target_uid = -1;
583         int ret = 0;
584
585         g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
586
587         _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
588                 pkgid, sender_uid, target_uid);
589
590         if (sender_uid != 0 && sender_uid != target_uid) {
591                 _E("Not permitted user!");
592                 _app2sd_server_return_method_error(invocation,
593                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
594                 return;
595         }
596
597         ret = app2sd_usr_force_clean(pkgid, target_uid);
598         if (ret) {
599                 _E("error(%d)", ret);
600                 result = ret;
601         }
602
603         param = g_variant_new("(i)", result);
604         g_dbus_method_invocation_return_value(invocation, param);
605 }
606
607 static void _app2sd_server_enable_full_pkg(GDBusConnection *connection, const gchar *sender,
608         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
609 {
610         GVariant *param = NULL;
611         int result = APP2EXT_SUCCESS;
612         int ret = 0;
613
614         _D("sender_uid(%d)", sender_uid);
615
616         if (sender_uid >= REGULAR_USER) {
617                 _E("Not permitted user!");
618                 _app2sd_server_return_method_error(invocation,
619                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
620                 return;
621         }
622
623         ret = app2sd_enable_full_pkg();
624         if (ret) {
625                 _E("error(%d)", ret);
626                 result = ret;
627         }
628
629         param = g_variant_new("(i)", result);
630         g_dbus_method_invocation_return_value(invocation, param);
631 }
632
633 static void _app2sd_server_disable_full_pkg(GDBusConnection *connection, const gchar *sender,
634         GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
635 {
636         GVariant *param = NULL;
637         int result = APP2EXT_SUCCESS;
638         int ret = 0;
639
640         _D("sender_uid(%d)", sender_uid);
641
642         if (sender_uid >= REGULAR_USER) {
643                 _E("Not permitted user!");
644                 _app2sd_server_return_method_error(invocation,
645                         APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
646                 return;
647         }
648
649         ret = app2sd_disable_full_pkg();
650         if (ret) {
651                 _E("error(%d)", ret);
652                 result = ret;
653         }
654
655         param = g_variant_new("(i)", result);
656         g_dbus_method_invocation_return_value(invocation, param);
657 }
658
659 static void handle_method_call(GDBusConnection *connection,
660         const gchar *sender, const gchar *object_path,
661         const gchar *interface_name, const gchar *method_name,
662         GVariant *parameters, GDBusMethodInvocation *invocation,
663         gpointer user_data)
664 {
665         uid_t sender_uid = -1;
666
667         sender_uid = (uid_t)__app2sd_get_sender_uid(connection, sender);
668
669         if (g_strcmp0(method_name, "PreAppInstall") == 0) {
670                 _app2sd_server_pre_app_install(connection, sender,
671                         parameters, invocation, sender_uid);
672         } else if (g_strcmp0(method_name, "PostAppInstall") == 0) {
673                 _app2sd_server_post_app_install(connection, sender,
674                         parameters, invocation, sender_uid);
675         } else if (g_strcmp0(method_name, "PreAppUpgrade") == 0) {
676                 _app2sd_server_pre_app_upgrade(connection, sender,
677                         parameters, invocation, sender_uid);
678         } else if (g_strcmp0(method_name, "PostAppUpgrade") == 0) {
679                 _app2sd_server_post_app_upgrade(connection, sender,
680                         parameters, invocation, sender_uid);
681         } else if (g_strcmp0(method_name, "PreAppUninstall") == 0) {
682                 _app2sd_server_pre_app_uninstall(connection, sender,
683                         parameters, invocation, sender_uid);
684         } else if (g_strcmp0(method_name, "PostAppUninstall") == 0) {
685                 _app2sd_server_post_app_uninstall(connection, sender,
686                         parameters, invocation, sender_uid);
687         } else if (g_strcmp0(method_name, "OndemandSetupInit") == 0) {
688                 _app2sd_server_ondemand_setup_init(connection, sender,
689                         parameters, invocation, sender_uid);
690         } else if (g_strcmp0(method_name, "OndemandSetupExit") == 0) {
691                 _app2sd_server_ondemand_setup_exit(connection, sender,
692                         parameters, invocation, sender_uid);
693         } else if (g_strcmp0(method_name, "MoveInstalledApp") == 0) {
694                 _app2sd_server_move_installed_app(connection, sender,
695                         parameters, invocation, sender_uid);
696         } else if (g_strcmp0(method_name, "ForceClean") == 0) {
697                 _app2sd_server_force_clean(connection, sender,
698                         parameters, invocation, sender_uid);
699         } else if (g_strcmp0(method_name, "EnableFullPkg") == 0) {
700                 _app2sd_server_enable_full_pkg(connection, sender,
701                         parameters, invocation, sender_uid);
702         } else if (g_strcmp0(method_name, "DisableFullPkg") == 0) {
703                 _app2sd_server_disable_full_pkg(connection, sender,
704                         parameters, invocation, sender_uid);
705         }
706
707         g_timeout_add_seconds(5, __exit_app2sd_server, NULL);
708 }
709
710 static const GDBusInterfaceVTable interface_vtable = {
711         handle_method_call,
712         NULL,
713         NULL
714 };
715
716 static void __app2sd_on_bus_acquired(GDBusConnection *connection,
717                 const gchar *name, gpointer user_data)
718 {
719         _I("bus acquired(%s)", name);
720
721         guint reg_id = 0;
722         GError *error = NULL;
723
724         reg_id = g_dbus_connection_register_object(connection,
725                 APP2SD_OBJECT_PATH,
726                 introspection_data->interfaces[0],
727                 &interface_vtable,
728                 NULL, NULL, &error);
729         if (reg_id == 0) {
730                 _E("g_dbus_connection_register_object error(%s)", error->message);
731                 g_error_free(error);
732         }
733 }
734
735 static void __app2sd_on_name_acquired(GDBusConnection *connection,
736                 const gchar *name, gpointer user_data)
737 {
738         _I("name acquired(%s)", name);
739 }
740
741 static void __app2sd_on_name_lost(GDBusConnection *connection,
742                 const gchar *name, gpointer user_data)
743 {
744         _E("name lost(%s)", name);
745         g_main_loop_quit(app2sd_mainloop);
746 }
747
748 static int __app2sd_server_init()
749 {
750         GError *error = NULL;
751         guint owner_id = 0;
752
753         /* gdbus setup for method call */
754         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
755         if (!introspection_data) {
756                 _E("g_dbus_node_info_new_for_xml error(%s)", error->message);
757                 g_error_free(error);
758                 return -1;
759         }
760
761         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
762                 APP2SD_BUS_NAME,
763                 G_BUS_NAME_OWNER_FLAGS_NONE,
764                 __app2sd_on_bus_acquired,
765                 __app2sd_on_name_acquired,
766                 __app2sd_on_name_lost,
767                 NULL, NULL);
768         if (!owner_id) {
769                 _E("g_bus_own_name error");
770                 g_dbus_node_info_unref(introspection_data);
771                 return -1;
772         }
773
774         /* add timer */
775
776         return 0;
777 }
778
779 static void __app2sd_finalize(void)
780 {
781         _D("app2sd finalize");
782
783         if (introspection_data)
784                 g_dbus_node_info_unref(introspection_data);
785
786         _D("app2sd finalize end");
787 }
788
789 int main(int argc, char *argv[])
790 {
791         int ret = 0;
792
793         _I("app2sd_server : start");
794
795         ret = __app2sd_server_init();
796         if (ret) {
797                 _E("app2sd_server init failed(%d)", ret);
798                 return -1;
799         }
800
801         app2sd_mainloop = g_main_loop_new(NULL, FALSE);
802         if (!app2sd_mainloop) {
803                 _E("g_main_loop_new failed");
804                 return -1;
805         }
806
807         g_main_loop_run(app2sd_mainloop);
808
809         __app2sd_finalize();
810
811         _I("app2sd_server : end");
812
813         return 0;
814 }