Add gio channel create/destroy functions to util 60/199060/2 accepted/tizen/unified/20190220.012544 submit/tizen/20190213.052953
authorYu <jiung.yu@samsung.com>
Fri, 1 Feb 2019 07:29:48 +0000 (16:29 +0900)
committerYu <jiung.yu@samsung.com>
Mon, 11 Feb 2019 07:10:45 +0000 (16:10 +0900)
Change-Id: Iaa2160d929b8531aaffbdf9502db5f25a62fa9d5
Signed-off-by: Yu Jiung <jiung.yu@samsung.com>
include/inm-util.h
src/inm-net-access.c
src/inm-netlink.c
src/inm-trace-route.c
src/inm-util.c

index a457ae7d8f20d83a2dd97409c775d1d623997364..fcc3b7590fe7839284a256a3c8aaebe91e56dc0b 100644 (file)
@@ -120,6 +120,8 @@ typedef struct {
        void *user_data;
 } inm_util_nl_data_s;
 
+int inm_util_create_gio_channel(int sock_fd, GIOChannel **channel);
+void inm_util_destroy_gio_channel(GIOChannel *gio_channel, gboolean flush);
 /**
  * @brief This function create and initialize a data related Sending/receiving ARP
  * @details The function open RAW socket and channel to attache g_source
index 59a960c959ce7bf159aadedb65c469fb93f689b4..acd163cf9413d7dc6178e233dcba8af02fa82d26 100644 (file)
@@ -409,10 +409,10 @@ cleanup:
 static void __connect_sockets(void)
 {
        inm_internet_param_s *net_params = NULL;
-       GIOFlags flags;
        struct sockaddr_in addr;
        GIOChannel *channel = NULL;
        int sock;
+       int ret;
 
        net_params = net_access_mon.net_params;
        if (net_params == NULL || net_params->addr == NULL)
@@ -438,12 +438,9 @@ static void __connect_sockets(void)
        addr.sin_port = htons(net_params->port);
 
        /* Register Watch */
-       channel = g_io_channel_unix_new(sock);
-
-       flags = g_io_channel_get_flags(channel);
-       g_io_channel_set_flags(channel, flags | G_IO_FLAG_NONBLOCK, NULL);
-       g_io_channel_set_encoding(channel, NULL, NULL);
-       g_io_channel_set_buffered(channel, FALSE);
+       ret = inm_util_create_gio_channel(sock, &channel);
+       if (ret != 0)
+               goto cleanup;
 
        if (connect(sock, (struct sockaddr *)&addr,
                        sizeof(struct sockaddr_in)) < 0) {
index e924c78c4ed3611501067b551a94220e9c009619..013e62949d4f4737f4a27050455774cce12c41a2 100644 (file)
@@ -363,6 +363,7 @@ int inm_rtnl_init(gpointer user_data)
        INM_LOGE("");
        struct sockaddr_nl addr;
        int fd;
+       int ret = 0;
 
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
@@ -380,11 +381,12 @@ int inm_rtnl_init(gpointer user_data)
                return -1;
        }
 
-       channel = g_io_channel_unix_new(fd);
-       g_io_channel_set_close_on_unref(channel, TRUE);
-
-       g_io_channel_set_encoding(channel, NULL, NULL);
-       g_io_channel_set_buffered(channel, FALSE);
+       ret = inm_util_create_gio_channel(fd, &channel);
+       if (ret != 0) {
+               INM_LOGI("Failed to create gio channel");
+               close(fd);
+               return -1;
+       }
 
        channel_watch = g_io_add_watch(channel,
                                G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
@@ -402,9 +404,7 @@ void inm_rtnl_cleanup(void)
                channel_watch = 0;
        }
 
-       g_io_channel_shutdown(channel, TRUE, NULL);
-       g_io_channel_unref(channel);
-
+       inm_util_destroy_gio_channel(channel, TRUE);
        channel = NULL;
 }
 
index 195597f5cbc0ec4c9dc3f96df4365a6e69dedb72..1667a0cd3f80528bcfca56bfdf68a9b275ebfe3f 100644 (file)
@@ -201,51 +201,6 @@ static inline int __resolve_target(gchar *target, gint port, struct addrinfo **p
        return 0;
 }
 
