mot-agent: fix crash if calling 'pairwise' and 'unpair' repeatelly.
authorsaerome.kim <saerome.kim@samsung.com>
Mon, 15 Jan 2018 12:18:14 +0000 (21:18 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:40 +0000 (19:38 +0900)
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
src/mot-agent/ma-subowner.c

index a7c37c96ba5a6ea529a9fec859076dfd55d5c7d8..69e28827ccfb709ce21ea067bdcd260a85a4211c 100644 (file)
@@ -829,6 +829,8 @@ static int _disc_mot_enb_devs(ma_service *service)
        con = g_malloc0(sizeof(ma_req_cb_s));
        if (NULL == con) {
                MA_LOGE( "g_malloc0() Fail=%d", errno);
+               /* Unset d2ds status 'pending' */
+               g_atomic_int_set(&service->pending, 0);
                return MA_ERROR_OUT_OF_MEMORY;
        }
 
@@ -880,6 +882,8 @@ static int _disc_owned_devs(ma_service *service)
        con = g_malloc0(sizeof(ma_req_cb_s));
        if (NULL == con) {
                MA_LOGE( "g_malloc0() Fail=%d", errno);
+               /* Unset d2ds status 'pending' */
+               g_atomic_int_set(&service->pending, 0);
                return MA_ERROR_OUT_OF_MEMORY;
        }
 
@@ -1027,7 +1031,7 @@ static int _ma_mot(ma_service *service, gchar *uuid_str, gchar *pin)
        g_thread_unref(con->thread);
 
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con);
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb, con);
 #endif
        return MA_ERROR_NONE;
 }
@@ -1134,7 +1138,7 @@ static int _ma_remove_mo(ma_service *service, gchar *uuid_str)
        g_thread_unref(con->thread);
 
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con);
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb, con);
 #endif
        return MA_ERROR_NONE;
 }
@@ -1220,7 +1224,7 @@ static int _ma_remove_cred_local(ma_service *service, gchar *uuid_str)
        g_thread_unref(con->thread);
 
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con);
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb, con);
 #endif
        return MA_ERROR_NONE;
 }
@@ -1444,26 +1448,13 @@ static void _provisioning_acl_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr
        }
        client->g_doneCB = true;
 }
-
-static void _provisioning_pairwise_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error)
-{
-       ma_subowner_s *client = (ma_subowner_s *)ctx;
-
-       if (!has_error)  {
-               MA_LOGD("Provision pairwise SUCCEEDED");
-       } else {
-               MA_LOGD( "Provision pairwise FAILED ");
-               _print_result_list((const OCProvisionResult_t*) arr, nOfRes);
-       }
-       client->g_doneCB = true;
-}
-
 static gpointer _provisioning_acl_func(gpointer data)
 {
        OCStackResult ret = OC_STACK_OK;
        ma_req_cb_s *con = (ma_req_cb_s *)data;
        OicSecAcl_t* acl = NULL;
-       OicUuid_t *uuid = NULL;
+       OicUuid_t *uuid1 = NULL;
+       OicUuid_t *uuid2 = NULL;
        OCProvisionDev_t *target_dev = NULL;
        OCProvisionDev_t *subject_dev = NULL;
 
@@ -1477,21 +1468,19 @@ static gpointer _provisioning_acl_func(gpointer data)
                goto PVACL_ERROR;
        }
 
-       uuid = _convert_uuid(con->uuid_target_str);
-       target_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
+       uuid1 = _convert_uuid(con->uuid_target_str);
+       target_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid1);
        if (!target_dev) {
                MA_LOGE("We can't find target in MOWNED dev list");
                goto PVACL_ERROR;
        }
-       g_free(uuid);
 
-       uuid = _convert_uuid(con->uuid_subject_str);
-       subject_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
+       uuid2 = _convert_uuid(con->uuid_subject_str);
+       subject_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid2);
        if (!subject_dev) {
                MA_LOGE("We can't find subject in MOWNED dev list");
                goto PVACL_ERROR;
        }
