diff options
Diffstat (limited to 'src/mds-registry')
-rw-r--r-- | src/mds-registry/mds-registry.c | 44 | ||||
-rw-r--r-- | src/mds-registry/reexec.c | 20 | ||||
-rw-r--r-- | src/mds-registry/registry.c | 103 | ||||
-rw-r--r-- | src/mds-registry/registry.h | 2 | ||||
-rw-r--r-- | src/mds-registry/slave.c | 98 | ||||
-rw-r--r-- | src/mds-registry/util.c | 10 |
6 files changed, 128 insertions, 149 deletions
diff --git a/src/mds-registry/mds-registry.c b/src/mds-registry/mds-registry.c index 741423c..4b49525 100644 --- a/src/mds-registry/mds-registry.c +++ b/src/mds-registry/mds-registry.c @@ -66,7 +66,7 @@ int preinitialise_server(void) return 0; - pfail: + fail: xperror(*argv); if (stage >= 1) pthread_mutex_destroy(&slave_mutex); if (stage >= 2) pthread_cond_destroy(&slave_cond); @@ -82,6 +82,7 @@ int preinitialise_server(void) */ int initialise_server(void) { + int stage = 0; const char* const message = "Command: intercept\n" "Message ID: 0\n" @@ -103,23 +104,20 @@ int initialise_server(void) that happen between the crash and the recovery. */ - if (full_send(message, strlen(message))) - return 1; - if (hash_table_create_tuned(®_table, 32)) - { - xperror(*argv); - hash_table_destroy(®_table, NULL, NULL); - return 1; - } + fail_if (full_send(message, strlen(message))); stage++; + fail_if (hash_table_create_tuned(®_table, 32)); stage++; reg_table.key_comparator = (compare_func*)string_comparator; reg_table.hasher = (hash_func*)string_hash; fail_if (server_initialised() < 0); fail_if (mds_message_initialise(&received)); return 0; - pfail: + fail: xperror(*argv); - mds_message_destroy(&received); + if (stage == 1) + hash_table_destroy(®_table, NULL, NULL); + if (stage == 2) + mds_message_destroy(&received); return 1; } @@ -135,13 +133,12 @@ int postinitialise_server(void) if (connected) return 0; - if (reconnect_to_display()) - { - mds_message_destroy(&received); - return 1; - } + fail_if (reconnect_to_display()); connected = 1; return 0; + fail: + mds_message_destroy(&received); + return 1; } @@ -171,27 +168,26 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; - else if (errno != ECONNRESET) - goto pfail; + else + fail_if (errno != ECONNRESET); eprint("lost connection to server."); mds_message_destroy(&received); mds_message_initialise(&received); connected = 0; - if (reconnect_to_display()) - goto fail; + fail_if (reconnect_to_display()); connected = 1; } rc = 0; - goto fail; - pfail: - xperror(*argv); + goto done; fail: + xperror(*argv); + done: /* Join with all slaves threads. */ with_mutex (slave_mutex, while (running_slaves > 0) diff --git a/src/mds-registry/reexec.c b/src/mds-registry/reexec.c index 6247e18..6f6510e 100644 --- a/src/mds-registry/reexec.c +++ b/src/mds-registry/reexec.c @@ -197,25 +197,15 @@ int unmarshal_server(char* state_buf) } return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); if (stage >= 1) hash_table_destroy(®_table, (free_func*)reg_table_free_key, (free_func*)reg_table_free_value); - if (stage >= 2) - free(command); - if (stage >= 3) - { - client_list_destroy(list); - free(list); - } - if (stage >= 5) - linked_list_destroy(&slave_list); - if (stage >= 6) - { - slave_destroy(slave); - free(slave); - } + if (stage >= 2) free(command); + if (stage >= 3) client_list_destroy(list), free(list); + if (stage >= 5) linked_list_destroy(&slave_list); + if (stage >= 6) slave_destroy(slave), free(slave); abort(); return -1; } 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(®_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(®_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(®_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; } diff --git a/src/mds-registry/registry.h b/src/mds-registry/registry.h index 0de4d6f..f01b383 100644 --- a/src/mds-registry/registry.h +++ b/src/mds-registry/registry.h @@ -22,7 +22,7 @@ /** * 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); diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 3825472..ef46963 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -55,13 +55,14 @@ static int slave_notify_client(slave_t* slave) while (left > 0) { sent = send_message(socket_fd, buf + ptr, left); - if ((sent < left) && errno && (errno != EINTR)) - return -1; + fail_if ((sent < left) && errno && (errno != EINTR)); left -= sent; ptr += sent; } return 0; + fail: + return -1; } @@ -114,7 +115,7 @@ static void* slave_loop(void* data) goto done; - pfail: + fail: xperror(*argv); done: with_mutex (slave_mutex, @@ -136,14 +137,12 @@ static void* slave_loop(void* data) */ int start_created_slave(slave_t* restrict slave) { - if ((errno = pthread_mutex_lock(&slave_mutex))) - return -1; + int locked = 0; - if ((errno = pthread_create(&(slave->thread), NULL, slave_loop, (void*)(intptr_t)slave))) - { - pthread_mutex_unlock(&slave_mutex); - return -1; - } + fail_if ((errno = pthread_mutex_lock(&slave_mutex))); + locked = 1; + + fail_if ((errno = pthread_create(&(slave->thread), NULL, slave_loop, (void*)(intptr_t)slave))); if ((errno = pthread_detach(slave->thread))) xperror(*argv); @@ -152,6 +151,10 @@ int start_created_slave(slave_t* restrict slave) pthread_mutex_unlock(&slave_mutex); return 0; + fail: + if (locked) + pthread_mutex_unlock(&slave_mutex); + return -1; } @@ -168,29 +171,28 @@ int start_slave(hash_table_t* restrict wait_set, const char* restrict recv_clien slave_t* slave = slave_create(wait_set, recv_client_id, recv_message_id); size_t slave_address, i; ssize_t node = LINKED_LIST_UNUSED; + int locked = 0; fail_if (slave == NULL); fail_if ((errno = pthread_mutex_lock(&slave_mutex))); + locked = 1; slave_address = (size_t)(void*)slave; slave->node = node = linked_list_insert_end(&slave_list, slave_address); - if (slave->node == LINKED_LIST_UNUSED) - goto pfail_in_mutex; + fail_if (slave->node == LINKED_LIST_UNUSED); for (i = 0; i < received.header_count; i++) if (startswith(received.headers[i], "Time to live: ")) { const char* ttl = received.headers[i] + strlen("Time to live: "); slave->timed = 1; - if (monotone(&(slave->dethklok))) - goto pfail_in_mutex; + fail_if (monotone(&(slave->dethklok))); slave->dethklok.tv_sec += (time_t)atoll(ttl); - /* It should really be `atol`, but we want to be future proof. */ + /* It should really be `atol`, but we want to be future-proof. */ break; } - if ((errno = pthread_create(&(slave->thread), NULL, slave_loop, (void*)(intptr_t)slave))) - goto pfail_in_mutex; + fail_if ((errno = pthread_create(&(slave->thread), NULL, slave_loop, (void*)(intptr_t)slave))); if ((errno = pthread_detach(slave->thread))) xperror(*argv); @@ -199,13 +201,10 @@ int start_slave(hash_table_t* restrict wait_set, const char* restrict recv_clien pthread_mutex_unlock(&slave_mutex); return 0; - pfail: - xperror(*argv); - goto more_fail; - pfail_in_mutex: + fail: xperror(*argv); - pthread_mutex_unlock(&slave_mutex); - more_fail: + if (locked) + pthread_mutex_unlock(&slave_mutex); if (node != LINKED_LIST_UNUSED) linked_list_remove(&slave_list, node); return -1; @@ -243,8 +242,7 @@ int advance_slaves(char* command) int signal_slaves = 0; ssize_t node; - if ((errno = pthread_mutex_lock(&slave_mutex))) - return -1; + fail_if ((errno = pthread_mutex_lock(&slave_mutex))); foreach_linked_list_node (slave_list, node) { @@ -261,6 +259,8 @@ int advance_slaves(char* command) pthread_mutex_unlock(&slave_mutex); return 0; + fail: + return -1; } @@ -275,26 +275,23 @@ int advance_slaves(char* command) slave_t* slave_create(hash_table_t* restrict wait_set, const char* restrict recv_client_id, const char* restrict recv_message_id) { slave_t* restrict rc = NULL; + int saved_errno; - if (xmalloc(rc, 1, slave_t)) - return NULL; + fail_if (xmalloc(rc, 1, slave_t)); slave_initialise(rc); rc->wait_set = wait_set; rc->client = parse_client_id(recv_client_id); - if ((rc->client_id = strdup(recv_client_id)) == NULL) - goto fail; - - if ((rc->message_id = strdup(recv_message_id)) == NULL) - goto fail; + fail_if ((rc->client_id = strdup(recv_client_id)) == NULL); + fail_if ((rc->message_id = strdup(recv_message_id)) == NULL); return rc; fail: - slave_destroy(rc); - free(rc); - return NULL; + saved_errno = errno; + slave_destroy(rc), free(rc); + return errno = saved_errno, NULL; } @@ -322,6 +319,9 @@ void slave_initialise(slave_t* restrict this) */ void slave_destroy(slave_t* restrict this) { + if (this == NULL) + return; + if (this->wait_set != NULL) { hash_table_destroy(this->wait_set, (free_func*)reg_table_free_key, NULL); @@ -414,7 +414,8 @@ size_t slave_marshal(const slave_t* restrict this, char* restrict data) size_t slave_unmarshal(slave_t* restrict this, char* restrict data) { size_t key, n, m, rc = 2 * sizeof(int) + sizeof(ssize_t) + sizeof(size_t) + sizeof(uint64_t); - char* protocol; + char* protocol = NULL; + int saved_errno; this->wait_set = NULL; this->client_id = NULL; @@ -431,42 +432,37 @@ size_t slave_unmarshal(slave_t* restrict this, char* restrict data) buf_get_next(data, long, this->dethklok.tv_nsec); n = (strlen((char*)data) + 1) * sizeof(char); - if ((this->client_id = malloc(n)) == NULL) - return 0; + fail_if ((this->client_id = malloc(n)) == NULL); memcpy(this->client_id, data, n); data += n, rc += n; n = (strlen((char*)data) + 1) * sizeof(char); - if ((this->message_id = malloc(n)) == NULL) - return 0; + fail_if ((this->message_id = malloc(n)) == NULL); memcpy(this->message_id, data, n); data += n, rc += n; - if ((this->wait_set = malloc(sizeof(hash_table_t))) == NULL) - return 0; - if (hash_table_create(this->wait_set)) - return 0; + fail_if ((this->wait_set = malloc(sizeof(hash_table_t))) == NULL); + fail_if (hash_table_create(this->wait_set)); buf_get_next(data, size_t, m); while (m--) { n = (strlen((char*)data) + 1) * sizeof(char); - if ((protocol = malloc(n)) == NULL) - return 0; + fail_if ((protocol = malloc(n)) == NULL); memcpy(protocol, data, n); data += n, rc += n; key = (size_t)(void*)protocol; if (hash_table_put(this->wait_set, key, 1) == 0) - if (errno) - { - free(protocol); - return 0; - } + fail_if (errno); } return rc; + fail: + saved_errno = errno; + free(protocol); + return errno = saved_errno, (size_t)0; } diff --git a/src/mds-registry/util.c b/src/mds-registry/util.c index 1963eeb..6b383c2 100644 --- a/src/mds-registry/util.c +++ b/src/mds-registry/util.c @@ -76,14 +76,14 @@ int full_send(const char* message, size_t length) eprint("Sent more of a message than exists in the message, aborting."); return -1; } - else if ((sent < length) && (errno != EINTR)) - { - xperror(*argv); - return -1; - } + else + fail_if ((sent < length) && (errno != EINTR)); message += sent; length -= sent; } return 0; + fail: + xperror(*argv); + return -1; } |