Bump release : v1.0.2
[profile/ivi/message-port.git] / daemon / main.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "config.h"
27
28 #include <glib.h>
29 #include "common/log.h"
30 #ifdef USE_SESSION_BUS
31 #include "common/dbus-error.h"
32 #include "common/dbus-server-glue.h"
33 #include "utils.h"
34 #endif
35 #include "dbus-server.h"
36
37 typedef struct {
38     GMainLoop             *m_loop;
39     MsgPortDbusServer     *server;
40 #ifdef USE_SESSION_BUS
41     MsgPortDbusGlueServer *dbus_skeleten;
42 #endif
43 } DaemonData;
44
45 static DaemonData *
46 daemon_data_new ()
47 {
48     return g_slice_new0 (DaemonData);
49 }
50
51 static void
52 daemon_data_free (DaemonData *data)
53 {
54     if (!data) return;
55     if (data->server) g_clear_object (&data->server);
56 #ifdef USE_SESSION_BUS
57     if (data->dbus_skeleten) {
58         g_dbus_interface_skeleton_unexport (
59               G_DBUS_INTERFACE_SKELETON (data->dbus_skeleten));
60         g_clear_object (&data->dbus_skeleten);
61     }
62 #endif
63     if (data->m_loop) g_main_loop_unref (data->m_loop);
64
65     g_slice_free (DaemonData, data);
66 }
67
68 #ifdef USE_SESSION_BUS
69 static gboolean
70 _handle_get_bus_address (DaemonData *data,
71                          GDBusMethodInvocation *invocation,
72                          gpointer               userdata)
73 {
74
75     msgport_return_val_if_fail (data, FALSE);
76     msgport_return_val_if_fail (data->server && MSGPORT_IS_DBUS_SERVER (data->server), FALSE);
77
78     const gchar *server_address = msgport_dbus_server_get_address (data->server);
79
80     if (server_address) {
81         msgport_dbus_glue_server_complete_get_bus_address (data->dbus_skeleten,
82                 invocation, server_address);
83     }
84     else {
85         g_dbus_method_invocation_take_error (invocation, msgport_error_unknown_new());
86     }
87
88     return TRUE;
89 }
90
91 static void
92 _on_bus_acquired (GDBusConnection *connection,
93                  const gchar     *name,
94                  gpointer         userdata)
95 {
96     DaemonData *data = (DaemonData *) userdata;
97
98     msgport_return_if_fail (data);
99     GError *error = NULL;
100
101     data->server = msgport_dbus_server_new ();
102  
103     data->dbus_skeleten = msgport_dbus_glue_server_skeleton_new ();
104
105     g_signal_connect_swapped (data->dbus_skeleten, "handle-get-bus-address",
106                G_CALLBACK (_handle_get_bus_address), data);
107
108     if (!g_dbus_interface_skeleton_export (
109             G_DBUS_INTERFACE_SKELETON (data->dbus_skeleten), connection, "/", &error)) {
110         WARN ("Failed to export interface: %s", error->message);
111         g_error_free (error);
112         g_main_loop_quit (data->m_loop);
113     }
114 }
115
116 static void
117 _on_name_acquired (GDBusConnection *connection,
118                   const gchar     *name,
119                   gpointer         userdata)
120 {
121     DBG ("D-Bus name acquired: %s", name);
122 }
123
124 static void
125 _on_name_lost (GDBusConnection *connection,
126               const gchar     *name,
127               gpointer         userdata)
128 {
129     DaemonData *data = (DaemonData *) userdata;
130     DBG ("D-Bus name lost: %s", name);
131     msgport_return_if_fail (data && data->m_loop);
132
133     g_main_loop_quit (data->m_loop);
134 }
135 #endif /* USE_SESSION_BUS */
136
137 static gboolean
138 _on_unix_signal (gpointer data)
139 {
140     g_main_loop_quit (((DaemonData *)data)->m_loop);
141
142     return FALSE;
143 }
144
145 int main (int argc, char *argv[])
146 {
147     DaemonData *data = daemon_data_new ();
148
149 #if !GLIB_CHECK_VERSION (2, 36, 0)
150     g_type_init (&argc, &argv);
151 #endif
152
153 #if USE_SESSION_BUS
154     guint bus_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
155             "org.tizen.messageport",
156             G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE,
157             _on_bus_acquired,
158             _on_name_acquired,
159             _on_name_lost,
160             data,
161             NULL);
162 #else
163     data->server = msgport_dbus_server_new();
164     if (!data->server) {
165         ERR ("Failed to start server");
166         daemon_data_free (data);
167         return (-1);
168     }
169 #endif /* USE_SESSION_BUS */
170
171     data->m_loop = g_main_loop_new (NULL, FALSE);
172     g_unix_signal_add (SIGTERM, _on_unix_signal, data);
173     g_unix_signal_add (SIGINT, _on_unix_signal, data);
174
175     g_main_loop_run (data->m_loop);
176
177     daemon_data_free (data);
178 #ifdef USE_SESSION_BUS
179     g_bus_unown_name (bus_owner_id);
180 #endif
181
182     DBG("Clean shutdown");
183
184     return 0;
185 }
186