-       g_free(uuid);
 
        g_client->g_doneCB = false;
        MA_LOGI(" Provisioning Selected ACL..");
@@ -1523,8 +1512,10 @@ PVACL_ERROR:
 
        if (acl)
                OCDeleteACLList(acl);
-       if (uuid)
-               g_free(uuid);
+       if (uuid1)
+               g_free(uuid1);
+       if (uuid2)
+               g_free(uuid2);
        _request_cleanup(con);
        g_thread_exit(GINT_TO_POINTER (1));
        return NULL;
@@ -1558,11 +1549,148 @@ static int _provisioning_acl(ma_service *service, gchar *target, gchar *subject,
        }
        g_thread_unref(con->thread);
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con);
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb, con);
+#endif
+       return MA_ERROR_NONE;
+}
+
+static void _provision_cred_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error)
+{
+       ma_subowner_s *client = (ma_subowner_s *)ctx;
+
+       if (!has_error)  {
+               MA_LOGD("Provision ACL SUCCEEDED");
+       } else {
+               MA_LOGD( "Provision ACL FAILED ");
+               _print_result_list((const OCProvisionResult_t*) arr, nOfRes);
+       }
+       client->g_doneCB = true;
+}
+
+static gpointer _cred_provisioning_func(gpointer data)
+{
+       OCStackResult ret = OC_STACK_OK;
+       ma_req_cb_s *con = (ma_req_cb_s *)data;
+
+       OicUuid_t *uuid1 = NULL;
+       OicUuid_t *uuid2 = NULL;
+       OCProvisionDev_t *dev1 = NULL;
+       OCProvisionDev_t *dev2 = NULL;
+
+       if (!con->uuid_dev1 ) {
+               MA_LOGE("dev1 is NULL");
+               goto PVCRED_ERROR;
+       }
+       if (!con->uuid_dev2) {
+               MA_LOGE("dev2 is NULL");
+               goto PVCRED_ERROR;
+       }
+
+       uuid1 = _convert_uuid(con->uuid_dev1);
+       dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid1);
+       if (!dev1) {
+               MA_LOGE("We can't find in MOWNED dev list");
+               goto PVCRED_ERROR;
+       }
+
+       uuid2 = _convert_uuid(con->uuid_dev2);
+       dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid2);
+       if (!dev2) {
+               MA_LOGE("We can't find in MOWNED dev list");
+               goto PVCRED_ERROR;
+       }
+
+       /* To do ACL provisioning only one */
+       dev1->next = NULL;
+
+       /* To do ACL provisioning only one */
+       dev2->next = NULL;
+
+       g_client->g_doneCB = false;
+
+       MA_LOGI("Provisioning Selected Pairwise Devices..");
+       ret = OCProvisionCredentials((void*) g_client,
+               SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_256,
+               dev1, dev2, _provision_cred_cb);
+       if (OC_STACK_OK != ret)  {
+               MA_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, _error_to_string(ret));
+               goto PVCRED_ERROR;
+       }
+
+       ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S);
+       if (ret)  {
+               MA_LOGE( "OCProvisionCredentials Faild = %d", errno);
+               goto PVCRED_ERROR;
+       }
+
+       /* display the pairwise-provisioned result */
+       MA_LOGI("Provisioned Selected Pairwise Devices");
+
+PVCRED_ERROR:
+       /* Notify ACL result  */
+       net_ma_emit_cred_done(ma_dbus_get_object(), (int)ret);
+
+       if (uuid1)
+               g_free(uuid1);
+       if (uuid2)
+               g_free(uuid2);
+       _request_cleanup(con);
+
+       g_thread_exit(GINT_TO_POINTER (1));
+
+       return NULL;
+}
+
+static int _provisioning_cred(ma_service *service,
+       gchar *uuid_dev1, gchar *uuid_dev2)
+{
+       ma_req_cb_s *con = NULL;
+       con = g_malloc0(sizeof(ma_req_cb_s));
+       if (!con) {
+               MA_LOGE( "g_malloc0() Fail=%d", errno);
+               /* Unset d2ds status 'pending' */
+               g_atomic_int_set(&service->pending, 0);
+               return MA_ERROR_OUT_OF_MEMORY;
+       }
+
+       con->userdata = service;
+       con->cid = MA_PROV_CRED;
+
+       if (!uuid_dev1 ||!uuid_dev2) {
+               MA_LOGE( "Invaild Prameters");
+               _request_cleanup(con);
+               return MA_ERROR_INVALID_PARAMETER;
+       }
+
+       con->uuid_dev1 = g_strdup(uuid_dev1);
+       con->uuid_dev2 = g_strdup(uuid_dev2);
+
+       con->thread = g_thread_try_new("prov_cred", _cred_provisioning_func, con, NULL);
+       if (!con->thread) {
+               MA_LOGE("Failed to create thread");
+               _request_cleanup(con);
+               return MA_ERROR_OUT_OF_MEMORY;
+       }
+       g_thread_unref(con->thread);
+#ifdef TIMEOUT_USED
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb, con);
 #endif
        return MA_ERROR_NONE;
 }
 
