Fixed thread safety procedure by adding SOCK_CLOEXEC flag
authorAshok Channa <ashok.channa@samsung.com>
Mon, 20 Apr 2015 21:56:55 +0000 (14:56 -0700)
committerErich Keane <erich.keane@intel.com>
Tue, 12 May 2015 16:43:01 +0000 (16:43 +0000)
JIRA ticket IOT-440 suggested to use SOCK_CLOEXEC flag for
socket file descriptor for thread safety.
This fix will ensure SOCK_CLOEXEC flag is checked
if kernal has supports  for it.

Signed-off-by: Ashok Channa <ashok.channa@samsung.com>
Change-Id: I84efdcd721c1f34f8d0931e08acbdcb147b9a5db
Reviewed-on: https://gerrit.iotivity.org/gerrit/774
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c

index 9787824..2d408fd 100644 (file)
@@ -205,7 +205,16 @@ static CAResult_t CAIPUpdateInterfaceInformation(u_arraylist_t **netInterfaceLis
     VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null");
 
     /* Get a socket handle. */
-    int sck = socket(AF_INET, SOCK_DGRAM, 0);
+    int sck = -1;
+#ifdef SOCK_CLOEXEC
+    sck = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
+#endif
+
+    if ( -1 == sck)
+    {
+        sck=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    }
+
     if (sck < 0)
     {
         OIC_LOG(ERROR, IP_MONITOR_TAG, "Error in socket creation");
index ad73e0f..b7b820b 100644 (file)
@@ -298,7 +298,17 @@ static CAResult_t CACreateSocket(int *socketFD, const char *localIp, uint16_t *p
     VERIFY_NON_NULL(localIp, IP_SERVER_TAG, "localIp is NULL");
     VERIFY_NON_NULL(port, IP_SERVER_TAG, "port is NULL");
     // Create a UDP socket
-    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    int sock = -1;
+
+#ifdef SOCK_CLOEXEC
+    sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
+#endif
+
+    if (-1 == sock)
+    {
+        sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    }
+
     if (-1 == sock)
     {
         OIC_LOG_V(ERROR, IP_SERVER_TAG, "Failed to create Socket, Error code: %s",