-static inline GIOChannel *__set_socket_channel(gint sock)
-{
-       GIOChannel *sock_io_channel;
-
-       sock_io_channel = g_io_channel_unix_new(sock);
-       if (!sock_io_channel) {
-               INM_LOGE("Failed to create channel");
-               return NULL;
-       }
-
-       g_io_channel_set_close_on_unref(sock_io_channel, TRUE);
-
-       if (G_IO_STATUS_NORMAL !=
-                       g_io_channel_set_encoding(sock_io_channel,
-                                       NULL, NULL))
-               INM_LOGI("Failed to set encoding NULL on io channel");
-
-       if (G_IO_STATUS_NORMAL !=
-                       g_io_channel_set_flags(sock_io_channel,
-                                       G_IO_FLAG_NONBLOCK, NULL))
-               INM_LOGI("Failed to set flags on io channel");
-
-       return sock_io_channel;
-}
-
-static inline void __clear_socket_io_channel(GIOChannel *sock_io_channel)
-{
-       GError* error = NULL;
-
-       if (!sock_io_channel)
-                       return;
-
-       if (G_IO_STATUS_NORMAL !=
-               g_io_channel_shutdown(sock_io_channel,
-                               FALSE,
-                               &error)) {
-               INM_LOGI("Failure received while shutdown io channel[%d]:[%s]",
-                               error->code, error->message);
-               g_error_free(error);
-       }
-
-       g_io_channel_unref(sock_io_channel);
-}
-
-
 static inline gboolean __check_is_udp_proto(struct addrinfo *p_addr)
 {
        return p_addr->ai_family == AF_INET &&
@@ -548,6 +503,7 @@ static inline gboolean __set_sock_opts(gint sock_fd)
 static inline int __create_sock_data()
 {
        inm_trace_route_data_s *p_trace_route = NULL;
+       int ret;
 
        __INM_FUNC_ENTER__;
 
@@ -560,8 +516,14 @@ static inline int __create_sock_data()
                return -1;
        }
 
-       p_trace_route->sock_io_channel = __set_socket_channel(p_trace_route->sock);
-       RET_MINUS_WITH_MSG_IF_NULL(p_trace_route->sock_io_channel, "Failed to create channel");
+       ret = inm_util_create_gio_channel(p_trace_route->sock, &p_trace_route->sock_io_channel);
+       if (ret != 0) {
+               INM_LOGI("Failed to create gio channel");
+               close(p_trace_route->sock);
+               p_trace_route->sock = -1;
+               __INM_FUNC_EXIT__;
+               return -1;
+       }
 
        p_trace_route->sock_source_id =
                        g_io_add_watch(p_trace_route->sock_io_channel,
@@ -580,7 +542,7 @@ static inline int __create_sock_data()
 static inline void __clear_trace_route_data(inm_trace_route_data_s *p_trace_route)
 {
        REMOVE_G_SOURCE(p_trace_route->tr_timer_source_id);
-       __clear_socket_io_channel(p_trace_route->sock_io_channel);
+       inm_util_destroy_gio_channel(p_trace_route->sock_io_channel, FALSE);
        g_free(p_trace_route->target);
        g_free(p_trace_route->packet);
        g_free(p_trace_route->received);
@@ -590,8 +552,6 @@ static inline void __clear_trace_route_data(inm_trace_route_data_s *p_trace_rout
 
 static void __destroy_trace_route_data()
 {
-       inm_trace_route_data_s *p_trace_route = NULL;
-
        __INM_FUNC_ENTER__;
 
        if (!g_p_tr_data) {
@@ -601,6 +561,7 @@ static void __destroy_trace_route_data()
 
        __clear_trace_route_data(g_p_tr_data);
        g_p_tr_data = NULL;
+
        __INM_FUNC_EXIT__;
 }
 
index b708b50ada209148144fd744c881bac70dbfd588..d870535bb2affb4fc892a243cb0aa65ec13e17bf 100644 (file)
@@ -351,51 +351,57 @@ void util_set_target_ip(inm_util_arp_data_s *arp_data, char *ip)
        return;
 }
 
-static inline GIOChannel *__set_socket_channel(gint sock)
+int inm_util_create_gio_channel(int sock_fd, GIOChannel **channel)
 {
-       GIOChannel *sock_io_channel;
+       GIOChannel *p_channel;
+       GIOFlags flags;
 
-       sock_io_channel = g_io_channel_unix_new(sock);
-       if (!sock_io_channel) {
-               INM_LOGE("Failed to create channel");
-               return NULL;
-       }
+       if (sock_fd == 0 || !channel)
+               return -1;
 
-       g_io_channel_set_close_on_unref(sock_io_channel, TRUE);
+       p_channel = g_io_channel_unix_new(sock_fd);
+       if (!p_channel)
+               return -1;
 
+       flags = g_io_channel_get_flags(p_channel);
        if (G_IO_STATUS_NORMAL !=
-                       g_io_channel_set_encoding(sock_io_channel,
-                                       NULL, NULL))
-               INM_LOGI("Failed to set encoding NULL on io channel");
+                       g_io_channel_set_flags(p_channel,
+                                       flags | G_IO_FLAG_NONBLOCK, NULL))
+               INM_LOGI("Failed to set flags on io channel");
 
        if (G_IO_STATUS_NORMAL !=
-                       g_io_channel_set_flags(sock_io_channel,
-                                       G_IO_FLAG_NONBLOCK, NULL))
-               INM_LOGI("Failed to set flags on io channel");
+                       g_io_channel_set_encoding(p_channel,
+                                       NULL, NULL))
+               INM_LOGI("Failed to set encoding NULL on io channel");
 
-       return sock_io_channel;
+       *channel = p_channel;
+       return 0;
 }
 
-static inline void __clear_socket_io_channel(GIOChannel *sock_io_channel)
+void inm_util_destroy_gio_channel(GIOChannel *gio_channel, gboolean flush)
 {
        GError* error = NULL;
 
+       if (!gio_channel)
+               return;
+
        if (G_IO_STATUS_NORMAL !=
-               g_io_channel_shutdown(sock_io_channel,
-                               FALSE,
+               g_io_channel_shutdown(gio_channel,
+                               flush,
                                &error)) {
                INM_LOGI("Failure received while shutdown io channel[%d]:[%s]",
                                error->code, error->message);
                g_error_free(error);
        }
 
-       g_io_channel_unref(sock_io_channel);
+       g_io_channel_unref(gio_channel);
 }
 
 void util_create_arp_sock(inm_util_arp_data_s *arp_data)
 {
        __INM_FUNC_ENTER__;
        gint sock = -1;
+       int ret;
 
        if (!arp_data) {
                INM_LOGW("NULL data");
@@ -408,16 +414,15 @@ void util_create_arp_sock(inm_util_arp_data_s *arp_data)
        sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
        RET_IF_SOCK_FAILURE(sock);
 
-       arp_data->sock = sock;
-       arp_data->sock_io_channel = __set_socket_channel(sock);
-       if (!arp_data->sock_io_channel) {
-               INM_LOGE("Failed to create channel");
-               arp_data->sock = -1;
+       ret = inm_util_create_gio_channel(sock, &arp_data->sock_io_channel);
+       if (ret != 0) {
+               INM_LOGI("Failed to create gio channel");
                close(sock);
                __INM_FUNC_EXIT__;
                return;
        }
 
+       arp_data->sock = sock;
        arp_data->sock_source_id =
                        g_io_add_watch(arp_data->sock_io_channel, G_IO_IN,
                                        __check_arp_receive, arp_data);
@@ -438,7 +443,7 @@ void util_destroy_arp_sock(inm_util_arp_data_s *arp_data)
 
        RET_IF_SOCK_FAILURE(arp_data->sock);
 
-       __clear_socket_io_channel(arp_data->sock_io_channel);
+       inm_util_destroy_gio_channel(arp_data->sock_io_channel, FALSE);
        REMOVE_G_SOURCE(arp_data->sock_source_id);
        close(arp_data->sock);
        arp_data->sock = -1;
@@ -560,22 +565,22 @@ void util_create_icmp_sock(inm_util_icmp_data_s *inm_util_icmp_data)
 {
        __INM_FUNC_ENTER__;
        gint sock = -1;
+       int ret;
 
        __init_icmp_sock_data(inm_util_icmp_data);
 
        sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        RET_IF_SOCK_FAILURE(sock);
 
-       inm_util_icmp_data->sock = sock;
-       inm_util_icmp_data->sock_io_channel = __set_socket_channel(sock);
-       if (!inm_util_icmp_data->sock_io_channel) {
-               INM_LOGE("Failed to create channel");
-               inm_util_icmp_data->sock = -1;
+       ret = inm_util_create_gio_channel(sock, &inm_util_icmp_data->sock_io_channel);
+       if (ret != 0) {
+               INM_LOGI("Failed to create gio channel");
                close(sock);
                __INM_FUNC_EXIT__;
                return;
        }
 
+       inm_util_icmp_data->sock = sock;
        inm_util_icmp_data->sock_source_id =
                        g_io_add_watch(inm_util_icmp_data->sock_io_channel, G_IO_IN,
                                        __check_icmp_receive, inm_util_icmp_data);
@@ -596,7 +601,7 @@ void util_destroy_icmp_sock(inm_util_icmp_data_s *inm_util_icmp_data)
 
        RET_IF_SOCK_FAILURE(inm_util_icmp_data->sock);
 
-       __clear_socket_io_channel(inm_util_icmp_data->sock_io_channel);
+       inm_util_destroy_gio_channel(inm_util_icmp_data->sock_io_channel, FALSE);
        REMOVE_G_SOURCE(inm_util_icmp_data->sock_source_id);
        close(inm_util_icmp_data->sock);
        inm_util_icmp_data->sock = -1;
@@ -994,9 +999,9 @@ static inline int __allocate_nl_cache_mgr(inm_util_nl_data_s *nl_data,
        }
 
        fd = nl_cache_mngr_get_fd(p_nl_cm);
-       nl_data->sock_io_channel = __set_socket_channel(fd);
-       if (!nl_data->sock_io_channel) {
-               INM_LOGE("Failed to create channel");
+       res = inm_util_create_gio_channel(fd, &nl_data->sock_io_channel);
+       if (res != 0) {
+               INM_LOGI("Failed to create gio channel");
                nl_cache_mngr_free(p_nl_cm);
                nl_socket_free(p_nl_sock);
                __INM_FUNC_EXIT__;
@@ -1023,7 +1028,7 @@ static inline void __free_nl_cache_mgr(inm_util_nl_data_s *nl_data)
                return;
        }
 
-       __clear_socket_io_channel(nl_data->sock_io_channel);
+       inm_util_destroy_gio_channel(nl_data->sock_io_channel, FALSE);
        REMOVE_G_SOURCE(nl_data->sock_source_id);
        nl_data->sock_io_channel = NULL;