+static void _provisioning_pairwise_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error)
+{
+       ma_subowner_s *client = (ma_subowner_s *)ctx;
+
+       if (!has_error)  {
+               MA_LOGD("Provision pairwise SUCCEEDED");
+       } else {
+               MA_LOGD( "Provision pairwise FAILED ");
+               _print_result_list((const OCProvisionResult_t*) arr, nOfRes);
+       }
+       client->g_doneCB = true;
+}
+
 static gpointer _provisioning_pairwise_func(gpointer data)
 {
        OCStackResult ret = OC_STACK_OK;
@@ -1593,35 +1721,35 @@ static gpointer _provisioning_pairwise_func(gpointer data)
                MA_LOGE("%s", con->rsrc_type_2 );
                MA_LOGE("%s", con->rsrc_interface_2 );
 
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        uuid_1 = _convert_uuid(con->target_1);
        target_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_1);
        if (!target_dev_1) {
                MA_LOGE("We can't find target in MOWNED dev list");
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        uuid_2 = _convert_uuid(con->target_2);
        target_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_2);
        if (!target_dev_2) {
                MA_LOGE("We can't find target in MOWNED dev list");
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        uuid_3 = _convert_uuid(con->subject_1);
        subject_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_3);
        if (!subject_dev_1) {
                MA_LOGE("We can't find subject in MOWNED dev list");
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        uuid_4 = _convert_uuid(con->subject_2);
        subject_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_4);
        if (!subject_dev_2) {
                MA_LOGE("We can't find subject in MOWNED dev list");
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        g_client->g_doneCB = false;
@@ -1633,7 +1761,7 @@ static gpointer _provisioning_pairwise_func(gpointer data)
                                                           con->rsrc_interface_1, con->permission_1);
        if (NULL == acl_1) {
                MA_LOGE( "Failed to create ACL for %s", con->rsrc_uri);
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        acl_2 = _create_target_acl(&target_dev_2->doxm->deviceID,
@@ -1642,7 +1770,7 @@ static gpointer _provisioning_pairwise_func(gpointer data)
                                                           con->rsrc_interface_2, con->permission_2);
        if (NULL == acl_2) {
                MA_LOGE( "Failed to create ACL for %s", con->rsrc_uri);
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        ret = OCProvisionPairwiseDevices((void*) g_client, SYMMETRIC_PAIR_WISE_KEY,
@@ -1651,18 +1779,18 @@ static gpointer _provisioning_pairwise_func(gpointer data)
        if (OC_STACK_OK != ret) {
                MA_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret,
                                   _error_to_string(ret));
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
 
        ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S);
        if (ret) {
                MA_LOGE( "OCProvisionPairwise Faild = %d", errno);
-               goto PVACL_ERROR;
+               goto PVPAIRWIE_ERROR;
        }
        /* display the pairwise-provisioned result */
-       MA_LOGI(" Provision pairwise Successfully");
+       MA_LOGI(" Provision pairwise Done");
 
-PVACL_ERROR:
+PVPAIRWIE_ERROR:
        /* Notify ACL result  */
        net_ma_emit_pairwise_done(ma_dbus_get_object(), (int)ret);
 
@@ -1722,26 +1850,13 @@ static int _provisioning_pairwise_link(ma_service *service, gchar *target_1, gch
        }
        g_thread_unref(con->thread);
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb,
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb,
                                                                         con);
 #endif
        return MA_ERROR_NONE;
 }
 
 
