From 04f2fb574bd7a2424544ee53a991221b34219c6f Mon Sep 17 00:00:00 2001 From: Ismo Puustinen Date: Thu, 11 Apr 2013 13:28:12 +0300 Subject: [PATCH] resource-c: fix a memory corruption. --- .../resource-native/libmurphy-resource/message.c | 33 ++++++++-------- .../resource-native/libmurphy-resource/resource.c | 10 +++-- .../resource-native/libmurphy-resource/rset.c | 45 +++++++++++++++------- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/plugins/resource-native/libmurphy-resource/message.c b/src/plugins/resource-native/libmurphy-resource/message.c index 0db38d6..dcabcea 100644 --- a/src/plugins/resource-native/libmurphy-resource/message.c +++ b/src/plugins/resource-native/libmurphy-resource/message.c @@ -445,20 +445,20 @@ int acquire_resource_set_request(mrp_res_context_t *cx, mrp_msg_t *msg = NULL; if (!cx->priv->connected) - goto error; - - rset->priv->seqno = cx->priv->next_seqno; + return -1; msg = mrp_msg_create( - RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno++, + RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno, RESPROTO_REQUEST_TYPE, MRP_MSG_FIELD_UINT16, RESPROTO_ACQUIRE_RESOURCE_SET, RESPROTO_RESOURCE_SET_ID, MRP_MSG_FIELD_UINT32, rset->priv->id, RESPROTO_MESSAGE_END); if (!msg) - goto error; + return -1; + rset->priv->seqno = cx->priv->next_seqno; + cx->priv->next_seqno++; if (!mrp_transport_send(cx->priv->transp, msg)) goto error; @@ -478,19 +478,20 @@ int release_resource_set_request(mrp_res_context_t *cx, mrp_msg_t *msg = NULL; if (!cx->priv->connected) - goto error; - - rset->priv->seqno = cx->priv->next_seqno; + return -1; msg = mrp_msg_create( - RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno++, + RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno, RESPROTO_REQUEST_TYPE, MRP_MSG_FIELD_UINT16, RESPROTO_RELEASE_RESOURCE_SET, RESPROTO_RESOURCE_SET_ID, MRP_MSG_FIELD_UINT32, rset->priv->id, RESPROTO_MESSAGE_END); if (!msg) - goto error; + return -1; + + rset->priv->seqno = cx->priv->next_seqno; + cx->priv->next_seqno++; if (!mrp_transport_send(cx->priv->transp, msg)) goto error; @@ -514,13 +515,10 @@ int create_resource_set_request(mrp_res_context_t *cx, return -1; if (!cx->priv->connected) - goto error; - - rset->priv->seqno = cx->priv->next_seqno; - + return -1; msg = mrp_msg_create( - RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno++, + RESPROTO_SEQUENCE_NO, MRP_MSG_FIELD_UINT32, cx->priv->next_seqno, RESPROTO_REQUEST_TYPE, MRP_MSG_FIELD_UINT16, RESPROTO_CREATE_RESOURCE_SET, RESPROTO_RESOURCE_FLAGS, MRP_MSG_FIELD_UINT32, 0, @@ -530,7 +528,10 @@ int create_resource_set_request(mrp_res_context_t *cx, RESPROTO_MESSAGE_END); if (!msg) - goto error; + return -1; + + rset->priv->seqno = cx->priv->next_seqno; + cx->priv->next_seqno++; for (i = 0; i < rset->priv->num_resources; i++) { int j; diff --git a/src/plugins/resource-native/libmurphy-resource/resource.c b/src/plugins/resource-native/libmurphy-resource/resource.c index 8229a50..34ef7dc 100644 --- a/src/plugins/resource-native/libmurphy-resource/resource.c +++ b/src/plugins/resource-native/libmurphy-resource/resource.c @@ -103,7 +103,7 @@ static void resource_event(mrp_msg_t *msg, uint32_t i; mrp_res_resource_set_t *rset; - mrp_log_info("Resource event (request no %u):", seqno); + printf("Resource event (request no %u):\n", seqno); if (!fetch_resource_set_id(msg, pcursor, &rset_id) || !fetch_resource_set_state(msg, pcursor, &state) || @@ -114,6 +114,7 @@ static void resource_event(mrp_msg_t *msg, } /* Update our "master copy" of the resource set. */ + printf("rset_id = %d\n", rset_id); rset = mrp_htbl_lookup(cx->priv->rset_mapping, u_to_p(rset_id)); @@ -193,7 +194,7 @@ static void resource_event(mrp_msg_t *msg, #endif } - mrp_log_error("advice = 0x%08x, grant = 0x%08x, mandatory = 0x%08x, all = 0x%08x", + mrp_log_info("advice = 0x%08x, grant = 0x%08x, mandatory = 0x%08x, all = 0x%08x", advice, grant, mandatory, all); if (grant) { @@ -314,7 +315,6 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, goto error; } - /* TODO: make new aqcuires fail until seqno == 0 */ rset->priv->seqno = 0; /* call the resource set callback */ @@ -429,7 +429,9 @@ static void htbl_free_rset_mapping(void *key, void *object) #else MRP_UNUSED(key); #endif - free_resource_set(object); + + mrp_res_resource_set_t *rset = object; + free_resource_set(rset); } /* public API */ diff --git a/src/plugins/resource-native/libmurphy-resource/rset.c b/src/plugins/resource-native/libmurphy-resource/rset.c index e8f0883..d742ce6 100644 --- a/src/plugins/resource-native/libmurphy-resource/rset.c +++ b/src/plugins/resource-native/libmurphy-resource/rset.c @@ -375,6 +375,7 @@ static mrp_res_resource_set_t *create_resource_set( return rs; error: + mrp_log_error("error creating resource set"); delete_resource_set(cx, rs); return NULL; } @@ -426,6 +427,7 @@ static int update_library_resource_set(mrp_res_context_t *cx, return 0; error: + mrp_log_error("error updating library resource set"); mrp_free(application_class); for (i = 0; i < num_resources; i++) { free_resource(resources[i]); @@ -504,7 +506,7 @@ mrp_res_resource_t *mrp_res_create_resource(mrp_res_context_t *cx, return res; error: - mrp_log_error("mrp_res_create_resource error"); + mrp_log_error("error creating a resource"); free_resource(res); return NULL; @@ -534,6 +536,7 @@ mrp_res_resource_set_t *mrp_res_copy_resource_set(mrp_res_context_t *cx, return copy; error: + mrp_log_error("error copying a resource set"); free_resource_set(copy); return NULL; } @@ -572,8 +575,7 @@ int mrp_res_release_resource_set(mrp_res_context_t *cx, return release_resource_set_request(cx, internal_set); error: - mrp_log_error("mrp_release_resources error"); - + mrp_log_error("error releasing a resource set"); return -1; } @@ -713,7 +715,6 @@ mrp_res_resource_t * mrp_res_get_resource_by_name(mrp_res_context_t *cx, int mrp_res_acquire_resource_set(mrp_res_context_t *cx, const mrp_res_resource_set_t *original) { - mrp_msg_t *msg = NULL; mrp_res_resource_set_t *rset; if (!cx->priv->connected) { @@ -734,7 +735,6 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, print_resource_set(rset); if (rset->priv->id) { - /* the set has been already created on server */ if (rset->state == MRP_RES_RESOURCE_ACQUIRED) { @@ -751,21 +751,38 @@ int mrp_res_acquire_resource_set(mrp_res_context_t *cx, return acquire_resource_set_request(cx, rset); } } + else { + mrp_list_hook_t *p, *n; + mrp_res_resource_set_private_t *pending_rset; + bool found = FALSE; + + /* Create the resource set. The acquisition is continued + * when the set is created. */ + + /* only append if not already present in the list */ + + mrp_list_foreach(&cx->priv->pending_sets, p, n) { + pending_rset = mrp_list_entry(p, mrp_res_resource_set_private_t, hook); + if (pending_rset == rset->priv) { + found = TRUE; + break; + } + } - /* Create the resource set. The acquisition is continued - * when the set is created. */ + if (!found) { + mrp_list_append(&cx->priv->pending_sets, &rset->priv->hook); + } - if (create_resource_set_request(cx, rset) < 0) { - mrp_log_error("creating resource set failed"); - goto error; + if (create_resource_set_request(cx, rset) < 0) { + mrp_log_error("creating resource set failed"); + mrp_list_delete(&rset->priv->hook); + goto error; + } } - mrp_list_append(&cx->priv->pending_sets, &rset->priv->hook); - - mrp_msg_unref(msg); return 0; error: - mrp_msg_unref(msg); + mrp_log_error("error acquiring a resource set"); return -1; } -- 2.7.4