aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-registry/registry.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-12-09 13:15:10 +0100
committerMattias Andrée <maandree@operamail.com>2014-12-09 13:15:10 +0100
commit7a994c9631c590d6a73c842fa5d2d3567b4771dd (patch)
tree228ade01f99ba217151a0ed20f29c5f3a5028619 /src/mds-registry/registry.c
parentmds-kbdc: compile-layout: macro_call: fix bug: do not duplicate the arguments if there are none (diff)
parentreport an error, rather than causing failure the caller but not for the called function (diff)
downloadmds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.gz
mds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.bz2
mds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.xz
merge track-errors and resolve conflict
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/mds-registry/registry.c103
1 files changed, 50 insertions, 53 deletions
diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c
index 2d0643f..f853f93 100644
--- a/src/mds-registry/registry.c
+++ b/src/mds-registry/registry.c
@@ -74,8 +74,7 @@ static int handle_close_message(void)
/* If no servers support the protocol, list the protocol for removal. */
fail_if ((keys == NULL) && xmalloc(keys, size, size_t));
- if (ptr == size ? growalloc(old_keys, keys, size, size_t) : 0)
- goto fail;
+ fail_if (ptr == size ? growalloc(old_keys, keys, size, size_t) : 0);
keys[ptr++] = entry->key;
}
@@ -108,9 +107,8 @@ static int handle_close_message(void)
free(keys);
return 0;
- pfail:
- xperror(*argv);
fail:
+ xperror(*argv);
free(keys);
return -1;
}
@@ -127,13 +125,14 @@ static int handle_close_message(void)
*/
static int registry_action_add(int has_key, char* command, size_t command_key, uint64_t client)
{
+ int saved_errno;
+
if (has_key)
{
/* Add server to protocol if the protocol is already in the table. */
size_t address = hash_table_get(&reg_table, command_key);
client_list_t* list = (client_list_t*)(void*)address;
- if (client_list_add(list, client) < 0)
- goto pfail;
+ fail_if (client_list_add(list, client) < 0);
}
else
{
@@ -142,13 +141,12 @@ static int registry_action_add(int has_key, char* command, size_t command_key, u
/* Allocate list of servers for the protocol. */
client_list_t* list = malloc(sizeof(client_list_t));
void* address = list;
- if (list == NULL)
- goto pfail;
+ fail_if (list == NULL);
/* Duplicate the protocol name so it can be accessed later. */
if ((command = strdup(command)) == NULL)
{
- free(list);
- goto pfail;
+ saved_errno = errno, free(list), errno = saved_errno;
+ fail_if (1);
}
/* Create list of servers, add server to list and add the protocol to the table. */
command_key = (size_t)(void*)command;
@@ -156,10 +154,12 @@ static int registry_action_add(int has_key, char* command, size_t command_key, u
client_list_add(list, client) ||
(hash_table_put(&reg_table, command_key, (size_t)address) == 0))
{
+ saved_errno = errno;
client_list_destroy(list);
free(list);
free(command);
- goto pfail;
+ errno = saved_errno;
+ fail_if (1);
}
}
@@ -167,7 +167,7 @@ static int registry_action_add(int has_key, char* command, size_t command_key, u
fail_if (advance_slaves(command));
return 0;
- pfail:
+ fail:
xperror(*argv);
return -1;
}
@@ -214,35 +214,34 @@ static int registry_action_act(char* command, int action, uint64_t client, hash_
{
size_t command_key = (size_t)(void*)command;
int has_key = hash_table_contains_key(&reg_table, command_key);
+ int saved_errno;
if (action == 1)
{
/* Register server to protocol. */
- if (registry_action_add(has_key, command, command_key, client))
- return -1;
+ fail_if (registry_action_add(has_key, command, command_key, client));
}
else if ((action == -1) && has_key)
/* Unregister server from protocol. */
registry_action_remove(command_key, client);
else if ((action == 0) && !has_key)
{
- /* Add protocl to wait set of not present in the protocol table. */
- if ((command = strdup(command)) == NULL)
- goto pfail_wait;
+ /* Add protocol to wait set of not present in the protocol table. */
+ fail_if ((command = strdup(command)) == NULL);
command_key = (size_t)(void*)command;
if (hash_table_put(wait_set, command_key, 1) == 0)
if (errno)
{
- free(command);
- goto pfail_wait;
+ saved_errno = errno, free(command), errno = saved_errno;
+ fail_if (1);
}
}
return 0;
- pfail_wait:
+ fail:
xperror(*argv);
- hash_table_destroy(wait_set, (free_func*)reg_table_free_key, NULL);
- free(wait_set);
+ if (action != 1)
+ hash_table_destroy(wait_set, (free_func*)reg_table_free_key, NULL), free(wait_set);
return -1;
}
@@ -264,21 +263,15 @@ static int registry_action(size_t length, int action, const char* recv_client_id
uint64_t client = action ? parse_client_id(recv_client_id) : 0;
hash_table_t* wait_set = NULL;
size_t begin;
+ int saved_errno;
/* If ‘Action: wait’, create a set for the protocols that are not already available. */
if (action == 0)
{
- wait_set = malloc(sizeof(hash_table_t));
- if (wait_set == NULL)
- return -1;
- if (hash_table_create(wait_set))
- {
- hash_table_destroy(wait_set, NULL, NULL);
- free(wait_set);
- return -1;
- }
+ fail_if (xmalloc(wait_set, 1, hash_table_t));
+ fail_if (hash_table_create(wait_set));
wait_set->key_comparator = (compare_func*)string_comparator;
wait_set->hasher = (hash_func*)string_hash;
}
@@ -288,14 +281,8 @@ static int registry_action(size_t length, int action, const char* recv_client_id
if (received.payload_size == length)
{
- if (growalloc(old, received.payload, received.payload_size, char))
- {
- if (wait_set != NULL)
- hash_table_destroy(wait_set, NULL, NULL), free(wait_set);
- return -1;
- }
- else
- payload = received.payload;
+ fail_if (growalloc(old, received.payload, received.payload_size, char));
+ payload = received.payload;
}
@@ -318,16 +305,22 @@ static int registry_action(size_t length, int action, const char* recv_client_id
if (len > 0)
if (registry_action_act(command, action, client, wait_set))
- return -1;
+ fail_if (wait_set = NULL, 1);
}
/* If ‘Action: wait’, start a new thread that waits for the protocols and the responds. */
if (action == 0)
- return start_slave(wait_set, recv_client_id, recv_message_id);
+ if (start_slave(wait_set, recv_client_id, recv_message_id))
+ fail_if (wait_set = NULL, 1);
return 0;
+ fail:
+ saved_errno = errno;
+ if (wait_set != NULL)
+ hash_table_destroy(wait_set, NULL, NULL), free(wait_set);
+ return errno = saved_errno, -1;
}
@@ -336,7 +329,7 @@ static int registry_action(size_t length, int action, const char* recv_client_id
*
* @param recv_client_id The ID of the client
* @param recv_message_id The ID of the received message
- * @return Zero on success -1 on error or interruption,
+ * @return Zero on success, -1 on error or interruption,
* `errno` will be set accordingly
*/
static int list_registry(const char* recv_client_id, const char* recv_message_id)
@@ -349,8 +342,7 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id
if (send_buffer_size == 0)
{
- if (xmalloc(send_buffer, 256, char))
- return -1;
+ fail_if (xmalloc(send_buffer, 256, char));
send_buffer_size = 256;
}
@@ -365,8 +357,7 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id
/* Make sure the send buffer can fit all protocols. */
while (ptr + len + 1 >= send_buffer_size)
- if (growalloc(old, send_buffer, send_buffer_size, char))
- return -1;
+ fail_if (growalloc(old, send_buffer, send_buffer_size, char));
memcpy(send_buffer + ptr, command, len * sizeof(char));
ptr += len;
@@ -380,8 +371,7 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id
i += strlen("To: %s\nIn response to: %s\nMessage ID: %" PRIu32 "\nLength: %" PRIu64 "\n\n");
while (ptr + i >= send_buffer_size)
- if (growalloc(old, send_buffer, send_buffer_size, char))
- return -1;
+ fail_if (growalloc(old, send_buffer, send_buffer_size, char));
/* Construct message headers. */
@@ -392,9 +382,10 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id
with_mutex (slave_mutex, message_id = message_id == UINT32_MAX ? 0 : (message_id + 1););
/* Send message. */
- if (full_send(send_buffer + ptr, strlen(send_buffer + ptr)))
- return 1;
+ fail_if (full_send(send_buffer + ptr, strlen(send_buffer + ptr)));
return full_send(send_buffer, ptr);
+ fail:
+ return -1;
}
@@ -488,7 +479,7 @@ static int handle_register_message(void)
/**
* Handle the received message
*
- * @return Zero on success -1 on error or interruption,
+ * @return Zero on success, -1 on error or interruption,
* `errno` will be set accordingly
*/
int handle_message(void)
@@ -496,7 +487,13 @@ int handle_message(void)
size_t i;
for (i = 0; i < received.header_count; i++)
if (strequals(received.headers[i], "Command: register"))
- return handle_register_message();
- return handle_close_message();
+ {
+ fail_if (handle_register_message());
+ return 0;
+ }
+ fail_if (handle_close_message());
+ return 0;
+ fail:
+ return -1;
}