Add retry mechanism for netlink socket creation 76/235976/2
authorNishant Chaprana <n.chaprana@samsung.com>
Thu, 11 Jun 2020 12:43:40 +0000 (18:13 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Fri, 12 Jun 2020 04:20:01 +0000 (09:50 +0530)
Change-Id: Ic0512f1bc680a68a9209c097b7ef4a2fb1a8548c
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
packaging/stc-manager.spec
src/helper/helper-nl.c

index 79eb620..a418017 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.99
+Version:    0.1.0
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
index ff9d1af..6d54e8f 100755 (executable)
 #include <linux/rtnetlink.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
-/**
- * create_netlink(): Create netlink socket and returns it.
- * Returns: Created socket on success and -1 on failure.
- */
-API int create_netlink(int protocol, uint32_t groups)
+#define NETLINK_SOCK_RETRY_COUNT 3
+
+int __create_netlink(int protocol, uint32_t groups, int retry)
 {
        /**
         * TODO it's one socket, in future make set of sockets
         * unique for protocol and groups
         */
        int sock;
-       sock = socket(PF_NETLINK, SOCK_RAW, protocol);
-       if (sock < 0)
+
+       if (retry <= 0)
                return -EINVAL; //LCOV_EXCL_LINE
 
+       errno = 0;
+       sock = socket(PF_NETLINK, SOCK_RAW, protocol);
+       if (sock < 0) {
+               STC_LOGE("failed to open socket errno [%d], retry [%d]",
+                               errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE
+               return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE
+       }
+
        struct sockaddr_nl src_addr = { 0, };
 
        src_addr.nl_family = AF_NETLINK;
        src_addr.nl_groups = groups;
 
+       errno = 0;
        if (bind(sock, (struct sockaddr *)&src_addr, sizeof(src_addr)) < 0) {
+               STC_LOGE("failed to bind socket errno [%d], retry [%d]",
+                               errno, NETLINK_SOCK_RETRY_COUNT - retry); //LCOV_EXCL_LINE
                close(sock); //LCOV_EXCL_LINE
-               return -1; //LCOV_EXCL_LINE
+               return __create_netlink(protocol, groups, --retry); //LCOV_EXCL_LINE
        }
 
        return sock;
 }
 
+/**
+ * create_netlink(): Create netlink socket and returns it.
+ * Returns: Created socket on success and -1 on failure.
+ */
+API int create_netlink(int protocol, uint32_t groups)
+{
+       return __create_netlink(protocol, groups, NETLINK_SOCK_RETRY_COUNT);
+}
+
 void fill_attribute_list(struct rtattr **atb, const int max_len,
                         struct rtattr *rt_na, int rt_len)
 {