Unref gio channel before closing socket 91/203591/5 accepted/tizen/unified/20190417.050125 submit/tizen/20190416.101341
authorSeonah Moon <seonah1.moon@samsung.com>
Tue, 16 Apr 2019 06:12:53 +0000 (15:12 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Tue, 16 Apr 2019 10:08:10 +0000 (19:08 +0900)
Change-Id: Ia8166741abbe3c8a674e6e8a7bd2027a5db37057

packaging/net-config.spec
src/ip-conflict-detect.c

index 0053dc5..c6bde99 100755 (executable)
@@ -1,6 +1,6 @@
 Name:          net-config
 Summary:       TIZEN Network Configuration service
-Version:       1.1.142
+Version:       1.1.143
 Release:       3
 Group:         System/Network
 License:       Apache-2.0
index d9712d4..1873d8b 100755 (executable)
@@ -206,7 +206,6 @@ skip:
                        if (sd->timer_id)
                                g_source_remove(sd->timer_id);
                        sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
-
                }
        }
 
@@ -214,6 +213,59 @@ out:
        return TRUE;
 }
 
+static void __close_channel_and_sock(struct sock_data* sd)
+{
+       INFO("+");
+       GError* error = NULL;
+
+       if (sd == NULL)
+               return;
+
+       if (G_IO_STATUS_NORMAL !=
+                       g_io_channel_shutdown(sd->chk_conflict_sock_io, FALSE,
+                               &error)) {
+               INFO("Failure received while shutdown io channel[%d]:[%s]", error->code, error->message);
+               g_error_free(error);
+       }
+
+       g_io_channel_unref(sd->chk_conflict_sock_io);
+       g_source_remove(sd->chk_conflict_data_id);
+       sd->chk_conflict_data_id = -1;
+
+       close(sd->chk_conflict_sd);
+       sd->chk_conflict_sd = -1;
+}
+
+static int __open_channel_and_sock(struct sock_data* sd)
+{
+       INFO("+");
+       if (sd == NULL)
+               return -1;
+
+       if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
+               INFO("socket Failed.\n");
+               return -1;
+       }
+
+       sd->chk_conflict_sock_io = g_io_channel_unix_new(sd->chk_conflict_sd);
+       if (sd->chk_conflict_sock_io == NULL) {
+               INFO("Failed to create channel");
+               g_free(sd);
+               return -1;
+       }
+       g_io_channel_set_close_on_unref(sd->chk_conflict_sock_io, TRUE);
+       if (G_IO_STATUS_NORMAL != g_io_channel_set_encoding(sd->chk_conflict_sock_io,
+                               NULL, NULL))
+               INFO("Failed to set encoding NULL on io channel");
+       if (G_IO_STATUS_NORMAL != g_io_channel_set_flags(sd->chk_conflict_sock_io,
+                               G_IO_FLAG_NONBLOCK, NULL))
+               INFO("Failed to set flags on io channel");
+       sd->chk_conflict_data_id = g_io_add_watch(sd->chk_conflict_sock_io, G_IO_IN,
+                       __netconfig_check_arp_receive, sd);
+       DBG("socket %d", sd->chk_conflict_sd);
+       return 0;
+}
+
 static gboolean send_arp(gpointer data)
 {
        struct sock_data *sd = data;
@@ -305,19 +357,13 @@ static gboolean send_arp(gpointer data)
        addr.sll_protocol = htons(ETH_P_ARP);
        memcpy(addr.sll_addr, broadcast_addr, ETHER_ADDR_LEN);
 
-       if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+       if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0,
+                               (struct sockaddr*)&addr, sizeof(addr)) < 0) {
                INFO("Sending ARP Packet Failed. Error. = %s\n",
                                strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
-               /* close socket */
-               close(sd->chk_conflict_sd);
-               sd->chk_conflict_sd = -1;
-
-               /* reopen socket */
-               if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
-                       INFO("socket %d", sd->chk_conflict_sd);
-                       INFO("socket Failed. Error = %s\n",
-                               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
-               }
+               __close_channel_and_sock(sd);
+               if (__open_channel_and_sock(sd) == -1)
+                       INFO("__open_channel_and_sock failed");
                goto err;
        } else {
                DBG("Sent ARP Packet \n");
@@ -349,7 +395,6 @@ struct sock_data * start_ip_conflict_mon(void)
                return NULL;
        }
 
-       char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
        initial_bursts = true;
 
        sd = g_try_malloc0(sizeof(struct sock_data));
@@ -362,61 +407,31 @@ struct sock_data * start_ip_conflict_mon(void)
        sd->timer_id = 0;
        sd->iteration = 0;
 
-       if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
-               INFO("socket Failed. Error = %s\n",
-                               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
+       if (__open_channel_and_sock(sd) == -1) {
+               INFO("__open_channel_and_sock failed");
                g_free(sd);
                return NULL;
-       } else {
-               sd->chk_conflict_sock_io = g_io_channel_unix_new(sd->chk_conflict_sd);
-               if (sd->chk_conflict_sock_io == NULL) {
-                       INFO("Failed to create channel");
-                       INFO("Exit");
-                       g_free(sd);
-                       return NULL;
-               }
-
-               g_io_channel_set_close_on_unref(sd->chk_conflict_sock_io, TRUE);
+       }
 
-               if (G_IO_STATUS_NORMAL != g_io_channel_set_encoding(sd->chk_conflict_sock_io,
-                                                                  NULL, NULL))
-                       INFO("Failed to set encoding NULL on io channel");
-               if (G_IO_STATUS_NORMAL != g_io_channel_set_flags(sd->chk_conflict_sock_io,
-                                                               G_IO_FLAG_NONBLOCK, NULL))
-                       INFO("Failed to set flags on io channel");
-               sd->chk_conflict_data_id = g_io_add_watch(sd->chk_conflict_sock_io, G_IO_IN,
-                                                         __netconfig_check_arp_receive, sd);
-               DBG("socket %d", sd->chk_conflict_sd);
+       sd->timeout = td.initial_time;
+       send_arp(sd);
+       is_ip_conflict_detect_enabled = true;
+       conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
 
-               sd->timeout = td.initial_time;
-               send_arp(sd);
-               is_ip_conflict_detect_enabled = true;
-               conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
-               return sd;
-       }
+       return sd;
 }
 
 void stop_ip_conflict_mon()
 {
        INFO("+");
-       GError* error = NULL;
        if (sd == NULL) {
                INFO("sd is NULL");
                return;
        }
-       if (-1 < sd->chk_conflict_sd) {
-               if (G_IO_STATUS_NORMAL !=
-                   g_io_channel_shutdown(sd->chk_conflict_sock_io, FALSE,
-                                         &error)) {
-                       INFO("Failure received while shutdown io channel[%d]:[%s]", error->code, error->message);
-                       g_error_free(error);
-               }
-               g_io_channel_unref(sd->chk_conflict_sock_io);
-               g_source_remove(sd->chk_conflict_data_id);
-               sd->chk_conflict_data_id = -1;
-               close(sd->chk_conflict_sd);
-               sd->chk_conflict_sd = -1;
-       }
+
+       if (-1 < sd->chk_conflict_sd)
+               __close_channel_and_sock(sd);
+
        if (sd->timer_id > 0) {
                g_source_remove(sd->timer_id);
                sd->timer_id = 0;
@@ -453,7 +468,6 @@ gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *contex
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
 
-
        if (detect == false) {
                if (sd != NULL)
                        stop_ip_conflict_mon();