From a45a3204a0b093d6899249b18808692e3f8ac863 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 8 Dec 2014 18:14:54 +0100 Subject: replace all goto pfail with fail_if, so that we can take whence it failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'src/mds.c') diff --git a/src/mds.c b/src/mds.c index 6d8707a..c940175 100644 --- a/src/mds.c +++ b/src/mds.c @@ -336,8 +336,7 @@ int spawn_and_respawn_server(int fd) respawn: pid = fork(); - if (pid == (pid_t)-1) - goto pfail; + fail_if (pid == (pid_t)-1); if (pid == 0) /* Child. */ { @@ -347,7 +346,7 @@ int spawn_and_respawn_server(int fd) umask(saved_umask); /* Change image into the master server. */ exec_master_server(child_args); - goto pfail; + fail_if (1); } @@ -358,8 +357,7 @@ int spawn_and_respawn_server(int fd) xperror(*argv); /* Wait for master server to die. */ - if (uninterruptable_waitpid(pid, &status, 0) == (pid_t)-1) - goto pfail; + fail_if (uninterruptable_waitpid(pid, &status, 0) == (pid_t)-1); /* If the server exited normally or SIGTERM, do not respawn. */ if (WIFEXITED(status) ? (WEXITSTATUS(status) == 0) : @@ -396,8 +394,7 @@ int spawn_and_respawn_server(int fd) first_spawn = 0; free(child_args[argc + 0]); child_args[argc + 0] = strdup("--respawn"); - if (child_args[argc + 0] == NULL) - goto pfail; + fail_if (child_args[argc + 0] == NULL); } goto respawn; @@ -443,14 +440,12 @@ 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) { - if (errno != EEXIST) /* Unlikely race condition. */ - goto pfail; + fail_if (errno != EEXIST); /* Unlikely race condition. */ } else { /* Set ownership. */ - if (chown(pathname, ROOT_USER_UID, ROOT_GROUP_GID) < 0) - goto pfail; + fail_if (chown(pathname, ROOT_USER_UID, ROOT_GROUP_GID) < 0); } return 0; @@ -525,7 +520,7 @@ int unlink_recursive(const char* pathname) if (stat(pathname, &_attr) < 0) return 0; /* Directory does not exist. */ errno = errno_; - goto pfail; + fail_if (1); } /* Remove the content of the directory. */ @@ -534,14 +529,12 @@ int unlink_recursive(const char* pathname) strcmp(file->d_name, "..") && (unlink(file->d_name) < 0)) { - if (errno != EISDIR) - goto pfail; + fail_if (errno != EISDIR); unlink_recursive(file->d_name); } /* Remove the drectory. */ - if (rmdir(pathname) < 0) - goto pfail; + fail_if (rmdir(pathname) < 0); done: if (dir != NULL) -- cgit v1.2.3-70-g09d2 From e9880491796516b7e2f835a3df6f20e5da06ebf7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 8 Dec 2014 19:15:24 +0100 Subject: no more goto fail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/client-list.c | 5 ++--- src/libmdsserver/linked-list.c | 26 +++++++++++++------------- src/libmdsserver/util.c | 5 ++--- src/mds-base.c | 28 ++++++++++++---------------- src/mds-clipboard.c | 9 ++++----- src/mds-echo.c | 9 ++++----- src/mds-kkbd.c | 9 ++++----- src/mds-registry/mds-registry.c | 9 ++++----- src/mds-registry/registry.c | 4 +--- src/mds-registry/slave.c | 15 +++++++-------- src/mds-server/client.c | 21 ++++++++++----------- src/mds-server/mds-server.c | 22 +++++++++++----------- src/mds-server/receiving.c | 16 +++++++++------- src/mds-vt.c | 16 +++++++--------- src/mds.c | 14 ++++++++------ 15 files changed, 98 insertions(+), 110 deletions(-) (limited to 'src/mds.c') 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 -#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; diff --git a/src/mds.c b/src/mds.c index c940175..23c6bac 100644 --- a/src/mds.c +++ b/src/mds.c @@ -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; } -- cgit v1.2.3-70-g09d2 From 718a400d599f3df3a64c2c0659a48fb9d22b6105 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 8 Dec 2014 19:28:41 +0100 Subject: m + make sure we never lose errno MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/client-list.c | 4 +++- src/libmdsserver/linked-list.c | 8 ++++++-- src/mds-registry/reexec.c | 18 ++++-------------- src/mds-registry/registry.c | 9 +++++++-- src/mds-registry/slave.c | 7 ++++--- src/mds-server/mds-server.c | 3 ++- src/mds-server/slavery.c | 8 +++----- src/mds.c | 4 ++-- 8 files changed, 31 insertions(+), 30 deletions(-) (limited to 'src/mds.c') diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index 5562747..1bcaa72 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -101,6 +101,7 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric { size_t n = this->capacity * sizeof(uint64_t); uint64_t* restrict new_clients = NULL; + int saved_errno; out->clients = NULL; @@ -116,8 +117,9 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric return 0; pfail: + saved_errno = errno; free(new_clients); - return -1; + return errno = saved_errno, -1; } diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index 83f7711..d17ba8b 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -116,6 +116,7 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric ssize_t* restrict new_next = NULL; ssize_t* restrict new_previous = NULL; ssize_t* restrict new_reusable; + int saved_errno; out->values = NULL; out->next = NULL; @@ -145,10 +146,11 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric return 0; pfail: + saved_errno = errno; free(new_values); free(new_next); free(new_previous); - return -1; + return errno = saved_errno, -1; } @@ -177,6 +179,7 @@ int linked_list_pack(linked_list_t* restrict this) size_t i = 0; ssize_t node; size_t* restrict vals; + int saved_errno; if (xmalloc(vals, cap, size_t)) return -1; @@ -219,10 +222,11 @@ int linked_list_pack(linked_list_t* restrict this) return 0; pfail: + saved_errno = errno; free(vals); free(new_next); free(new_previous); - return -1; + return errno = saved_errno, -1; } diff --git a/src/mds-registry/reexec.c b/src/mds-registry/reexec.c index 6247e18..6b36cc8 100644 --- a/src/mds-registry/reexec.c +++ b/src/mds-registry/reexec.c @@ -202,20 +202,10 @@ int unmarshal_server(char* state_buf) 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 b8a24ac..918b137 100644 --- a/src/mds-registry/registry.c +++ b/src/mds-registry/registry.c @@ -125,6 +125,8 @@ 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. */ @@ -143,7 +145,7 @@ static int registry_action_add(int has_key, char* command, size_t command_key, u /* Duplicate the protocol name so it can be accessed later. */ if ((command = strdup(command)) == NULL) { - free(list); + 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. */ @@ -152,9 +154,11 @@ 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); + errno = saved_errno; fail_if (1); } } @@ -210,6 +214,7 @@ 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) { @@ -228,7 +233,7 @@ static int registry_action_act(char* command, int action, uint64_t client, hash_ if (hash_table_put(wait_set, command_key, 1) == 0) if (errno) { - free(command); + saved_errno = errno, free(command), errno = saved_errno; fail_if (1); } } diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 8e29f17..54774e2 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -271,6 +271,7 @@ 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; fail_if (xmalloc(rc, 1, slave_t)); @@ -284,9 +285,9 @@ slave_t* slave_create(hash_table_t* restrict wait_set, const char* restrict recv return rc; pfail: - slave_destroy(rc); - free(rc); - return NULL; + saved_errno = errno; + slave_destroy(rc), free(rc); + return errno = saved_errno, NULL; } diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index 499ba96..abfc871 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -403,6 +403,7 @@ 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++) @@ -440,7 +441,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) *colon = '\0'; if ((headers[i] = strdup(msg)) == NULL) { - free(headers[i]); + saved_errno = errno, free(headers[i]), errno = saved_errno; header_count = i; fail_if (1); } diff --git a/src/mds-server/slavery.c b/src/mds-server/slavery.c index 283251a..a951f99 100644 --- a/src/mds-server/slavery.c +++ b/src/mds-server/slavery.c @@ -101,9 +101,8 @@ int create_slave(pthread_t* thread_slot, int slave_fd) 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. */ @@ -132,7 +131,7 @@ client_t* initialise_client(int client_fd) pfail: - errno_ = errno; + saved_errno = errno; if (locked) pthread_mutex_unlock(&slave_mutex); free(information); @@ -140,7 +139,6 @@ client_t* initialise_client(int client_fd) { with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); } - errno = errno_; - return NULL; + return saved_errno = errno, NULL; } diff --git a/src/mds.c b/src/mds.c index 23c6bac..1c0f002 100644 --- a/src/mds.c +++ b/src/mds.c @@ -517,11 +517,11 @@ int unlink_recursive(const char* pathname) /* Check that we could examine the directory. */ if (dir == NULL) { - int errno_ = errno; + int saved_errno = errno; struct stat _attr; if (stat(pathname, &_attr) < 0) return 0; /* Directory does not exist. */ - errno = errno_; + errno = saved_errno; fail_if (1); } -- cgit v1.2.3-70-g09d2 From 3e07c8b1657e28f19072b430936e88be040fb319 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 8 Dec 2014 19:34:26 +0100 Subject: rename pfail to fail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/client-list.c | 2 +- src/libmdsserver/linked-list.c | 4 ++-- src/libmdsserver/macros.h | 4 ++-- src/libmdsserver/mds-message.c | 2 +- src/libmdsserver/util.c | 2 +- src/mds-base.c | 12 +++++----- src/mds-clipboard.c | 16 ++++++------- src/mds-echo.c | 4 ++-- src/mds-kbdc/callables.c | 2 +- src/mds-kbdc/compile-layout.c | 26 ++++++++++----------- src/mds-kbdc/eliminate-dead-code.c | 4 ++-- src/mds-kbdc/include-stack.c | 6 ++--- src/mds-kbdc/make-tree.c | 48 +++++++++++++++++++------------------- src/mds-kbdc/mds-kbdc.c | 2 +- src/mds-kbdc/parsed.c | 2 +- src/mds-kbdc/paths.c | 6 ++--- src/mds-kbdc/process-includes.c | 6 ++--- src/mds-kbdc/raw-data.c | 10 ++++---- src/mds-kbdc/simplify-tree.c | 14 +++++------ src/mds-kbdc/tree.c | 2 +- src/mds-kbdc/validate-tree.c | 22 ++++++++--------- src/mds-kkbd.c | 8 +++---- src/mds-registry/mds-registry.c | 6 ++--- src/mds-registry/reexec.c | 2 +- src/mds-registry/registry.c | 6 ++--- src/mds-registry/slave.c | 6 ++--- src/mds-respawn.c | 6 ++--- src/mds-server/client.c | 2 +- src/mds-server/mds-server.c | 10 ++++---- src/mds-server/receiving.c | 10 ++++---- src/mds-server/slavery.c | 4 ++-- src/mds-vt.c | 6 ++--- src/mds.c | 8 +++---- 33 files changed, 135 insertions(+), 135 deletions(-) (limited to 'src/mds.c') diff --git a/src/libmdsserver/client-list.c b/src/libmdsserver/client-list.c index 1bcaa72..ff3ffe2 100644 --- a/src/libmdsserver/client-list.c +++ b/src/libmdsserver/client-list.c @@ -116,7 +116,7 @@ int client_list_clone(const client_list_t* restrict this, client_list_t* restric return 0; - pfail: + fail: saved_errno = errno; free(new_clients); return errno = saved_errno, -1; diff --git a/src/libmdsserver/linked-list.c b/src/libmdsserver/linked-list.c index d17ba8b..7756d90 100644 --- a/src/libmdsserver/linked-list.c +++ b/src/libmdsserver/linked-list.c @@ -145,7 +145,7 @@ int linked_list_clone(const linked_list_t* restrict this, linked_list_t* restric return 0; - pfail: + fail: saved_errno = errno; free(new_values); free(new_next); @@ -221,7 +221,7 @@ int linked_list_pack(linked_list_t* restrict this) return 0; - pfail: + fail: saved_errno = errno; free(vals); free(new_next); diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index 84b6060..fc47e0a 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -379,13 +379,13 @@ /** - * Go to the label `pfail` if a condition is met + * Go to the label `fail` if a condition is met * * @param ... The condition */ #define fail_if(...) \ if (__VA_ARGS__) \ - do { fprintf(stderr, "failure at %s:%i\n", __FILE__, __LINE__); goto pfail; } while (0) + do { fprintf(stderr, "failure at %s:%i\n", __FILE__, __LINE__); goto fail; } while (0) /** diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index d616355..4075f57 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -545,7 +545,7 @@ int mds_message_unmarshal(mds_message_t* restrict this, char* restrict data) return 0; - pfail: + fail: return -1; } diff --git a/src/libmdsserver/util.c b/src/libmdsserver/util.c index 70faf3d..2e72229 100644 --- a/src/libmdsserver/util.c +++ b/src/libmdsserver/util.c @@ -369,7 +369,7 @@ pid_t uninterruptable_waitpid(pid_t pid, int* restrict status, int options) /* Don't let the CPU catch fire! */ errno = EINTR; } - pfail: + fail: return rc; } diff --git a/src/mds-base.c b/src/mds-base.c index f8fca62..df0e680 100644 --- a/src/mds-base.c +++ b/src/mds-base.c @@ -189,7 +189,7 @@ int __attribute__((weak)) connect_to_display(void) return 0; - pfail: + fail: xperror(*argv); if (socket_fd >= 0) close(socket_fd); @@ -417,7 +417,7 @@ static int base_unmarshal(void) fail_if (reexec_failure_recover()); return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -460,7 +460,7 @@ static int base_marshal(int reexec_fd) free(state_buf); return 0; - pfail: + fail: xperror(*argv); free(state_buf); return 1; @@ -490,7 +490,7 @@ static void perform_reexec(void) /* Re-exec the server. */ reexec_server(argc, argv, is_reexec); - pfail: + fail: xperror(*argv); if (reexec_fd >= 0) { @@ -578,7 +578,7 @@ int main(int argc_, char** argv_) return 0; - pfail: + fail: xperror(*argv); if (socket_fd >= 0) close(socket_fd); @@ -638,7 +638,7 @@ int trap_signals(void) { fail_if (xsigaction(SIGDANGER, received_danger) < 0); } return 0; - pfail: + fail: xperror(*argv); return 1; } diff --git a/src/mds-clipboard.c b/src/mds-clipboard.c index a2a43eb..2488179 100644 --- a/src/mds-clipboard.c +++ b/src/mds-clipboard.c @@ -138,7 +138,7 @@ int initialise_server(void) return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); while (--i >= 0) @@ -305,7 +305,7 @@ int unmarshal_server(char* state_buf) } return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); for (i = 0; i < CLIPBOARD_LEVELS; i++) @@ -374,7 +374,7 @@ int master_loop(void) rc = 0; goto done; - pfail: + fail: xperror(*argv); done: if (!rc && reexecing) @@ -636,7 +636,7 @@ static int clipboard_purge(int level, const char* client_id) } return 0; - pfail: + fail: xperror(*argv); return -1; } @@ -727,7 +727,7 @@ int clipboard_add(int level, const char* time_to_live, const char* recv_client_i clipboard[level][0] = new_clip; return 0; - pfail: + fail: xperror(*argv); return -1; } @@ -801,7 +801,7 @@ int clipboard_read(int level, size_t index, const char* recv_client_id, const ch free(message); return 0; - pfail: + fail: xperror(*argv); return -1; } @@ -859,7 +859,7 @@ int clipboard_set_size(int level, size_t size) } return 0; - pfail: + fail: xperror(*argv); return -1; } @@ -904,7 +904,7 @@ int clipboard_get_size(int level, const char* recv_client_id, const char* recv_m free(message); return 0; - pfail: + fail: xperror(*argv); free(message); return -1; diff --git a/src/mds-echo.c b/src/mds-echo.c index 4cbaabb..caefecd 100644 --- a/src/mds-echo.c +++ b/src/mds-echo.c @@ -112,7 +112,7 @@ int initialise_server(void) fail_if (mds_message_initialise(&received)); return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); return 1; @@ -246,7 +246,7 @@ int master_loop(void) rc = 0; goto done; - pfail: + fail: xperror(*argv); done: if (rc || !reexecing) diff --git a/src/mds-kbdc/callables.c b/src/mds-kbdc/callables.c index 10eddcc..4c984c1 100644 --- a/src/mds-kbdc/callables.c +++ b/src/mds-kbdc/callables.c @@ -142,7 +142,7 @@ int callables_set(const char* restrict name, size_t arg_count, mds_kbdc_tree_t* list_ptr++; return 0; - pfail: + fail: saved_errno = errno; free(dupname); free(new_names); diff --git a/src/mds-kbdc/compile-layout.c b/src/mds-kbdc/compile-layout.c index 9441802..ccf647c 100644 --- a/src/mds-kbdc/compile-layout.c +++ b/src/mds-kbdc/compile-layout.c @@ -58,7 +58,7 @@ /** * Beginning of failure clause */ -#define FAIL_BEGIN pfail: saved_errno = errno +#define FAIL_BEGIN fail: saved_errno = errno /** * End of failure clause @@ -247,7 +247,7 @@ static int check_set_3_get_2_call(mds_kbdc_tree_t* restrict tree, int is_set, co FUN_ERROR(tree, ERROR, "‘\\%zu’ does not hold %zu elements", (size_t)*variable_arg, (size_t)*index_arg);/* TODO test */ return 0; - pfail: + fail: return -1; #undef FUN_ERROR #undef F @@ -470,7 +470,7 @@ static char32_t* parse_function_call(mds_kbdc_tree_t* restrict tree, const char* *rc = -1; goto done; - pfail: + fail: saved_errno = errno; free(rc); if (old_arguments) @@ -586,7 +586,7 @@ static void check_function_call(const mds_kbdc_tree_t* restrict tree, const char error->end = lineoff + (size_t)(*end - raw); return; - pfail: + fail: *rc |= -1; } @@ -706,7 +706,7 @@ static char32_t* parse_escape(mds_kbdc_tree_t* restrict tree, const char* restri *escape = 0; *end = raw; return rc; - pfail: + fail: saved_errno = errno; free(rc); return errno = saved_errno, NULL; @@ -838,7 +838,7 @@ static char32_t* parse_quoted_string(mds_kbdc_tree_t* restrict tree, const char* free(buf); return rc; - pfail: + fail: saved_errno = errno; free(subrc); free(old_rc); @@ -890,7 +890,7 @@ static char32_t* parse_unquoted_string(mds_kbdc_tree_t* restrict tree, const cha fail_if (rc = malloc(2 * sizeof(char32_t)), rc == NULL); return rc[0] = buf, rc[1] = -1, rc; - pfail: + fail: return NULL; #undef CHAR_ERROR #undef R @@ -1026,7 +1026,7 @@ static char32_t* parse_keys(mds_kbdc_tree_t* restrict tree, const char* restrict free(buf); return last_value_statement = old_last_value_statement, rc; - pfail: + fail: saved_errno = errno; free(subrc); free(old_rc); @@ -1079,7 +1079,7 @@ static size_t parse_variable(mds_kbdc_tree_t* restrict tree, const char* restric if (strlen(dotless + 1) != (size_t)snprintf(NULL, 0, "%zu", var)) return errno = ERANGE, (size_t)0; return var; - pfail: + fail: return 0; bad: @@ -1224,7 +1224,7 @@ static int get_macro(mds_kbdc_tree_macro_call_t* restrict macro_call, *macro = NULL; return 0; - pfail: + fail: return -1; } @@ -1848,7 +1848,7 @@ static int check_name_suffix(struct mds_kbdc_tree_callable* restrict tree) } return 0; - pfail: + fail: return -1; name_error: error->start = tree->loc_end; @@ -2187,7 +2187,7 @@ static int evaluate_element(mds_kbdc_tree_t* restrict node) } return bad; - pfail: + fail: return -1; } @@ -2260,7 +2260,7 @@ static int check_nonnul(mds_kbdc_tree_t* restrict tree) tree = tree->next; goto again; - pfail: + fail: return -1; } diff --git a/src/mds-kbdc/eliminate-dead-code.c b/src/mds-kbdc/eliminate-dead-code.c index 0881b20..4916fa4 100644 --- a/src/mds-kbdc/eliminate-dead-code.c +++ b/src/mds-kbdc/eliminate-dead-code.c @@ -105,7 +105,7 @@ static int eliminate_if(mds_kbdc_tree_if_t* restrict tree) if (elimination > elimination_level) elimination = elimination_level; return 0; - pfail: + fail: return -1; } @@ -156,7 +156,7 @@ static int eliminate_subtree(mds_kbdc_tree_t* restrict tree) tree = tree->next; goto again; - pfail: + fail: return -1; #undef E #undef e diff --git a/src/mds-kbdc/include-stack.c b/src/mds-kbdc/include-stack.c index d86374e..bec1425 100644 --- a/src/mds-kbdc/include-stack.c +++ b/src/mds-kbdc/include-stack.c @@ -85,7 +85,7 @@ int mds_kbdc_include_stack_dump(size_t ptr) result->pathname = old_pathname; result->source_code = old_source_code; return 0; - pfail: + fail: result->pathname = old_pathname; result->source_code = old_source_code; return -1; @@ -149,7 +149,7 @@ int mds_kbdc_include_stack_push(const mds_kbdc_tree_include_t* restrict tree, vo latest_save = NULL; return 0; - pfail: + fail: saved_errno = errno; free(old); return errno = saved_errno, -1; @@ -205,7 +205,7 @@ mds_kbdc_include_stack_t* mds_kbdc_include_stack_save(void) memcpy(latest_save->stack, includes, latest_save->ptr * sizeof(const mds_kbdc_tree_include_t*)); return latest_save; - pfail: + fail: saved_errno = errno; free(latest_save->stack); latest_save = NULL; diff --git a/src/mds-kbdc/make-tree.c b/src/mds-kbdc/make-tree.c index bb9d59e..8a7bb1e 100644 --- a/src/mds-kbdc/make-tree.c +++ b/src/mds-kbdc/make-tree.c @@ -459,7 +459,7 @@ static int get_pathname(const char* restrict filename) } return 1; - pfail: + fail: saved_errno = errno; free(cwd); return errno = saved_errno, -1; @@ -488,7 +488,7 @@ static int allocate_stacks(void) fail_if (xmalloc(tree_stack, line_n + max_line_length + 1, mds_kbdc_tree_t**)); return 0; - pfail: + fail: return -1; } @@ -504,7 +504,7 @@ static int read_source_code(void) fail_if (read_source_lines(result->pathname, result->source_code) < 0); return 0; - pfail: + fail: return -1; } @@ -545,7 +545,7 @@ static int check_for_premature_end_of_file(void) } return 0; - pfail: + fail: return -1; } @@ -565,7 +565,7 @@ static int check_whether_file_is_empty(void) NEW_ERROR(0, WARNING, "file is empty"); return 0; - pfail: + fail: return -1; } @@ -592,7 +592,7 @@ static int no_parameters(const char* restrict keyword) } return 0; - pfail: + fail: return -1; } @@ -653,7 +653,7 @@ static int names_1(char** restrict var) } return 0; - pfail: + fail: return -1; } @@ -704,7 +704,7 @@ static int chars(char** restrict var) } return 0; - pfail: + fail: return -1; } @@ -731,7 +731,7 @@ static int quotes(void) line = line_; return 0; - pfail: + fail: return -1; } @@ -756,7 +756,7 @@ static int have_more_parameters(void) return 0; } return 1; - pfail: + fail: return -1; } @@ -789,7 +789,7 @@ static int test_for_keyword(const char* restrict keyword) NEW_ERROR(1, ERROR, "expecting keyword ‘%s’", keyword); return 0; - pfail: + fail: return -1; } @@ -844,7 +844,7 @@ static int keys(mds_kbdc_tree_t** restrict var) line = end; return 0; - pfail: + fail: return -1; } @@ -889,7 +889,7 @@ static int pure_keys(char** restrict var) end = arg_end, line = end; return 0; - pfail: + fail: return -1; } @@ -959,7 +959,7 @@ static int sequence(int mapseq, size_t stack_orig) } return 0; - pfail: + fail: return -1; } @@ -984,7 +984,7 @@ static int sequence_fully_popped(size_t stack_orig) } return 0; - pfail: + fail: return -1; } @@ -1052,7 +1052,7 @@ static int parse_else(void) } return 0; - pfail: + fail: return -1; } @@ -1074,7 +1074,7 @@ static int parse_for(void) BRANCH("for"); return 0; - pfail: + fail: return -1; } @@ -1128,7 +1128,7 @@ static int parse_let(void) } return 0; - pfail: + fail: return -1; } @@ -1159,7 +1159,7 @@ static int parse_end(void) NEXT; return 0; - pfail: + fail: return -1; } @@ -1218,7 +1218,7 @@ static int parse_map(void) NEW_ERROR(1, ERROR, "too many parameters"); return 0; - pfail: + fail: return -1; } @@ -1283,7 +1283,7 @@ static int parse_macro_call(void) NEW_ERROR(1, ERROR, "invalid syntax ‘%s’", line); return 0; - pfail: + fail: return -1; } @@ -1323,7 +1323,7 @@ static int parse_array_elements(void) } } - pfail: + fail: return -1; } @@ -1375,7 +1375,7 @@ static int parse_line(void) *end = prev_end_char; return 0; - pfail: + fail: return -1; #undef p } @@ -1442,7 +1442,7 @@ int parse_to_tree(const char* restrict filename, mds_kbdc_parsed_t* restrict res free(tree_stack); return 0; - pfail: + fail: saved_errno = errno; free(keyword_stack); free(tree_stack); diff --git a/src/mds-kbdc/mds-kbdc.c b/src/mds-kbdc/mds-kbdc.c index ed690a7..33b97b2 100644 --- a/src/mds-kbdc/mds-kbdc.c +++ b/src/mds-kbdc/mds-kbdc.c @@ -85,7 +85,7 @@ int main(int argc_, char** argv_) mds_kbdc_parsed_destroy(&result); return fatal; - pfail: + fail: xperror(*argv); mds_kbdc_parsed_destroy(&result); return 1; diff --git a/src/mds-kbdc/parsed.c b/src/mds-kbdc/parsed.c index a049b9c..8ab483b 100644 --- a/src/mds-kbdc/parsed.c +++ b/src/mds-kbdc/parsed.c @@ -150,7 +150,7 @@ mds_kbdc_parse_error_t* mds_kbdc_parsed_new_error(mds_kbdc_parsed_t* restrict th } return error; - pfail: + fail: saved_errno = errno; free(error); this->errors_ptr = old_errors_ptr; diff --git a/src/mds-kbdc/paths.c b/src/mds-kbdc/paths.c index 552aaae..a14681e 100644 --- a/src/mds-kbdc/paths.c +++ b/src/mds-kbdc/paths.c @@ -50,7 +50,7 @@ char* curpath(void) } return cwd; - pfail: + fail: saved_errno = errno; free(old); free(cwd); @@ -104,7 +104,7 @@ char* abspath(const char* path) free(cwd); return buf; - pfail: + fail: saved_errno = errno; free(cwd); errno = saved_errno; @@ -154,7 +154,7 @@ char* relpath(const char* path, const char* base) free(abs); free(absbase); return buf; - pfail: + fail: saved_errno = errno; free(abs); free(absbase); diff --git a/src/mds-kbdc/process-includes.c b/src/mds-kbdc/process-includes.c index 18d995f..57e0502 100644 --- a/src/mds-kbdc/process-includes.c +++ b/src/mds-kbdc/process-includes.c @@ -135,7 +135,7 @@ static int transfer_errors(mds_kbdc_parsed_t* restrict subresult, mds_kbdc_tree_ free(errors); return 0; - pfail: + fail: saved_errno = errno; while (errors_ptr--) mds_kbdc_parse_error_free(errors[errors_ptr]); @@ -223,7 +223,7 @@ static int process_include(mds_kbdc_tree_include_t* restrict tree) mds_kbdc_parsed_destroy(&subresult); return 0; - pfail: + fail: saved_errno = errno; free(dirname); free(cwd); @@ -309,7 +309,7 @@ int process_includes(mds_kbdc_parsed_t* restrict result_) free(included), included_size = 0; return errno = saved_errno, r; - pfail: + fail: return -1; } diff --git a/src/mds-kbdc/raw-data.c b/src/mds-kbdc/raw-data.c index c47269a..359abe8 100644 --- a/src/mds-kbdc/raw-data.c +++ b/src/mds-kbdc/raw-data.c @@ -145,7 +145,7 @@ static char* read_file(const char* restrict pathname, size_t* restrict size) *size = buf_ptr; return content; - pfail: + fail: xperror(*argv); free(old); free(content); @@ -325,7 +325,7 @@ static char** line_split(char* content, size_t length) return lines; - pfail: + fail: xperror(*argv); return NULL; } @@ -370,7 +370,7 @@ static int expand(char** restrict content, size_t* restrict content_size) while (++col % 8); return 0; - pfail: + fail: return -1; } @@ -429,7 +429,7 @@ int read_source_lines(const char* restrict pathname, mds_kbdc_source_code_t* res source_code->line_count = line_count; return 0; - pfail: + fail: xperror(*argv); free(old); free(content); @@ -519,7 +519,7 @@ char* parse_raw_string(const char* restrict string) *p = '\0'; return rc; - pfail: + fail: free(rc); return NULL; #undef r diff --git a/src/mds-kbdc/simplify-tree.c b/src/mds-kbdc/simplify-tree.c index 713a898..c46649e 100644 --- a/src/mds-kbdc/simplify-tree.c +++ b/src/mds-kbdc/simplify-tree.c @@ -321,7 +321,7 @@ static int simplify_macro_call(mds_kbdc_tree_macro_call_t* restrict tree) */ return 0; - pfail: + fail: saved_errno = errno; mds_kbdc_tree_free(dup_arguments); return errno = saved_errno, -1; @@ -352,7 +352,7 @@ static int check_value_statement_before_simplification(mds_kbdc_tree_map_t* rest fail_if (simplify(tree->sequence)); goto again; - pfail: + fail: return -1; } @@ -374,7 +374,7 @@ static int check_value_statement_after_simplification(mds_kbdc_tree_map_t* restr NEW_ERROR(tree->sequence, ERROR, "bad value type"); return 0; - pfail: + fail: return -1; } @@ -530,7 +530,7 @@ static int simplify_map(mds_kbdc_tree_map_t* restrict tree) */ return 0; - pfail: + fail: saved_errno = errno; mds_kbdc_tree_free(dup_sequence); return errno = saved_errno, -1; @@ -603,7 +603,7 @@ static int simplify_alternation(mds_kbdc_tree_alternation_t* restrict tree) } return 0; - pfail: + fail: return -1; } @@ -678,7 +678,7 @@ static mds_kbdc_tree_t* create_permutations(mds_kbdc_tree_t* elements) return first; - pfail: + fail: saved_errno = errno; mds_kbdc_tree_free(first); mds_kbdc_tree_free(subperms); @@ -790,7 +790,7 @@ static int simplify_unordered(mds_kbdc_tree_unordered_t* restrict tree) mds_kbdc_tree_free(arguments); return 0; - pfail: + fail: return -1; } diff --git a/src/mds-kbdc/tree.c b/src/mds-kbdc/tree.c index dd0eb68..ac9de2b 100644 --- a/src/mds-kbdc/tree.c +++ b/src/mds-kbdc/tree.c @@ -331,7 +331,7 @@ mds_kbdc_tree_t* mds_kbdc_tree_dup(const mds_kbdc_tree_t* restrict this) node = &(n->next); goto again; - pfail: + fail: saved_errno = errno; mds_kbdc_tree_free(rc); return errno = saved_errno, NULL; diff --git a/src/mds-kbdc/validate-tree.c b/src/mds-kbdc/validate-tree.c index ed96343..eeb5735 100644 --- a/src/mds-kbdc/validate-tree.c +++ b/src/mds-kbdc/validate-tree.c @@ -151,7 +151,7 @@ static int validate_function(mds_kbdc_tree_function_t* restrict tree) def_includes_ptr = includes_ptr; r = validate_subtree(tree->inner); return function = NULL, r; - pfail: + fail: return -1; } @@ -193,7 +193,7 @@ static int validate_macro(mds_kbdc_tree_macro_t* restrict tree) def_includes_ptr = includes_ptr; r = validate_subtree(tree->inner); return macro = NULL, r; - pfail: + fail: return -1; } @@ -235,7 +235,7 @@ static int validate_information(mds_kbdc_tree_information_t* restrict tree) def_includes_ptr = includes_ptr; r = validate_subtree(tree->inner); return information = NULL, r; - pfail: + fail: return -1; } @@ -277,7 +277,7 @@ static int validate_assumption(mds_kbdc_tree_assumption_t* restrict tree) def_includes_ptr = includes_ptr; r = validate_subtree(tree->inner); return assumption = NULL, r; - pfail: + fail: return -1; } @@ -305,7 +305,7 @@ static int validate_map(mds_kbdc_tree_map_t* restrict tree) else if (function) NEW_ERROR(tree, includes_ptr, ERROR, "mapping-statement inside function definition"); return 0; - pfail: + fail: return -1; } @@ -325,7 +325,7 @@ static int validate_macro_call(mds_kbdc_tree_macro_call_t* restrict tree) else if (function) NEW_ERROR(tree, includes_ptr, ERROR, "macro call inside function definition"); return 0; - pfail: + fail: return -1; } @@ -369,7 +369,7 @@ static int validate_return(mds_kbdc_tree_return_t* restrict tree) if ((function == NULL) && (macro == NULL)) NEW_ERROR(tree, includes_ptr, ERROR, "‘return’ outside function and macro definition"); return 0; - pfail: + fail: return -1; } @@ -385,7 +385,7 @@ static int validate_break(mds_kbdc_tree_break_t* restrict tree) if (fors == 0) NEW_ERROR(tree, includes_ptr, ERROR, "‘break’ outside ‘for’"); return 0; - pfail: + fail: return -1; } @@ -401,7 +401,7 @@ static int validate_continue(mds_kbdc_tree_continue_t* restrict tree) if (fors == 0) NEW_ERROR(tree, includes_ptr, ERROR, "‘continue’ outside ‘for’"); return 0; - pfail: + fail: return -1; } @@ -417,7 +417,7 @@ static int validate_assumption_data(mds_kbdc_tree_t* restrict tree) if (assumption == NULL) NEW_ERROR(tree, includes_ptr, ERROR, "assumption outside assumption clause"); return 0; - pfail: + fail: return -1; } @@ -433,7 +433,7 @@ static int validate_information_data(mds_kbdc_tree_t* restrict tree) if (information == NULL) NEW_ERROR(tree, includes_ptr, ERROR, "information outside information clause"); return 0; - pfail: + fail: return -1; } diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c index 82b3d22..95b7f02 100644 --- a/src/mds-kkbd.c +++ b/src/mds-kkbd.c @@ -250,7 +250,7 @@ int initialise_server(void) return 0; - pfail: + fail: xperror(*argv); if (stage < 5) { @@ -386,7 +386,7 @@ int unmarshal_server(char* state_buf) fail_if (mds_message_unmarshal(&received, state_buf)); return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); free(mapping); @@ -456,7 +456,7 @@ int master_loop(void) fail_if ((errno = pthread_join(kbd_thread, &kbd_ret))); rc = kbd_ret == NULL ? 0 : 1; goto done; - pfail: + fail: xperror(*argv); done: pthread_mutex_destroy(&send_mutex); @@ -490,7 +490,7 @@ void* keyboard_loop(void* data) return NULL; - pfail: + fail: xperror(*argv); raise(SIGTERM); return (void*)1024; diff --git a/src/mds-registry/mds-registry.c b/src/mds-registry/mds-registry.c index 9751ff4..44cbe86 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); @@ -117,7 +117,7 @@ int initialise_server(void) fail_if (mds_message_initialise(&received)); return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); return 1; @@ -188,7 +188,7 @@ int master_loop(void) rc = 0; goto done; - pfail: + fail: xperror(*argv); done: /* Join with all slaves threads. */ diff --git a/src/mds-registry/reexec.c b/src/mds-registry/reexec.c index 6b36cc8..6f6510e 100644 --- a/src/mds-registry/reexec.c +++ b/src/mds-registry/reexec.c @@ -197,7 +197,7 @@ int unmarshal_server(char* state_buf) } return 0; - pfail: + fail: xperror(*argv); mds_message_destroy(&received); if (stage >= 1) diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c index 918b137..5f241ea 100644 --- a/src/mds-registry/registry.c +++ b/src/mds-registry/registry.c @@ -107,7 +107,7 @@ static int handle_close_message(void) free(keys); return 0; - pfail: + fail: xperror(*argv); free(keys); return -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; } @@ -239,7 +239,7 @@ static int registry_action_act(char* command, int action, uint64_t client, hash_ } return 0; - pfail: + fail: xperror(*argv); hash_table_destroy(wait_set, (free_func*)reg_table_free_key, NULL); free(wait_set); diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 54774e2..8a4d221 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -114,7 +114,7 @@ static void* slave_loop(void* data) goto done; - pfail: + fail: xperror(*argv); done: with_mutex (slave_mutex, @@ -198,7 +198,7 @@ int start_slave(hash_table_t* restrict wait_set, const char* restrict recv_clien pthread_mutex_unlock(&slave_mutex); return 0; - pfail: + fail: xperror(*argv); if (locked) pthread_mutex_unlock(&slave_mutex); @@ -284,7 +284,7 @@ slave_t* slave_create(hash_table_t* restrict wait_set, const char* restrict recv return rc; - pfail: + fail: saved_errno = errno; slave_destroy(rc), free(rc); return errno = saved_errno, NULL; diff --git a/src/mds-respawn.c b/src/mds-respawn.c index 3e3406a..b95f40e 100644 --- a/src/mds-respawn.c +++ b/src/mds-respawn.c @@ -157,7 +157,7 @@ int parse_cmdline(void) return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -243,7 +243,7 @@ int preinitialise_server(void) fail_if (xsigaction(SIGUSR2, received_revive) < 0); return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -302,7 +302,7 @@ int postinitialise_server(void) spawn_server(i); return 0; - pfail: + fail: xperror(*argv); return 1; } diff --git a/src/mds-server/client.c b/src/mds-server/client.c index 37ba906..4f5f997 100644 --- a/src/mds-server/client.c +++ b/src/mds-server/client.c @@ -262,7 +262,7 @@ size_t client_unmarshal(client_t* restrict this, char* restrict data) rc += n * sizeof(char); return rc; - pfail: + fail: saved_errno = errno; mds_message_destroy(&(this->message)); for (i = 0; i < this->interception_conditions_count; i++) diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index abfc871..8ef44cc 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -148,7 +148,7 @@ int preinitialise_server(void) return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -348,7 +348,7 @@ void* slave_loop(void* data) return NULL; - pfail: + fail: xperror(*argv); goto done; @@ -483,7 +483,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) multicast->message_prefix = n; message = NULL; -#define pfail fail_in_mutex +#define fail fail_in_mutex /* Queue message multicasting. */ with_mutex (sender->mutex, new_buf = sender->multicasts; @@ -496,7 +496,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) fail_in_mutex: xperror(*argv); ); -#undef pfail +#undef fail done: /* Release resources. */ @@ -509,7 +509,7 @@ void queue_message_multicast(char* message, size_t length, client_t* sender) free(multicast); return; - pfail: + fail: xperror(*argv); goto done; } diff --git a/src/mds-server/receiving.c b/src/mds-server/receiving.c index 100d717..71f1210 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) { @@ -205,7 +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 +#define fail fail_in_mutex with_mutex (client->mutex, if (client->send_pending_size == 0) { @@ -226,9 +226,9 @@ static int assign_and_send_id(client_t* client, const char* message_id) (msgbuf = NULL, rc = 0, errno = 0); fail_in_mutex: ); -#undef pfail +#undef fail - pfail: /* Also success. */ + fail: /* Also success. */ xperror(*argv); free(msgbuf); return rc; @@ -329,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/slavery.c b/src/mds-server/slavery.c index a951f99..7ff801d 100644 --- a/src/mds-server/slavery.c +++ b/src/mds-server/slavery.c @@ -130,7 +130,7 @@ client_t* initialise_client(int client_fd) return information; - pfail: + fail: saved_errno = errno; if (locked) pthread_mutex_unlock(&slave_mutex); @@ -139,6 +139,6 @@ client_t* initialise_client(int client_fd) { with_mutex (slave_mutex, linked_list_remove(&client_list, entry);); } - return saved_errno = errno, NULL; + return errno = saved_errno, NULL; } diff --git a/src/mds-vt.c b/src/mds-vt.c index b268647..1cc377e 100644 --- a/src/mds-vt.c +++ b/src/mds-vt.c @@ -277,7 +277,7 @@ int initialise_server(void) no_display: eprint("no display has been set, how did this happen."); return 1; - pfail: + fail: xperror(*argv); unlink(vtfile_path); if (display_tty_fd >= 0) @@ -451,7 +451,7 @@ int master_loop(void) xperror(*argv); vt_close(display_tty_fd, &old_vt_stat); goto done; - pfail: + fail: xperror(*argv); done: rc |= secondary_thread_failed; @@ -503,7 +503,7 @@ void* secondary_loop(void* data) } goto done; - pfail: + fail: xperror(*argv); secondary_thread_failed = 1; done: diff --git a/src/mds.c b/src/mds.c index 1c0f002..bad0060 100644 --- a/src/mds.c +++ b/src/mds.c @@ -217,7 +217,7 @@ int main(int argc_, char** argv_) return rc; - pfail: + fail: xperror(*argv); rc = 1; goto done; @@ -412,7 +412,7 @@ int spawn_and_respawn_server(int fd) _exit(1); return rc; - pfail: + fail: xperror(*argv); goto done; } @@ -452,7 +452,7 @@ int create_directory_root(const char* pathname) return 0; - pfail: + fail: xperror(*argv); return 1; } @@ -544,7 +544,7 @@ int unlink_recursive(const char* pathname) return rc; - pfail: + fail: xperror(*argv); rc = -1; goto done; -- cgit v1.2.3-70-g09d2 From 8b69e6f4f9e239e660b26695c6a7f5007fcadf4e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Dec 2014 03:42:16 +0100 Subject: with a few exceptions, never return directly on failure, always goto fail by invoking fail_if MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-server/interceptors.c | 45 +++++++++++++++++++--------------------- src/mds-server/interceptors.h | 2 +- src/mds-server/mds-server.c | 12 +++++------ src/mds-server/mds-server.h | 2 +- src/mds-server/receiving.c | 14 ++++++------- src/mds-server/reexec.c | 39 +++++++++++++++++------------------ src/mds-server/slavery.c | 19 ++++++++++++----- src/mds-server/slavery.h | 2 +- src/mds.c | 48 +++++++++++++++++++++---------------------- 9 files changed, 93 insertions(+), 90 deletions(-) (limited to 'src/mds.c') 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); diff --git a/src/mds.c b/src/mds.c index bad0060..8dce0d1 100644 --- a/src/mds.c +++ b/src/mds.c @@ -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; } -- cgit v1.2.3-70-g09d2 From 7bd34436ab676eee1ace2684eb785e536846e199 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 9 Dec 2014 04:03:11 +0100 Subject: make fail_if friendlier to complex if-statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/macros.h | 8 ++++---- src/mds-base.c | 4 ++-- src/mds.c | 30 +++++++++++------------------- 3 files changed, 17 insertions(+), 25 deletions(-) (limited to 'src/mds.c') diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h index f82c01d..5329298 100644 --- a/src/libmdsserver/macros.h +++ b/src/libmdsserver/macros.h @@ -384,14 +384,14 @@ * @param ... The condition */ #define fail_if(...) \ - if (__VA_ARGS__) \ - do \ + do \ + if (__VA_ARGS__) \ { \ if ((errno != EMSGSIZE) && (errno != ECONNRESET) && (errno != EINTR)) \ fprintf(stderr, "failure at %s:%i\n", __FILE__, __LINE__); \ - goto fail; \ + goto fail; \ } \ - while (0) + while (0) /** diff --git a/src/mds-base.c b/src/mds-base.c index e80b902..85f0ef7 100644 --- a/src/mds-base.c +++ b/src/mds-base.c @@ -630,9 +630,9 @@ int trap_signals(void) /* Implement death on SIGDANGER or ignoral of SIGDANGER. */ if (server_characteristics.danger_is_deadly && !is_immortal) - { fail_if (xsigaction(SIGDANGER, commit_suicide) < 0); } + fail_if (xsigaction(SIGDANGER, commit_suicide) < 0); else - { fail_if (xsigaction(SIGDANGER, received_danger) < 0); } + fail_if (xsigaction(SIGDANGER, received_danger) < 0); return 0; fail: diff --git a/src/mds.c b/src/mds.c index 8dce0d1..29b1d94 100644 --- a/src/mds.c +++ b/src/mds.c @@ -445,15 +445,11 @@ int create_directory_root(const char* pathname) else /* Directory is missing, create it. */ if (mkdir(pathname, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) - { - /* Unlikely race condition. */ - fail_if (errno != EEXIST); - } + /* Unlikely race condition. */ + fail_if (errno != EEXIST); else - { - /* Set ownership. */ - fail_if (chown(pathname, ROOT_USER_UID, ROOT_GROUP_GID) < 0); - } + /* Set ownership. */ + fail_if (chown(pathname, ROOT_USER_UID, ROOT_GROUP_GID) < 0); return 0; fail: @@ -483,17 +479,13 @@ int create_directory_user(const char* pathname) } } else - { - /* Directory is missing, create it. */ - if (mkdir(pathname, S_IRWXU) < 0) - { - /* Unlikely race condition. */ - fail_if (errno != EEXIST); - } - else - /* Set ownership. */ - fail_if (chown(pathname, getuid(), NOBODY_GROUP_GID) < 0); - } + /* Directory is missing, create it. */ + if (mkdir(pathname, S_IRWXU) < 0) + /* Unlikely race condition. */ + fail_if (errno != EEXIST); + else + /* Set ownership. */ + fail_if (chown(pathname, getuid(), NOBODY_GROUP_GID) < 0); return 0; fail: -- cgit v1.2.3-70-g09d2