diff options
58 files changed, 1049 insertions, 1218 deletions
diff --git a/.clang-format b/.clang-format index 87ad91d..64a2caf 100644 --- a/.clang-format +++ b/.clang-format @@ -1,14 +1,29 @@ --- -# The "Linux kernel" style, borrowed and modified from -# https://clang.llvm.org/docs/ClangFormatStyleOptions.html#examples -BasedOnStyle: LLVM +Language: Cpp +BasedOnStyle: Chromium + IndentWidth: 8 UseTab: AlignWithSpaces -BreakBeforeBraces: Linux -AllowShortIfStatementsOnASingleLine: false -IndentCaseLabels: false -ColumnLimit: 100 -AlignConsecutiveMacros: Consecutive +AccessModifierOffset: -4 +AlignConsecutiveMacros: AcrossComments AllowShortFunctionsOnASingleLine: Empty +BinPackArguments: false +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma BreakStringLiterals: false +ColumnLimit: 100 +IncludeBlocks: Regroup +SpacesBeforeTrailingComments: 1 +WrapNamespaceBodyWithEmptyLines: Always + +IncludeCategories: + - Regex: '^".+' + Priority: 1 + - Regex: '^<git2\.h>|<json-c\/|<sodium\.h>|<sqlite3\.h>' + Priority: 2 + - Regex: '^<.*\.h>$' + Priority: 3 + - Regex: '.*' + Priority: 4 +... diff --git a/src/base64.c b/src/base64.c index 82b1c24..733c700 100644 --- a/src/base64.c +++ b/src/base64.c @@ -6,6 +6,7 @@ */ #include "base64.h" + #include "log.h" #include <sodium.h> @@ -16,11 +17,10 @@ static const int base64_variant = sodium_base64_VARIANT_ORIGINAL; -int base64_encode(const unsigned char *src, size_t src_len, char **_dst) -{ +int base64_encode(const unsigned char* src, size_t src_len, char** _dst) { const size_t dst_len = sodium_base64_encoded_len(src_len, base64_variant); - char *dst = calloc(dst_len, 1); + char* dst = calloc(dst_len, 1); if (!dst) { log_errno("calloc"); return -1; @@ -32,13 +32,12 @@ int base64_encode(const unsigned char *src, size_t src_len, char **_dst) return 0; } -int base64_decode(const char *src, unsigned char **_dst, size_t *_dst_len) -{ +int base64_decode(const char* src, unsigned char** _dst, size_t* _dst_len) { const size_t src_len = strlen(src); const size_t dst_max_len = src_len / 4 * 3; size_t dst_len = 0; - unsigned char *dst = calloc(dst_max_len, 1); + unsigned char* dst = calloc(dst_max_len, 1); if (!dst) { log_errno("calloc"); return -1; diff --git a/src/base64.h b/src/base64.h index 0850633..a1cc7b8 100644 --- a/src/base64.h +++ b/src/base64.h @@ -10,7 +10,7 @@ #include <stddef.h> -int base64_encode(const unsigned char *src, size_t src_len, char **dst); -int base64_decode(const char *src, unsigned char **dst, size_t *dst_len); +int base64_encode(const unsigned char* src, size_t src_len, char** dst); +int base64_decode(const char* src, unsigned char** dst, size_t* dst_len); #endif @@ -6,6 +6,7 @@ */ #include "buf.h" + #include "log.h" #include <stdint.h> @@ -14,12 +15,11 @@ struct buf { uint32_t size; - const void *data; + const void* data; }; -int buf_create(struct buf **_buf, const void *data, uint32_t size) -{ - struct buf *buf = malloc(sizeof(struct buf)); +int buf_create(struct buf** _buf, const void* data, uint32_t size) { + struct buf* buf = malloc(sizeof(struct buf)); if (!buf) { log_errno("malloc"); return -1; @@ -32,22 +32,18 @@ int buf_create(struct buf **_buf, const void *data, uint32_t size) return 0; } -int buf_create_from_string(struct buf **buf, const char *str) -{ +int buf_create_from_string(struct buf** buf, const char* str) { return buf_create(buf, str, strlen(str) + 1); } -void buf_destroy(struct buf *buf) -{ +void buf_destroy(struct buf* buf) { free(buf); } -uint32_t buf_get_size(const struct buf *buf) -{ +uint32_t buf_get_size(const struct buf* buf) { return buf->size; } -const void *buf_get_data(const struct buf *buf) -{ +const void* buf_get_data(const struct buf* buf) { return buf->data; } @@ -12,11 +12,11 @@ struct buf; -int buf_create(struct buf **, const void *, uint32_t); -int buf_create_from_string(struct buf **, const char *); -void buf_destroy(struct buf *); +int buf_create(struct buf**, const void*, uint32_t); +int buf_create_from_string(struct buf**, const char*); +void buf_destroy(struct buf*); -uint32_t buf_get_size(const struct buf *); -const void *buf_get_data(const struct buf *); +uint32_t buf_get_size(const struct buf*); +const void* buf_get_data(const struct buf*); #endif @@ -6,12 +6,14 @@ */ #include "ci.h" + #include "file.h" #include "git.h" #include "log.h" #include "process.h" #include <git2.h> + #include <stddef.h> #include <stdlib.h> @@ -31,15 +33,13 @@ static const char *ci_env[] = { }; /* clang-format on */ -static int ci_run_script(const char *script, struct process_output *result) -{ - const char *args[] = {script, NULL}; +static int ci_run_script(const char* script, struct process_output* result) { + const char* args[] = {script, NULL}; return process_execute_and_capture(args, ci_env, result); } -int ci_run(struct process_output *result) -{ - for (const char **script = ci_scripts; *script; ++script) { +int ci_run(struct process_output* result) { + for (const char** script = ci_scripts; *script; ++script) { if (!file_exists(*script)) continue; log("Going to run: %s\n", *script); @@ -50,14 +50,12 @@ int ci_run(struct process_output *result) return -1; } -static void ci_cleanup_git_repo(git_repository *repo) -{ +static void ci_cleanup_git_repo(git_repository* repo) { rm_rf(git_repository_workdir(repo)); libgit_repository_free(repo); } -static int ci_prepare_git_repo(git_repository **repo, const char *url, const char *rev) -{ +static int ci_prepare_git_repo(git_repository** repo, const char* url, const char* rev) { int ret = 0; ret = libgit_clone_to_tmp(repo, url); @@ -76,10 +74,9 @@ cleanup_repo: return ret; } -int ci_run_git_repo(const char *url, const char *rev, struct process_output *output) -{ - char *oldpwd = NULL; - git_repository *repo = NULL; +int ci_run_git_repo(const char* url, const char* rev, struct process_output* output) { + char* oldpwd = NULL; + git_repository* repo = NULL; int ret = 0; ret = ci_prepare_git_repo(&repo, url, rev); @@ -10,7 +10,7 @@ #include "process.h" -int ci_run(struct process_output *); +int ci_run(struct process_output*); /* * This is a high-level function. It's basically equivalent to the following @@ -25,6 +25,6 @@ int ci_run(struct process_output *); * rm -rf "$dir" * */ -int ci_run_git_repo(const char *url, const char *rev, struct process_output *); +int ci_run_git_repo(const char* url, const char* rev, struct process_output*); #endif diff --git a/src/client.c b/src/client.c index 323ff40..d703baa 100644 --- a/src/client.c +++ b/src/client.c @@ -6,6 +6,7 @@ */ #include "client.h" + #include "cmd_line.h" #include "compiler.h" #include "const.h" @@ -22,11 +23,10 @@ struct client { int dummy; }; -int client_create(struct client **_client) -{ +int client_create(struct client** _client) { int ret = 0; - struct client *client = malloc(sizeof(struct client)); + struct client* client = malloc(sizeof(struct client)); if (!client) { log_errno("malloc"); return -1; @@ -36,13 +36,11 @@ int client_create(struct client **_client) return ret; } -void client_destroy(struct client *client) -{ +void client_destroy(struct client* client) { free(client); } -static int make_request(struct jsonrpc_request **request, int argc, const char **argv) -{ +static int make_request(struct jsonrpc_request** request, int argc, const char** argv) { if (argc < 1) { exit_with_usage_err("no action specified"); return -1; @@ -52,7 +50,7 @@ static int make_request(struct jsonrpc_request **request, int argc, const char * if (argc != 3) return -1; - struct run *run = NULL; + struct run* run = NULL; int ret = run_queued(&run, argv[1], argv[2]); if (ret < 0) return ret; @@ -69,12 +67,13 @@ static int make_request(struct jsonrpc_request **request, int argc, const char * return -1; } -int client_main(UNUSED const struct client *client, const struct settings *settings, int argc, - const char **argv) -{ +int client_main(UNUSED const struct client* client, + const struct settings* settings, + int argc, + const char** argv) { int ret = 0; - struct jsonrpc_request *request = NULL; + struct jsonrpc_request* request = NULL; ret = make_request(&request, argc, argv); if (ret < 0) { exit_with_usage_err("invalid request"); @@ -90,12 +89,12 @@ int client_main(UNUSED const struct client *client, const struct settings *setti if (ret < 0) goto close; - struct jsonrpc_response *response = NULL; + struct jsonrpc_response* response = NULL; ret = jsonrpc_response_recv(&response, fd); if (ret < 0) goto close; - const char *response_str = jsonrpc_response_to_string(response); + const char* response_str = jsonrpc_response_to_string(response); if (response_str) { if (jsonrpc_response_is_error(response)) ret = -1; diff --git a/src/client.h b/src/client.h index 0d97a29..5df5561 100644 --- a/src/client.h +++ b/src/client.h @@ -9,15 +9,15 @@ #define __CLIENT_H__ struct settings { - const char *host; - const char *port; + const char* host; + const char* port; }; struct client; -int client_create(struct client **); -void client_destroy(struct client *); +int client_create(struct client**); +void client_destroy(struct client*); -int client_main(const struct client *, const struct settings *, int argc, const char **argv); +int client_main(const struct client*, const struct settings*, int argc, const char** argv); #endif diff --git a/src/client_main.c b/src/client_main.c index 35c03ce..863cf9d 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -13,8 +13,7 @@ #include <getopt.h> #include <unistd.h> -static struct settings default_settings(void) -{ +static struct settings default_settings(void) { struct settings settings = { .host = default_host, .port = default_port, @@ -22,16 +21,14 @@ static struct settings default_settings(void) return settings; } -const char *get_usage_string(void) -{ +const char* get_usage_string(void) { return "[-h|--help] [-V|--version] [-v|--verbose] [-H|--host HOST] [-p|--port PORT] ACTION [ARG...]\n\ \n\ available actions:\n\ \t" CMD_QUEUE_RUN " URL REV - schedule a CI run of repository at URL, revision REV"; } -static int parse_settings(struct settings *settings, int argc, char *argv[]) -{ +static int parse_settings(struct settings* settings, int argc, char* argv[]) { int opt, longind; *settings = default_settings(); @@ -49,34 +46,33 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "hVvH:p:", long_options, &longind)) != -1) { switch (opt) { - case 'h': - exit_with_usage(0); - break; - case 'V': - exit_with_version(); - break; - case 'v': - g_log_lvl = LOG_LVL_DEBUG; - break; - case 'H': - settings->host = optarg; - break; - case 'p': - settings->port = optarg; - break; - default: - exit_with_usage(1); - break; + case 'h': + exit_with_usage(0); + break; + case 'V': + exit_with_version(); + break; + case 'v': + g_log_lvl = LOG_LVL_DEBUG; + break; + case 'H': + settings->host = optarg; + break; + case 'p': + settings->port = optarg; + break; + default: + exit_with_usage(1); + break; } } return 0; } -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { struct settings settings; - struct client *client = NULL; + struct client* client = NULL; int ret = 0; ret = parse_settings(&settings, argc, argv); @@ -87,7 +83,7 @@ int main(int argc, char *argv[]) if (ret < 0) return ret; - ret = client_main(client, &settings, argc - optind, (const char **)argv + optind); + ret = client_main(client, &settings, argc - optind, (const char**)argv + optind); if (ret < 0) goto destroy_client; diff --git a/src/cmd_line.c b/src/cmd_line.c index bf3aa24..be1a947 100644 --- a/src/cmd_line.c +++ b/src/cmd_line.c @@ -6,6 +6,7 @@ */ #include "cmd_line.h" + #include "const.h" #include "file.h" #include "log.h" @@ -14,20 +15,18 @@ #include <stdlib.h> #include <string.h> -static char *get_current_binary_path(void) -{ +static char* get_current_binary_path(void) { return readlink_wrapper("/proc/self/exe"); } -static char *get_current_binary_name(void) -{ - char *path = get_current_binary_path(); +static char* get_current_binary_name(void) { + char* path = get_current_binary_path(); if (!path) return NULL; - char *name = basename(path); + char* name = basename(path); - char *result = strdup(name); + char* result = strdup(name); if (!result) { log_errno("strdup"); goto free_path; @@ -39,29 +38,26 @@ free_path: return result; } -void exit_with_usage(int ec) -{ - FILE *dest = stdout; +void exit_with_usage(int ec) { + FILE* dest = stdout; if (ec) dest = stderr; - char *binary = get_current_binary_name(); + char* binary = get_current_binary_name(); fprintf(dest, "usage: %s %s\n", binary ? binary : "prog", get_usage_string()); free(binary); exit(ec); } -void exit_with_usage_err(const char *msg) -{ +void exit_with_usage_err(const char* msg) { if (msg) fprintf(stderr, "usage error: %s\n", msg); exit_with_usage(1); } -void exit_with_version(void) -{ - char *binary = get_current_binary_name(); +void exit_with_version(void) { + char* binary = get_current_binary_name(); printf("%s v%s (%s)\n", binary ? binary : "prog", project_version, project_rev); free(binary); diff --git a/src/cmd_line.h b/src/cmd_line.h index e1a7e96..fcdaf85 100644 --- a/src/cmd_line.h +++ b/src/cmd_line.h @@ -8,10 +8,10 @@ #ifndef __CMD_LINE_H__ #define __CMD_LINE_H__ -const char *get_usage_string(void); +const char* get_usage_string(void); void exit_with_usage(int ec); -void exit_with_usage_err(const char *msg); +void exit_with_usage_err(const char* msg); void exit_with_version(void); #endif diff --git a/src/command.c b/src/command.c index d480709..bba4ace 100644 --- a/src/command.c +++ b/src/command.c @@ -6,6 +6,7 @@ */ #include "command.h" + #include "compiler.h" #include "event_loop.h" #include "json_rpc.h" @@ -16,13 +17,12 @@ #include <string.h> struct cmd_dispatcher { - struct cmd_desc *cmds; + struct cmd_desc* cmds; size_t numof_cmds; - void *ctx; + void* ctx; }; -static int copy_cmd(struct cmd_desc *dest, const struct cmd_desc *src) -{ +static int copy_cmd(struct cmd_desc* dest, const struct cmd_desc* src) { dest->name = strdup(src->name); if (!dest->name) { log_errno("strdup"); @@ -32,13 +32,11 @@ static int copy_cmd(struct cmd_desc *dest, const struct cmd_desc *src) return 0; } -static void free_cmd(struct cmd_desc *desc) -{ +static void free_cmd(struct cmd_desc* desc) { free(desc->name); } -static int copy_cmds(struct cmd_desc *dest, const struct cmd_desc *src, size_t numof_cmds) -{ +static int copy_cmds(struct cmd_desc* dest, const struct cmd_desc* src, size_t numof_cmds) { size_t numof_copied = 0; int ret = 0; @@ -57,18 +55,18 @@ free: return -1; } -static void free_cmds(struct cmd_desc *cmds, size_t numof_cmds) -{ +static void free_cmds(struct cmd_desc* cmds, size_t numof_cmds) { for (size_t i = 0; i < numof_cmds; ++i) free_cmd(&cmds[i]); } -int cmd_dispatcher_create(struct cmd_dispatcher **_dispatcher, struct cmd_desc *cmds, - size_t numof_cmds, void *ctx) -{ +int cmd_dispatcher_create(struct cmd_dispatcher** _dispatcher, + struct cmd_desc* cmds, + size_t numof_cmds, + void* ctx) { int ret = 0; - struct cmd_dispatcher *dispatcher = malloc(sizeof(struct cmd_dispatcher)); + struct cmd_dispatcher* dispatcher = malloc(sizeof(struct cmd_dispatcher)); if (!dispatcher) { log_errno("malloc"); return -1; @@ -99,21 +97,20 @@ free: return -1; } -void cmd_dispatcher_destroy(struct cmd_dispatcher *dispatcher) -{ +void cmd_dispatcher_destroy(struct cmd_dispatcher* dispatcher) { free_cmds(dispatcher->cmds, dispatcher->numof_cmds); free(dispatcher->cmds); free(dispatcher); } -static int cmd_dispatcher_handle_internal(const struct cmd_dispatcher *dispatcher, - const struct jsonrpc_request *request, - struct jsonrpc_response **result, void *arg) -{ - const char *actual_cmd = jsonrpc_request_get_method(request); +static int cmd_dispatcher_handle_internal(const struct cmd_dispatcher* dispatcher, + const struct jsonrpc_request* request, + struct jsonrpc_response** result, + void* arg) { + const char* actual_cmd = jsonrpc_request_get_method(request); for (size_t i = 0; i < dispatcher->numof_cmds; ++i) { - struct cmd_desc *cmd = &dispatcher->cmds[i]; + struct cmd_desc* cmd = &dispatcher->cmds[i]; if (strcmp(cmd->name, actual_cmd)) continue; @@ -125,15 +122,14 @@ static int cmd_dispatcher_handle_internal(const struct cmd_dispatcher *dispatche return -1; } -int cmd_dispatcher_handle(const struct cmd_dispatcher *dispatcher, - const struct jsonrpc_request *command, struct jsonrpc_response **result) -{ +int cmd_dispatcher_handle(const struct cmd_dispatcher* dispatcher, + const struct jsonrpc_request* command, + struct jsonrpc_response** result) { return cmd_dispatcher_handle_internal(dispatcher, command, result, dispatcher->ctx); } -static struct cmd_conn_ctx *make_conn_ctx(int fd, void *arg) -{ - struct cmd_conn_ctx *ctx = malloc(sizeof(struct cmd_conn_ctx)); +static struct cmd_conn_ctx* make_conn_ctx(int fd, void* arg) { + struct cmd_conn_ctx* ctx = malloc(sizeof(struct cmd_conn_ctx)); if (!ctx) { log_errno("malloc"); return NULL; @@ -145,40 +141,39 @@ static struct cmd_conn_ctx *make_conn_ctx(int fd, void *arg) return ctx; } -static int cmd_dispatcher_handle_conn_internal(int conn_fd, struct cmd_dispatcher *dispatcher) -{ +static int cmd_dispatcher_handle_conn_internal(int conn_fd, struct cmd_dispatcher* dispatcher) { int ret = 0; - struct cmd_conn_ctx *new_ctx = make_conn_ctx(conn_fd, dispatcher->ctx); + struct cmd_conn_ctx* new_ctx = make_conn_ctx(conn_fd, dispatcher->ctx); if (!new_ctx) return -1; - struct jsonrpc_request *request = NULL; + struct jsonrpc_request* request = NULL; ret = jsonrpc_request_recv(&request, conn_fd); if (ret < 0) goto free_ctx; const int requires_response = !jsonrpc_request_is_notification(request); - struct jsonrpc_response *default_response = NULL; + struct jsonrpc_response* default_response = NULL; if (requires_response) { ret = jsonrpc_response_create(&default_response, request, NULL); if (ret < 0) goto free_request; } - struct jsonrpc_response *default_error = NULL; + struct jsonrpc_response* default_error = NULL; if (requires_response) { ret = jsonrpc_error_create(&default_error, request, -1, "An error occured"); if (ret < 0) goto free_default_response; } - struct jsonrpc_response *response = NULL; + struct jsonrpc_response* response = NULL; ret = cmd_dispatcher_handle_internal(dispatcher, request, &response, new_ctx); if (requires_response) { - struct jsonrpc_response *actual_response = response; + struct jsonrpc_response* actual_response = response; if (!actual_response) { actual_response = ret < 0 ? default_error : default_response; } @@ -207,18 +202,18 @@ free_ctx: return ret; } -int cmd_dispatcher_handle_conn(int conn_fd, void *_dispatcher) -{ - return cmd_dispatcher_handle_conn_internal(conn_fd, (struct cmd_dispatcher *)_dispatcher); +int cmd_dispatcher_handle_conn(int conn_fd, void* _dispatcher) { + return cmd_dispatcher_handle_conn_internal(conn_fd, (struct cmd_dispatcher*)_dispatcher); } -int cmd_dispatcher_handle_event(UNUSED struct event_loop *loop, int fd, short revents, - void *_dispatcher) -{ +int cmd_dispatcher_handle_event(UNUSED struct event_loop* loop, + int fd, + short revents, + void* _dispatcher) { if (!(revents & POLLIN)) { log_err("Descriptor %d is not readable\n", fd); return -1; } - return cmd_dispatcher_handle_conn_internal(fd, (struct cmd_dispatcher *)_dispatcher); + return cmd_dispatcher_handle_conn_internal(fd, (struct cmd_dispatcher*)_dispatcher); } diff --git a/src/command.h b/src/command.h index be8b97a..fea3118 100644 --- a/src/command.h +++ b/src/command.h @@ -13,32 +13,33 @@ #include <stddef.h> -typedef int (*cmd_handler)(const struct jsonrpc_request *request, - struct jsonrpc_response **response, void *ctx); +typedef int (*cmd_handler)(const struct jsonrpc_request* request, + struct jsonrpc_response** response, + void* ctx); struct cmd_desc { - char *name; + char* name; cmd_handler handler; }; struct cmd_dispatcher; -int cmd_dispatcher_create(struct cmd_dispatcher **, struct cmd_desc *, size_t numof_defs, - void *ctx); -void cmd_dispatcher_destroy(struct cmd_dispatcher *); +int cmd_dispatcher_create(struct cmd_dispatcher**, struct cmd_desc*, size_t numof_defs, void* ctx); +void cmd_dispatcher_destroy(struct cmd_dispatcher*); -int cmd_dispatcher_handle(const struct cmd_dispatcher *, const struct jsonrpc_request *command, - struct jsonrpc_response **response); +int cmd_dispatcher_handle(const struct cmd_dispatcher*, + const struct jsonrpc_request* command, + struct jsonrpc_response** response); struct cmd_conn_ctx { int fd; - void *arg; + void* arg; }; /* This is supposed to be used as an argument to tcp_server_accept. */ -int cmd_dispatcher_handle_conn(int conn_fd, void *dispatcher); +int cmd_dispatcher_handle_conn(int conn_fd, void* dispatcher); /* This is supposed to be used as an argument to event_loop_add. */ -int cmd_dispatcher_handle_event(struct event_loop *, int fd, short revents, void *arg); +int cmd_dispatcher_handle_event(struct event_loop*, int fd, short revents, void* arg); #endif diff --git a/src/const.h b/src/const.h index d7555dd..15079e1 100644 --- a/src/const.h +++ b/src/const.h @@ -8,12 +8,12 @@ #ifndef __CONST_H__ #define __CONST_H__ -extern const char *project_version; -extern const char *project_rev; +extern const char* project_version; +extern const char* project_rev; -extern const char *default_host; -extern const char *default_port; -extern const char *default_sqlite_path; +extern const char* default_host; +extern const char* default_port; +extern const char* default_sqlite_path; #define CMD_QUEUE_RUN "queue-run" #define CMD_NEW_WORKER "new-worker" diff --git a/src/event_loop.c b/src/event_loop.c index 736a553..54b83db 100644 --- a/src/event_loop.c +++ b/src/event_loop.c @@ -6,6 +6,7 @@ */ #include "event_loop.h" + #include "log.h" #include "string.h" @@ -20,15 +21,14 @@ struct event_fd { int fd; short events; event_handler handler; - void *arg; + void* arg; int once; SIMPLEQ_ENTRY(event_fd) entries; }; -static struct event_fd *event_fd_create(int fd, short events, event_handler handler, void *arg) -{ - struct event_fd *res = calloc(1, sizeof(struct event_fd)); +static struct event_fd* event_fd_create(int fd, short events, event_handler handler, void* arg) { + struct event_fd* res = calloc(1, sizeof(struct event_fd)); if (!res) { log_errno("calloc"); return NULL; @@ -43,21 +43,18 @@ static struct event_fd *event_fd_create(int fd, short events, event_handler hand return res; } -static void event_fd_destroy(struct event_fd *entry) -{ +static void event_fd_destroy(struct event_fd* entry) { free(entry); } -static void event_fd_queue_create(struct event_fd_queue *queue) -{ +static void event_fd_queue_create(struct event_fd_queue* queue) { SIMPLEQ_INIT(queue); } -static void event_fd_queue_destroy(struct event_fd_queue *queue) -{ - struct event_fd *entry1 = SIMPLEQ_FIRST(queue); +static void event_fd_queue_destroy(struct event_fd_queue* queue) { + struct event_fd* entry1 = SIMPLEQ_FIRST(queue); while (entry1) { - struct event_fd *entry2 = SIMPLEQ_NEXT(entry1, entries); + struct event_fd* entry2 = SIMPLEQ_NEXT(entry1, entries); event_fd_destroy(entry1); entry1 = entry2; } @@ -69,11 +66,10 @@ struct event_loop { struct event_fd_queue entries; }; -int event_loop_create(struct event_loop **_loop) -{ +int event_loop_create(struct event_loop** _loop) { int ret = 0; - struct event_loop *loop = calloc(1, sizeof(struct event_loop)); + struct event_loop* loop = calloc(1, sizeof(struct event_loop)); if (!loop) { log_errno("calloc"); return -1; @@ -85,14 +81,12 @@ int event_loop_create(struct event_loop **_loop) return ret; } -void event_loop_destroy(struct event_loop *loop) -{ +void event_loop_destroy(struct event_loop* loop) { event_fd_queue_destroy(&loop->entries); free(loop); } -static void event_loop_add_internal(struct event_loop *loop, struct event_fd *entry) -{ +static void event_loop_add_internal(struct event_loop* loop, struct event_fd* entry) { log_debug("Adding descriptor %d to event loop\n", entry->fd); nfds_t nfds = loop->nfds + 1; @@ -100,19 +94,24 @@ static void event_loop_add_internal(struct event_loop *loop, struct event_fd *en loop->nfds = nfds; } -int event_loop_add(struct event_loop *loop, int fd, short events, event_handler handler, void *arg) -{ - struct event_fd *entry = event_fd_create(fd, events, handler, arg); +int event_loop_add(struct event_loop* loop, + int fd, + short events, + event_handler handler, + void* arg) { + struct event_fd* entry = event_fd_create(fd, events, handler, arg); if (!entry) return -1; event_loop_add_internal(loop, entry); return 0; } -int event_loop_add_once(struct event_loop *loop, int fd, short events, event_handler handler, - void *arg) -{ - struct event_fd *entry = event_fd_create(fd, events, handler, arg); +int event_loop_add_once(struct event_loop* loop, + int fd, + short events, + event_handler handler, + void* arg) { + struct event_fd* entry = event_fd_create(fd, events, handler, arg); if (!entry) return -1; entry->once = 1; @@ -120,8 +119,7 @@ int event_loop_add_once(struct event_loop *loop, int fd, short events, event_han return 0; } -static void event_loop_remove(struct event_loop *loop, struct event_fd *entry) -{ +static void event_loop_remove(struct event_loop* loop, struct event_fd* entry) { log_debug("Removing descriptor %d from event loop\n", entry->fd); SIMPLEQ_REMOVE(&loop->entries, entry, event_fd, entries); @@ -129,21 +127,19 @@ static void event_loop_remove(struct event_loop *loop, struct event_fd *entry) --loop->nfds; } -static char *append_event(char *buf, size_t sz, char *ptr, const char *event) -{ +static char* append_event(char* buf, size_t sz, char* ptr, const char* event) { if (ptr > buf) ptr = string_append(ptr, buf + sz, ","); return string_append(ptr, buf + sz, event); } -static char *events_to_string(short events) -{ +static char* events_to_string(short events) { const size_t sz = 128; - char *buf = calloc(1, sz); + char* buf = calloc(1, sz); if (!buf) return NULL; - char *ptr = buf; + char* ptr = buf; if (events & POLLNVAL) ptr = append_event(buf, sz, ptr, "POLLNVAL"); @@ -163,15 +159,14 @@ static char *events_to_string(short events) return buf; } -static struct pollfd *make_pollfds(const struct event_loop *loop) -{ - struct pollfd *fds = calloc(loop->nfds, sizeof(struct pollfd)); +static struct pollfd* make_pollfds(const struct event_loop* loop) { + struct pollfd* fds = calloc(loop->nfds, sizeof(struct pollfd)); if (!fds) { log_errno("calloc"); return NULL; } - struct event_fd *entry = SIMPLEQ_FIRST(&loop->entries); + struct event_fd* entry = SIMPLEQ_FIRST(&loop->entries); for (nfds_t i = 0; i < loop->nfds; ++i, entry = SIMPLEQ_NEXT(entry, entries)) { fds[i].fd = entry->fd; fds[i].events = entry->events; @@ -179,7 +174,7 @@ static struct pollfd *make_pollfds(const struct event_loop *loop) log_debug("Descriptors:\n"); for (nfds_t i = 0; i < loop->nfds; ++i) { - char *events = events_to_string(fds[i].events); + char* events = events_to_string(fds[i].events); log_debug(" %d (%s)\n", fds[i].fd, events ? events : ""); free(events); } @@ -187,13 +182,12 @@ static struct pollfd *make_pollfds(const struct event_loop *loop) return fds; } -int event_loop_run(struct event_loop *loop) -{ +int event_loop_run(struct event_loop* loop) { /* Cache the number of event descriptors so that event handlers can * append new ones. */ const nfds_t nfds = loop->nfds; - struct pollfd *fds = make_pollfds(loop); + struct pollfd* fds = make_pollfds(loop); if (!fds) return -1; @@ -204,14 +198,14 @@ int event_loop_run(struct event_loop *loop) } ret = 0; - struct event_fd *entry = SIMPLEQ_FIRST(&loop->entries); + struct event_fd* entry = SIMPLEQ_FIRST(&loop->entries); for (nfds_t i = 0; i < nfds; ++i) { - struct event_fd *next = SIMPLEQ_NEXT(entry, entries); + struct event_fd* next = SIMPLEQ_NEXT(entry, entries); if (!fds[i].revents) goto next; - char *events = events_to_string(fds[i].revents); + char* events = events_to_string(fds[i].revents); log_debug("Descriptor %d is ready: %s\n", fds[i].fd, events ? events : ""); free(events); diff --git a/src/event_loop.h b/src/event_loop.h index 992eb5c..2c23198 100644 --- a/src/event_loop.h +++ b/src/event_loop.h @@ -10,14 +10,14 @@ struct event_loop; -int event_loop_create(struct event_loop **); -void event_loop_destroy(struct event_loop *); +int event_loop_create(struct event_loop**); +void event_loop_destroy(struct event_loop*); -int event_loop_run(struct event_loop *); +int event_loop_run(struct event_loop*); -typedef int (*event_handler)(struct event_loop *, int fd, short revents, void *arg); +typedef int (*event_handler)(struct event_loop*, int fd, short revents, void* arg); -int event_loop_add(struct event_loop *, int fd, short events, event_handler, void *arg); -int event_loop_add_once(struct event_loop *, int fd, short events, event_handler, void *arg); +int event_loop_add(struct event_loop*, int fd, short events, event_handler, void* arg); +int event_loop_add_once(struct event_loop*, int fd, short events, event_handler, void* arg); #endif @@ -6,6 +6,7 @@ */ #include "file.h" + #include "compiler.h" #include "log.h" @@ -16,9 +17,10 @@ #include <sys/stat.h> #include <unistd.h> -static int unlink_cb(const char *fpath, UNUSED const struct stat *sb, UNUSED int typeflag, - UNUSED struct FTW *ftwbuf) -{ +static int unlink_cb(const char* fpath, + UNUSED const struct stat* sb, + UNUSED int typeflag, + UNUSED struct FTW* ftwbuf) { int ret = 0; ret = remove(fpath); @@ -30,14 +32,12 @@ static int unlink_cb(const char *fpath, UNUSED const struct stat *sb, UNUSED int return ret; } -int rm_rf(const char *dir) -{ +int rm_rf(const char* dir) { log("Recursively removing directory: %s\n", dir); return nftw(dir, unlink_cb, 64, FTW_DEPTH | FTW_PHYS); } -int chdir_wrapper(const char *dir, char **old) -{ +int chdir_wrapper(const char* dir, char** old) { int ret = 0; if (old) { @@ -63,13 +63,12 @@ free_old: return ret; } -char *readlink_wrapper(const char *path) -{ +char* readlink_wrapper(const char* path) { size_t current_size = 256; - char *buf = NULL; + char* buf = NULL; while (1) { - char *tmp_buf = realloc(buf, current_size); + char* tmp_buf = realloc(buf, current_size); if (!tmp_buf) { log_errno("realloc"); goto free; @@ -99,8 +98,7 @@ free: return NULL; } -int file_dup(int fd) -{ +int file_dup(int fd) { int ret = 0; ret = fcntl(fd, F_DUPFD_CLOEXEC, 0); @@ -112,26 +110,23 @@ int file_dup(int fd) return ret; } -void file_close(int fd) -{ +void file_close(int fd) { log_errno_if(close(fd), "close"); } -int file_exists(const char *path) -{ +int file_exists(const char* path) { struct stat stat; int ret = lstat(path, &stat); return !ret && S_ISREG(stat.st_mode); } -int file_read(int fd, unsigned char **_contents, size_t *_size) -{ +int file_read(int fd, unsigned char** _contents, size_t* _size) { size_t alloc_size = 256; - unsigned char *contents = NULL; + unsigned char* contents = NULL; size_t size = 0; while (1) { - unsigned char *tmp_contents = realloc(contents, alloc_size); + unsigned char* tmp_contents = realloc(contents, alloc_size); if (!tmp_contents) { log_errno("realloc"); free(contents); @@ -10,19 +10,19 @@ #include <stddef.h> -int rm_rf(const char *dir); +int rm_rf(const char* dir); /* This chdir(2) wrapper optionally saves the previous working directory in the * `old` pointer, allowing the user to switch back to it if necessary. */ -int chdir_wrapper(const char *dir, char **old); +int chdir_wrapper(const char* dir, char** old); /* This readlink(2) wrapper allocates enough memory dynamically. */ -char *readlink_wrapper(const char *path); +char* readlink_wrapper(const char* path); int file_dup(int fd); void file_close(int fd); -int file_exists(const char *path); -int file_read(int fd, unsigned char **output, size_t *size); +int file_exists(const char* path); +int file_read(int fd, unsigned char** output, size_t* size); #endif @@ -6,21 +6,21 @@ */ #include "git.h" + #include "log.h" #include <git2.h> #include <stdlib.h> -#define git_log_err(fn) \ - do { \ - const git_error *error = git_error_last(); \ - const char *msg = error && error->message ? error->message : "???"; \ - log_err("%s: %s\n", fn, msg); \ +#define git_log_err(fn) \ + do { \ + const git_error* error = git_error_last(); \ + const char* msg = error && error->message ? error->message : "???"; \ + log_err("%s: %s\n", fn, msg); \ } while (0) -int libgit_init(void) -{ +int libgit_init(void) { int ret = 0; ret = git_libgit2_init(); @@ -32,13 +32,11 @@ int libgit_init(void) return 0; } -void libgit_shutdown(void) -{ +void libgit_shutdown(void) { git_libgit2_shutdown(); } -int libgit_clone(git_repository **repo, const char *url, const char *dir) -{ +int libgit_clone(git_repository** repo, const char* url, const char* dir) { git_clone_options opts; int ret = 0; @@ -60,8 +58,7 @@ int libgit_clone(git_repository **repo, const char *url, const char *dir) return 0; } -int libgit_clone_to_tmp(git_repository **repo, const char *url) -{ +int libgit_clone_to_tmp(git_repository** repo, const char* url) { char dir[] = "/tmp/git.XXXXXX"; if (!mkdtemp(dir)) { @@ -72,15 +69,13 @@ int libgit_clone_to_tmp(git_repository **repo, const char *url) return libgit_clone(repo, url, dir); } -void libgit_repository_free(git_repository *repo) -{ +void libgit_repository_free(git_repository* repo) { git_repository_free(repo); } -int libgit_checkout(git_repository *repo, const char *rev) -{ +int libgit_checkout(git_repository* repo, const char* rev) { git_checkout_options opts; - git_object *obj; + git_object* obj; int ret = 0; log("Checking out revision %s\n", rev); @@ -14,13 +14,13 @@ int libgit_init(void); void libgit_shutdown(void); /* These never check out any files (so that we don't have to do 2 checkouts). */ -int libgit_clone(git_repository **, const char *url, const char *dir); -int libgit_clone_to_tmp(git_repository **, const char *url); +int libgit_clone(git_repository**, const char* url, const char* dir); +int libgit_clone_to_tmp(git_repository**, const char* url); /* Free a cloned repository. */ -void libgit_repository_free(git_repository *); +void libgit_repository_free(git_repository*); /* I tried to make this an equivalent of `git checkout`. */ -int libgit_checkout(git_repository *, const char *rev); +int libgit_checkout(git_repository*, const char* rev); #endif @@ -6,6 +6,7 @@ */ #include "json.h" + #include "buf.h" #include "log.h" #include "net.h" @@ -18,19 +19,17 @@ #include <stdlib.h> #include <string.h> -#define libjson_errno(fn) \ - do { \ - log_err("JSON: %s failed\n", fn); \ +#define libjson_errno(fn) \ + do { \ + log_err("JSON: %s failed\n", fn); \ } while (0) -void libjson_free(struct json_object *obj) -{ +void libjson_free(struct json_object* obj) { json_object_put(obj); } -static const char *libjson_to_string_internal(struct json_object *obj, int flags) -{ - const char *result = json_object_to_json_string_ext(obj, flags); +static const char* libjson_to_string_internal(struct json_object* obj, int flags) { + const char* result = json_object_to_json_string_ext(obj, flags); if (!result) { libjson_errno("json_object_to_json_string"); return NULL; @@ -38,21 +37,18 @@ static const char *libjson_to_string_internal(struct json_object *obj, int flags return result; } -const char *libjson_to_string(struct json_object *obj) -{ +const char* libjson_to_string(struct json_object* obj) { return libjson_to_string_internal(obj, JSON_C_TO_STRING_SPACED); } -const char *libjson_to_string_pretty(struct json_object *obj) -{ +const char* libjson_to_string_pretty(struct json_object* obj) { return libjson_to_string_internal(obj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); } -struct json_object *libjson_from_string(const char *src) -{ +struct json_object* libjson_from_string(const char* src) { enum json_tokener_error error; - struct json_object *result = json_tokener_parse_verbose(src, &error); + struct json_object* result = json_tokener_parse_verbose(src, &error); if (!result) { libjson_errno("json_tokener_parse_verbose"); log_err("JSON: parsing failed: %s\n", json_tokener_error_desc(error)); @@ -62,16 +58,15 @@ struct json_object *libjson_from_string(const char *src) return result; } -int libjson_clone(const struct json_object *obj, const char *key, struct json_object **_value) -{ +int libjson_clone(const struct json_object* obj, const char* key, struct json_object** _value) { int ret = 0; - struct json_object *old_value = NULL; + struct json_object* old_value = NULL; ret = libjson_get(obj, key, &old_value); if (ret < 0) return ret; - struct json_object *new_value = NULL; + struct json_object* new_value = NULL; ret = json_object_deep_copy(old_value, &new_value, NULL); if (ret < 0) return ret; @@ -80,15 +75,14 @@ int libjson_clone(const struct json_object *obj, const char *key, struct json_ob return ret; } -int libjson_send(struct json_object *obj, int fd) -{ +int libjson_send(struct json_object* obj, int fd) { int ret = 0; - const char *str = libjson_to_string(obj); + const char* str = libjson_to_string(obj); if (!str) return -1; - struct buf *buf = NULL; + struct buf* buf = NULL; ret = buf_create_from_string(&buf, str); if (ret < 0) return ret; @@ -98,30 +92,28 @@ int libjson_send(struct json_object *obj, int fd) return ret; } -struct json_object *libjson_recv(int fd) -{ - struct json_object *result = NULL; +struct json_object* libjson_recv(int fd) { + struct json_object* result = NULL; int ret = 0; - struct buf *buf = NULL; + struct buf* buf = NULL; ret = net_recv_buf(fd, &buf); if (ret < 0) return NULL; - result = libjson_from_string((const char *)buf_get_data(buf)); + result = libjson_from_string((const char*)buf_get_data(buf)); if (!result) goto destroy_buf; destroy_buf: - free((void *)buf_get_data(buf)); + free((void*)buf_get_data(buf)); buf_destroy(buf); return result; } -int libjson_new_object(struct json_object **_obj) -{ - struct json_object *obj = json_object_new_object(); +int libjson_new_object(struct json_object** _obj) { + struct json_object* obj = json_object_new_object(); if (!obj) { libjson_errno("json_object_new_object"); return -1; @@ -130,9 +122,8 @@ int libjson_new_object(struct json_object **_obj) return 0; } -int libjson_new_array(struct json_object **_arr) -{ - struct json_object *arr = json_object_new_array(); +int libjson_new_array(struct json_object** _arr) { + struct json_object* arr = json_object_new_array(); if (!arr) { libjson_errno("json_object_new_array"); return -1; @@ -141,13 +132,11 @@ int libjson_new_array(struct json_object **_arr) return 0; } -int libjson_has(const struct json_object *obj, const char *key) -{ +int libjson_has(const struct json_object* obj, const char* key) { return json_object_object_get_ex(obj, key, NULL); } -int libjson_get(const struct json_object *obj, const char *key, struct json_object **value) -{ +int libjson_get(const struct json_object* obj, const char* key, struct json_object** value) { if (!libjson_has(obj, key)) { log_err("JSON: key is missing: %s\n", key); return -1; @@ -156,9 +145,8 @@ int libjson_get(const struct json_object *obj, const char *key, struct json_obje return json_object_object_get_ex(obj, key, value); } -int libjson_get_string(const struct json_object *obj, const char *key, const char **_value) -{ - struct json_object *value = NULL; +int libjson_get_string(const struct json_object* obj, const char* key, const char** _value) { + struct json_object* value = NULL; int ret = libjson_get(obj, key, &value); if (ret < 0) @@ -173,9 +161,8 @@ int libjson_get_string(const struct json_object *obj, const char *key, const cha return 0; } -int libjson_get_int(const struct json_object *obj, const char *key, int64_t *_value) -{ - struct json_object *value = NULL; +int libjson_get_int(const struct json_object* obj, const char* key, int64_t* _value) { + struct json_object* value = NULL; int ret = libjson_get(obj, key, &value); if (ret < 0) @@ -197,9 +184,10 @@ int libjson_get_int(const struct json_object *obj, const char *key, int64_t *_va return 0; } -static int libjson_set_internal(struct json_object *obj, const char *key, struct json_object *value, - unsigned flags) -{ +static int libjson_set_internal(struct json_object* obj, + const char* key, + struct json_object* value, + unsigned flags) { int ret = 0; ret = json_object_object_add_ex(obj, key, value, flags); @@ -211,10 +199,11 @@ static int libjson_set_internal(struct json_object *obj, const char *key, struct return 0; } -static int libjson_set_string_internal(struct json_object *obj, const char *key, const char *_value, - unsigned flags) -{ - struct json_object *value = json_object_new_string(_value); +static int libjson_set_string_internal(struct json_object* obj, + const char* key, + const char* _value, + unsigned flags) { + struct json_object* value = json_object_new_string(_value); if (!value) { libjson_errno("json_object_new_string"); return -1; @@ -232,10 +221,11 @@ free_value: return ret; } -static int libjson_set_int_internal(struct json_object *obj, const char *key, int64_t _value, - unsigned flags) -{ - struct json_object *value = json_object_new_int64(_value); +static int libjson_set_int_internal(struct json_object* obj, + const char* key, + int64_t _value, + unsigned flags) { + struct json_object* value = json_object_new_int64(_value); if (!value) { libjson_errno("json_object_new_int"); return -1; @@ -253,18 +243,15 @@ free_value: return ret; } -int libjson_set(struct json_object *obj, const char *key, struct json_object *value) -{ +int libjson_set(struct json_object* obj, const char* key, struct json_object* value) { return libjson_set_internal(obj, key, value, 0); } -int libjson_set_string(struct json_object *obj, const char *key, const char *value) -{ +int libjson_set_string(struct json_object* obj, const char* key, const char* value) { return libjson_set_string_internal(obj, key, value, 0); } -int libjson_set_int(struct json_object *obj, const char *key, int64_t value) -{ +int libjson_set_int(struct json_object* obj, const char* key, int64_t value) { return libjson_set_int_internal(obj, key, value, 0); } @@ -273,23 +260,19 @@ int libjson_set_int(struct json_object *obj, const char *key, int64_t value) #endif static const unsigned json_const_key_flags = JSON_C_OBJECT_ADD_CONSTANT_KEY; -int libjson_set_const_key(struct json_object *obj, const char *key, struct json_object *value) -{ +int libjson_set_const_key(struct json_object* obj, const char* key, struct json_object* value) { return libjson_set_internal(obj, key, value, json_const_key_flags); } -int libjson_set_string_const_key(struct json_object *obj, const char *key, const char *value) -{ +int libjson_set_string_const_key(struct json_object* obj, const char* key, const char* value) { return libjson_set_string_internal(obj, key, value, json_const_key_flags); } -int libjson_set_int_const_key(struct json_object *obj, const char *key, int64_t value) -{ +int libjson_set_int_const_key(struct json_object* obj, const char* key, int64_t value) { return libjson_set_int_internal(obj, key, value, json_const_key_flags); } -int libjson_append(struct json_object *arr, struct json_object *elem) -{ +int libjson_append(struct json_object* arr, struct json_object* elem) { int ret = json_object_array_add(arr, elem); if (ret < 0) { libjson_errno("json_object_array_add"); @@ -12,34 +12,34 @@ #include <stdint.h> -void libjson_free(struct json_object *); +void libjson_free(struct json_object*); -const char *libjson_to_string(struct json_object *); -const char *libjson_to_string_pretty(struct json_object *); -struct json_object *libjson_from_string(const char *); +const char* libjson_to_string(struct json_object*); +const char* libjson_to_string_pretty(struct json_object*); +struct json_object* libjson_from_string(const char*); -int libjson_clone(const struct json_object *, const char *key, struct json_object **value); +int libjson_clone(const struct json_object*, const char* key, struct json_object** value); -int libjson_send(struct json_object *, int fd); -struct json_object *libjson_recv(int fd); +int libjson_send(struct json_object*, int fd); +struct json_object* libjson_recv(int fd); -int libjson_new_object(struct json_object **); -int libjson_new_array(struct json_object **); +int libjson_new_object(struct json_object**); +int libjson_new_array(struct json_object**); -int libjson_has(const struct json_object *, const char *key); +int libjson_has(const struct json_object*, const char* key); -int libjson_get(const struct json_object *, const char *key, struct json_object **value); -int libjson_get_string(const struct json_object *, const char *key, const char **value); -int libjson_get_int(const struct json_object *, const char *key, int64_t *value); +int libjson_get(const struct json_object*, const char* key, struct json_object** value); +int libjson_get_string(const struct json_object*, const char* key, const char** value); +int libjson_get_int(const struct json_object*, const char* key, int64_t* value); -int libjson_set(struct json_object *, const char *key, struct json_object *value); -int libjson_set_string(struct json_object *, const char *key, const char *value); -int libjson_set_int(struct json_object *, const char *key, int64_t value); +int libjson_set(struct json_object*, const char* key, struct json_object* value); +int libjson_set_string(struct json_object*, const char* key, const char* value); +int libjson_set_int(struct json_object*, const char* key, int64_t value); -int libjson_set_const_key(struct json_object *, const char *, struct json_object *value); -int libjson_set_string_const_key(struct json_object *, const char *, const char *value); -int libjson_set_int_const_key(struct json_object *, const char *, int64_t value); +int libjson_set_const_key(struct json_object*, const char*, struct json_object* value); +int libjson_set_string_const_key(struct json_object*, const char*, const char* value); +int libjson_set_int_const_key(struct json_object*, const char*, int64_t value); -int libjson_append(struct json_object *arr, struct json_object *elem); +int libjson_append(struct json_object* arr, struct json_object* elem); #endif diff --git a/src/json_rpc.c b/src/json_rpc.c index b81a5a0..2ab4624 100644 --- a/src/json_rpc.c +++ b/src/json_rpc.c @@ -6,6 +6,7 @@ */ #include "json_rpc.h" + #include "json.h" #include "log.h" @@ -17,24 +18,23 @@ #include <string.h> struct jsonrpc_request { - struct json_object *impl; + struct json_object* impl; }; struct jsonrpc_response { - struct json_object *impl; + struct json_object* impl; }; -static const char *const jsonrpc_key_version = "jsonrpc"; -static const char *const jsonrpc_key_id = "id"; -static const char *const jsonrpc_key_method = "method"; -static const char *const jsonrpc_key_params = "params"; +static const char* const jsonrpc_key_version = "jsonrpc"; +static const char* const jsonrpc_key_id = "id"; +static const char* const jsonrpc_key_method = "method"; +static const char* const jsonrpc_key_params = "params"; -static const char *const jsonrpc_value_version = "2.0"; +static const char* const jsonrpc_value_version = "2.0"; -static int jsonrpc_check_version(struct json_object *obj) -{ - const char *key = jsonrpc_key_version; - const char *version = NULL; +static int jsonrpc_check_version(struct json_object* obj) { + const char* key = jsonrpc_key_version; + const char* version = NULL; int ret = libjson_get_string(obj, key, &version); if (ret < 0) @@ -48,20 +48,17 @@ static int jsonrpc_check_version(struct json_object *obj) return 0; } -static int jsonrpc_set_version(struct json_object *obj) -{ +static int jsonrpc_set_version(struct json_object* obj) { return libjson_set_string_const_key(obj, jsonrpc_key_version, jsonrpc_value_version); } static _Atomic int jsonrpc_id_counter = 1; -int jsonrpc_generate_request_id(void) -{ +int jsonrpc_generate_request_id(void) { return jsonrpc_id_counter++; } -static int jsonrpc_check_id_type(struct json_object *id) -{ +static int jsonrpc_check_id_type(struct json_object* id) { if (!json_object_is_type(id, json_type_string) && !json_object_is_type(id, json_type_int)) { log_err("JSON-RPC: key '%s' must be either an integer or a string\n", jsonrpc_key_id); @@ -70,9 +67,8 @@ static int jsonrpc_check_id_type(struct json_object *id) return 0; } -static int jsonrpc_check_id(struct json_object *obj, int required) -{ - const char *key = jsonrpc_key_id; +static int jsonrpc_check_id(struct json_object* obj, int required) { + const char* key = jsonrpc_key_id; if (!libjson_has(obj, key)) { if (!required) @@ -81,7 +77,7 @@ static int jsonrpc_check_id(struct json_object *obj, int required) return -1; } - struct json_object *id = NULL; + struct json_object* id = NULL; int ret = libjson_get(obj, key, &id); if (ret < 0) @@ -89,25 +85,21 @@ static int jsonrpc_check_id(struct json_object *obj, int required) return jsonrpc_check_id_type(id); } -static int jsonrpc_set_id(struct json_object *obj, int id) -{ +static int jsonrpc_set_id(struct json_object* obj, int id) { return libjson_set_int_const_key(obj, jsonrpc_key_id, id); } -static int jsonrpc_check_method(struct json_object *obj) -{ - const char *key = jsonrpc_key_method; - const char *method = NULL; +static int jsonrpc_check_method(struct json_object* obj) { + const char* key = jsonrpc_key_method; + const char* method = NULL; return libjson_get_string(obj, key, &method); } -static int jsonrpc_set_method(struct json_object *obj, const char *method) -{ +static int jsonrpc_set_method(struct json_object* obj, const char* method) { return libjson_set_string_const_key(obj, jsonrpc_key_method, method); } -static int jsonrpc_check_params_type(struct json_object *params) -{ +static int jsonrpc_check_params_type(struct json_object* params) { if (!json_object_is_type(params, json_type_object) && !json_object_is_type(params, json_type_array)) { log_err("JSON-RPC: key '%s' must be either an object or an array\n", @@ -117,14 +109,13 @@ static int jsonrpc_check_params_type(struct json_object *params) return 0; } -static int jsonrpc_check_params(struct json_object *obj) -{ - const char *key = jsonrpc_key_params; +static int jsonrpc_check_params(struct json_object* obj) { + const char* key = jsonrpc_key_params; if (!libjson_has(obj, key)) return 0; - struct json_object *params = NULL; + struct json_object* params = NULL; int ret = libjson_get(obj, key, ¶ms); if (ret < 0) @@ -132,9 +123,8 @@ static int jsonrpc_check_params(struct json_object *obj) return jsonrpc_check_params_type(params); } -static int jsonrpc_set_params(struct json_object *obj, struct json_object *params) -{ - const char *key = jsonrpc_key_params; +static int jsonrpc_set_params(struct json_object* obj, struct json_object* params) { + const char* key = jsonrpc_key_params; int ret = jsonrpc_check_params_type(params); if (ret < 0) @@ -142,16 +132,15 @@ static int jsonrpc_set_params(struct json_object *obj, struct json_object *param return libjson_set_const_key(obj, key, params); } -static const char *const jsonrpc_key_result = "result"; -static const char *const jsonrpc_key_error = "error"; +static const char* const jsonrpc_key_result = "result"; +static const char* const jsonrpc_key_error = "error"; -static const char *const jsonrpc_key_code = "code"; -static const char *const jsonrpc_key_message = "message"; +static const char* const jsonrpc_key_code = "code"; +static const char* const jsonrpc_key_message = "message"; -static int jsonrpc_check_error(struct json_object *obj) -{ - const char *key = jsonrpc_key_error; - struct json_object *error = NULL; +static int jsonrpc_check_error(struct json_object* obj) { + const char* key = jsonrpc_key_error; + struct json_object* error = NULL; int ret = libjson_get(obj, key, &error); if (ret < 0) @@ -165,7 +154,7 @@ static int jsonrpc_check_error(struct json_object *obj) return -1; } - const char *message = NULL; + const char* message = NULL; ret = libjson_get_string(error, jsonrpc_key_message, &message); if (ret < 0) { @@ -176,10 +165,9 @@ static int jsonrpc_check_error(struct json_object *obj) return ret; } -static int jsonrpc_check_result_or_error(struct json_object *obj) -{ - const char *key_result = jsonrpc_key_result; - const char *key_error = jsonrpc_key_error; +static int jsonrpc_check_result_or_error(struct json_object* obj) { + const char* key_result = jsonrpc_key_result; + const char* key_error = jsonrpc_key_error; if (!libjson_has(obj, key_result) && !libjson_has(obj, key_error)) { log_err("JSON-RPC: either '%s' or '%s' must be present\n", key_result, key_error); @@ -192,12 +180,13 @@ static int jsonrpc_check_result_or_error(struct json_object *obj) return jsonrpc_check_error(obj); } -static int jsonrpc_request_create_internal(struct jsonrpc_request **_request, int *id, - const char *method, struct json_object *params) -{ +static int jsonrpc_request_create_internal(struct jsonrpc_request** _request, + int* id, + const char* method, + struct json_object* params) { int ret = 0; - struct jsonrpc_request *request = malloc(sizeof(struct jsonrpc_request)); + struct jsonrpc_request* request = malloc(sizeof(struct jsonrpc_request)); if (!request) { log_errno("malloc"); ret = -1; @@ -239,31 +228,29 @@ exit: return ret; } -int jsonrpc_request_create(struct jsonrpc_request **_request, int id, const char *method, - struct json_object *params) -{ +int jsonrpc_request_create(struct jsonrpc_request** _request, + int id, + const char* method, + struct json_object* params) { return jsonrpc_request_create_internal(_request, &id, method, params); } -void jsonrpc_request_destroy(struct jsonrpc_request *request) -{ +void jsonrpc_request_destroy(struct jsonrpc_request* request) { libjson_free(request->impl); free(request); } -int jsonrpc_notification_create(struct jsonrpc_request **_request, const char *method, - struct json_object *params) -{ +int jsonrpc_notification_create(struct jsonrpc_request** _request, + const char* method, + struct json_object* params) { return jsonrpc_request_create_internal(_request, NULL, method, params); } -int jsonrpc_request_is_notification(const struct jsonrpc_request *request) -{ +int jsonrpc_request_is_notification(const struct jsonrpc_request* request) { return !libjson_has(request->impl, jsonrpc_key_id); } -static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct json_object *impl) -{ +static int jsonrpc_request_from_json(struct jsonrpc_request** _request, struct json_object* impl) { int ret = 0; ret = jsonrpc_check_version(impl); @@ -279,7 +266,7 @@ static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct j if (ret < 0) return ret; - struct jsonrpc_request *request = malloc(sizeof(struct jsonrpc_request)); + struct jsonrpc_request* request = malloc(sizeof(struct jsonrpc_request)); if (!request) { log_errno("malloc"); return -1; @@ -290,14 +277,12 @@ static int jsonrpc_request_from_json(struct jsonrpc_request **_request, struct j return ret; } -int jsonrpc_request_send(const struct jsonrpc_request *request, int fd) -{ +int jsonrpc_request_send(const struct jsonrpc_request* request, int fd) { return libjson_send(request->impl, fd); } -int jsonrpc_request_recv(struct jsonrpc_request **request, int fd) -{ - struct json_object *impl = libjson_recv(fd); +int jsonrpc_request_recv(struct jsonrpc_request** request, int fd) { + struct json_object* impl = libjson_recv(fd); if (!impl) { log_err("JSON-RPC: failed to receive request\n"); return -1; @@ -315,9 +300,8 @@ free_impl: return ret; } -const char *jsonrpc_request_get_method(const struct jsonrpc_request *request) -{ - const char *method = NULL; +const char* jsonrpc_request_get_method(const struct jsonrpc_request* request) { + const char* method = NULL; int ret = libjson_get_string(request->impl, jsonrpc_key_method, &method); if (ret < 0) { /* Should never happen. */ @@ -326,13 +310,12 @@ const char *jsonrpc_request_get_method(const struct jsonrpc_request *request) return method; } -static struct json_object *jsonrpc_request_create_params(struct jsonrpc_request *request) -{ +static struct json_object* jsonrpc_request_create_params(struct jsonrpc_request* request) { int ret = 0; - const char *const key = jsonrpc_key_params; + const char* const key = jsonrpc_key_params; if (!libjson_has(request->impl, key)) { - struct json_object *params = NULL; + struct json_object* params = NULL; ret = libjson_new_object(¶ms); if (ret < 0) @@ -346,62 +329,62 @@ static struct json_object *jsonrpc_request_create_params(struct jsonrpc_request return params; } - struct json_object *params = NULL; + struct json_object* params = NULL; ret = libjson_get(request->impl, key, ¶ms); if (ret < 0) return NULL; return params; } -int jsonrpc_request_get_param_string(const struct jsonrpc_request *request, const char *name, - const char **value) -{ - struct json_object *params = NULL; +int jsonrpc_request_get_param_string(const struct jsonrpc_request* request, + const char* name, + const char** value) { + struct json_object* params = NULL; int ret = libjson_get(request->impl, jsonrpc_key_params, ¶ms); if (ret < 0) return ret; return libjson_get_string(params, name, value); } -int jsonrpc_request_set_param_string(struct jsonrpc_request *request, const char *name, - const char *value) -{ - struct json_object *params = jsonrpc_request_create_params(request); +int jsonrpc_request_set_param_string(struct jsonrpc_request* request, + const char* name, + const char* value) { + struct json_object* params = jsonrpc_request_create_params(request); if (!params) return -1; return libjson_set_string(params, name, value); } -int jsonrpc_request_get_param_int(const struct jsonrpc_request *request, const char *name, - int64_t *value) -{ - struct json_object *params = NULL; +int jsonrpc_request_get_param_int(const struct jsonrpc_request* request, + const char* name, + int64_t* value) { + struct json_object* params = NULL; int ret = libjson_get(request->impl, jsonrpc_key_params, ¶ms); if (ret < 0) return ret; return libjson_get_int(params, name, value); } -int jsonrpc_request_set_param_int(struct jsonrpc_request *request, const char *name, int64_t value) -{ - struct json_object *params = jsonrpc_request_create_params(request); +int jsonrpc_request_set_param_int(struct jsonrpc_request* request, + const char* name, + int64_t value) { + struct json_object* params = jsonrpc_request_create_params(request); if (!params) return -1; return libjson_set_int(params, name, value); } -const char *jsonrpc_response_to_string(const struct jsonrpc_response *response) -{ +const char* jsonrpc_response_to_string(const struct jsonrpc_response* response) { return libjson_to_string_pretty(response->impl); } -int jsonrpc_response_create_internal(struct jsonrpc_response **_response, - const struct jsonrpc_request *request, - struct json_object *result, struct json_object *error) -{ +int jsonrpc_response_create_internal(struct jsonrpc_response** _response, + const struct jsonrpc_request* request, + struct json_object* result, + struct json_object* error) { int ret = 0; - struct jsonrpc_response *response = malloc(sizeof(struct jsonrpc_response)); + struct jsonrpc_response* response = malloc(sizeof(struct jsonrpc_response)); if (!response) { log_errno("malloc"); ret = -1; @@ -416,7 +399,7 @@ int jsonrpc_response_create_internal(struct jsonrpc_response **_response, if (ret < 0) goto free_impl; - struct json_object *id = NULL; + struct json_object* id = NULL; ret = libjson_clone(request->impl, jsonrpc_key_id, &id); if (ret < 0) goto free_impl; @@ -448,23 +431,23 @@ exit: return ret; } -int jsonrpc_response_create(struct jsonrpc_response **response, - const struct jsonrpc_request *request, struct json_object *result) -{ +int jsonrpc_response_create(struct jsonrpc_response** response, + const struct jsonrpc_request* request, + struct json_object* result) { return jsonrpc_response_create_internal(response, request, result, NULL); } -void jsonrpc_response_destroy(struct jsonrpc_response *response) -{ +void jsonrpc_response_destroy(struct jsonrpc_response* response) { libjson_free(response->impl); free(response); } -int jsonrpc_error_create(struct jsonrpc_response **response, struct jsonrpc_request *request, - int code, const char *message) -{ +int jsonrpc_error_create(struct jsonrpc_response** response, + struct jsonrpc_request* request, + int code, + const char* message) { int ret = 0; - struct json_object *error = NULL; + struct json_object* error = NULL; ret = libjson_new_object(&error); if (ret < 0) @@ -489,13 +472,12 @@ free: return ret; } -int jsonrpc_response_is_error(const struct jsonrpc_response *response) -{ +int jsonrpc_response_is_error(const struct jsonrpc_response* response) { return libjson_has(response->impl, jsonrpc_key_error); } -static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struct json_object *impl) -{ +static int jsonrpc_response_from_json(struct jsonrpc_response** _response, + struct json_object* impl) { int ret = 0; ret = jsonrpc_check_version(impl); @@ -508,7 +490,7 @@ static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struc if (ret < 0) return ret; - struct jsonrpc_response *response = malloc(sizeof(struct jsonrpc_response)); + struct jsonrpc_response* response = malloc(sizeof(struct jsonrpc_response)); if (!response) { log_errno("malloc"); return -1; @@ -519,14 +501,12 @@ static int jsonrpc_response_from_json(struct jsonrpc_response **_response, struc return ret; } -int jsonrpc_response_send(const struct jsonrpc_response *response, int fd) -{ +int jsonrpc_response_send(const struct jsonrpc_response* response, int fd) { return libjson_send(response->impl, fd); } -int jsonrpc_response_recv(struct jsonrpc_response **response, int fd) -{ - struct json_object *impl = libjson_recv(fd); +int jsonrpc_response_recv(struct jsonrpc_response** response, int fd) { + struct json_object* impl = libjson_recv(fd); if (!impl) { log_err("JSON-RPC: failed to receive response\n"); return -1; diff --git a/src/json_rpc.h b/src/json_rpc.h index e47d081..0d6d34d 100644 --- a/src/json_rpc.h +++ b/src/json_rpc.h @@ -18,38 +18,43 @@ struct jsonrpc_request; int jsonrpc_generate_request_id(void); -int jsonrpc_request_create(struct jsonrpc_request **, int id, const char *method, - struct json_object *params); -void jsonrpc_request_destroy(struct jsonrpc_request *); +int jsonrpc_request_create(struct jsonrpc_request**, + int id, + const char* method, + struct json_object* params); +void jsonrpc_request_destroy(struct jsonrpc_request*); -int jsonrpc_notification_create(struct jsonrpc_request **, const char *method, - struct json_object *params); -int jsonrpc_request_is_notification(const struct jsonrpc_request *); +int jsonrpc_notification_create(struct jsonrpc_request**, + const char* method, + struct json_object* params); +int jsonrpc_request_is_notification(const struct jsonrpc_request*); -int jsonrpc_request_send(const struct jsonrpc_request *, int fd); -int jsonrpc_request_recv(struct jsonrpc_request **, int fd); +int jsonrpc_request_send(const struct jsonrpc_request*, int fd); +int jsonrpc_request_recv(struct jsonrpc_request**, int fd); -const char *jsonrpc_request_get_method(const struct jsonrpc_request *); +const char* jsonrpc_request_get_method(const struct jsonrpc_request*); -int jsonrpc_request_get_param_string(const struct jsonrpc_request *, const char *name, - const char **); -int jsonrpc_request_set_param_string(struct jsonrpc_request *, const char *name, const char *); -int jsonrpc_request_get_param_int(const struct jsonrpc_request *, const char *name, int64_t *); -int jsonrpc_request_set_param_int(struct jsonrpc_request *, const char *name, int64_t); +int jsonrpc_request_get_param_string(const struct jsonrpc_request*, const char* name, const char**); +int jsonrpc_request_set_param_string(struct jsonrpc_request*, const char* name, const char*); +int jsonrpc_request_get_param_int(const struct jsonrpc_request*, const char* name, int64_t*); +int jsonrpc_request_set_param_int(struct jsonrpc_request*, const char* name, int64_t); struct jsonrpc_response; -const char *jsonrpc_response_to_string(const struct jsonrpc_response *); +const char* jsonrpc_response_to_string(const struct jsonrpc_response*); -int jsonrpc_response_create(struct jsonrpc_response **, const struct jsonrpc_request *, - struct json_object *result); -void jsonrpc_response_destroy(struct jsonrpc_response *); +int jsonrpc_response_create(struct jsonrpc_response**, + const struct jsonrpc_request*, + struct json_object* result); +void jsonrpc_response_destroy(struct jsonrpc_response*); -int jsonrpc_error_create(struct jsonrpc_response **, struct jsonrpc_request *, int code, - const char *message); -int jsonrpc_response_is_error(const struct jsonrpc_response *); +int jsonrpc_error_create(struct jsonrpc_response**, + struct jsonrpc_request*, + int code, + const char* message); +int jsonrpc_response_is_error(const struct jsonrpc_response*); -int jsonrpc_response_send(const struct jsonrpc_response *, int fd); -int jsonrpc_response_recv(struct jsonrpc_response **, int fd); +int jsonrpc_response_send(const struct jsonrpc_response*, int fd); +int jsonrpc_response_recv(struct jsonrpc_response**, int fd); #endif @@ -14,8 +14,7 @@ int g_log_lvl = LOG_LVL_INFO; -static inline void log_prefix_timestamp(FILE *dest) -{ +static inline void log_prefix_timestamp(FILE* dest) { struct timeval tv; struct tm tm; char buf[64]; @@ -33,13 +32,11 @@ static inline void log_prefix_timestamp(FILE *dest) fprintf(dest, "%s", buf); } -static inline void log_prefix_thread_id(FILE *dest) -{ +static inline void log_prefix_thread_id(FILE* dest) { fprintf(dest, "%d | ", gettid()); } -int log_entry_start(int lvl, FILE *dest) -{ +int log_entry_start(int lvl, FILE* dest) { if (lvl < g_log_lvl) return 0; flockfile(dest); @@ -48,7 +45,6 @@ int log_entry_start(int lvl, FILE *dest) return 1; } -void log_entry_end(FILE *dest) -{ +void log_entry_end(FILE* dest) { funlockfile(dest); } @@ -20,67 +20,67 @@ extern int g_log_lvl; -int log_entry_start(int lvl, FILE *dest); -void log_entry_end(FILE *dest); +int log_entry_start(int lvl, FILE* dest); +void log_entry_end(FILE* dest); -#define log_debug(...) \ - do { \ - if (!log_entry_start(LOG_LVL_DEBUG, stdout)) \ - break; \ - fprintf(stdout, __VA_ARGS__); \ - log_entry_end(stdout); \ +#define log_debug(...) \ + do { \ + if (!log_entry_start(LOG_LVL_DEBUG, stdout)) \ + break; \ + fprintf(stdout, __VA_ARGS__); \ + log_entry_end(stdout); \ } while (0) -#define log(...) \ - do { \ - if (!log_entry_start(LOG_LVL_INFO, stdout)) \ - break; \ - fprintf(stdout, __VA_ARGS__); \ - log_entry_end(stdout); \ +#define log(...) \ + do { \ + if (!log_entry_start(LOG_LVL_INFO, stdout)) \ + break; \ + fprintf(stdout, __VA_ARGS__); \ + log_entry_end(stdout); \ } while (0) -#define _log_err_file_loc() \ - do { \ - fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__); \ +#define _log_err_file_loc() \ + do { \ + fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__); \ } while (0) -#define log_err(...) \ - do { \ - if (!log_entry_start(LOG_LVL_ERR, stderr)) \ - break; \ - _log_err_file_loc(); \ - fprintf(stderr, __VA_ARGS__); \ - log_entry_end(stderr); \ +#define log_err(...) \ + do { \ + if (!log_entry_start(LOG_LVL_ERR, stderr)) \ + break; \ + _log_err_file_loc(); \ + fprintf(stderr, __VA_ARGS__); \ + log_entry_end(stderr); \ } while (0) -#define log_errno(fn) \ - do { \ - if (!log_entry_start(LOG_LVL_ERR, stderr)) \ - break; \ - _log_err_file_loc(); \ - perror(fn); \ - log_entry_end(stderr); \ +#define log_errno(fn) \ + do { \ + if (!log_entry_start(LOG_LVL_ERR, stderr)) \ + break; \ + _log_err_file_loc(); \ + perror(fn); \ + log_entry_end(stderr); \ } while (0) -#define log_errno_if(expr, fn) \ - do { \ - int CONCAT(ret, __LINE__) = expr; \ - if (CONCAT(ret, __LINE__) < 0) \ - log_errno(fn); \ +#define log_errno_if(expr, fn) \ + do { \ + int CONCAT(ret, __LINE__) = expr; \ + if (CONCAT(ret, __LINE__) < 0) \ + log_errno(fn); \ } while (0) -#define pthread_errno(var, fn) \ - do { \ - errno = var; \ - log_errno(fn); \ - var = -var; \ +#define pthread_errno(var, fn) \ + do { \ + errno = var; \ + log_errno(fn); \ + var = -var; \ } while (0) -#define pthread_errno_if(expr, fn) \ - do { \ - int CONCAT(ret, __LINE__) = expr; \ - if (CONCAT(ret, __LINE__)) \ - pthread_errno(CONCAT(ret, __LINE__), fn); \ +#define pthread_errno_if(expr, fn) \ + do { \ + int CONCAT(ret, __LINE__) = expr; \ + if (CONCAT(ret, __LINE__)) \ + pthread_errno(CONCAT(ret, __LINE__), fn); \ } while (0) #endif @@ -6,6 +6,7 @@ */ #include "net.h" + #include "buf.h" #include "file.h" #include "log.h" @@ -20,8 +21,7 @@ #define gai_log_errno(ec) log_err("getaddrinfo: %s\n", gai_strerror(ec)) -int net_bind(const char *port) -{ +int net_bind(const char* port) { static const int flags = SOCK_CLOEXEC; struct addrinfo *result = NULL, *it = NULL; struct addrinfo hints; @@ -92,8 +92,7 @@ fail: return ret; } -int net_accept(int fd) -{ +int net_accept(int fd) { static const int flags = SOCK_CLOEXEC; int ret = 0; @@ -106,8 +105,7 @@ int net_accept(int fd) return ret; } -int net_connect(const char *host, const char *port) -{ +int net_connect(const char* host, const char* port) { static const int flags = SOCK_CLOEXEC; struct addrinfo *result = NULL, *it = NULL; struct addrinfo hints; @@ -151,13 +149,11 @@ int net_connect(const char *host, const char *port) return socket_fd; } -void net_close(int fd) -{ +void net_close(int fd) { file_close(fd); } -static ssize_t net_send_part(int fd, const void *buf, size_t size) -{ +static ssize_t net_send_part(int fd, const void* buf, size_t size) { static const int flags = MSG_NOSIGNAL; ssize_t ret = send(fd, buf, size, flags); @@ -169,13 +165,12 @@ static ssize_t net_send_part(int fd, const void *buf, size_t size) return ret; } -int net_send(int fd, const void *buf, size_t size) -{ +int net_send(int fd, const void* buf, size_t size) { size_t sent_total = 0; while (sent_total < size) { ssize_t sent_now = - net_send_part(fd, (const char *)buf + sent_total, size - sent_total); + net_send_part(fd, (const char*)buf + sent_total, size - sent_total); if (sent_now < 0) return -1; sent_total += sent_now; @@ -184,12 +179,11 @@ int net_send(int fd, const void *buf, size_t size) return 0; } -int net_recv(int fd, void *buf, size_t size) -{ +int net_recv(int fd, void* buf, size_t size) { ssize_t read_total = 0; while ((size_t)read_total < size) { - ssize_t read_now = read(fd, (unsigned char *)buf + read_total, size - read_total); + ssize_t read_now = read(fd, (unsigned char*)buf + read_total, size - read_total); if (!read_now) break; @@ -209,8 +203,7 @@ int net_recv(int fd, void *buf, size_t size) return 0; } -int net_send_buf(int fd, const struct buf *buf) -{ +int net_send_buf(int fd, const struct buf* buf) { int ret = 0; uint32_t size = htonl(buf_get_size(buf)); @@ -225,8 +218,7 @@ int net_send_buf(int fd, const struct buf *buf) return ret; } -int net_recv_buf(int fd, struct buf **buf) -{ +int net_recv_buf(int fd, struct buf** buf) { uint32_t size = 0; int ret = 0; @@ -237,7 +229,7 @@ int net_recv_buf(int fd, struct buf **buf) } size = ntohl(size); - void *data = malloc(size); + void* data = malloc(size); if (!data) { log_errno("malloc"); goto fail; @@ -12,15 +12,15 @@ #include <stddef.h> -int net_bind(const char *port); +int net_bind(const char* port); int net_accept(int fd); -int net_connect(const char *host, const char *port); +int net_connect(const char* host, const char* port); void net_close(int fd); -int net_send(int fd, const void *, size_t); -int net_recv(int fd, void *, size_t); +int net_send(int fd, const void*, size_t); +int net_recv(int fd, void*, size_t); -int net_send_buf(int fd, const struct buf *); -int net_recv_buf(int fd, struct buf **); +int net_send_buf(int fd, const struct buf*); +int net_recv_buf(int fd, struct buf**); #endif diff --git a/src/process.c b/src/process.c index 1c452ac..8994de0 100644 --- a/src/process.c +++ b/src/process.c @@ -6,6 +6,7 @@ */ #include "process.h" + #include "file.h" #include "log.h" @@ -14,14 +15,13 @@ #include <sys/wait.h> #include <unistd.h> -static int exec_child(const char *args[], const char *envp[]) -{ - static const char *default_envp[] = {NULL}; +static int exec_child(const char* args[], const char* envp[]) { + static const char* default_envp[] = {NULL}; if (!envp) envp = default_envp; - int ret = execvpe(args[0], (char *const *)args, (char *const *)envp); + int ret = execvpe(args[0], (char* const*)args, (char* const*)envp); if (ret < 0) { log_errno("execvpe"); return ret; @@ -30,8 +30,7 @@ static int exec_child(const char *args[], const char *envp[]) return ret; } -static int wait_for_child(pid_t pid, int *ec) -{ +static int wait_for_child(pid_t pid, int* ec) { int status; pid_t ret = waitpid(pid, &status, __WNOTHREAD); @@ -58,8 +57,7 @@ static int wait_for_child(pid_t pid, int *ec) return 0; } -int process_execute(const char *args[], const char *envp[], int *ec) -{ +int process_execute(const char* args[], const char* envp[], int* ec) { pid_t child_pid = fork(); if (child_pid < 0) { log_errno("fork"); @@ -72,8 +70,7 @@ int process_execute(const char *args[], const char *envp[], int *ec) return wait_for_child(child_pid, ec); } -static int redirect_and_exec_child(int pipe_fds[2], const char *args[], const char *envp[]) -{ +static int redirect_and_exec_child(int pipe_fds[2], const char* args[], const char* envp[]) { int ret = 0; file_close(pipe_fds[0]); @@ -93,9 +90,9 @@ static int redirect_and_exec_child(int pipe_fds[2], const char *args[], const ch return exec_child(args, envp); } -int process_execute_and_capture(const char *args[], const char *envp[], - struct process_output *result) -{ +int process_execute_and_capture(const char* args[], + const char* envp[], + struct process_output* result) { static const int flags = O_CLOEXEC; int pipe_fds[2]; int ret = 0; @@ -138,9 +135,8 @@ close_pipe: return ret; } -int process_output_create(struct process_output **_output) -{ - struct process_output *output = calloc(1, sizeof(struct process_output)); +int process_output_create(struct process_output** _output) { + struct process_output* output = calloc(1, sizeof(struct process_output)); if (!output) { log_errno("calloc"); return -1; @@ -154,14 +150,12 @@ int process_output_create(struct process_output **_output) return 0; } -void process_output_destroy(struct process_output *output) -{ +void process_output_destroy(struct process_output* output) { free(output->data); free(output); } -void process_output_dump(const struct process_output *output) -{ +void process_output_dump(const struct process_output* output) { log("Process exit code: %d\n", output->ec); log("Process output: %zu bytes\n", output->data_size); } diff --git a/src/process.h b/src/process.h index 965cb5e..00b68f9 100644 --- a/src/process.h +++ b/src/process.h @@ -12,23 +12,24 @@ struct process_output { int ec; - unsigned char *data; + unsigned char* data; size_t data_size; }; /* The exit code is only valid if the functions returns a non-negative number. */ -int process_execute(const char *args[], const char *envp[], int *ec); +int process_execute(const char* args[], const char* envp[], int* ec); /* Similarly, the contents of the process_output structure is only valid if the function returns a * non-negative number. * * In that case, you'll need to free the output. */ -int process_execute_and_capture(const char *args[], const char *envp[], - struct process_output *result); +int process_execute_and_capture(const char* args[], + const char* envp[], + struct process_output* result); -int process_output_create(struct process_output **); -void process_output_destroy(struct process_output *); +int process_output_create(struct process_output**); +void process_output_destroy(struct process_output*); -void process_output_dump(const struct process_output *); +void process_output_dump(const struct process_output*); #endif diff --git a/src/protocol.c b/src/protocol.c index 0fa06f1..e650c49 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -6,6 +6,7 @@ */ #include "protocol.h" + #include "base64.h" #include "compiler.h" #include "const.h" @@ -18,12 +19,11 @@ #include <stdint.h> #include <stdlib.h> -static const char *const run_key_id = "id"; -static const char *const run_key_url = "url"; -static const char *const run_key_rev = "rev"; +static const char* const run_key_id = "id"; +static const char* const run_key_url = "url"; +static const char* const run_key_rev = "rev"; -int request_create_queue_run(struct jsonrpc_request **request, const struct run *run) -{ +int request_create_queue_run(struct jsonrpc_request** request, const struct run* run) { int ret = 0; ret = jsonrpc_request_create(request, jsonrpc_generate_request_id(), CMD_QUEUE_RUN, NULL); @@ -44,15 +44,14 @@ free_request: return ret; } -int request_parse_queue_run(const struct jsonrpc_request *request, struct run **run) -{ +int request_parse_queue_run(const struct jsonrpc_request* request, struct run** run) { int ret = 0; - const char *url = NULL; + const char* url = NULL; ret = jsonrpc_request_get_param_string(request, run_key_url, &url); if (ret < 0) return ret; - const char *rev = NULL; + const char* rev = NULL; ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev); if (ret < 0) return ret; @@ -60,18 +59,15 @@ int request_parse_queue_run(const struct jsonrpc_request *request, struct run ** return run_queued(run, url, rev); } -int request_create_new_worker(struct jsonrpc_request **request) -{ +int request_create_new_worker(struct jsonrpc_request** request) { return jsonrpc_notification_create(request, CMD_NEW_WORKER, NULL); } -int request_parse_new_worker(UNUSED const struct jsonrpc_request *request) -{ +int request_parse_new_worker(UNUSED const struct jsonrpc_request* request) { return 0; } -int request_create_start_run(struct jsonrpc_request **request, const struct run *run) -{ +int request_create_start_run(struct jsonrpc_request** request, const struct run* run) { int ret = 0; ret = jsonrpc_notification_create(request, CMD_START_RUN, NULL); @@ -95,19 +91,18 @@ free_request: return ret; } -int request_parse_start_run(const struct jsonrpc_request *request, struct run **run) -{ +int request_parse_start_run(const struct jsonrpc_request* request, struct run** run) { int ret = 0; int64_t id = 0; ret = jsonrpc_request_get_param_int(request, run_key_id, &id); if (ret < 0) return ret; - const char *url = NULL; + const char* url = NULL; ret = jsonrpc_request_get_param_string(request, run_key_url, &url); if (ret < 0) return ret; - const char *rev = NULL; + const char* rev = NULL; ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev); if (ret < 0) return ret; @@ -115,13 +110,13 @@ int request_parse_start_run(const struct jsonrpc_request *request, struct run ** return run_created(run, (int)id, url, rev); } -static const char *const finished_key_run_id = "run_id"; -static const char *const finished_key_ec = "exit_code"; -static const char *const finished_key_data = "output"; +static const char* const finished_key_run_id = "run_id"; +static const char* const finished_key_ec = "exit_code"; +static const char* const finished_key_data = "output"; -int request_create_finished_run(struct jsonrpc_request **request, int run_id, - const struct process_output *output) -{ +int request_create_finished_run(struct jsonrpc_request** request, + int run_id, + const struct process_output* output) { int ret = 0; ret = jsonrpc_notification_create(request, CMD_FINISHED_RUN, NULL); @@ -134,7 +129,7 @@ int request_create_finished_run(struct jsonrpc_request **request, int run_id, if (ret < 0) goto free_request; - char *b64data = NULL; + char* b64data = NULL; ret = base64_encode(output->data, output->data_size, &b64data); if (ret < 0) goto free_request; @@ -152,12 +147,12 @@ free_request: return ret; } -int request_parse_finished_run(const struct jsonrpc_request *request, int *_run_id, - struct process_output **_output) -{ +int request_parse_finished_run(const struct jsonrpc_request* request, + int* _run_id, + struct process_output** _output) { int ret = 0; - struct process_output *output = NULL; + struct process_output* output = NULL; ret = process_output_create(&output); if (ret < 0) return ret; @@ -173,7 +168,7 @@ int request_parse_finished_run(const struct jsonrpc_request *request, int *_run_ goto free_output; output->ec = (int)ec; - const char *b64data = NULL; + const char* b64data = NULL; ret = jsonrpc_request_get_param_string(request, finished_key_data, &b64data); if (ret < 0) goto free_output; @@ -192,20 +187,18 @@ free_output: return ret; } -int request_create_get_runs(struct jsonrpc_request **request) -{ +int request_create_get_runs(struct jsonrpc_request** request) { return jsonrpc_request_create(request, jsonrpc_generate_request_id(), CMD_GET_RUNS, NULL); } -int request_parse_get_runs(UNUSED const struct jsonrpc_request *request) -{ +int request_parse_get_runs(UNUSED const struct jsonrpc_request* request) { return 0; } -int response_create_get_runs(struct jsonrpc_response **response, - const struct jsonrpc_request *request, const struct run_queue *runs) -{ - struct json_object *runs_json = NULL; +int response_create_get_runs(struct jsonrpc_response** response, + const struct jsonrpc_request* request, + const struct run_queue* runs) { + struct json_object* runs_json = NULL; int ret = 0; ret = run_queue_to_json(runs, &runs_json); diff --git a/src/protocol.h b/src/protocol.h index c496449..31d0091 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -12,24 +12,23 @@ #include "process.h" #include "run_queue.h" -int request_create_queue_run(struct jsonrpc_request **, const struct run *); -int request_parse_queue_run(const struct jsonrpc_request *, struct run **); +int request_create_queue_run(struct jsonrpc_request**, const struct run*); +int request_parse_queue_run(const struct jsonrpc_request*, struct run**); -int request_create_new_worker(struct jsonrpc_request **); -int request_parse_new_worker(const struct jsonrpc_request *); +int request_create_new_worker(struct jsonrpc_request**); +int request_parse_new_worker(const struct jsonrpc_request*); -int request_create_start_run(struct jsonrpc_request **, const struct run *); -int request_parse_start_run(const struct jsonrpc_request *, struct run **); +int request_create_start_run(struct jsonrpc_request**, const struct run*); +int request_parse_start_run(const struct jsonrpc_request*, struct run**); -int request_create_finished_run(struct jsonrpc_request **, int run_id, - const struct process_output *); -int request_parse_finished_run(const struct jsonrpc_request *, int *run_id, - struct process_output **); +int request_create_finished_run(struct jsonrpc_request**, int run_id, const struct process_output*); +int request_parse_finished_run(const struct jsonrpc_request*, int* run_id, struct process_output**); -int request_create_get_runs(struct jsonrpc_request **); -int request_parse_get_runs(const struct jsonrpc_request *); +int request_create_get_runs(struct jsonrpc_request**); +int request_parse_get_runs(const struct jsonrpc_request*); -int response_create_get_runs(struct jsonrpc_response **, const struct jsonrpc_request *, - const struct run_queue *); +int response_create_get_runs(struct jsonrpc_response**, + const struct jsonrpc_request*, + const struct run_queue*); #endif diff --git a/src/run_queue.c b/src/run_queue.c index 2f50f92..652096a 100644 --- a/src/run_queue.c +++ b/src/run_queue.c @@ -6,6 +6,7 @@ */ #include "run_queue.h" + #include "json.h" #include "log.h" @@ -17,30 +18,33 @@ struct run { int id; - char *repo_url; - char *repo_rev; + char* repo_url; + char* repo_rev; int status; int exit_code; SIMPLEQ_ENTRY(run) entries; }; -int run_new(struct run **_entry, int id, const char *_repo_url, const char *_repo_rev, - enum run_status status, int exit_code) -{ - struct run *entry = malloc(sizeof(struct run)); +int run_new(struct run** _entry, + int id, + const char* _repo_url, + const char* _repo_rev, + enum run_status status, + int exit_code) { + struct run* entry = malloc(sizeof(struct run)); if (!entry) { log_errno("malloc"); goto fail; } - char *repo_url = strdup(_repo_url); + char* repo_url = strdup(_repo_url); if (!repo_url) { log_errno("strdup"); goto free_entry; } - char *repo_rev = strdup(_repo_rev); + char* repo_rev = strdup(_repo_rev); if (!repo_rev) { log_errno("strdup"); goto free_repo_url; @@ -65,26 +69,22 @@ fail: return -1; } -void run_destroy(struct run *entry) -{ +void run_destroy(struct run* entry) { free(entry->repo_rev); free(entry->repo_url); free(entry); } -int run_queued(struct run **entry, const char *repo_url, const char *repo_rev) -{ +int run_queued(struct run** entry, const char* repo_url, const char* repo_rev) { return run_new(entry, -1, repo_url, repo_rev, RUN_STATUS_CREATED, -1); } -int run_created(struct run **entry, int id, const char *repo_url, const char *repo_rev) -{ +int run_created(struct run** entry, int id, const char* repo_url, const char* repo_rev) { return run_new(entry, id, repo_url, repo_rev, RUN_STATUS_CREATED, -1); } -int run_to_json(const struct run *entry, struct json_object **_json) -{ - struct json_object *json = NULL; +int run_to_json(const struct run* entry, struct json_object** _json) { + struct json_object* json = NULL; int ret = 0; ret = libjson_new_object(&json); @@ -112,55 +112,47 @@ free: return ret; } -int run_get_id(const struct run *entry) -{ +int run_get_id(const struct run* entry) { return entry->id; } -const char *run_get_repo_url(const struct run *entry) -{ +const char* run_get_repo_url(const struct run* entry) { return entry->repo_url; } -const char *run_get_repo_rev(const struct run *entry) -{ +const char* run_get_repo_rev(const struct run* entry) { return entry->repo_rev; } -void run_set_id(struct run *entry, int id) -{ +void run_set_id(struct run* entry, int id) { entry->id = id; } -void run_queue_create(struct run_queue *queue) -{ +void run_queue_create(struct run_queue* queue) { SIMPLEQ_INIT(queue); } -void run_queue_destroy(struct run_queue *queue) -{ - struct run *entry1 = SIMPLEQ_FIRST(queue); +void run_queue_destroy(struct run_queue* queue) { + struct run* entry1 = SIMPLEQ_FIRST(queue); while (entry1) { - struct run *entry2 = SIMPLEQ_NEXT(entry1, entries); + struct run* entry2 = SIMPLEQ_NEXT(entry1, entries); run_destroy(entry1); entry1 = entry2; } SIMPLEQ_INIT(queue); } -int run_queue_to_json(const struct run_queue *queue, struct json_object **_json) -{ - struct json_object *json = NULL; +int run_queue_to_json(const struct run_queue* queue, struct json_object** _json) { + struct json_object* json = NULL; int ret = 0; ret = libjson_new_array(&json); if (ret < 0) return ret; - struct run *entry = NULL; - SIMPLEQ_FOREACH(entry, queue, entries) - { - struct json_object *entry_json = NULL; + struct run* entry = NULL; + SIMPLEQ_FOREACH(entry, queue, entries) { + struct json_object* entry_json = NULL; ret = run_to_json(entry, &entry_json); if (ret < 0) goto free; @@ -181,24 +173,20 @@ free: return ret; } -int run_queue_is_empty(const struct run_queue *queue) -{ +int run_queue_is_empty(const struct run_queue* queue) { return SIMPLEQ_EMPTY(queue); } -void run_queue_add_first(struct run_queue *queue, struct run *entry) -{ +void run_queue_add_first(struct run_queue* queue, struct run* entry) { SIMPLEQ_INSERT_HEAD(queue, entry, entries); } -void run_queue_add_last(struct run_queue *queue, struct run *entry) -{ +void run_queue_add_last(struct run_queue* queue, struct run* entry) { SIMPLEQ_INSERT_TAIL(queue, entry, entries); } -struct run *run_queue_remove_first(struct run_queue *queue) -{ - struct run *entry = SIMPLEQ_FIRST(queue); +struct run* run_queue_remove_first(struct run_queue* queue) { + struct run* entry = SIMPLEQ_FIRST(queue); SIMPLEQ_REMOVE_HEAD(queue, entries); return entry; } diff --git a/src/run_queue.h b/src/run_queue.h index e1d9333..406227e 100644 --- a/src/run_queue.h +++ b/src/run_queue.h @@ -19,33 +19,37 @@ enum run_status { struct run; -int run_new(struct run **, int id, const char *repo_url, const char *repo_rev, enum run_status, +int run_new(struct run**, + int id, + const char* repo_url, + const char* repo_rev, + enum run_status, int exit_code); -void run_destroy(struct run *); +void run_destroy(struct run*); -int run_queued(struct run **, const char *repo_url, const char *repo_rev); -int run_created(struct run **, int id, const char *repo_url, const char *repo_rev); +int run_queued(struct run**, const char* repo_url, const char* repo_rev); +int run_created(struct run**, int id, const char* repo_url, const char* repo_rev); -int run_to_json(const struct run *, struct json_object **); +int run_to_json(const struct run*, struct json_object**); -int run_get_id(const struct run *); -const char *run_get_repo_url(const struct run *); -const char *run_get_repo_rev(const struct run *); +int run_get_id(const struct run*); +const char* run_get_repo_url(const struct run*); +const char* run_get_repo_rev(const struct run*); -void run_set_id(struct run *, int id); +void run_set_id(struct run*, int id); SIMPLEQ_HEAD(run_queue, run); -void run_queue_create(struct run_queue *); -void run_queue_destroy(struct run_queue *); +void run_queue_create(struct run_queue*); +void run_queue_destroy(struct run_queue*); -int run_queue_to_json(const struct run_queue *, struct json_object **); +int run_queue_to_json(const struct run_queue*, struct json_object**); -int run_queue_is_empty(const struct run_queue *); +int run_queue_is_empty(const struct run_queue*); -void run_queue_add_first(struct run_queue *, struct run *); -void run_queue_add_last(struct run_queue *, struct run *); +void run_queue_add_first(struct run_queue*, struct run*); +void run_queue_add_last(struct run_queue*, struct run*); -struct run *run_queue_remove_first(struct run_queue *); +struct run* run_queue_remove_first(struct run_queue*); #endif diff --git a/src/server.c b/src/server.c index b917f7c..264a165 100644 --- a/src/server.c +++ b/src/server.c @@ -6,6 +6,7 @@ */ #include "server.h" + #include "command.h" #include "compiler.h" #include "const.h" @@ -33,9 +34,9 @@ struct server { int stopping; - struct cmd_dispatcher *cmd_dispatcher; + struct cmd_dispatcher* cmd_dispatcher; - struct event_loop *event_loop; + struct event_loop* event_loop; int signalfd; struct worker_queue worker_queue; @@ -45,11 +46,10 @@ struct server { pthread_t main_thread; - struct tcp_server *tcp_server; + struct tcp_server* tcp_server; }; -static int server_lock(struct server *server) -{ +static int server_lock(struct server* server) { int ret = pthread_mutex_lock(&server->server_mtx); if (ret) { pthread_errno(ret, "pthread_mutex_lock"); @@ -58,13 +58,11 @@ static int server_lock(struct server *server) return ret; } -static void server_unlock(struct server *server) -{ +static void server_unlock(struct server* server) { pthread_errno_if(pthread_mutex_unlock(&server->server_mtx), "pthread_mutex_unlock"); } -static int server_wait(struct server *server) -{ +static int server_wait(struct server* server) { int ret = pthread_cond_wait(&server->server_cv, &server->server_mtx); if (ret) { pthread_errno(ret, "pthread_cond_wait"); @@ -73,15 +71,15 @@ static int server_wait(struct server *server) return ret; } -static void server_notify(struct server *server) -{ +static void server_notify(struct server* server) { pthread_errno_if(pthread_cond_signal(&server->server_cv), "pthread_cond_signal"); } -static int server_set_stopping(UNUSED struct event_loop *loop, UNUSED int fd, UNUSED short revents, - void *_server) -{ - struct server *server = (struct server *)_server; +static int server_set_stopping(UNUSED struct event_loop* loop, + UNUSED int fd, + UNUSED short revents, + void* _server) { + struct server* server = (struct server*)_server; int ret = 0; ret = server_lock(server); @@ -95,13 +93,11 @@ static int server_set_stopping(UNUSED struct event_loop *loop, UNUSED int fd, UN return ret; } -static int server_has_workers(const struct server *server) -{ +static int server_has_workers(const struct server* server) { return !worker_queue_is_empty(&server->worker_queue); } -static int server_enqueue_worker(struct server *server, struct worker *worker) -{ +static int server_enqueue_worker(struct server* server, struct worker* worker) { int ret = 0; ret = server_lock(server); @@ -116,13 +112,11 @@ static int server_enqueue_worker(struct server *server, struct worker *worker) return ret; } -static int server_has_runs(const struct server *server) -{ +static int server_has_runs(const struct server* server) { return !run_queue_is_empty(&server->run_queue); } -static int server_enqueue_run(struct server *server, struct run *run) -{ +static int server_enqueue_run(struct server* server, struct run* run) { int ret = 0; ret = storage_run_create(&server->storage, run_get_repo_url(run), run_get_repo_rev(run)); @@ -135,7 +129,8 @@ static int server_enqueue_run(struct server *server, struct run *run) return ret; run_queue_add_last(&server->run_queue, run); - log("Added a new run %d for repository %s to the queue\n", run_get_id(run), + log("Added a new run %d for repository %s to the queue\n", + run_get_id(run), run_get_repo_url(run)); server_notify(server); @@ -143,13 +138,11 @@ static int server_enqueue_run(struct server *server, struct run *run) return ret; } -static int server_ready_for_action(const struct server *server) -{ +static int server_ready_for_action(const struct server* server) { return server->stopping || (server_has_runs(server) && server_has_workers(server)); } -static int server_wait_for_action(struct server *server) -{ +static int server_wait_for_action(struct server* server) { int ret = 0; while (!server_ready_for_action(server)) { @@ -161,16 +154,16 @@ static int server_wait_for_action(struct server *server) return ret; } -static void server_assign_run(struct server *server) -{ - struct run *run = run_queue_remove_first(&server->run_queue); - log("Removed run %d for repository %s from the queue\n", run_get_id(run), +static void server_assign_run(struct server* server) { + struct run* run = run_queue_remove_first(&server->run_queue); + log("Removed run %d for repository %s from the queue\n", + run_get_id(run), run_get_repo_url(run)); - struct worker *worker = worker_queue_remove_first(&server->worker_queue); + struct worker* worker = worker_queue_remove_first(&server->worker_queue); log("Removed worker %d from the queue\n", worker_get_fd(worker)); - struct jsonrpc_request *start_request = NULL; + struct jsonrpc_request* start_request = NULL; int ret = 0; ret = request_create_start_run(&start_request, run); @@ -185,20 +178,22 @@ static void server_assign_run(struct server *server) exit: if (ret < 0) { log("Failed to assign run for repository %s to worker %d, requeueing\n", - run_get_repo_url(run), worker_get_fd(worker)); + run_get_repo_url(run), + worker_get_fd(worker)); run_queue_add_first(&server->run_queue, run); } else { - log("Assigned run %d for repository %s to worker %d\n", run_get_id(run), - run_get_repo_url(run), worker_get_fd(worker)); + log("Assigned run %d for repository %s to worker %d\n", + run_get_id(run), + run_get_repo_url(run), + worker_get_fd(worker)); run_destroy(run); } worker_destroy(worker); } -static void *server_main_thread(void *_server) -{ - struct server *server = (struct server *)_server; +static void* server_main_thread(void* _server) { + struct server* server = (struct server*)_server; int ret = 0; ret = server_lock(server); @@ -223,11 +218,11 @@ exit: return NULL; } -static int server_handle_cmd_new_worker(UNUSED const struct jsonrpc_request *request, - UNUSED struct jsonrpc_response **response, void *_ctx) -{ - struct cmd_conn_ctx *ctx = (struct cmd_conn_ctx *)_ctx; - struct server *server = (struct server *)ctx->arg; +static int server_handle_cmd_new_worker(UNUSED const struct jsonrpc_request* request, + UNUSED struct jsonrpc_response** response, + void* _ctx) { + struct cmd_conn_ctx* ctx = (struct cmd_conn_ctx*)_ctx; + struct server* server = (struct server*)ctx->arg; int ret = 0; ret = file_dup(ctx->fd); @@ -235,7 +230,7 @@ static int server_handle_cmd_new_worker(UNUSED const struct jsonrpc_request *req return ret; const int fd = ret; - struct worker *worker = NULL; + struct worker* worker = NULL; ret = worker_create(&worker, fd); if (ret < 0) @@ -256,14 +251,14 @@ close: return ret; } -static int server_handle_cmd_queue_run(const struct jsonrpc_request *request, - struct jsonrpc_response **response, void *_ctx) -{ - struct cmd_conn_ctx *ctx = (struct cmd_conn_ctx *)_ctx; - struct server *server = (struct server *)ctx->arg; +static int server_handle_cmd_queue_run(const struct jsonrpc_request* request, + struct jsonrpc_response** response, + void* _ctx) { + struct cmd_conn_ctx* ctx = (struct cmd_conn_ctx*)_ctx; + struct server* server = (struct server*)ctx->arg; int ret = 0; - struct run *run = NULL; + struct run* run = NULL; ret = request_parse_queue_run(request, &run); if (ret < 0) @@ -289,15 +284,15 @@ destroy_run: return ret; } -static int server_handle_cmd_finished_run(const struct jsonrpc_request *request, - UNUSED struct jsonrpc_response **response, void *_ctx) -{ - struct cmd_conn_ctx *ctx = (struct cmd_conn_ctx *)_ctx; - struct server *server = (struct server *)ctx->arg; +static int server_handle_cmd_finished_run(const struct jsonrpc_request* request, + UNUSED struct jsonrpc_response** response, + void* _ctx) { + struct cmd_conn_ctx* ctx = (struct cmd_conn_ctx*)_ctx; + struct server* server = (struct server*)ctx->arg; int ret = 0; int run_id = 0; - struct process_output *output; + struct process_output* output; ret = request_parse_finished_run(request, &run_id, &output); if (ret < 0) @@ -317,11 +312,11 @@ free_output: return ret; } -static int server_handle_cmd_get_runs(const struct jsonrpc_request *request, - struct jsonrpc_response **response, void *_ctx) -{ - struct cmd_conn_ctx *ctx = (struct cmd_conn_ctx *)_ctx; - struct server *server = (struct server *)ctx->arg; +static int server_handle_cmd_get_runs(const struct jsonrpc_request* request, + struct jsonrpc_response** response, + void* _ctx) { + struct cmd_conn_ctx* ctx = (struct cmd_conn_ctx*)_ctx; + struct server* server = (struct server*)ctx->arg; int ret = 0; ret = request_parse_get_runs(request); @@ -355,12 +350,11 @@ static struct cmd_desc commands[] = { static const size_t numof_commands = sizeof(commands) / sizeof(commands[0]); -int server_create(struct server **_server, const struct settings *settings) -{ +int server_create(struct server** _server, const struct settings* settings) { struct storage_settings storage_settings; int ret = 0; - struct server *server = malloc(sizeof(struct server)); + struct server* server = malloc(sizeof(struct server)); if (!server) { log_errno("malloc"); return -1; @@ -393,8 +387,8 @@ int server_create(struct server **_server, const struct settings *settings) goto destroy_event_loop; server->signalfd = ret; - ret = event_loop_add(server->event_loop, server->signalfd, POLLIN, server_set_stopping, - server); + ret = event_loop_add( + server->event_loop, server->signalfd, POLLIN, server_set_stopping, server); if (ret < 0) goto close_signalfd; @@ -413,8 +407,11 @@ int server_create(struct server **_server, const struct settings *settings) if (ret < 0) goto destroy_storage; - ret = tcp_server_create(&server->tcp_server, server->event_loop, settings->port, - cmd_dispatcher_handle_conn, server->cmd_dispatcher); + ret = tcp_server_create(&server->tcp_server, + server->event_loop, + settings->port, + cmd_dispatcher_handle_conn, + server->cmd_dispatcher); if (ret < 0) goto destroy_run_queue; @@ -460,8 +457,7 @@ free: return ret; } -void server_destroy(struct server *server) -{ +void server_destroy(struct server* server) { log("Shutting down\n"); pthread_errno_if(pthread_join(server->main_thread, NULL), "pthread_join"); @@ -477,8 +473,7 @@ void server_destroy(struct server *server) free(server); } -static int server_listen_thread(struct server *server) -{ +static int server_listen_thread(struct server* server) { int ret = 0; while (!server->stopping) { @@ -492,7 +487,6 @@ static int server_listen_thread(struct server *server) return 0; } -int server_main(struct server *server) -{ +int server_main(struct server* server) { return server_listen_thread(server); } diff --git a/src/server.h b/src/server.h index e26fe7c..862a752 100644 --- a/src/server.h +++ b/src/server.h @@ -9,16 +9,16 @@ #define __SERVER_H__ struct settings { - const char *port; + const char* port; - const char *sqlite_path; + const char* sqlite_path; }; struct server; -int server_create(struct server **, const struct settings *); -void server_destroy(struct server *); +int server_create(struct server**, const struct settings*); +void server_destroy(struct server*); -int server_main(struct server *); +int server_main(struct server*); #endif diff --git a/src/server_main.c b/src/server_main.c index d000972..ecbbc7e 100644 --- a/src/server_main.c +++ b/src/server_main.c @@ -13,8 +13,7 @@ #include <getopt.h> #include <unistd.h> -static struct settings default_settings(void) -{ +static struct settings default_settings(void) { struct settings settings = { .port = default_port, .sqlite_path = default_sqlite_path, @@ -22,13 +21,11 @@ static struct settings default_settings(void) return settings; } -const char *get_usage_string(void) -{ +const char* get_usage_string(void) { return "[-h|--help] [-V|--version] [-v|--verbose] [-p|--port PORT] [-s|--sqlite PATH]"; } -static int parse_settings(struct settings *settings, int argc, char *argv[]) -{ +static int parse_settings(struct settings* settings, int argc, char* argv[]) { int opt, longind; *settings = default_settings(); @@ -46,34 +43,33 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "hVvp:s:", long_options, &longind)) != -1) { switch (opt) { - case 'h': - exit_with_usage(0); - break; - case 'V': - exit_with_version(); - break; - case 'v': - g_log_lvl = LOG_LVL_DEBUG; - break; - case 'p': - settings->port = optarg; - break; - case 's': - settings->sqlite_path = optarg; - break; - default: - exit_with_usage(1); - break; + case 'h': + exit_with_usage(0); + break; + case 'V': + exit_with_version(); + break; + case 'v': + g_log_lvl = LOG_LVL_DEBUG; + break; + case 'p': + settings->port = optarg; + break; + case 's': + settings->sqlite_path = optarg; + break; + default: + exit_with_usage(1); + break; } } return 0; } -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { struct settings settings; - struct server *server = NULL; + struct server* server = NULL; int ret = 0; ret = parse_settings(&settings, argc, argv); diff --git a/src/signal.c b/src/signal.c index 3168a58..93eea31 100644 --- a/src/signal.c +++ b/src/signal.c @@ -6,6 +6,7 @@ */ #include "signal.h" + #include "file.h" #include "log.h" @@ -15,15 +16,13 @@ static int sigterm_signals[] = {SIGINT, SIGTERM, SIGQUIT}; -static void sigterms_mask(sigset_t *set) -{ +static void sigterms_mask(sigset_t* set) { sigemptyset(set); for (size_t i = 0; i < sizeof(sigterm_signals) / sizeof(sigterm_signals[0]); ++i) sigaddset(set, sigterm_signals[i]); } -static int signal_set_mask_internal(const sigset_t *new, sigset_t *old) -{ +static int signal_set_mask_internal(const sigset_t* new, sigset_t* old) { int ret = 0; ret = pthread_sigmask(SIG_SETMASK, new, old); @@ -35,27 +34,23 @@ static int signal_set_mask_internal(const sigset_t *new, sigset_t *old) return ret; } -int signal_set_mask(const sigset_t *new) -{ +int signal_set_mask(const sigset_t* new) { return signal_set_mask_internal(new, NULL); } -int signal_block_all(sigset_t *old) -{ +int signal_block_all(sigset_t* old) { sigset_t new; sigfillset(&new); return signal_set_mask_internal(&new, old); } -int signal_block_sigterms(void) -{ +int signal_block_sigterms(void) { sigset_t set; sigterms_mask(&set); return signal_set_mask_internal(&set, NULL); } -int signalfd_create(const sigset_t *set) -{ +int signalfd_create(const sigset_t* set) { static const int flags = SFD_CLOEXEC; sigset_t old; int ret = 0; @@ -76,14 +71,12 @@ restore: return ret; } -int signalfd_create_sigterms(void) -{ +int signalfd_create_sigterms(void) { sigset_t set; sigterms_mask(&set); return signalfd_create(&set); } -void signalfd_destroy(int fd) -{ +void signalfd_destroy(int fd) { file_close(fd); } diff --git a/src/signal.h b/src/signal.h index 468b5f5..6f2051c 100644 --- a/src/signal.h +++ b/src/signal.h @@ -10,12 +10,12 @@ #include <signal.h> -int signal_set_mask(const sigset_t *new); +int signal_set_mask(const sigset_t* new); -int signal_block_all(sigset_t *old); +int signal_block_all(sigset_t* old); int signal_block_sigterms(void); -int signalfd_create(const sigset_t *); +int signalfd_create(const sigset_t*); int signalfd_create_sigterms(void); void signalfd_destroy(int fd); diff --git a/src/sqlite.c b/src/sqlite.c index 4c2eded..5cb764a 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -6,6 +6,7 @@ */ #include "sqlite.h" + #include "compiler.h" #include "log.h" @@ -16,21 +17,20 @@ #include <stdlib.h> #include <string.h> -#define sqlite_errno(var, fn) \ - do { \ - log_err("%s: %s\n", fn, sqlite3_errstr(var)); \ - var = -var; \ +#define sqlite_errno(var, fn) \ + do { \ + log_err("%s: %s\n", fn, sqlite3_errstr(var)); \ + var = -var; \ } while (0) -#define sqlite_errno_if(expr, fn) \ - do { \ - int CONCAT(ret, __LINE__) = expr; \ - if (CONCAT(ret, __LINE__)) \ - sqlite_errno(CONCAT(ret, __LINE__), fn); \ +#define sqlite_errno_if(expr, fn) \ + do { \ + int CONCAT(ret, __LINE__) = expr; \ + if (CONCAT(ret, __LINE__)) \ + sqlite_errno(CONCAT(ret, __LINE__), fn); \ } while (0) -int sqlite_init(void) -{ +int sqlite_init(void) { int ret = 0; ret = sqlite3_initialize(); @@ -42,13 +42,11 @@ int sqlite_init(void) return ret; } -void sqlite_destroy(void) -{ +void sqlite_destroy(void) { sqlite_errno_if(sqlite3_shutdown(), "sqlite3_shutdown"); } -static int sqlite_open(const char *path, sqlite3 **db, int flags) -{ +static int sqlite_open(const char* path, sqlite3** db, int flags) { int ret = 0; ret = sqlite3_open_v2(path, db, flags, NULL); @@ -60,25 +58,21 @@ static int sqlite_open(const char *path, sqlite3 **db, int flags) return ret; } -int sqlite_open_rw(const char *path, sqlite3 **db) -{ +int sqlite_open_rw(const char* path, sqlite3** db) { static const int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; return sqlite_open(path, db, flags); } -int sqlite_open_ro(const char *path, sqlite3 **db) -{ +int sqlite_open_ro(const char* path, sqlite3** db) { static const int flags = SQLITE_OPEN_READONLY; return sqlite_open(path, db, flags); } -void sqlite_close(sqlite3 *db) -{ +void sqlite_close(sqlite3* db) { sqlite_errno_if(sqlite3_close(db), "sqlite3_close"); } -int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback, void *arg) -{ +int sqlite_exec(sqlite3* db, const char* stmt, sqlite3_callback callback, void* arg) { int ret = 0; ret = sqlite3_exec(db, stmt, callback, arg, NULL); @@ -90,8 +84,10 @@ int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback, void * return ret; } -int sqlite_exec_log_result(UNUSED void *arg, int numof_columns, char **values, char **column_names) -{ +int sqlite_exec_log_result(UNUSED void* arg, + int numof_columns, + char** values, + char** column_names) { log("Row:\n"); for (int i = 0; i < numof_columns; ++i) { log("\t%s: %s\n", column_names[i], values[i]); @@ -99,8 +95,7 @@ int sqlite_exec_log_result(UNUSED void *arg, int numof_columns, char **values, c return 0; } -int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result) -{ +int sqlite_prepare(sqlite3* db, const char* stmt, sqlite3_stmt** result) { int ret = 0; ret = sqlite3_prepare_v2(db, stmt, -1, result, NULL); @@ -112,45 +107,40 @@ int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result) return ret; } -void sqlite_reset(sqlite3_stmt *stmt) -{ +void sqlite_reset(sqlite3_stmt* stmt) { sqlite_errno_if(sqlite3_reset(stmt), "sqlite3_reset"); sqlite_errno_if(sqlite3_clear_bindings(stmt), "sqlite3_clear_bindings"); } -void sqlite_finalize(sqlite3_stmt *stmt) -{ +void sqlite_finalize(sqlite3_stmt* stmt) { sqlite_errno_if(sqlite3_finalize(stmt), "sqlite3_finalize"); } -int sqlite_step(sqlite3_stmt *stmt) -{ +int sqlite_step(sqlite3_stmt* stmt) { int ret = 0; ret = sqlite3_step(stmt); switch (ret) { - case SQLITE_ROW: - return 1; - case SQLITE_DONE: - return 0; + case SQLITE_ROW: + return 1; + case SQLITE_DONE: + return 0; - default: - sqlite_errno(ret, "sqlite3_step"); - return ret; + default: + sqlite_errno(ret, "sqlite3_step"); + return ret; } } -int sqlite_column_int(sqlite3_stmt *stmt, int index) -{ +int sqlite_column_int(sqlite3_stmt* stmt, int index) { return sqlite3_column_int(stmt, index); } -int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result) -{ +int sqlite_column_text(sqlite3_stmt* stmt, int index, char** _result) { int ret = 0; - const unsigned char *value = sqlite3_column_text(stmt, index); + const unsigned char* value = sqlite3_column_text(stmt, index); if (!value) { ret = sqlite3_errcode(sqlite3_db_handle(stmt)); if (ret) { @@ -165,7 +155,7 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result) ret = sqlite3_column_bytes(stmt, index); size_t nb = (size_t)ret; - char *result = calloc(nb + 1, 1); + char* result = calloc(nb + 1, 1); if (!result) { log_errno("calloc"); return -1; @@ -176,11 +166,10 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result) return 0; } -int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) -{ +int sqlite_column_blob(sqlite3_stmt* stmt, int index, unsigned char** _result) { int ret = 0; - const unsigned char *value = sqlite3_column_blob(stmt, index); + const unsigned char* value = sqlite3_column_blob(stmt, index); if (!value) { ret = sqlite3_errcode(sqlite3_db_handle(stmt)); if (ret) { @@ -195,7 +184,7 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) ret = sqlite3_column_bytes(stmt, index); size_t nb = (size_t)ret; - unsigned char *result = malloc(nb); + unsigned char* result = malloc(nb); if (!result) { log_errno("malloc"); return -1; @@ -206,8 +195,7 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) return 0; } -int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value) -{ +int sqlite_bind_int(sqlite3_stmt* stmt, int index, int value) { int ret = 0; ret = sqlite3_bind_int(stmt, index, value); @@ -219,8 +207,7 @@ int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value) return ret; } -int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value) -{ +int sqlite_bind_text(sqlite3_stmt* stmt, int index, const char* value) { int ret = 0; ret = sqlite3_bind_text(stmt, index, value, -1, SQLITE_STATIC); @@ -232,8 +219,7 @@ int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value) return ret; } -int sqlite_bind_blob(sqlite3_stmt *stmt, int index, unsigned char *value, size_t nb) -{ +int sqlite_bind_blob(sqlite3_stmt* stmt, int index, unsigned char* value, size_t nb) { int ret = 0; ret = sqlite3_bind_blob64(stmt, index, value, nb, SQLITE_STATIC); @@ -245,16 +231,15 @@ int sqlite_bind_blob(sqlite3_stmt *stmt, int index, unsigned char *value, size_t return ret; } -int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt) -{ - static const char *const fmt = "BEGIN; %s COMMIT;"; +int sqlite_exec_as_transaction(sqlite3* db, const char* stmt) { + static const char* const fmt = "BEGIN; %s COMMIT;"; int ret = 0; ret = snprintf(NULL, 0, fmt, stmt); size_t nb = (size_t)ret + 1; ret = 0; - char *full_stmt = malloc(nb); + char* full_stmt = malloc(nb); if (!full_stmt) { log_errno("malloc"); return -1; @@ -270,11 +255,10 @@ free: return ret; } -int sqlite_get_user_version(sqlite3 *db, unsigned int *output) -{ - static const char *const sql = "PRAGMA user_version;"; +int sqlite_get_user_version(sqlite3* db, unsigned int* output) { + static const char* const sql = "PRAGMA user_version;"; - sqlite3_stmt *stmt = NULL; + sqlite3_stmt* stmt = NULL; int result = -1, ret = 0; ret = sqlite_prepare(db, sql, &stmt); @@ -305,8 +289,7 @@ finalize: return ret; } -int sqlite_set_foreign_keys(sqlite3 *db) -{ - static const char *const sql = "PRAGMA foreign_keys = ON;"; +int sqlite_set_foreign_keys(sqlite3* db) { + static const char* const sql = "PRAGMA foreign_keys = ON;"; return sqlite_exec(db, sql, NULL, NULL); } diff --git a/src/sqlite.h b/src/sqlite.h index 6c9157b..985377f 100644 --- a/src/sqlite.h +++ b/src/sqlite.h @@ -15,29 +15,29 @@ int sqlite_init(void); void sqlite_destroy(void); -int sqlite_open_rw(const char *path, sqlite3 **db); -int sqlite_open_ro(const char *path, sqlite3 **db); -void sqlite_close(sqlite3 *db); +int sqlite_open_rw(const char* path, sqlite3** db); +int sqlite_open_ro(const char* path, sqlite3** db); +void sqlite_close(sqlite3* db); -int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback, void *arg); -int sqlite_exec_log_result(void *, int, char **, char **); +int sqlite_exec(sqlite3* db, const char* stmt, sqlite3_callback callback, void* arg); +int sqlite_exec_log_result(void*, int, char**, char**); -int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result); -void sqlite_reset(sqlite3_stmt *); -void sqlite_finalize(sqlite3_stmt *); -int sqlite_step(sqlite3_stmt *); +int sqlite_prepare(sqlite3* db, const char* stmt, sqlite3_stmt** result); +void sqlite_reset(sqlite3_stmt*); +void sqlite_finalize(sqlite3_stmt*); +int sqlite_step(sqlite3_stmt*); -int sqlite_bind_int(sqlite3_stmt *, int column_index, int value); -int sqlite_bind_text(sqlite3_stmt *, int column_index, const char *value); -int sqlite_bind_blob(sqlite3_stmt *, int column_index, unsigned char *value, size_t nb); +int sqlite_bind_int(sqlite3_stmt*, int column_index, int value); +int sqlite_bind_text(sqlite3_stmt*, int column_index, const char* value); +int sqlite_bind_blob(sqlite3_stmt*, int column_index, unsigned char* value, size_t nb); -int sqlite_column_int(sqlite3_stmt *, int column_index); -int sqlite_column_text(sqlite3_stmt *, int column_index, char **result); -int sqlite_column_blob(sqlite3_stmt *, int column_index, unsigned char **result); +int sqlite_column_int(sqlite3_stmt*, int column_index); +int sqlite_column_text(sqlite3_stmt*, int column_index, char** result); +int sqlite_column_blob(sqlite3_stmt*, int column_index, unsigned char** result); -int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt); +int sqlite_exec_as_transaction(sqlite3* db, const char* stmt); -int sqlite_get_user_version(sqlite3 *db, unsigned int *version); -int sqlite_set_foreign_keys(sqlite3 *db); +int sqlite_get_user_version(sqlite3* db, unsigned int* version); +int sqlite_set_foreign_keys(sqlite3* db); #endif diff --git a/src/storage.c b/src/storage.c index 54a62c2..92c0030 100644 --- a/src/storage.c +++ b/src/storage.c @@ -6,6 +6,7 @@ */ #include "storage.h" + #include "log.h" #include "process.h" #include "run_queue.h" @@ -13,14 +14,14 @@ #include <stddef.h> -typedef void (*storage_settings_destroy_t)(const struct storage_settings *); -typedef int (*storage_create_t)(struct storage *, const struct storage_settings *); -typedef void (*storage_destroy_t)(struct storage *); +typedef void (*storage_settings_destroy_t)(const struct storage_settings*); +typedef int (*storage_create_t)(struct storage*, const struct storage_settings*); +typedef void (*storage_destroy_t)(struct storage*); -typedef int (*storage_run_create_t)(struct storage *, const char *repo_url, const char *rev); -typedef int (*storage_run_finished_t)(struct storage *, int repo_id, const struct process_output *); +typedef int (*storage_run_create_t)(struct storage*, const char* repo_url, const char* rev); +typedef int (*storage_run_finished_t)(struct storage*, int repo_id, const struct process_output*); -typedef int (*storage_get_runs_t)(struct storage *, struct run_queue *); +typedef int (*storage_get_runs_t)(struct storage*, struct run_queue*); typedef storage_get_runs_t storage_get_run_queue_t; struct storage_api { @@ -49,13 +50,11 @@ static const struct storage_api apis[] = { }, }; -static size_t numof_apis(void) -{ +static size_t numof_apis(void) { return sizeof(apis) / sizeof(apis[0]); } -static const struct storage_api *get_api(enum storage_type type) -{ +static const struct storage_api* get_api(enum storage_type type) { if (type < 0) goto invalid_type; if ((size_t)type > numof_apis()) @@ -68,18 +67,16 @@ invalid_type: return NULL; } -void storage_settings_destroy(const struct storage_settings *settings) -{ - const struct storage_api *api = get_api(settings->type); +void storage_settings_destroy(const struct storage_settings* settings) { + const struct storage_api* api = get_api(settings->type); if (!api) return; api->destroy_settings(settings); } -int storage_create(struct storage *storage, const struct storage_settings *settings) -{ +int storage_create(struct storage* storage, const struct storage_settings* settings) { int ret = 0; - const struct storage_api *api = get_api(settings->type); + const struct storage_api* api = get_api(settings->type); if (!api) return -1; ret = api->create(storage, settings); @@ -89,41 +86,36 @@ int storage_create(struct storage *storage, const struct storage_settings *setti return ret; } -void storage_destroy(struct storage *storage) -{ - const struct storage_api *api = get_api(storage->type); +void storage_destroy(struct storage* storage) { + const struct storage_api* api = get_api(storage->type); if (!api) return; api->destroy(storage); } -int storage_run_create(struct storage *storage, const char *repo_url, const char *rev) -{ - const struct storage_api *api = get_api(storage->type); +int storage_run_create(struct storage* storage, const char* repo_url, const char* rev) { + const struct storage_api* api = get_api(storage->type); if (!api) return -1; return api->run_create(storage, repo_url, rev); } -int storage_run_finished(struct storage *storage, int run_id, const struct process_output *output) -{ - const struct storage_api *api = get_api(storage->type); +int storage_run_finished(struct storage* storage, int run_id, const struct process_output* output) { + const struct storage_api* api = get_api(storage->type); if (!api) return -1; return api->run_finished(storage, run_id, output); } -int storage_get_runs(struct storage *storage, struct run_queue *queue) -{ - const struct storage_api *api = get_api(storage->type); +int storage_get_runs(struct storage* storage, struct run_queue* queue) { + const struct storage_api* api = get_api(storage->type); if (!api) return -1; return api->get_runs(storage, queue); } -int storage_get_run_queue(struct storage *storage, struct run_queue *queue) -{ - const struct storage_api *api = get_api(storage->type); +int storage_get_run_queue(struct storage* storage, struct run_queue* queue) { + const struct storage_api* api = get_api(storage->type); if (!api) return -1; return api->get_run_queue(storage, queue); diff --git a/src/storage.h b/src/storage.h index 01ec25a..d20e08e 100644 --- a/src/storage.h +++ b/src/storage.h @@ -19,26 +19,26 @@ enum storage_type { struct storage_settings { enum storage_type type; union { - struct storage_sqlite_settings *sqlite; + struct storage_sqlite_settings* sqlite; }; }; -void storage_settings_destroy(const struct storage_settings *); +void storage_settings_destroy(const struct storage_settings*); struct storage { enum storage_type type; union { - struct storage_sqlite *sqlite; + struct storage_sqlite* sqlite; }; }; -int storage_create(struct storage *, const struct storage_settings *); -void storage_destroy(struct storage *); +int storage_create(struct storage*, const struct storage_settings*); +void storage_destroy(struct storage*); -int storage_run_create(struct storage *, const char *repo_url, const char *rev); -int storage_run_finished(struct storage *, int run_id, const struct process_output *); +int storage_run_create(struct storage*, const char* repo_url, const char* rev); +int storage_run_finished(struct storage*, int run_id, const struct process_output*); -int storage_get_runs(struct storage *, struct run_queue *); -int storage_get_run_queue(struct storage *, struct run_queue *); +int storage_get_runs(struct storage*, struct run_queue*); +int storage_get_run_queue(struct storage*, struct run_queue*); #endif diff --git a/src/storage_sqlite.c b/src/storage_sqlite.c index 942c24b..ff651af 100644 --- a/src/storage_sqlite.c +++ b/src/storage_sqlite.c @@ -6,6 +6,7 @@ */ #include "storage_sqlite.h" + #include "log.h" #include "process.h" #include "run_queue.h" @@ -21,12 +22,11 @@ #include <string.h> struct storage_sqlite_settings { - char *path; + char* path; }; -int storage_sqlite_settings_create(struct storage_settings *settings, const char *path) -{ - struct storage_sqlite_settings *sqlite = malloc(sizeof(struct storage_sqlite_settings)); +int storage_sqlite_settings_create(struct storage_settings* settings, const char* path) { + struct storage_sqlite_settings* sqlite = malloc(sizeof(struct storage_sqlite_settings)); if (!sqlite) { log_errno("malloc"); return -1; @@ -48,19 +48,17 @@ free: return -1; } -void storage_sqlite_settings_destroy(const struct storage_settings *settings) -{ +void storage_sqlite_settings_destroy(const struct storage_settings* settings) { free(settings->sqlite->path); free(settings->sqlite); } struct prepared_stmt { pthread_mutex_t mtx; - sqlite3_stmt *impl; + sqlite3_stmt* impl; }; -static int prepared_stmt_init(struct prepared_stmt *stmt, sqlite3 *db, const char *sql) -{ +static int prepared_stmt_init(struct prepared_stmt* stmt, sqlite3* db, const char* sql) { int ret = 0; ret = pthread_mutex_init(&stmt->mtx, NULL); @@ -81,14 +79,12 @@ destroy_mtx: return ret; } -static void prepared_stmt_destroy(struct prepared_stmt *stmt) -{ +static void prepared_stmt_destroy(struct prepared_stmt* stmt) { pthread_errno_if(pthread_mutex_destroy(&stmt->mtx), "pthread_mutex_destroy"); sqlite_finalize(stmt->impl); } -static int prepared_stmt_lock(struct prepared_stmt *stmt) -{ +static int prepared_stmt_lock(struct prepared_stmt* stmt) { int ret = pthread_mutex_lock(&stmt->mtx); if (ret) { pthread_errno(ret, "pthread_mutex_unlock"); @@ -97,13 +93,12 @@ static int prepared_stmt_lock(struct prepared_stmt *stmt) return ret; } -static void prepared_stmt_unlock(struct prepared_stmt *stmt) -{ +static void prepared_stmt_unlock(struct prepared_stmt* stmt) { pthread_errno_if(pthread_mutex_unlock(&stmt->mtx), "pthread_mutex_unlock"); } struct storage_sqlite { - sqlite3 *db; + sqlite3* db; struct prepared_stmt stmt_repo_find; struct prepared_stmt stmt_repo_insert; @@ -113,18 +108,17 @@ struct storage_sqlite { struct prepared_stmt stmt_get_run_queue; }; -static int storage_sqlite_upgrade_to(struct storage_sqlite *storage, size_t version) -{ - static const char *const fmt = "%s PRAGMA user_version = %zu;"; +static int storage_sqlite_upgrade_to(struct storage_sqlite* storage, size_t version) { + static const char* const fmt = "%s PRAGMA user_version = %zu;"; - const char *script = sqlite_schemas[version]; + const char* script = sqlite_schemas[version]; int ret = 0; ret = snprintf(NULL, 0, fmt, script, version + 1); size_t nb = (size_t)ret + 1; ret = 0; - char *full_script = malloc(nb); + char* full_script = malloc(nb); if (!full_script) { log_errno("malloc"); return -1; @@ -140,8 +134,7 @@ free: return ret; } -static int storage_sqlite_upgrade_from_to(struct storage_sqlite *storage, size_t from, size_t to) -{ +static int storage_sqlite_upgrade_from_to(struct storage_sqlite* storage, size_t from, size_t to) { int ret = 0; for (size_t i = from; i < to; ++i) { @@ -156,8 +149,7 @@ static int storage_sqlite_upgrade_from_to(struct storage_sqlite *storage, size_t return ret; } -static int storage_sqlite_upgrade(struct storage_sqlite *storage) -{ +static int storage_sqlite_upgrade(struct storage_sqlite* storage) { unsigned int current_version = 0; int ret = 0; @@ -182,8 +174,7 @@ static int storage_sqlite_upgrade(struct storage_sqlite *storage) return storage_sqlite_upgrade_from_to(storage, current_version, newest_version); } -static int storage_sqlite_setup(struct storage_sqlite *storage) -{ +static int storage_sqlite_setup(struct storage_sqlite* storage) { int ret = 0; ret = sqlite_set_foreign_keys(storage->db); @@ -197,18 +188,17 @@ static int storage_sqlite_setup(struct storage_sqlite *storage) return ret; } -static int storage_sqlite_prepare_statements(struct storage_sqlite *storage) -{ - static const char *const fmt_repo_find = "SELECT id FROM cimple_repos WHERE url = ?;"; - static const char *const fmt_repo_insert = +static int storage_sqlite_prepare_statements(struct storage_sqlite* storage) { + static const char* const fmt_repo_find = "SELECT id FROM cimple_repos WHERE url = ?;"; + static const char* const fmt_repo_insert = "INSERT INTO cimple_repos(url) VALUES (?) ON CONFLICT(url) DO NOTHING;"; - static const char *const fmt_run_insert = + static const char* const fmt_run_insert = "INSERT INTO cimple_runs(status, exit_code, output, repo_id, repo_rev) VALUES (?, -1, x'', ?, ?) RETURNING id;"; - static const char *const fmt_run_finished = + static const char* const fmt_run_finished = "UPDATE cimple_runs SET status = ?, exit_code = ?, output = ? WHERE id = ?;"; - static const char *const fmt_get_runs = + static const char* const fmt_get_runs = "SELECT id, status, exit_code, repo_url, repo_rev FROM cimple_runs_view ORDER BY id DESC"; - static const char *const fmt_get_run_queue = + static const char* const fmt_get_run_queue = "SELECT id, status, exit_code, repo_url, repo_rev FROM cimple_runs_view WHERE status = ? ORDER BY id;"; int ret = 0; @@ -248,8 +238,7 @@ finalize_repo_find: return ret; } -static void storage_sqlite_finalize_statements(struct storage_sqlite *storage) -{ +static void storage_sqlite_finalize_statements(struct storage_sqlite* storage) { prepared_stmt_destroy(&storage->stmt_get_run_queue); prepared_stmt_destroy(&storage->stmt_get_runs); prepared_stmt_destroy(&storage->stmt_run_finished); @@ -258,13 +247,12 @@ static void storage_sqlite_finalize_statements(struct storage_sqlite *storage) prepared_stmt_destroy(&storage->stmt_repo_find); } -int storage_sqlite_create(struct storage *storage, const struct storage_settings *settings) -{ +int storage_sqlite_create(struct storage* storage, const struct storage_settings* settings) { int ret = 0; log("Using SQLite database at %s\n", settings->sqlite->path); - struct storage_sqlite *sqlite = malloc(sizeof(struct storage_sqlite)); + struct storage_sqlite* sqlite = malloc(sizeof(struct storage_sqlite)); if (!sqlite) { log_errno("malloc"); return -1; @@ -296,17 +284,15 @@ free: return ret; } -void storage_sqlite_destroy(struct storage *storage) -{ +void storage_sqlite_destroy(struct storage* storage) { storage_sqlite_finalize_statements(storage->sqlite); sqlite_close(storage->sqlite->db); sqlite_destroy(); free(storage->sqlite); } -static int storage_sqlite_find_repo(struct storage_sqlite *storage, const char *url) -{ - struct prepared_stmt *stmt = &storage->stmt_repo_find; +static int storage_sqlite_find_repo(struct storage_sqlite* storage, const char* url) { + struct prepared_stmt* stmt = &storage->stmt_repo_find; int ret = 0; ret = prepared_stmt_lock(stmt); @@ -332,9 +318,8 @@ reset: return ret; } -static int storage_sqlite_insert_repo(struct storage_sqlite *storage, const char *url) -{ - struct prepared_stmt *stmt = &storage->stmt_repo_insert; +static int storage_sqlite_insert_repo(struct storage_sqlite* storage, const char* url) { + struct prepared_stmt* stmt = &storage->stmt_repo_insert; int ret = 0; ret = prepared_stmt_lock(stmt); @@ -357,9 +342,8 @@ reset: return storage_sqlite_find_repo(storage, url); } -static int storage_sqlite_insert_run(struct storage_sqlite *storage, int repo_id, const char *rev) -{ - struct prepared_stmt *stmt = &storage->stmt_run_insert; +static int storage_sqlite_insert_run(struct storage_sqlite* storage, int repo_id, const char* rev) { + struct prepared_stmt* stmt = &storage->stmt_run_insert; int ret = 0; ret = prepared_stmt_lock(stmt); @@ -394,8 +378,7 @@ reset: return ret; } -int storage_sqlite_run_create(struct storage *storage, const char *repo_url, const char *rev) -{ +int storage_sqlite_run_create(struct storage* storage, const char* repo_url, const char* rev) { int ret = 0; ret = storage_sqlite_insert_repo(storage->sqlite, repo_url); @@ -409,10 +392,10 @@ int storage_sqlite_run_create(struct storage *storage, const char *repo_url, con return ret; } -int storage_sqlite_run_finished(struct storage *storage, int run_id, - const struct process_output *output) -{ - struct prepared_stmt *stmt = &storage->sqlite->stmt_run_finished; +int storage_sqlite_run_finished(struct storage* storage, + int run_id, + const struct process_output* output) { + struct prepared_stmt* stmt = &storage->sqlite->stmt_run_finished; int ret = 0; ret = prepared_stmt_lock(stmt); @@ -441,20 +424,19 @@ reset: return ret; } -static int storage_sqlite_row_to_run(struct sqlite3_stmt *stmt, struct run **run) -{ +static int storage_sqlite_row_to_run(struct sqlite3_stmt* stmt, struct run** run) { int ret = 0; int id = sqlite_column_int(stmt, 0); int status = sqlite_column_int(stmt, 1); int exit_code = sqlite_column_int(stmt, 2); - char *url = NULL; + char* url = NULL; ret = sqlite_column_text(stmt, 3, &url); if (ret < 0) return ret; - char *rev = NULL; + char* rev = NULL; ret = sqlite_column_text(stmt, 4, &rev); if (ret < 0) goto free_url; @@ -474,8 +456,7 @@ free_url: return ret; } -static int storage_sqlite_rows_to_runs(struct sqlite3_stmt *stmt, struct run_queue *queue) -{ +static int storage_sqlite_rows_to_runs(struct sqlite3_stmt* stmt, struct run_queue* queue) { int ret = 0; run_queue_create(queue); @@ -487,7 +468,7 @@ static int storage_sqlite_rows_to_runs(struct sqlite3_stmt *stmt, struct run_que if (ret < 0) goto run_queue_destroy; - struct run *run = NULL; + struct run* run = NULL; ret = storage_sqlite_row_to_run(stmt, &run); if (ret < 0) @@ -504,9 +485,8 @@ run_queue_destroy: return ret; } -int storage_sqlite_get_runs(struct storage *storage, struct run_queue *queue) -{ - struct prepared_stmt *stmt = &storage->sqlite->stmt_get_runs; +int storage_sqlite_get_runs(struct storage* storage, struct run_queue* queue) { + struct prepared_stmt* stmt = &storage->sqlite->stmt_get_runs; int ret = 0; ret = prepared_stmt_lock(stmt); @@ -523,9 +503,8 @@ reset: return ret; } -int storage_sqlite_get_run_queue(struct storage *storage, struct run_queue *queue) -{ - struct prepared_stmt *stmt = &storage->sqlite->stmt_get_run_queue; +int storage_sqlite_get_run_queue(struct storage* storage, struct run_queue* queue) { + struct prepared_stmt* stmt = &storage->sqlite->stmt_get_run_queue; int ret = 0; ret = prepared_stmt_lock(stmt); diff --git a/src/storage_sqlite.h b/src/storage_sqlite.h index 2a4df2b..b55eaf3 100644 --- a/src/storage_sqlite.h +++ b/src/storage_sqlite.h @@ -17,16 +17,16 @@ struct storage_sqlite_setttings; struct storage; struct storage_sqlite; -int storage_sqlite_settings_create(struct storage_settings *, const char *path); -void storage_sqlite_settings_destroy(const struct storage_settings *); +int storage_sqlite_settings_create(struct storage_settings*, const char* path); +void storage_sqlite_settings_destroy(const struct storage_settings*); -int storage_sqlite_create(struct storage *, const struct storage_settings *); -void storage_sqlite_destroy(struct storage *); +int storage_sqlite_create(struct storage*, const struct storage_settings*); +void storage_sqlite_destroy(struct storage*); -int storage_sqlite_run_create(struct storage *, const char *repo_url, const char *rev); -int storage_sqlite_run_finished(struct storage *, int id, const struct process_output *); +int storage_sqlite_run_create(struct storage*, const char* repo_url, const char* rev); +int storage_sqlite_run_finished(struct storage*, int id, const struct process_output*); -int storage_sqlite_get_runs(struct storage *, struct run_queue *runs); -int storage_sqlite_get_run_queue(struct storage *, struct run_queue *runs); +int storage_sqlite_get_runs(struct storage*, struct run_queue* runs); +int storage_sqlite_get_run_queue(struct storage*, struct run_queue* runs); #endif diff --git a/src/string.c b/src/string.c index b610e2f..7c09f4f 100644 --- a/src/string.c +++ b/src/string.c @@ -6,6 +6,7 @@ */ #include "string.h" + #include "log.h" #include <errno.h> @@ -14,14 +15,13 @@ /* glibc calls this stpecpy; it's not provided by glibc; however, it does * provide a possible implementation in string_copying(7), which I copied from. */ -char *string_append(char *dst, char *end, const char *src) -{ +char* string_append(char* dst, char* end, const char* src) { if (!dst) return NULL; if (dst == end) return end; - char *p = memccpy(dst, src, '\0', end - dst); + char* p = memccpy(dst, src, '\0', end - dst); if (p) return p - 1; @@ -29,9 +29,8 @@ char *string_append(char *dst, char *end, const char *src) return end; } -int string_to_int(const char *src, int *result) -{ - char *endptr = NULL; +int string_to_int(const char* src, int* result) { + char* endptr = NULL; errno = 0; long ret = strtol(src, &endptr, 10); diff --git a/src/string.h b/src/string.h index e9f2b89..32f51d4 100644 --- a/src/string.h +++ b/src/string.h @@ -21,8 +21,8 @@ * ptr = string_append(ptr, buf + 128, "strings"); * ptr = string_append(ptr, buf + 128, "can be appended safely"); */ -char *string_append(char *dst, char *end, const char *src); +char* string_append(char* dst, char* end, const char* src); -int string_to_int(const char *src, int *result); +int string_to_int(const char* src, int* result); #endif diff --git a/src/tcp_server.c b/src/tcp_server.c index 5e1a5ea..f5c39fc 100644 --- a/src/tcp_server.c +++ b/src/tcp_server.c @@ -6,6 +6,7 @@ */ #include "tcp_server.h" + #include "compiler.h" #include "event_loop.h" #include "file.h" @@ -59,7 +60,7 @@ */ struct client { - struct tcp_server *server; + struct tcp_server* server; int conn_fd; int cleanup_fd; @@ -73,18 +74,17 @@ struct client { SIMPLEQ_HEAD(client_queue, client); struct tcp_server { - struct event_loop *loop; + struct event_loop* loop; tcp_server_conn_handler conn_handler; - void *conn_handler_arg; + void* conn_handler_arg; struct client_queue client_queue; int accept_fd; }; -static void client_destroy(struct client *client) -{ +static void client_destroy(struct client* client) { log_debug("Cleaning up client thread %d\n", client->tid); SIMPLEQ_REMOVE(&client->server->client_queue, client, client, entries); @@ -94,19 +94,19 @@ static void client_destroy(struct client *client) free(client); } -static int client_destroy_handler(UNUSED struct event_loop *loop, UNUSED int fd, - UNUSED short revents, void *_client) -{ - struct client *client = (struct client *)_client; +static int client_destroy_handler(UNUSED struct event_loop* loop, + UNUSED int fd, + UNUSED short revents, + void* _client) { + struct client* client = (struct client*)_client; log_debug("Client thread %d indicated that it's done\n", client->tid); client_destroy(client); return 0; } -static void *client_thread_func(void *_client) -{ - struct client *client = (struct client *)_client; +static void* client_thread_func(void* _client) { + struct client* client = (struct client*)_client; int ret = 0; client->tid = gettid(); @@ -128,8 +128,7 @@ cleanup: return NULL; } -static int client_create_thread(struct client *client) -{ +static int client_create_thread(struct client* client) { sigset_t old_mask; int ret = 0; @@ -153,11 +152,10 @@ restore_mask: return ret; } -static int client_create(struct tcp_server *server, int conn_fd) -{ +static int client_create(struct tcp_server* server, int conn_fd) { int ret = 0; - struct client *client = calloc(1, sizeof(struct client)); + struct client* client = calloc(1, sizeof(struct client)); if (!client) { log_errno("calloc"); return -1; @@ -173,8 +171,8 @@ static int client_create(struct tcp_server *server, int conn_fd) } client->cleanup_fd = ret; - ret = event_loop_add_once(server->loop, client->cleanup_fd, POLLIN, client_destroy_handler, - client); + ret = event_loop_add_once( + server->loop, client->cleanup_fd, POLLIN, client_destroy_handler, client); if (ret < 0) goto close_cleanup_fd; @@ -198,34 +196,35 @@ free: return ret; } -static void client_queue_create(struct client_queue *client_queue) -{ +static void client_queue_create(struct client_queue* client_queue) { SIMPLEQ_INIT(client_queue); } -static void client_queue_destroy(struct client_queue *client_queue) -{ - struct client *entry1 = SIMPLEQ_FIRST(client_queue); +static void client_queue_destroy(struct client_queue* client_queue) { + struct client* entry1 = SIMPLEQ_FIRST(client_queue); while (entry1) { - struct client *entry2 = SIMPLEQ_NEXT(entry1, entries); + struct client* entry2 = SIMPLEQ_NEXT(entry1, entries); client_destroy(entry1); entry1 = entry2; } } -static int tcp_server_accept_handler(UNUSED struct event_loop *loop, UNUSED int fd, - UNUSED short revents, void *_server) -{ - struct tcp_server *server = (struct tcp_server *)_server; +static int tcp_server_accept_handler(UNUSED struct event_loop* loop, + UNUSED int fd, + UNUSED short revents, + void* _server) { + struct tcp_server* server = (struct tcp_server*)_server; return tcp_server_accept(server); } -int tcp_server_create(struct tcp_server **_server, struct event_loop *loop, const char *port, - tcp_server_conn_handler conn_handler, void *conn_handler_arg) -{ +int tcp_server_create(struct tcp_server** _server, + struct event_loop* loop, + const char* port, + tcp_server_conn_handler conn_handler, + void* conn_handler_arg) { int ret = 0; - struct tcp_server *server = calloc(1, sizeof(struct tcp_server)); + struct tcp_server* server = calloc(1, sizeof(struct tcp_server)); if (!server) { log_errno("calloc"); return -1; @@ -258,15 +257,13 @@ free: return ret; } -void tcp_server_destroy(struct tcp_server *server) -{ +void tcp_server_destroy(struct tcp_server* server) { net_close(server->accept_fd); client_queue_destroy(&server->client_queue); free(server); } -int tcp_server_accept(struct tcp_server *server) -{ +int tcp_server_accept(struct tcp_server* server) { int conn_fd = -1, ret = 0; ret = net_accept(server->accept_fd); diff --git a/src/tcp_server.h b/src/tcp_server.h index d159160..cae43fd 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -12,12 +12,15 @@ struct tcp_server; -typedef int (*tcp_server_conn_handler)(int conn_fd, void *arg); +typedef int (*tcp_server_conn_handler)(int conn_fd, void* arg); -int tcp_server_create(struct tcp_server **, struct event_loop *, const char *port, - tcp_server_conn_handler, void *arg); -void tcp_server_destroy(struct tcp_server *); +int tcp_server_create(struct tcp_server**, + struct event_loop*, + const char* port, + tcp_server_conn_handler, + void* arg); +void tcp_server_destroy(struct tcp_server*); -int tcp_server_accept(struct tcp_server *); +int tcp_server_accept(struct tcp_server*); #endif diff --git a/src/worker.c b/src/worker.c index 8ee0703..b64152c 100644 --- a/src/worker.c +++ b/src/worker.c @@ -6,6 +6,7 @@ */ #include "worker.h" + #include "ci.h" #include "command.h" #include "compiler.h" @@ -24,21 +25,20 @@ #include <string.h> struct worker { - struct settings *settings; + struct settings* settings; int stopping; - struct cmd_dispatcher *cmd_dispatcher; + struct cmd_dispatcher* cmd_dispatcher; - struct event_loop *event_loop; + struct event_loop* event_loop; int signalfd; - struct run *run; + struct run* run; }; -static struct settings *worker_settings_copy(const struct settings *src) -{ - struct settings *result = malloc(sizeof(*src)); +static struct settings* worker_settings_copy(const struct settings* src) { + struct settings* result = malloc(sizeof(*src)); if (!result) { log_errno("malloc"); return NULL; @@ -59,7 +59,7 @@ static struct settings *worker_settings_copy(const struct settings *src) return result; free_host: - free((void *)result->host); + free((void*)result->host); free_result: free(result); @@ -67,26 +67,26 @@ free_result: return NULL; } -static void worker_settings_destroy(struct settings *settings) -{ - free((void *)settings->port); - free((void *)settings->host); +static void worker_settings_destroy(struct settings* settings) { + free((void*)settings->port); + free((void*)settings->host); free(settings); } -static int worker_set_stopping(UNUSED struct event_loop *loop, UNUSED int fd, UNUSED short revents, - void *_worker) -{ - struct worker *worker = (struct worker *)_worker; +static int worker_set_stopping(UNUSED struct event_loop* loop, + UNUSED int fd, + UNUSED short revents, + void* _worker) { + struct worker* worker = (struct worker*)_worker; worker->stopping = 1; return 0; } -static int worker_handle_cmd_start_run(const struct jsonrpc_request *request, - UNUSED struct jsonrpc_response **response, void *_ctx) -{ - struct cmd_conn_ctx *ctx = (struct cmd_conn_ctx *)_ctx; - struct worker *worker = (struct worker *)ctx->arg; +static int worker_handle_cmd_start_run(const struct jsonrpc_request* request, + UNUSED struct jsonrpc_response** response, + void* _ctx) { + struct cmd_conn_ctx* ctx = (struct cmd_conn_ctx*)_ctx; + struct worker* worker = (struct worker*)ctx->arg; int ret = 0; ret = request_parse_start_run(request, &worker->run); @@ -102,11 +102,10 @@ static struct cmd_desc commands[] = { static const size_t numof_commands = sizeof(commands) / sizeof(commands[0]); -int worker_create(struct worker **_worker, const struct settings *settings) -{ +int worker_create(struct worker** _worker, const struct settings* settings) { int ret = 0; - struct worker *worker = malloc(sizeof(struct worker)); + struct worker* worker = malloc(sizeof(struct worker)); if (!worker) { log_errno("malloc"); return -1; @@ -133,8 +132,8 @@ int worker_create(struct worker **_worker, const struct settings *settings) goto destroy_event_loop; worker->signalfd = ret; - ret = event_loop_add(worker->event_loop, worker->signalfd, POLLIN, worker_set_stopping, - worker); + ret = event_loop_add( + worker->event_loop, worker->signalfd, POLLIN, worker_set_stopping, worker); if (ret < 0) goto close_signalfd; @@ -163,8 +162,7 @@ free: return ret; } -void worker_destroy(struct worker *worker) -{ +void worker_destroy(struct worker* worker) { log("Shutting down\n"); libgit_shutdown(); @@ -175,11 +173,10 @@ void worker_destroy(struct worker *worker) free(worker); } -static int worker_do_run(struct worker *worker) -{ +static int worker_do_run(struct worker* worker) { int ret = 0; - struct process_output *result = NULL; + struct process_output* result = NULL; ret = process_output_create(&result); if (ret < 0) return ret; @@ -192,7 +189,7 @@ static int worker_do_run(struct worker *worker) process_output_dump(result); - struct jsonrpc_request *finished_request = NULL; + struct jsonrpc_request* finished_request = NULL; ret = request_create_finished_run(&finished_request, run_get_id(worker->run), result); if (ret < 0) @@ -221,8 +218,7 @@ free_output: return ret; } -static int worker_get_run(struct worker *worker) -{ +static int worker_get_run(struct worker* worker) { int ret = 0, fd = -1; ret = net_connect(worker->settings->host, worker->settings->port); @@ -230,7 +226,7 @@ static int worker_get_run(struct worker *worker) return ret; fd = ret; - struct jsonrpc_request *new_worker_request = NULL; + struct jsonrpc_request* new_worker_request = NULL; ret = request_create_new_worker(&new_worker_request); if (ret < 0) goto close; @@ -240,8 +236,8 @@ static int worker_get_run(struct worker *worker) if (ret < 0) goto close; - ret = event_loop_add_once(worker->event_loop, fd, POLLIN, cmd_dispatcher_handle_event, - worker->cmd_dispatcher); + ret = event_loop_add_once( + worker->event_loop, fd, POLLIN, cmd_dispatcher_handle_event, worker->cmd_dispatcher); if (ret < 0) goto close; @@ -257,8 +253,7 @@ close: return ret; } -int worker_main(struct worker *worker) -{ +int worker_main(struct worker* worker) { int ret = 0; while (1) { diff --git a/src/worker.h b/src/worker.h index 95491a8..b9450d6 100644 --- a/src/worker.h +++ b/src/worker.h @@ -9,15 +9,15 @@ #define __WORKER_H__ struct settings { - const char *host; - const char *port; + const char* host; + const char* port; }; struct worker; -int worker_create(struct worker **, const struct settings *); -void worker_destroy(struct worker *); +int worker_create(struct worker**, const struct settings*); +void worker_destroy(struct worker*); -int worker_main(struct worker *); +int worker_main(struct worker*); #endif diff --git a/src/worker_main.c b/src/worker_main.c index df60f73..da6db25 100644 --- a/src/worker_main.c +++ b/src/worker_main.c @@ -13,8 +13,7 @@ #include <getopt.h> #include <unistd.h> -static struct settings default_settings(void) -{ +static struct settings default_settings(void) { struct settings settings = { .host = default_host, .port = default_port, @@ -22,13 +21,11 @@ static struct settings default_settings(void) return settings; } -const char *get_usage_string(void) -{ +const char* get_usage_string(void) { return "[-h|--help] [-V|--version] [-v|--verbose] [-H|--host HOST] [-p|--port PORT]"; } -static int parse_settings(struct settings *settings, int argc, char *argv[]) -{ +static int parse_settings(struct settings* settings, int argc, char* argv[]) { int opt, longind; *settings = default_settings(); @@ -46,34 +43,33 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "hVvH:p:", long_options, &longind)) != -1) { switch (opt) { - case 'h': - exit_with_usage(0); - break; - case 'V': - exit_with_version(); - break; - case 'v': - g_log_lvl = LOG_LVL_DEBUG; - break; - case 'H': - settings->host = optarg; - break; - case 'p': - settings->port = optarg; - break; - default: - exit_with_usage(1); - break; + case 'h': + exit_with_usage(0); + break; + case 'V': + exit_with_version(); + break; + case 'v': + g_log_lvl = LOG_LVL_DEBUG; + break; + case 'H': + settings->host = optarg; + break; + case 'p': + settings->port = optarg; + break; + default: + exit_with_usage(1); + break; } } return 0; } -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { struct settings settings; - struct worker *worker = NULL; + struct worker* worker = NULL; int ret = 0; ret = parse_settings(&settings, argc, argv); diff --git a/src/worker_queue.c b/src/worker_queue.c index 1db396e..84f9c50 100644 --- a/src/worker_queue.c +++ b/src/worker_queue.c @@ -6,6 +6,7 @@ */ #include "worker_queue.h" + #include "log.h" #include "net.h" @@ -17,9 +18,8 @@ struct worker { SIMPLEQ_ENTRY(worker) entries; }; -int worker_create(struct worker **_entry, int fd) -{ - struct worker *entry = malloc(sizeof(struct worker)); +int worker_create(struct worker** _entry, int fd) { + struct worker* entry = malloc(sizeof(struct worker)); if (!entry) { log_errno("malloc"); return -1; @@ -31,51 +31,43 @@ int worker_create(struct worker **_entry, int fd) return 0; } -void worker_destroy(struct worker *entry) -{ +void worker_destroy(struct worker* entry) { net_close(entry->fd); free(entry); } -int worker_get_fd(const struct worker *entry) -{ +int worker_get_fd(const struct worker* entry) { return entry->fd; } -void worker_queue_create(struct worker_queue *queue) -{ +void worker_queue_create(struct worker_queue* queue) { SIMPLEQ_INIT(queue); } -void worker_queue_destroy(struct worker_queue *queue) -{ - struct worker *entry1 = SIMPLEQ_FIRST(queue); +void worker_queue_destroy(struct worker_queue* queue) { + struct worker* entry1 = SIMPLEQ_FIRST(queue); while (entry1) { - struct worker *entry2 = SIMPLEQ_NEXT(entry1, entries); + struct worker* entry2 = SIMPLEQ_NEXT(entry1, entries); worker_destroy(entry1); entry1 = entry2; } SIMPLEQ_INIT(queue); } -int worker_queue_is_empty(const struct worker_queue *queue) -{ +int worker_queue_is_empty(const struct worker_queue* queue) { return SIMPLEQ_EMPTY(queue); } -void worker_queue_add_first(struct worker_queue *queue, struct worker *entry) -{ +void worker_queue_add_first(struct worker_queue* queue, struct worker* entry) { SIMPLEQ_INSERT_HEAD(queue, entry, entries); } -void worker_queue_add_last(struct worker_queue *queue, struct worker *entry) -{ +void worker_queue_add_last(struct worker_queue* queue, struct worker* entry) { SIMPLEQ_INSERT_TAIL(queue, entry, entries); } -struct worker *worker_queue_remove_first(struct worker_queue *queue) -{ - struct worker *entry = SIMPLEQ_FIRST(queue); +struct worker* worker_queue_remove_first(struct worker_queue* queue) { + struct worker* entry = SIMPLEQ_FIRST(queue); SIMPLEQ_REMOVE_HEAD(queue, entries); return entry; } diff --git a/src/worker_queue.h b/src/worker_queue.h index 88e52c4..26de708 100644 --- a/src/worker_queue.h +++ b/src/worker_queue.h @@ -12,21 +12,21 @@ struct worker; -int worker_create(struct worker **, int fd); -void worker_destroy(struct worker *); +int worker_create(struct worker**, int fd); +void worker_destroy(struct worker*); -int worker_get_fd(const struct worker *); +int worker_get_fd(const struct worker*); SIMPLEQ_HEAD(worker_queue, worker); -void worker_queue_create(struct worker_queue *); -void worker_queue_destroy(struct worker_queue *); +void worker_queue_create(struct worker_queue*); +void worker_queue_destroy(struct worker_queue*); -int worker_queue_is_empty(const struct worker_queue *); +int worker_queue_is_empty(const struct worker_queue*); -void worker_queue_add_first(struct worker_queue *, struct worker *); -void worker_queue_add_last(struct worker_queue *, struct worker *); +void worker_queue_add_first(struct worker_queue*, struct worker*); +void worker_queue_add_last(struct worker_queue*, struct worker*); -struct worker *worker_queue_remove_first(struct worker_queue *); +struct worker* worker_queue_remove_first(struct worker_queue*); #endif diff --git a/test/sigsegv/lib.c b/test/sigsegv/lib.c index 1e136cc..e67b5e4 100644 --- a/test/sigsegv/lib.c +++ b/test/sigsegv/lib.c @@ -2,4 +2,4 @@ #include <stddef.h> -struct data *data = NULL; +struct data* data = NULL; diff --git a/test/sigsegv/lib.h b/test/sigsegv/lib.h index 1f162d5..15f2246 100644 --- a/test/sigsegv/lib.h +++ b/test/sigsegv/lib.h @@ -5,6 +5,6 @@ struct data { int x; }; -extern struct data *data; +extern struct data* data; #endif diff --git a/test/sigsegv/main.c b/test/sigsegv/main.c index 428d168..4336073 100644 --- a/test/sigsegv/main.c +++ b/test/sigsegv/main.c @@ -2,8 +2,7 @@ #include <stdio.h> -int main(void) -{ +int main(void) { puts("Started the test program."); fflush(stdout); |
