diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-08 19:15:24 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-08 19:15:24 +0100 |
commit | e9880491796516b7e2f835a3df6f20e5da06ebf7 (patch) | |
tree | dc10120bd50d39aa092bea82c1143e5df845ee04 | |
parent | replace all variants of goto pfail with fail_if (diff) | |
download | mds-e9880491796516b7e2f835a3df6f20e5da06ebf7.tar.gz mds-e9880491796516b7e2f835a3df6f20e5da06ebf7.tar.bz2 mds-e9880491796516b7e2f835a3df6f20e5da06ebf7.tar.xz |
no more goto fail
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | src/libmdsserver/client-list.c | 5 | ||||
-rw-r--r-- | src/libmdsserver/linked-list.c | 26 | ||||
-rw-r--r-- | src/libmdsserver/util.c | 5 | ||||
-rw-r--r-- | src/mds-base.c | 28 | ||||
-rw-r--r-- | src/mds-clipboard.c | 9 | ||||
-rw-r--r-- | src/mds-echo.c | 9 | ||||
-rw-r--r-- | src/mds-kkbd.c | 9 | ||||
-rw-r--r-- | src/mds-registry/mds-registry.c | 9 | ||||
-rw-r--r-- | src/mds-registry/registry.c | 4 | ||||
-rw-r--r-- | src/mds-registry/slave.c | 15 | ||||
-rw-r--r-- | src/mds-server/client.c | 21 | ||||
-rw-r--r-- | src/mds-server/mds-server.c | 22 | ||||
-rw-r--r-- | src/mds-server/receiving.c | 16 | ||||
-rw-r--r-- | src/mds-vt.c | 16 | ||||
-rw-r--r-- | src/mds.c | 14 |
15 files changed, 98 insertions, 110 deletions
diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index 66fc2ae..5562747 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -104,8 +104,7 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric out->clients = NULL; - if ((new_clients = malloc(n)) == NULL) - goto fail; + fail_if ((new_clients = malloc(n)) == NULL); out->clients = new_clients; @@ -116,7 +115,7 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric return 0; - fail: + pfail: free(new_clients); return -1; } diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 6c8e185..83f7711 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -95,10 +95,10 @@ int linked_list_create(linked_list_t* restrict this, size_t capacity) */ void linked_list_destroy(linked_list_t* restrict this) { - free(this->reusable); this->reusable = NULL; - free(this->values); this->values = NULL; - free(this->next); this->next = NULL; - free(this->previous); this->previous = NULL; + free(this->reusable), this->reusable = NULL; + free(this->values), this->values = NULL; + free(this->next), this->next = NULL; + free(this->previous), this->previous = NULL; } @@ -122,10 +122,10 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric out->previous = NULL; out->reusable = NULL; - if ((new_values = malloc(n)) == NULL) goto fail; - if ((new_next = malloc(n)) == NULL) goto fail; - if ((new_previous = malloc(n)) == NULL) goto fail; - if ((new_reusable = malloc(n)) == NULL) goto fail; + fail_if ((new_values = malloc(n)) == NULL); + fail_if ((new_next = malloc(n)) == NULL); + fail_if ((new_previous = malloc(n)) == NULL); + fail_if ((new_reusable = malloc(n)) == NULL); out->values = new_values; out->next = new_next; @@ -144,7 +144,7 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric return 0; - fail: + pfail: free(new_values); free(new_next); free(new_previous); @@ -191,9 +191,9 @@ int linked_list_pack(linked_list_t* restrict this) if (cap != this->capacity) { - if (xmalloc(new_next, cap, ssize_t)) goto fail; - if (xmalloc(new_previous, cap, ssize_t)) goto fail; - if (xmalloc(new_reusable, cap, ssize_t)) goto fail; + fail_if (xmalloc(new_next, cap, ssize_t)); + fail_if (xmalloc(new_previous, cap, ssize_t)); + fail_if (xmalloc(new_reusable, cap, ssize_t)); free(this->next); free(this->previous); @@ -218,7 +218,7 @@ int linked_list_pack(linked_list_t* restrict this) return 0; - fail: + pfail: free(vals); free(new_next); free(new_previous); diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index b1dff3c..70faf3d 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -360,8 +360,7 @@ pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options) rc = waitpid(pid, status, options); if (rc == (pid_t)-1) { - if (errno != EINTR) - goto fail; + fail_if (errno != EINTR); if (have_time && (monotone(&time_intr) >= 0)) if (time_start.tv_sec != time_intr.tv_sec) intr_count = 0; @@ -370,7 +369,7 @@ pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options) /* Don't let the CPU catch fire! */ errno = EINTR; } - fail: + pfail: return rc; } diff --git a/src/mds-base.c b/src/mds-base.c index 5bce448..f8fca62 100644 --- a/src/mds-base.c +++ b/src/mds-base.c @@ -36,7 +36,7 @@ #include <fcntl.h> -#define try(INSTRUCTION) if ((r = INSTRUCTION)) goto fail +#define try(INSTRUCTION) fail_if ((r = (INSTRUCTION))) /** @@ -482,24 +482,21 @@ static void perform_reexec(void) /* Marshal the state of the server. */ xsnprintf(shm_path, SHM_PATH_PATTERN, (unsigned long int)pid); reexec_fd = shm_open(shm_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU); - if (reexec_fd < 0) - { - xperror(*argv); - return; - } - if (base_marshal(reexec_fd) < 0) - goto fail; + fail_if (reexec_fd < 0); + fail_if (base_marshal(reexec_fd) < 0); close(reexec_fd); reexec_fd = -1; /* Re-exec the server. */ reexec_server(argc, argv, is_reexec); - xperror(*argv); - fail: + pfail: + xperror(*argv); if (reexec_fd >= 0) - close(reexec_fd); - shm_unlink(shm_path); + { + close(reexec_fd); + shm_unlink(shm_path); + } } @@ -520,7 +517,8 @@ int main(int argc_, char** argv_) if (server_characteristics.require_privileges == 0) /* Drop privileges like it's hot. */ - fail_if (drop_privileges()); + if (drop_privileges()) + fail_if ((r = 1)); /* Use /proc/self/exe when re:exec-ing */ @@ -573,7 +571,7 @@ int main(int argc_, char** argv_) if (reexecing) { perform_reexec(); - goto fail; + fail_if (1); } close(socket_fd); @@ -582,8 +580,6 @@ int main(int argc_, char** argv_) pfail: xperror(*argv); - r = 1; - fail: if (socket_fd >= 0) close(socket_fd); return r; diff --git a/src/mds-clipboard.c b/src/mds-clipboard.c index 09cb599..a2a43eb 100644 --- a/src/mds-clipboard.c +++ b/src/mds-clipboard.c @@ -357,7 +357,7 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; @@ -368,16 +368,15 @@ int master_loop(void) 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; + goto done; pfail: xperror(*argv); - fail: + done: if (!rc && reexecing) return 0; mds_message_destroy(&received); diff --git a/src/mds-echo.c b/src/mds-echo.c index e7fc4d4..4cbaabb 100644 --- a/src/mds-echo.c +++ b/src/mds-echo.c @@ -229,7 +229,7 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; @@ -240,16 +240,15 @@ int master_loop(void) 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; + goto done; pfail: xperror(*argv); - fail: + done: if (rc || !reexecing) mds_message_destroy(&received); free(echo_buffer); diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c index 32ae6db..82b3d22 100644 --- a/src/mds-kkbd.c +++ b/src/mds-kkbd.c @@ -437,7 +437,7 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; @@ -448,18 +448,17 @@ int master_loop(void) mds_message_destroy(&received); mds_message_initialise(&received); connected = 0; - if (reconnect_to_display()) - goto fail; + fail_if (reconnect_to_display()); connected = 1; } joined = 1; fail_if ((errno = pthread_join(kbd_thread, &kbd_ret))); rc = kbd_ret == NULL ? 0 : 1; - goto fail; + goto done; pfail: xperror(*argv); - fail: + done: pthread_mutex_destroy(&send_mutex); pthread_mutex_destroy(&mapping_mutex); free(send_buffer); diff --git a/src/mds-registry/mds-registry.c b/src/mds-registry/mds-registry.c index 8b2a3e5..9751ff4 100644 --- a/src/mds-registry/mds-registry.c +++ b/src/mds-registry/mds-registry.c @@ -171,7 +171,7 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; @@ -182,16 +182,15 @@ int master_loop(void) 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; + goto done; pfail: xperror(*argv); - fail: + done: /* Join with all slaves threads. */ with_mutex (slave_mutex, while (running_slaves > 0) diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c index 8b1c126..b8a24ac 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; } @@ -110,7 +109,6 @@ static int handle_close_message(void) return 0; pfail: xperror(*argv); - fail: free(keys); return -1; } diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 9999aa0..8e29f17 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -272,22 +272,18 @@ slave_t* slave_create(hash_table_t* restrict wait_set, const char* restrict recv { slave_t* restrict rc = NULL; - 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: + pfail: slave_destroy(rc); free(rc); return NULL; @@ -318,6 +314,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); diff --git a/src/mds-server/client.c b/src/mds-server/client.c index 99d561e..37ba906 100644 --- a/src/mds-server/client.c +++ b/src/mds-server/client.c @@ -203,6 +203,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; this->interception_conditions = NULL; this->multicasts = NULL; this->send_pending = NULL; @@ -223,35 +224,32 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) 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); @@ -264,7 +262,8 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) rc += n * sizeof(char); return rc; - fail: + pfail: + saved_errno = errno; mds_message_destroy(&(this->message)); for (i = 0; i < this->interception_conditions_count; i++) free(this->interception_conditions[i].condition); @@ -278,7 +277,7 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) mds_message_destroy(this->modify_message); free(this->modify_message); } - return 0; + return errno = saved_errno, (size_t)0; } /** diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index f058396..499ba96 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -302,7 +302,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 +329,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); @@ -350,7 +350,7 @@ void* slave_loop(void* data) pfail: xperror(*argv); - goto fail; + goto done; reexec: @@ -482,22 +482,22 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) multicast->message_prefix = n; message = NULL; +#define pfail 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 pfail - 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); @@ -510,7 +510,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) pfail: xperror(*argv); - goto fail; + goto done; } diff --git a/src/mds-server/receiving.c b/src/mds-server/receiving.c index 15ebb4d..100d717 100644 --- a/src/mds-server/receiving.c +++ b/src/mds-server/receiving.c @@ -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 pfail 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 pfail - return 0; - - pfail: + pfail: /* Also success. */ + xperror(*argv); free(msgbuf); - return -1; + return rc; } diff --git a/src/mds-vt.c b/src/mds-vt.c index 0aae0c2..b268647 100644 --- a/src/mds-vt.c +++ b/src/mds-vt.c @@ -429,7 +429,7 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; @@ -440,8 +440,7 @@ int master_loop(void) mds_message_destroy(&received); mds_message_initialise(&received); connected = 0; - if (reconnect_to_display()) - goto fail; + fail_if (reconnect_to_display()); connected = 1; } @@ -451,10 +450,10 @@ int master_loop(void) if (unlink(vtfile_path) < 0) xperror(*argv); vt_close(display_tty_fd, &old_vt_stat); - goto fail; + goto done; pfail: xperror(*argv); - fail: + done: rc |= secondary_thread_failed; if (rc || !reexecing) mds_message_destroy(&received); @@ -489,7 +488,8 @@ void* secondary_loop(void* data) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + secondary_thread_failed = 1; + goto done; } else if (errno == EINTR) continue; @@ -499,14 +499,12 @@ void* secondary_loop(void* data) eprint("lost secondary connection to server."); mds_message_destroy(&secondary_received); mds_message_initialise(&secondary_received); - if (reconnect_fd_to_display(&secondary_socket_fd) < 0) - goto fail; + fail_if (reconnect_fd_to_display(&secondary_socket_fd) < 0); } goto done; pfail: xperror(*argv); - fail: secondary_thread_failed = 1; done: secondary_thread_started = 0; @@ -362,7 +362,11 @@ int spawn_and_respawn_server(int fd) /* If the server exited normally or SIGTERM, do not respawn. */ if (WIFEXITED(status) ? (WEXITSTATUS(status) == 0) : ((WTERMSIG(status) == SIGTERM) || (WTERMSIG(status) == SIGINT))) - goto done; /* Child exited normally, stop. */ + { + /* Child exited normally, stop. */ + rc--; + goto done; + } /* Get the current time. (End of child process.) */ time_error |= (monotone(&time_end) < 0); @@ -377,7 +381,7 @@ int spawn_and_respawn_server(int fd) { xperror(*argv); eprintf("`%s' died abnormally, not respawning because we could not read the time.", master_server); - goto fail; + goto done; } /* Respawn if the server did not die too fast. */ @@ -386,7 +390,7 @@ int spawn_and_respawn_server(int fd) else { eprintf("`%s' died abnormally, died too fast, not respawning.", master_server); - goto fail; + goto done; } if (first_spawn) @@ -401,8 +405,6 @@ int spawn_and_respawn_server(int fd) done: - rc--; - fail: rc++; free(child_args[0]); free(child_args[argc + 0]); @@ -412,7 +414,7 @@ int spawn_and_respawn_server(int fd) pfail: xperror(*argv); - goto fail; + goto done; } |