diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-09 13:15:10 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-09 13:15:10 +0100 |
commit | 7a994c9631c590d6a73c842fa5d2d3567b4771dd (patch) | |
tree | 228ade01f99ba217151a0ed20f29c5f3a5028619 /src/mds-server | |
parent | mds-kbdc: compile-layout: macro_call: fix bug: do not duplicate the arguments if there are none (diff) | |
parent | report an error, rather than causing failure the caller but not for the called function (diff) | |
download | mds-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 'src/mds-server')
-rw-r--r-- | src/mds-server/client.c | 34 | ||||
-rw-r--r-- | src/mds-server/interception-condition.c | 5 | ||||
-rw-r--r-- | src/mds-server/interceptors.c | 45 | ||||
-rw-r--r-- | src/mds-server/interceptors.h | 2 | ||||
-rw-r--r-- | src/mds-server/mds-server.c | 47 | ||||
-rw-r--r-- | src/mds-server/mds-server.h | 2 | ||||
-rw-r--r-- | src/mds-server/multicast.c | 8 | ||||
-rw-r--r-- | src/mds-server/receiving.c | 34 | ||||
-rw-r--r-- | src/mds-server/reexec.c | 39 | ||||
-rw-r--r-- | src/mds-server/slavery.c | 29 | ||||
-rw-r--r-- | src/mds-server/slavery.h | 2 |
11 files changed, 128 insertions, 119 deletions
diff --git a/src/mds-server/client.c b/src/mds-server/client.c index 99d561e..f72a2ce 100644 --- a/src/mds-server/client.c +++ b/src/mds-server/client.c @@ -82,16 +82,18 @@ int client_initialise_threading(client_t* restrict this) /* Create mutex to make sure two thread to not try to send messages concurrently, and other client local actions. */ - if ((errno = pthread_mutex_init(&(this->mutex), NULL))) return -1; + fail_if ((errno = pthread_mutex_init(&(this->mutex), NULL))); this->mutex_created = 1; /* Create mutex and codition for multicast interception replies. */ - if ((errno = pthread_mutex_init(&(this->modify_mutex), NULL))) return -1; + fail_if ((errno = pthread_mutex_init(&(this->modify_mutex), NULL))); this->modify_mutex_created = 1; - if ((errno = pthread_cond_init(&(this->modify_cond), NULL))) return -1; + fail_if ((errno = pthread_cond_init(&(this->modify_cond), NULL))); this->modify_cond_created = 1; return 0; + fail: + return -1; } @@ -203,6 +205,7 @@ size_t client_marshal(const client_t* restrict this, char* restrict data) size_t client_unmarshal(client_t* restrict this, char* restrict data) { size_t i, n, rc = sizeof(ssize_t) + 3 * sizeof(int) + sizeof(uint64_t) + 5 * sizeof(size_t); + int saved_errno, stage = 0; this->interception_conditions = NULL; this->multicasts = NULL; this->send_pending = NULL; @@ -218,40 +221,37 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) buf_get_next(data, uint64_t, this->id); buf_get_next(data, size_t, n); if (n > 0) - if (mds_message_unmarshal(&(this->message), data)) - return 0; + fail_if (mds_message_unmarshal(&(this->message), data)); + stage++; data += n / sizeof(char); rc += n; buf_get_next(data, size_t, this->interception_conditions_count); - if (xmalloc(this->interception_conditions, this->interception_conditions_count, interception_condition_t)) - goto fail; + fail_if (xmalloc(this->interception_conditions, + this->interception_conditions_count, interception_condition_t)); for (i = 0; i < this->interception_conditions_count; i++) { n = interception_condition_unmarshal(this->interception_conditions + i, data); if (n == 0) { this->interception_conditions_count = i - 1; - goto fail; + fail_if (1); } data += n / sizeof(char); rc += n; } buf_get_next(data, size_t, n); - if (xmalloc(this->multicasts, n, multicast_t)) - goto fail; + fail_if (xmalloc(this->multicasts, n, multicast_t)); for (i = 0; i < n; i++, this->multicasts_count++) { size_t m = multicast_unmarshal(this->multicasts + i, data); - if (m == 0) - goto fail; + fail_if (m == 0); data += m / sizeof(char); rc += m; } buf_get_next(data, size_t, this->send_pending_size); if (this->send_pending_size > 0) { - if (xmalloc(this->send_pending, this->send_pending_size, char)) - goto fail; + fail_if (xmalloc(this->send_pending, this->send_pending_size, char)); memcpy(this->send_pending, data, this->send_pending_size * sizeof(char)); data += this->send_pending_size; rc += this->send_pending_size * sizeof(char); @@ -265,6 +265,9 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) return rc; fail: + saved_errno = errno; + if (stage == 0) + goto done_failing; mds_message_destroy(&(this->message)); for (i = 0; i < this->interception_conditions_count; i++) free(this->interception_conditions[i].condition); @@ -278,7 +281,8 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) mds_message_destroy(this->modify_message); free(this->modify_message); } - return 0; + done_failing: + return errno = saved_errno, (size_t)0; } /** diff --git a/src/mds-server/interception-condition.c b/src/mds-server/interception-condition.c index 31905c9..cfe5d0c 100644 --- a/src/mds-server/interception-condition.c +++ b/src/mds-server/interception-condition.c @@ -71,10 +71,11 @@ size_t interception_condition_unmarshal(interception_condition_t* restrict this, buf_get_next(data, int64_t, this->priority); buf_get_next(data, int, this->modifying); n = (strlen(data) + 1) * sizeof(char); - if ((this->condition = malloc(n)) == NULL) - return 0; + fail_if ((this->condition = malloc(n)) == NULL); memcpy(this->condition, data, n); return sizeof(size_t) + sizeof(int64_t) + 2 * sizeof(int) + n; + fail: + return 0; } diff --git a/src/mds-server/interceptors.c b/src/mds-server/interceptors.c index 6d798d9..8a24e33 100644 --- a/src/mds-server/interceptors.c +++ b/src/mds-server/interceptors.c @@ -140,19 +140,10 @@ void add_intercept_condition(client_t* client, char* condition, int64_t priority else { /* Duplicate condition string. */ - if ((condition = strdup(condition)) == NULL) - { - xperror(*argv); - return; - } + fail_if ((condition = strdup(condition)) == NULL); /* Grow the interception condition list. */ - if (xrealloc(conds, n + 1, interception_condition_t)) - { - xperror(*argv); - free(condition); - return; - } + fail_if (xrealloc(conds, n + 1, interception_condition_t)); client->interception_conditions = conds; /* Store condition. */ client->interception_conditions_count++; @@ -174,6 +165,12 @@ void add_intercept_condition(client_t* client, char* condition, int64_t priority conds[n] = temp; } } + + return; + fail: + xperror(*argv); + free(condition); + return; } @@ -220,9 +217,7 @@ int find_matching_condition(client_t* client, size_t* hashes, char** keys, char* interception_condition_t* conds = client->interception_conditions; size_t n = 0, i; - errno = pthread_mutex_lock(&(mutex)); - if (errno) - return -1; + fail_if ((errno = pthread_mutex_lock(&(mutex)))); /* Look for a matching condition. */ if (client->open) @@ -240,6 +235,8 @@ int find_matching_condition(client_t* client, size_t* hashes, char** keys, char* pthread_mutex_unlock(&(mutex)); return i < n; + fail: + return -1; } @@ -252,23 +249,22 @@ int find_matching_condition(client_t* client, size_t* hashes, char** keys, char* * @param headers The header name–value pairs * @param count The number of accepted patterns * @param interceptions_count_out Slot at where to store the number of found interceptors - * @return The found interceptors, NULL on error + * @return The found interceptors, `NULL` on error */ queued_interception_t* get_interceptors(client_t* sender, size_t* hashes, char** keys, char** headers, size_t count, size_t* interceptions_count_out) { queued_interception_t* interceptions = NULL; - size_t interceptions_count = 0; - size_t n = 0; + size_t interceptions_count = 0, n = 0; ssize_t node; + int saved_errno; /* Count clients. */ foreach_linked_list_node (client_list, node) n++; /* Allocate interceptor list. */ - if (xmalloc(interceptions, n, queued_interception_t)) - return NULL; + fail_if (xmalloc(interceptions, n, queued_interception_t)); /* Search clients. */ foreach_linked_list_node (client_list, node) @@ -280,11 +276,7 @@ queued_interception_t* get_interceptors(client_t* sender, size_t* hashes, char** { int r = find_matching_condition(client, hashes, keys, headers, count, interceptions + interceptions_count); - if (r == -1) - { - free(interceptions); - return NULL; - } + fail_if (r == -1); if (r) /* List client of there was a matching condition. */ interceptions_count++; @@ -293,5 +285,10 @@ queued_interception_t* get_interceptors(client_t* sender, size_t* hashes, char** *interceptions_count_out = interceptions_count; return interceptions; + + fail: + saved_errno = errno; + free(interceptions); + return errno = saved_errno, NULL; } diff --git a/src/mds-server/interceptors.h b/src/mds-server/interceptors.h index 7bb60e5..e2e6041 100644 --- a/src/mds-server/interceptors.h +++ b/src/mds-server/interceptors.h @@ -77,7 +77,7 @@ int find_matching_condition(client_t* client, size_t* hashes, char** keys, char* * @param headers The header name–value pairs * @param count The number of accepted patterns * @param interceptions_count_out Slot at where to store the number of found interceptors - * @return The found interceptors, NULL on error + * @return The found interceptors, `NULL` on error */ queued_interception_t* get_interceptors(client_t* sender, size_t* hashes, char** keys, char** headers, size_t count, size_t* interceptions_count_out); diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index 58eec60..fabdffa 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -130,8 +130,7 @@ int preinitialise_server(void) close_files((fd > 2) || (fd == socket_fd)); /* Run mdsinitrc. */ - run_initrc(unparsed_args); - return 1; + run_initrc(unparsed_args); /* Does not return. */ } } @@ -148,7 +147,7 @@ int preinitialise_server(void) return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -302,7 +301,7 @@ void* slave_loop(void* data) if ((r == 0) && message_received(information)) goto terminate; else if (r == -2) - goto fail; + goto done; else if (r && (errno == EINTR) && terminating) goto terminate; /* Stop the thread if we are re-exec:ing or terminating the server. */ } @@ -329,7 +328,7 @@ void* slave_loop(void* data) if (reexecing) goto reexec; - fail: /* This done on success as well. */ + done: /* Close socket and free resources. */ close(slave_fd); free(msgbuf); @@ -348,9 +347,9 @@ void* slave_loop(void* data) return NULL; - pfail: + fail: xperror(*argv); - goto fail; + goto done; reexec: @@ -403,15 +402,13 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) uint64_t modify_id; char modify_id_header[13 + 3 * sizeof(uint64_t)]; void* new_buf; + int saved_errno; /* Count the number of headers. */ for (i = 0; i < n; i++) if (message[i] == '\n') - { - header_count++; - if (message[i + 1] == '\n') - break; - } + if (header_count++, message[i + 1] == '\n') + break; if (header_count == 0) return; /* Invalid message. */ @@ -435,14 +432,14 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) if ((header_values[i] = strdup(msg)) == NULL) { header_count = i; - goto pfail; + fail_if (1); } *colon = '\0'; if ((headers[i] = strdup(msg)) == NULL) { - free(headers[i]); + saved_errno = errno, free(headers[i]), errno = saved_errno; header_count = i; - goto pfail; + fail_if (1); } *colon = ':'; *end = '\n'; @@ -482,22 +479,22 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) multicast->message_prefix = n; message = NULL; +#define fail fail_in_mutex /* Queue message multicasting. */ with_mutex (sender->mutex, new_buf = sender->multicasts; - if (xrealloc(new_buf, sender->multicasts_count + 1, multicast_t)) - { - xperror(*argv); - goto fail_queue; - } + fail_if (xrealloc(new_buf, sender->multicasts_count + 1, multicast_t)); sender->multicasts = new_buf; sender->multicasts[sender->multicasts_count++] = *multicast; free(multicast); multicast = NULL; - fail_queue: + errno = 0; + fail_in_mutex: + xperror(*argv); ); +#undef fail - fail: /* This is done before this function returns even if there was no error. */ + done: /* Release resources. */ xfree(headers, header_count); xfree(header_values, header_count); @@ -508,9 +505,9 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) free(multicast); return; - pfail: + fail: xperror(*argv); - goto fail; + goto done; } @@ -582,5 +579,7 @@ void run_initrc(char** args) /* Everything failed. */ eprintf("unable to run %s file, you might as well kill me.", INITRC_FILE); + /* (‘me’ actually refers to the parant, whence it will to be coming.) */ + exit(0); } diff --git a/src/mds-server/mds-server.h b/src/mds-server/mds-server.h index 4d6c5e7..4624761 100644 --- a/src/mds-server/mds-server.h +++ b/src/mds-server/mds-server.h @@ -53,7 +53,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender); * * @param args The arguments to the child process */ -void run_initrc(char** args); +void run_initrc(char** args) __attribute__((noreturn)); #endif diff --git a/src/mds-server/multicast.c b/src/mds-server/multicast.c index 7a0b309..c3d09bc 100644 --- a/src/mds-server/multicast.c +++ b/src/mds-server/multicast.c @@ -124,8 +124,7 @@ size_t multicast_unmarshal(multicast_t* restrict this, char* restrict data) buf_get_next(data, size_t, this->message_ptr); buf_get_next(data, size_t, this->message_prefix); if (this->interceptions_count > 0) - if (xmalloc(this->interceptions, this->interceptions_count, queued_interception_t)) - return 0; + fail_if (xmalloc(this->interceptions, this->interceptions_count, queued_interception_t)); for (i = 0; i < this->interceptions_count; i++) { n = queued_interception_unmarshal(this->interceptions + i, data); @@ -134,12 +133,13 @@ size_t multicast_unmarshal(multicast_t* restrict this, char* restrict data) } if (this->message_length > 0) { - if (xmalloc(this->message, this->message_length, char)) - return 0; + fail_if (xmalloc(this->message, this->message_length, char)); memcpy(this->message, data, this->message_length * sizeof(char)); rc += this->message_length * sizeof(char); } return rc; + fail: + return 0; } diff --git a/src/mds-server/receiving.c b/src/mds-server/receiving.c index 15ebb4d..8571944 100644 --- a/src/mds-server/receiving.c +++ b/src/mds-server/receiving.c @@ -93,7 +93,7 @@ static int modifying_notify(client_t* client, mds_message_t message, uint64_t mo return 0; - pfail: + fail: xperror(*argv); if (multicast != NULL) { @@ -116,14 +116,13 @@ static int modifying_notify(client_t* client, mds_message_t message, uint64_t mo */ static int add_intercept_conditions_from_message(client_t* client, int modifying, int64_t priority, int stop) { - int errno_ = 0; + int saved_errno; char* payload = client->message.payload; size_t payload_size = client->message.payload_size; size_t size = 64; char* buf; - if (xmalloc(buf, size + 1, char)) - return -1; + fail_if (xmalloc(buf, size + 1, char)); /* All messages */ if (client->message.payload_size == 0) @@ -149,10 +148,10 @@ static int add_intercept_conditions_from_message(client_t* client, int modifying char* old_buf = buf; if (xrealloc(buf, (size <<= 1) + 1, char)) { - errno_ = errno; + saved_errno = errno; free(old_buf); pthread_mutex_unlock(&(client->mutex)); - break; + fail_if (errno = saved_errno, 1); } } memcpy(buf, payload, len); @@ -166,8 +165,9 @@ static int add_intercept_conditions_from_message(client_t* client, int modifying done: free(buf); - errno = errno_; - return errno_ ? -1 : 0; + return 0; + fail: + return -1; } @@ -183,6 +183,7 @@ static int assign_and_send_id(client_t* client, const char* message_id) char* msgbuf = NULL; char* msgbuf_; size_t n; + int rc = -1; /* Construct response. */ n = 2 * 10 + strlen(message_id) + 1; @@ -204,6 +205,7 @@ static int assign_and_send_id(client_t* client, const char* message_id) /* Queue message to be sent when this function returns. This done to simplify `multicast_message` for re-exec and termination. */ +#define fail fail_in_mutex with_mutex (client->mutex, if (client->send_pending_size == 0) { @@ -216,20 +218,20 @@ static int assign_and_send_id(client_t* client, const char* message_id) /* Concatenate message to already pending messages. */ size_t new_len = client->send_pending_size + n; char* msg_new = client->send_pending; - if (xrealloc(msg_new, new_len, char)) - goto fail; + fail_if (xrealloc(msg_new, new_len, char)); memcpy(msg_new + client->send_pending_size, msgbuf, n * sizeof(char)); client->send_pending = msg_new; client->send_pending_size = new_len; } - fail: + (msgbuf = NULL, rc = 0, errno = 0); + fail_in_mutex: ); +#undef fail - return 0; - - pfail: + fail: /* Also success. */ + xperror(*argv); free(msgbuf); - return -1; + return rc; } @@ -327,7 +329,7 @@ int message_received(client_t* client) return 0; - pfail: + fail: xperror(*argv); free(msgbuf); return 0; diff --git a/src/mds-server/reexec.c b/src/mds-server/reexec.c index 981569e..fe8c196 100644 --- a/src/mds-server/reexec.c +++ b/src/mds-server/reexec.c @@ -179,15 +179,13 @@ int unmarshal_server(char* state_buf) ssize_t node; pthread_t slave_thread; - +#define fail soft_fail + /* Create memory address remapping table. */ - if (hash_table_create(&unmarshal_remap_map)) - { - xperror(*argv); - hash_table_destroy(&unmarshal_remap_map, NULL, NULL); - return -1; - } - + fail_if (hash_table_create(&unmarshal_remap_map)); + +#undef fail +#define fail clients_fail /* Get the marshal protocal version. Not needed, there is only the one version right now. */ /* buf_get(state_buf, int, 0, MDS_SERVER_VARS_VERSION); */ @@ -210,20 +208,16 @@ int unmarshal_server(char* state_buf) client_t* value; /* Allocate the client's information. */ - if (xmalloc(value, 1, client_t)) - goto clients_fail; + fail_if (xmalloc(value, 1, client_t)); /* Unmarshal the address, it is used the the client list and the client map, that are also marshalled. */ buf_get_next(state_buf, size_t, value_address); /* Unmarshal the client information. */ - n = client_unmarshal(value, state_buf); - if (n == 0) - goto clients_fail; + fail_if (n = client_unmarshal(value, state_buf), n == 0); /* Populate the remapping table. */ if (hash_table_put(&unmarshal_remap_map, value_address, (size_t)(void*)value) == 0) - if (errno) - goto clients_fail; + fail_if (errno); /* Delayed seeking. */ state_buf += n / sizeof(char); @@ -247,14 +241,15 @@ int unmarshal_server(char* state_buf) break; } +#undef fail +#define fail critical_fail + /* Unmarshal the client list. */ - if (linked_list_unmarshal(&client_list, state_buf)) - goto critical_fail; + fail_if (linked_list_unmarshal(&client_list, state_buf)); state_buf += list_size / sizeof(char); /* Unmarshal the client map. */ - if (fd_table_unmarshal(&client_map, state_buf, unmarshal_remapper)) - goto critical_fail; + fail_if (fd_table_unmarshal(&client_map, state_buf, unmarshal_remapper)); /* Remove non-found elements from the fd table. */ #define __bit(I, _OP_) client_map.used[I / 64] _OP_ ((uint64_t)1 << (I % 64)) @@ -291,8 +286,12 @@ int unmarshal_server(char* state_buf) hash_table_destroy(&unmarshal_remap_map, NULL, NULL); return with_error; +#undef fail - + soft_fail: + xperror(*argv); + hash_table_destroy(&unmarshal_remap_map, NULL, NULL); + return -1; critical_fail: xperror(*argv); abort(); diff --git a/src/mds-server/slavery.c b/src/mds-server/slavery.c index df6d552..088b6af 100644 --- a/src/mds-server/slavery.c +++ b/src/mds-server/slavery.c @@ -47,11 +47,15 @@ void* slave_loop(void*); int fetch_message(client_t* client) { int r = mds_message_read(&(client->message), client->socket_fd); + if (r == 0) return 0; if (r == -2) - eprint("corrupt message received."); + { + eprint("corrupt message received."); + fail_if (1); + } else if (errno == ECONNRESET) { r = mds_message_read(&(client->message), client->socket_fd); @@ -60,11 +64,14 @@ int fetch_message(client_t* client) } else if (errno != EINTR) { - r = -2; xperror(*argv); + fail_if (1); } + fail_if (r == -2); return r; + fail: + return -2; } @@ -81,14 +88,16 @@ int create_slave(pthread_t* thread_slot, int slave_fd) { xperror(*argv); with_mutex (slave_mutex, running_slaves--;); - return -1; + fail_if (1); } if ((errno = pthread_detach(*thread_slot))) { xperror(*argv); - return -1; + fail_if (1); } return 0; + fail: + return -1; } @@ -96,14 +105,13 @@ int create_slave(pthread_t* thread_slot, int slave_fd) * Initialise a client, except for threading * * @param client_fd The file descriptor of the client's socket - * @return The client information, NULL on error + * @return The client information, `NULL` on error */ client_t* initialise_client(int client_fd) { ssize_t entry = LINKED_LIST_UNUSED; - int locked = 0; client_t* information; - int errno_; + int locked = 0, saved_errno; size_t tmp; /* Create information table. */ @@ -131,14 +139,13 @@ client_t* initialise_client(int client_fd) return information; - pfail: - errno_ = errno; + fail: + saved_errno = errno; if (locked) pthread_mutex_unlock(&slave_mutex); free(information); if (entry != LINKED_LIST_UNUSED) with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); - errno = errno_; - return NULL; + return errno = saved_errno, NULL; } diff --git a/src/mds-server/slavery.h b/src/mds-server/slavery.h index 92144fd..00f6084 100644 --- a/src/mds-server/slavery.h +++ b/src/mds-server/slavery.h @@ -45,7 +45,7 @@ int create_slave(pthread_t* thread_slot, int slave_fd); * Initialise a client, except for threading * * @param client_fd The file descriptor of the client's socket - * @return The client information, NULL on error + * @return The client information, `NULL` on error */ client_t* initialise_client(int client_fd); |