-static void _provision_cred_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error)
-{
-       ma_subowner_s *client = (ma_subowner_s *)ctx;
-
-       if (!has_error)  {
-               MA_LOGD("Provision ACL SUCCEEDED");
-       } else {
-               MA_LOGD( "Provision ACL FAILED ");
-               _print_result_list((const OCProvisionResult_t*) arr, nOfRes);
-       }
-       client->g_doneCB = true;
-}
-
 static void _unlink_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error)
 {
        ma_subowner_s *client = (ma_subowner_s *)ctx;
@@ -1759,35 +1874,35 @@ static gpointer _unlink_provisioning_func(gpointer data)
 {
        OCStackResult ret = OC_STACK_OK;
        ma_req_cb_s *con = (ma_req_cb_s *)data;
+       ma_service *service = (ma_service *)con->userdata;
 
-       OicUuid_t *uuid = NULL;
+       OicUuid_t *uuid1 = NULL;
+       OicUuid_t *uuid2 = NULL;
        OCProvisionDev_t *dev1 = NULL;
        OCProvisionDev_t *dev2 = NULL;
 
        if (!con->uuid_dev1 ) {
                MA_LOGE("dev1 is NULL");
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
        if (!con->uuid_dev2) {
                MA_LOGE("dev2 is NULL");
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
 
-       uuid = _convert_uuid(con->uuid_dev1);
-       dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
+       uuid1 = _convert_uuid(con->uuid_dev1);
+       dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid1);
        if (!dev1) {
                MA_LOGE("We can't find in MOWNED dev list");
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
-       g_free(uuid);
 
-       uuid = _convert_uuid(con->uuid_dev2);
-       dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
+       uuid2 = _convert_uuid(con->uuid_dev2);
+       dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid2);
        if (!dev2) {
                MA_LOGE("We can't find in MOWNED dev list");
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
-       g_free(uuid);
 
        /* To do ACL provisioning only one */
        dev1->next = NULL;
@@ -1802,26 +1917,26 @@ static gpointer _unlink_provisioning_func(gpointer data)
 
        if (OC_STACK_OK != ret)  {
                MA_LOGD( "OCUnlinkDevices API error: %d (%s)", ret, _error_to_string(ret));
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
 
        ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S);
        if (ret)  {
                MA_LOGE( "OCUnlinkDevices Faild = %d", errno);
-               goto PVPWS_ERROR;
+               goto PVUNLINKPAIRWISE_ERROR;
        }
 
        /* display the pairwise-provisioned result */
        MA_LOGI("Unlink Devices");
 
-       return 0;
-
-PVPWS_ERROR:
+PVUNLINKPAIRWISE_ERROR:
        /* Notify ACL result  */
        net_ma_emit_unpair_done(ma_dbus_get_object(), (int)ret);
 
-       if (uuid)
-               g_free(uuid);
+       if (uuid1)
+               g_free(uuid1);
+       if (uuid2)
+               g_free(uuid2);
        _request_cleanup(con);
 
        g_thread_exit(GINT_TO_POINTER (1));
@@ -1829,118 +1944,6 @@ PVPWS_ERROR:
        return NULL;
 }
 
-static gpointer _cred_provisioning_func(gpointer data)
-{
-       OCStackResult ret = OC_STACK_OK;
-       ma_req_cb_s *con = (ma_req_cb_s *)data;
-
-       OicUuid_t *uuid = NULL;
-       OCProvisionDev_t *dev1 = NULL;
-       OCProvisionDev_t *dev2 = NULL;
-
-       if (!con->uuid_dev1 ) {
-               MA_LOGE("dev1 is NULL");
-               goto PVPWS_ERROR;
-       }
-       if (!con->uuid_dev2) {
-               MA_LOGE("dev2 is NULL");
-               goto PVPWS_ERROR;
-       }
-
-       uuid = _convert_uuid(con->uuid_dev1);
-       dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
-       if (!dev1) {
-               MA_LOGE("We can't find in MOWNED dev list");
-               goto PVPWS_ERROR;
-       }
-       g_free(uuid);
-
-       uuid = _convert_uuid(con->uuid_dev2);
-       dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid);
-       if (!dev2) {
-               MA_LOGE("We can't find in MOWNED dev list");
-               goto PVPWS_ERROR;
-       }
-       g_free(uuid);
-
-       /* To do ACL provisioning only one */
-       dev1->next = NULL;
-
-       /* To do ACL provisioning only one */
-       dev2->next = NULL;
-
-       g_client->g_doneCB = false;
-
-       MA_LOGI("Provisioning Selected Pairwise Devices..");
-       ret = OCProvisionCredentials((void*) g_client,
-               SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_256,
-               dev1, dev2, _provision_cred_cb);
-       if (OC_STACK_OK != ret)  {
-               MA_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, _error_to_string(ret));
-               goto PVPWS_ERROR;
-       }
-
-       ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S);
-       if (ret)  {
-               MA_LOGE( "OCProvisionCredentials Faild = %d", errno);
-               goto PVPWS_ERROR;
-       }
-
-       /* display the pairwise-provisioned result */
-       MA_LOGI("Provisioned Selected Pairwise Devices");
-
-       return 0;
-
-PVPWS_ERROR:
-       /* Notify ACL result  */
-       net_ma_emit_cred_done(ma_dbus_get_object(), (int)ret);
-
-       if (uuid)
-               g_free(uuid);
-       _request_cleanup(con);
-
-       g_thread_exit(GINT_TO_POINTER (1));
-
-       return NULL;
-}
-
-static int _provisioning_cred(ma_service *service,
-       gchar *uuid_dev1, gchar *uuid_dev2)
-{
-       ma_req_cb_s *con = NULL;
-       con = g_malloc0(sizeof(ma_req_cb_s));
-       if (!con) {
-               MA_LOGE( "g_malloc0() Fail=%d", errno);
-               /* Unset d2ds status 'pending' */
-               g_atomic_int_set(&service->pending, 0);
-               return MA_ERROR_OUT_OF_MEMORY;
-       }
-
-       con->userdata = service;
-       con->cid = MA_PROV_CRED;
-
-       if (!uuid_dev1 ||!uuid_dev2) {
-               MA_LOGE( "Invaild Prameters");
-               _request_cleanup(con);
-               return MA_ERROR_INVALID_PARAMETER;
-       }
-
-       con->uuid_dev1 = g_strdup(uuid_dev1);
-       con->uuid_dev2 = g_strdup(uuid_dev2);
-
-       con->thread = g_thread_try_new("prov_cred", _cred_provisioning_func, con, NULL);
-       if (!con->thread) {
-               MA_LOGE("Failed to create thread");
-               _request_cleanup(con);
-               return MA_ERROR_OUT_OF_MEMORY;
-       }
-       g_thread_unref(con->thread);
-#ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con);
-#endif
-       return MA_ERROR_NONE;
-}
-
 static int _provisioning_unlink(ma_service *service,
        gchar *uuid_dev1, gchar *uuid_dev2)
 {
@@ -1974,7 +1977,7 @@ static int _provisioning_unlink(ma_service *service,
        }
        g_thread_unref(con->thread);
 #ifdef TIMEOUT_USED
-       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb,
+       con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S * 2, _ma_timeout_cb,
                                                                         con);
 #endif
        return MA_ERROR_NONE;