tizen 2.4 release
[framework/appfw/aul-1.git] / delegator / delegator_server.c
1 #define _GNU_SOURCE
2 #include <bundle.h>
3 #include <string.h>
4 #include <security-server.h>
5 #include "aul.h"
6 #include "aul_svc.h"
7 #include "aul_zone.h"
8 #include "delegator_config.h"
9 #include "delegator_client_gdbus_generated.h"
10 #include "vasum.h"
11
12 #ifdef LOG_TAG
13 #undef LOG_TAG
14 #endif
15 #define LOG_TAG "DELEGATOR_SERVER"
16
17 GMainLoop *gMainLoop;
18 GDBusConnection *context_connection = NULL;
19 static int result = -1;
20
21 static int __get_caller_pid(const char *name)
22 {
23         guint pid = -1;
24         GVariant *ret;
25         GError *error = NULL;
26
27         ret = g_dbus_connection_call_sync (context_connection,
28                                            "org.freedesktop.DBus",
29                                            "/org/freedesktop/DBus",
30                                            "org.freedesktop.DBus",
31                                            "GetConnectionUnixProcessID",
32                                            g_variant_new ("(s)", name),
33                                            NULL,
34                                            G_DBUS_CALL_FLAGS_NONE,
35                                            -1,
36                                            NULL,
37                                            &error);
38         g_variant_get (ret, "(u)", &pid);
39         g_variant_unref (ret);
40
41         return pid;
42 }
43
44 static gboolean on_handle_launch(OrgTizenAulDelegator *object,
45                                  GDBusMethodInvocation *invocation, const gchar *arg_container,
46                                  const gchar *arg_bundle)
47 {
48         SECURE_LOGD("delegator server on_handle_launch(%s)", arg_container);
49
50         vsm_context_h ctx;
51         vsm_zone_h dom;
52
53         int pid;
54         int ret;
55         const char *name = g_dbus_method_invocation_get_sender(invocation);
56
57         pid = __get_caller_pid(name);
58
59         ret = security_server_check_privilege_by_pid(pid, "aul::jump", "x");
60         if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
61                 SECURE_LOGE("delegator launch request has been denied by smack");
62                 org_tizen_aul_delegator_complete_launch(object, invocation,
63                         AUL_SVC_RET_EREJECTED);
64                 return TRUE;
65         }
66
67         ctx = vsm_create_context();
68
69         if (ctx) {
70                 dom = vsm_lookup_zone_by_name(ctx, arg_container);
71                 if (dom) {
72                         char *focus = NULL;
73                         bundle *b = bundle_decode((const bundle_raw *)arg_bundle, strlen(arg_bundle));
74                         bundle_get_str(b, AUL_SVC_K_FOCUS_ZONE, &focus);
75                         if (focus == NULL || strcmp(focus, "true") == 0)
76                                 vsm_set_foreground(dom);
77                         SECURE_LOGD("delegator server domain was changed");
78
79                         char *old_zone = NULL;
80
81                         aul_set_zone(arg_container, &old_zone);
82                         result = aul_svc_run_service(b, 0, NULL, NULL);
83                         bundle_free(b);
84                         if (old_zone)
85                                 free(old_zone);
86                         SECURE_LOGD("delegator launch result = %d", result);
87                         org_tizen_aul_delegator_complete_launch(object, invocation, result);
88                         vsm_cleanup_context(ctx);
89                         return TRUE;
90
91                 }
92                 org_tizen_aul_delegator_complete_launch(object, invocation,
93                         AUL_SVC_RET_EINVAL);
94                 vsm_cleanup_context(ctx);
95         }
96
97         return TRUE;
98 }
99
100 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
101                              gpointer user_data)
102 {
103         SECURE_LOGD("delegator server on_name_acquired ++");
104
105         OrgTizenAulDelegator *skeleton;
106
107         skeleton = org_tizen_aul_delegator_skeleton_new();
108         g_signal_connect(skeleton, "handle-launch", G_CALLBACK(on_handle_launch),
109                          NULL);
110         g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(skeleton),
111                                          connection,
112                                          DELEGATOR_NODE, NULL);
113         context_connection = connection;
114         SECURE_LOGD("delegator server on_name_acquired -- ");
115 }
116
117 int main(int argc, char *argv[])
118 {
119         gMainLoop = g_main_loop_new(NULL, FALSE);
120         if (!gMainLoop) {
121                 SECURE_LOGE("delegator server g_main_loop_new failed\n");
122                 return -1;
123         }
124
125         guint id = g_bus_own_name(G_BUS_TYPE_SYSTEM, DELEGATOR_INTERFACE,
126                                   G_BUS_NAME_OWNER_FLAGS_NONE, NULL, on_name_acquired, NULL, NULL,
127                                   NULL);
128
129         SECURE_LOGD("delegator server Main loop is created.");
130         g_main_loop_run(gMainLoop);
131         g_bus_unown_name(id);
132         g_main_loop_unref(gMainLoop);
133
134         return 0;
135 }
136