CONPRO-1320] Cleanup observers of deleted resource. 81/190681/1
authorAmit KS <amit.s12@samsung.com>
Thu, 4 Oct 2018 15:00:41 +0000 (20:30 +0530)
committerAmit KS <amit.s12@samsung.com>
Thu, 4 Oct 2018 15:05:38 +0000 (20:35 +0530)
Observers were not cleane up for deleted resource, this
caused crash in trying to access deleted resource while
deleting these observers when TCP connection is closed.
Changes are made to clean up the observers while deleting
resource itself

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/318
(cherry picked from commit a7f8adbd8574db8b22a9fbf2340721ebac4c148e)

Signed-off-by: Harish Kumara M <h.marappa@samsung.com>
Signed-off-by: Amit KS <amit.s12@samsung.com>
Change-Id: I2860a6aaa6324b3ff55070860e8e6b0090f6d2eb

resource/csdk/stack/include/internal/ocobserve.h
resource/csdk/stack/src/ocobserve.c
resource/csdk/stack/src/ocstack.c

index 2fa67241fb3936dfc919efea0ca56dd6c74ae729..f5fc6c1f6416bfddecda909a360aa75ba1c6bf09 100644 (file)
@@ -213,6 +213,15 @@ OCStackResult AddObserver (const char         *resUri,
   */
 OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr);
 
+/**
+ * Delete all observers of a resource.
+ *
+ * @param resource Handle of resource whose observers needs to be deleted.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult DeleteObserverUsingResource(OCResource *res);
+
 /**
  * Search the list of observers for the specified token.
  *
index 9fde469bd84c3030d8cd006dbe61f907a7e67cf8..18de8c68baaf902f4d529fa0e3c73d38bdb66dfe 100644 (file)
@@ -767,6 +767,30 @@ OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr)
     return OC_STACK_OK;
 }
 
+OCStackResult DeleteObserverUsingResource(OCResource *res)
+{
+    if (!res)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    oc_mutex_lock(g_serverObsListMutex);
+    ResourceObserver *obs = NULL;
+    ResourceObserver *next = NULL;
+    LL_FOREACH_SAFE(g_serverObsList, obs, next)
+    {
+        if (obs->resource == res)
+        {
+            OIC_LOG_V(INFO, TAG, "Deleting observer: id-%u, token-", obs->observeId);
+            OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obs->token, obs->tokenLength);
+            LL_DELETE(g_serverObsList, obs);
+            FreeObserver(obs);
+        }
+    }
+    oc_mutex_unlock(g_serverObsListMutex);
+    return OC_STACK_OK;
+}
+
 void DeleteObserverList()
 {
     oc_mutex_lock(g_serverObsListMutex);
index f469c51f33739f371f9ce9bab2ee87a975d5ccdc..139ff3a2a1659c7c4834e1baa28c5a195af07b84 100755 (executable)
@@ -4649,6 +4649,9 @@ OCStackResult deleteResource(OCResource *resource)
                 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
             }
 #endif
+            // Delete resource's all observers
+            DeleteObserverUsingResource(resource);
+
             // Only resource in list.
             if (temp == headResource && temp == tailResource)
             {
@@ -4717,6 +4720,8 @@ void deleteResourceElements(OCResource *resource)
         OCDeleteResourceAttributes(resource->rsrcAttributes);
         resource->rsrcAttributes = NULL;
     }
+
+    resource->entityHandler = NULL;
     resource = NULL;
 }