diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-09 03:42:16 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-09 03:42:16 +0100 |
commit | 8b69e6f4f9e239e660b26695c6a7f5007fcadf4e (patch) | |
tree | fb80b3ef0e017ca36568da6312c292c479122a27 /src | |
parent | with a few exceptions and some remaining files, never return directly on failure, always goto fail by invoking fail_if (diff) | |
download | mds-8b69e6f4f9e239e660b26695c6a7f5007fcadf4e.tar.gz mds-8b69e6f4f9e239e660b26695c6a7f5007fcadf4e.tar.bz2 mds-8b69e6f4f9e239e660b26695c6a7f5007fcadf4e.tar.xz |
with a few exceptions, never return directly on failure, always goto fail by invoking fail_if
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-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 | 12 | ||||
-rw-r--r-- | src/mds-server/mds-server.h | 2 | ||||
-rw-r--r-- | src/mds-server/receiving.c | 14 | ||||
-rw-r--r-- | src/mds-server/reexec.c | 39 | ||||
-rw-r--r-- | src/mds-server/slavery.c | 19 | ||||
-rw-r--r-- | src/mds-server/slavery.h | 2 | ||||
-rw-r--r-- | src/mds.c | 48 |
9 files changed, 93 insertions, 90 deletions
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 8ef44cc..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. */ } } @@ -408,11 +407,8 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) /* 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. */ @@ -583,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/receiving.c b/src/mds-server/receiving.c index 71f1210..8571944 100644 --- a/src/mds-server/receiving.c +++ b/src/mds-server/receiving.c @@ -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; } 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 7ff801d..cf5e45d 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,7 +105,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) { 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); @@ -73,7 +73,7 @@ int main(int argc_, char** argv_) struct sockaddr_un address; char pathname[PATH_MAX]; char piddata[64]; - unsigned int display; + unsigned int display = DISPLAY_MAX; FILE* f; int rc; int j, r; @@ -116,8 +116,7 @@ int main(int argc_, char** argv_) saved_umask = umask(0); /* Create directory for socket files, PID files and such. */ - if (create_directory_root(MDS_RUNTIME_ROOT_DIRECTORY)) - return 1; + fail_if (create_directory_root(MDS_RUNTIME_ROOT_DIRECTORY)); /* Determine display index. */ @@ -148,14 +147,12 @@ int main(int argc_, char** argv_) /* Yes, the directory could have been removed, but it probably was not. */ /* Create PID file. */ - fail_if ((f = fopen(pathname, "w")) == NULL); + fail_if (f = fopen(pathname, "w"), f == NULL); xsnprintf(piddata, "%u\n", getpid()); if (fwrite(piddata, 1, strlen(piddata), f) < strlen(piddata)) { fclose(f); - if (unlink(pathname) < 0) - xperror(*argv); - return 1; + fail_if (1); } fflush(f); fclose(f); @@ -205,6 +202,9 @@ int main(int argc_, char** argv_) unlink(pathname); } + if (display == DISPLAY_MAX) + return rc; + /* Remove PID file. */ xsnprintf(pathname, "%s/%u.pid", MDS_RUNTIME_ROOT_DIRECTORY, display); unlink(pathname); @@ -236,9 +236,8 @@ int is_pid_file_reusable(FILE* f) size_t read_len; read_len = fread(piddata, 1, sizeof(piddata) / sizeof(char), f); - if (ferror(f)) /* Failed to read. */ - xperror(*argv); - else if (feof(f) == 0) /* Did not read everything. */ + fail_if (ferror(f)); /* Failed to read. */ + if (feof(f) == 0) /* Did not read everything. */ eprint("the content of a PID file is larger than expected."); else { @@ -251,6 +250,9 @@ int is_pid_file_reusable(FILE* f) } return 0; + fail: + xperror(*argv); + return 0; } @@ -293,11 +295,13 @@ static void exec_master_server(char** child_args) { /* Drop privileges. They most not be propagated non-authorised components. */ /* setgid should not be set, but just to be safe we are restoring both user and group. */ - if (drop_privileges()) - return; + fail_if (drop_privileges()); /* Start master server. */ execv(master_server, child_args); + fail_if (1); + fail: + return; } @@ -442,7 +446,8 @@ int create_directory_root(const char* pathname) /* Directory is missing, create it. */ if (mkdir(pathname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) { - fail_if (errno != EEXIST); /* Unlikely race condition. */ + /* Unlikely race condition. */ + fail_if (errno != EEXIST); } else { @@ -451,7 +456,6 @@ int create_directory_root(const char* pathname) } return 0; - fail: xperror(*argv); return 1; @@ -483,22 +487,18 @@ int create_directory_user(const char* pathname) /* Directory is missing, create it. */ if (mkdir(pathname, S_IRWXU) < 0) { - if (errno != EEXIST) /* Unlikely race condition. */ - { - xperror(*argv); - return 1; - } + /* Unlikely race condition. */ + fail_if (errno != EEXIST); } else /* Set ownership. */ - if (chown(pathname, getuid(), NOBODY_GROUP_GID) < 0) - { - xperror(*argv); - return 1; - } + fail_if (chown(pathname, getuid(), NOBODY_GROUP_GID) < 0); } return 0; + fail: + xperror(*argv); + return 1; } |