[net-config] Fixed stopping of mdnsd when dns-sd application terminates abruptly 94/88294/1 submit/tizen/20160921.025733
authorSaurav Babu <saurav.babu@samsung.com>
Fri, 16 Sep 2016 06:12:45 +0000 (11:42 +0530)
committerSaurav Babu <saurav.babu@samsung.com>
Fri, 16 Sep 2016 06:12:45 +0000 (11:42 +0530)
This patch accepts unique dbus name in handle_launch_mdns() and registers
NameOwnerChanged() signal for all the unique dbus name. Whenever
application terminates abruptly or deinitializes then dbus connection is
closed and net-config receives NameOwnerChanged signal corresponding to
that application and decreases ref count of mdnsd daemon. When ref count
drops to 0 then mdnsd is stopped.

Change-Id: I0d0d5c683ab6ae7b346bec43f70f031a8afee7cf
Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
include/util.h
interfaces/netconfig-iface-network-state.xml
src/network-state.c
src/utils/util.c

index 6a5e541..9df584b 100755 (executable)
@@ -63,9 +63,8 @@ int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gi
 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family);
 
 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context);
-gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context);
-gboolean handle_ref_mdns(Network *object, GDBusMethodInvocation *context);
-gboolean handle_unref_mdns(Network *object, GDBusMethodInvocation *context);
+gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
+                                                       gchar *name);
 
 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * data);
 int netconfig_send_message_to_net_popup(const char *title,
index d059cfe..7d0a32c 100755 (executable)
@@ -23,8 +23,9 @@
                <method name="CheckGetPrivilege"></method>
                <method name="CheckProfilePrivilege"></method>
                <method name="CheckInternetPrivilege"></method>
-               <method name="LaunchMdns"></method>
-               <method name="UnrefMdns"></method>
+               <method name="LaunchMdns">
+                       <arg type="s" name="name" direction="in"/>
+               </method>
                <method name="DevicePolicySetWifi">
                        <arg type="i" name="state" direction="in"/>
                </method>
index 9b2b9c0..f0c488f 100755 (executable)
@@ -1087,8 +1087,6 @@ void state_object_create_and_init(void)
                                G_CALLBACK(handle_remove_route), NULL);
        g_signal_connect(netconfigstate, "handle-launch-mdns",
                                G_CALLBACK(handle_launch_mdns), NULL);
-       g_signal_connect(netconfigstate, "handle-unref-mdns",
-                               G_CALLBACK(handle_unref_mdns), NULL);
        g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
                                G_CALLBACK(handle_device_policy_set_wifi), NULL);
        g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
index d028cd5..9755f48 100755 (executable)
 #include "util.h"
 #include "neterror.h"
 #include "wifi-state.h"
+#include "netdbus.h"
 
+#define DBUS_SERVICE_DBUS              "org.freedesktop.DBus"
+#define DBUS_INTERFACE_DBUS            "org.freedesktop.DBus"
 #define MAC_INFO_FILEPATH              tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
 #define MAC_ADDRESS_MAX_LEN            18
 
 static gboolean netconfig_device_picker_test = FALSE;
 static int mdnsd_ref_count = 0;
+typedef struct {
+       char *conn_name;
+       int conn_id;
+} dnssd_conn_destroy_data;
 
 GKeyFile *netconfig_keyfile_load(const char *pathname)
 {
@@ -774,45 +781,78 @@ int execute_mdnsd_script(char* op) {
        return netconfig_execute_file(path, args, envs);
 }
 
-gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context)
+static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
+               const gchar *Name, const gchar *path, const gchar *interface,
+               const gchar *sig, GVariant *param, gpointer user_data)
 {
-       DBG("Launch mdnsresponder daemon");
+       gchar *name = NULL;
+       gchar *old = NULL;
+       gchar *new = NULL;
+       dnssd_conn_destroy_data *data = user_data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
+
+       if (param == NULL)
+               return;
 
-       if (execute_mdnsd_script("start") < 0) {
-               ERR("Failed to launch mdnsresponder daemon");
-               netconfig_error_invalid_parameter(context);
-               return FALSE;
+       g_variant_get(param, "(sss)", &name, &old, &new);
+
+       if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
+               DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
+                       data->conn_id);
+               mdnsd_ref_count--;
+               g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
+               if (mdnsd_ref_count == 0) {
+                       if (execute_mdnsd_script("stop") < 0)
+                               ERR("Failed to stop mdnsresponder daemon");
+               }
        }
+       g_free(name);
+       g_free(old);
+       g_free(new);
+       g_free(data->conn_name);
+       g_free(data);
+       return;
+}
 
-       mdnsd_ref_count++;
-       DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
+static void register_dnssd_conn_destroy_signal(gchar *name)
+{
+       dnssd_conn_destroy_data *data;
+       GDBusConnection *connection = NULL;
+       connection = netdbus_get_connection();
 
-       network_complete_launch_mdns(object, context);
-       return TRUE;
+       if (connection == NULL) {
+               ERR("Failed to get GDbus Connection");
+               return;
+       }
+
+       data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
+       data->conn_name = g_strdup(name);
+
+       data->conn_id = g_dbus_connection_signal_subscribe(connection,
+                                                       DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
+                                                       "NameOwnerChanged", NULL, name,
+                                                       G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
+                                                       data, NULL);
+       return;
 }
 
-gboolean handle_unref_mdns(Network *object, GDBusMethodInvocation *context)
+gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
+                                                       gchar *name)
 {
-       DBG("Unef mdnsresponder daemon");
+       DBG("Launch mdnsresponder daemon");
 
-       if (mdnsd_ref_count <= 0) {
-               ERR("Invalid access");
+       if (execute_mdnsd_script("start") < 0) {
+               ERR("Failed to launch mdnsresponder daemon");
                netconfig_error_invalid_parameter(context);
                return FALSE;
        }
 
-       mdnsd_ref_count--;
-
-       DBG("Unref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
-       if (mdnsd_ref_count == 0) {
-               if (execute_mdnsd_script("stop") < 0) {
-                       ERR("Failed to stop mdnsresponder daemon");
-                       netconfig_error_invalid_parameter(context);
-                       return FALSE;
-               }
-       }
+       mdnsd_ref_count++;
+       register_dnssd_conn_destroy_signal(name);
+       DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
 
-       network_complete_unref_mdns(object, context);
+       network_complete_launch_mdns(object, context);
        return TRUE;
 }