From e162ba645dc48b4cfc8555e9483f6aca5eeb9490 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 16 Nov 2026 17:35:28 +0000 Subject: [PATCH] dtchat: add the adapter process manager (T1.5) adapter.c spawns a helper, waits for hello (ipc 1), sends configure/connect, routes events, drains stderr, buffers stdin, reaps zombies and restarts with 2..60 s backoff unless the child said bye. fake_adapter.c and test_adapter.c cover normal handshake, send/message echo, join/answer, stop/bye, crash restart and invalid JSON. make mcde-host exit=0; test_adapter 'all ok'; ASan/UBSan clean. test_adapter_DEPENDENCIES now includes libDtChat/libDtChatIpc to avoid the parallel relink race. --- lib/libDtChat/Makefile.am | 13 +- lib/libDtChat/adapter.c | 744 +++++++++++++++++++++++++++++++++++ lib/libDtChat/dc_internal.h | 40 ++ lib/libDtChat/fake_adapter.c | 202 ++++++++++ lib/libDtChat/test_adapter.c | 267 +++++++++++++ 5 files changed, 1263 insertions(+), 3 deletions(-) create mode 100644 lib/libDtChat/adapter.c create mode 100644 lib/libDtChat/fake_adapter.c create mode 100644 lib/libDtChat/test_adapter.c diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index 3844cff0..fdaae38b 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -10,13 +10,13 @@ libDtChatIpc_la_SOURCES = json.c libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include -libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c dc_internal.h +libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c dc_internal.h libDtChat_la_LIBADD = libDtChatIpc.la libDtChat_la_LDFLAGS = -version-info 1:0:0 # Built by make mcde-host as well, so tools/mcde-tests.sh finds them in # build/mcde-host; make check still builds and runs them. -noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text +noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_adapter fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -35,4 +35,11 @@ test_conv_LDADD = libDtChat.la test_text_SOURCES = test_text.c test_text_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_text_LDADD = libDtChat.la libDtChatIpc.la -TESTS = test_json test_frame test_loop test_store test_conv test_text +fake_adapter_SOURCES = fake_adapter.c +fake_adapter_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +fake_adapter_LDADD = libDtChatIpc.la +test_adapter_SOURCES = test_adapter.c +test_adapter_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_adapter_LDADD = libDtChat.la libDtChatIpc.la +test_adapter_DEPENDENCIES = fake_adapter$(EXEEXT) libDtChat.la libDtChatIpc.la +TESTS = test_json test_frame test_loop test_store test_conv test_text test_adapter diff --git a/lib/libDtChat/adapter.c b/lib/libDtChat/adapter.c new file mode 100644 index 00000000..2db16b34 --- /dev/null +++ b/lib/libDtChat/adapter.c @@ -0,0 +1,744 @@ +/* + * adapter.c -- the core's adapter processes + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * One child process per account, JSON lines on stdin/stdout, stderr drained + * and discarded. The manager waits for hello, sends configure and connect, + * routes every later object to the core's callback, and restarts a child + * that dies without saying bye (2, 4, 8 ... 60 s). + */ +#include "dc_internal.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct DcAdapter { + DcLoop *loop; + char *path; + char *account; + DcJson *settings; + DcJson *secrets; + DcAdapterEventFn fn; + void *arg; + + pid_t pid; + int in_fd; + int out_fd; + int err_fd; + int out_id; + int err_id; + int write_id; + DcFrameBuf in_frame; + + char *out_buf; + size_t out_len; + size_t out_off; + + DcAdapterState state; + int got_hello; + int stop_requested; + int bye; + int restarts; + long long backoff_ms; + int restart_timer; + int stop_timer; +}; + +static void handle_exit(DcAdapter *adapter); +static void schedule_restart(DcAdapter *adapter); +static void restart_cb(void *arg); +static int spawn(DcAdapter *adapter); +static int flush_output(DcAdapter *adapter); +static void on_writable(int fd, short revents, void *arg); + +static int +set_nonblock_cloexec(int fd) +{ + int fl = fcntl(fd, F_GETFL, 0); + + if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) + return -1; + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) + return -1; + return 0; +} + +static DcJson * +json_clone(const DcJson *src) +{ + char *text; + size_t len = 0; + DcJson *copy; + + if (src == NULL) + return dc_json_new_object(); + text = dc_json_write(src, &len); + if (text == NULL) + return NULL; + copy = dc_json_parse(text, len, NULL, 0); + free(text); + return copy; +} + +static void +emit(DcAdapter *adapter, DcJson *event) +{ + if (event == NULL) + return; + if (adapter->fn != NULL) + adapter->fn(adapter, event, adapter->arg); + dc_json_free(event); +} + +static void +emit_state(DcAdapter *adapter, const char *state, long long delay_ms) +{ + DcJson *event = dc_json_new_object(); + + if (event == NULL) + return; + (void)dc_json_object_set(event, "type", + dc_json_new_string("state")); + (void)dc_json_object_set(event, "state", + dc_json_new_string(state)); + if (delay_ms >= 0) + (void)dc_json_object_set(event, "delay_ms", + dc_json_new_int(delay_ms)); + emit(adapter, event); +} + +static void +emit_error(DcAdapter *adapter, const char *text) +{ + DcJson *event = dc_json_new_object(); + + if (event == NULL) + return; + (void)dc_json_object_set(event, "type", + dc_json_new_string("error")); + (void)dc_json_object_set(event, "fatal", dc_json_new_bool(0)); + (void)dc_json_object_set(event, "text", + dc_json_new_string(text != NULL ? text : "adapter error")); + emit(adapter, event); +} + +static void +detach_io(DcAdapter *adapter) +{ + if (adapter->out_id != 0) { + (void)dc_loop_del_fd(adapter->loop, adapter->out_id); + adapter->out_id = 0; + } + if (adapter->err_id != 0) { + (void)dc_loop_del_fd(adapter->loop, adapter->err_id); + adapter->err_id = 0; + } + if (adapter->write_id != 0) { + (void)dc_loop_del_fd(adapter->loop, adapter->write_id); + adapter->write_id = 0; + } + if (adapter->out_fd >= 0) { + close(adapter->out_fd); + adapter->out_fd = -1; + } + if (adapter->err_fd >= 0) { + close(adapter->err_fd); + adapter->err_fd = -1; + } + if (adapter->in_fd >= 0) { + close(adapter->in_fd); + adapter->in_fd = -1; + } + dc_frame_reset(&adapter->in_frame); + free(adapter->out_buf); + adapter->out_buf = NULL; + adapter->out_len = 0; + adapter->out_off = 0; +} + +static void +reap_child(DcAdapter *adapter) +{ + pid_t pid = adapter->pid; + int status; + int i; + + adapter->pid = -1; + if (pid <= 0) + return; + for (i = 0; i < 100; i++) { + pid_t r = waitpid(pid, &status, WNOHANG); + + if (r == pid || r < 0) + return; + { + struct timespec ts = { 0, 10000000 }; + + (void)nanosleep(&ts, NULL); + } + } + (void)kill(pid, SIGKILL); + (void)waitpid(pid, &status, 0); +} + +static int +queue_json(DcAdapter *adapter, const DcJson *json) +{ + char *text; + size_t len = 0; + char *next; + size_t need; + + if (adapter->in_fd < 0 || adapter->state == DC_ADAPTER_STOPPED) + return -1; + text = dc_json_write(json, &len); + if (text == NULL || len > DC_IPC_MAX_LINE) { + free(text); + return -1; + } + if (len > SIZE_MAX - adapter->out_len - 2) { + free(text); + return -1; + } + need = adapter->out_len + len + 1; + next = realloc(adapter->out_buf, need); + if (next == NULL) { + free(text); + return -1; + } + adapter->out_buf = next; + memcpy(adapter->out_buf + adapter->out_len, text, len); + adapter->out_len += len; + adapter->out_buf[adapter->out_len++] = '\n'; + free(text); + return flush_output(adapter); +} + +static int +flush_output(DcAdapter *adapter) +{ + while (adapter->out_off < adapter->out_len) { + ssize_t n = write(adapter->in_fd, + adapter->out_buf + adapter->out_off, + adapter->out_len - adapter->out_off); + + if (n < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN || errno == EWOULDBLOCK) + break; + return -1; + } + if (n == 0) + break; + adapter->out_off += (size_t)n; + } + if (adapter->out_off == adapter->out_len) { + adapter->out_len = 0; + adapter->out_off = 0; + if (adapter->write_id != 0) { + (void)dc_loop_del_fd(adapter->loop, adapter->write_id); + adapter->write_id = 0; + } + return 0; + } + if (adapter->write_id == 0) { + adapter->write_id = dc_loop_add_fd(adapter->loop, + adapter->in_fd, POLLOUT, on_writable, adapter); + if (adapter->write_id <= 0) { + adapter->write_id = 0; + return -1; + } + } + return 0; +} + +static void +on_writable(int fd, short revents, void *arg) +{ + DcAdapter *adapter = arg; + + (void)fd; + (void)revents; + if (flush_output(adapter) != 0) { + emit_error(adapter, "adapter write failed"); + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); + handle_exit(adapter); + } +} + +static void +send_configure(DcAdapter *adapter) +{ + DcJson *cmd = dc_json_new_object(); + DcJson *settings = json_clone(adapter->settings); + DcJson *secrets = json_clone(adapter->secrets); + + if (cmd == NULL || settings == NULL || secrets == NULL) { + dc_json_free(cmd); + dc_json_free(settings); + dc_json_free(secrets); + return; + } + (void)dc_json_object_set(cmd, "type", + dc_json_new_string("configure")); + (void)dc_json_object_set(cmd, "account", + dc_json_new_string(adapter->account)); + (void)dc_json_object_set(cmd, "settings", settings); + (void)dc_json_object_set(cmd, "secrets", secrets); + (void)queue_json(adapter, cmd); + dc_json_free(cmd); +} + +static void +send_connect(DcAdapter *adapter) +{ + DcJson *cmd = dc_json_new_object(); + + if (cmd == NULL) + return; + (void)dc_json_object_set(cmd, "type", + dc_json_new_string("connect")); + (void)queue_json(adapter, cmd); + dc_json_free(cmd); +} + +static void +handle_line(DcAdapter *adapter, const char *line, size_t len) +{ + DcJson *event = dc_json_parse(line, len, NULL, 0); + const char *type; + const char *state; + int ipc; + + if (event == NULL) { + emit_error(adapter, "invalid JSON from adapter"); + dc_json_free(event); + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); + handle_exit(adapter); + return; + } + type = dc_json_string(dc_json_get(event, "type")); + if (type == NULL) { + emit_error(adapter, "adapter event without type"); + dc_json_free(event); + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); + handle_exit(adapter); + return; + } + if (strcmp(type, "hello") == 0) { + if (adapter->got_hello) { + emit_error(adapter, "duplicate hello"); + dc_json_free(event); + return; + } + ipc = (int)dc_json_int(dc_json_get(event, "ipc")); + if (ipc != DC_IPC_VERSION || + dc_json_string(dc_json_get(event, "protocol")) == NULL) { + emit_error(adapter, "bad hello"); + dc_json_free(event); + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); + handle_exit(adapter); + return; + } + adapter->got_hello = 1; + adapter->state = DC_ADAPTER_CONFIGURED; + emit(adapter, event); + send_configure(adapter); + send_connect(adapter); + return; + } + if (strcmp(type, "state") == 0) { + state = dc_json_string(dc_json_get(event, "state")); + if (state != NULL && strcmp(state, "online") == 0) { + adapter->state = DC_ADAPTER_ONLINE; + adapter->backoff_ms = 0; + } + } + if (strcmp(type, "bye") == 0) + adapter->bye = 1; + if (strcmp(type, "error") == 0 && + dc_json_bool(dc_json_get(event, "fatal"))) + adapter->bye = 1; + emit(adapter, event); +} + +static void +on_out_readable(int fd, short revents, void *arg) +{ + DcAdapter *adapter = arg; + char buf[4096]; + + (void)revents; + for (;;) { + ssize_t n = read(fd, buf, sizeof(buf)); + + if (n < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN || errno == EWOULDBLOCK) + return; + handle_exit(adapter); + return; + } + if (n == 0) { + handle_exit(adapter); + return; + } + if (dc_frame_feed(&adapter->in_frame, buf, (size_t)n) != 0) { + emit_error(adapter, "adapter line too long or NUL"); + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); + handle_exit(adapter); + return; + } + for (;;) { + size_t len = 0; + char *line = dc_frame_next(&adapter->in_frame, &len); + + if (line == NULL) + break; + handle_line(adapter, line, len); + free(line); + if (adapter->out_fd < 0) + return; + } + } +} + +static void +on_err_readable(int fd, short revents, void *arg) +{ + char buf[512]; + ssize_t n; + + (void)revents; + (void)arg; + do { + n = read(fd, buf, sizeof(buf)); + } while (n < 0 && errno == EINTR); +} + +static void +stop_timer_cb(void *arg) +{ + DcAdapter *adapter = arg; + + adapter->stop_timer = 0; + if (adapter->pid > 0) + (void)kill(adapter->pid, SIGTERM); +} + +static void +schedule_restart(DcAdapter *adapter) +{ + if (adapter->stop_requested || adapter->bye) { + adapter->state = DC_ADAPTER_STOPPED; + emit_state(adapter, "offline", -1); + return; + } + adapter->restarts++; + adapter->backoff_ms = adapter->backoff_ms != 0 ? + adapter->backoff_ms * 2 : 2000; + if (adapter->backoff_ms > 60000) + adapter->backoff_ms = 60000; + adapter->state = DC_ADAPTER_BACKOFF; + emit_state(adapter, "backoff", adapter->backoff_ms); + adapter->restart_timer = dc_loop_add_timer(adapter->loop, + adapter->backoff_ms, restart_cb, adapter); + (void)adapter; +} + +static int +spawn(DcAdapter *adapter) +{ + int status; + pid_t pid; + int in[2] = { -1, -1 }, out[2] = { -1, -1 }, err[2] = { -1, -1 }; + + if (pipe(in) != 0) + return -1; + if (pipe(out) != 0) { + close(in[0]); + close(in[1]); + return -1; + } + if (pipe(err) != 0) { + close(in[0]); + close(in[1]); + close(out[0]); + close(out[1]); + return -1; + } + pid = fork(); + if (pid < 0) { + close(in[0]); close(in[1]); + close(out[0]); close(out[1]); + close(err[0]); close(err[1]); + return -1; + } + if (pid == 0) { + (void)dup2(in[0], STDIN_FILENO); + (void)dup2(out[1], STDOUT_FILENO); + (void)dup2(err[1], STDERR_FILENO); + close(in[0]); close(in[1]); + close(out[0]); close(out[1]); + close(err[0]); close(err[1]); + execl(adapter->path, adapter->path, "--stdio", (char *)NULL); + _exit(127); + } + close(in[0]); + close(out[1]); + close(err[1]); + if (set_nonblock_cloexec(in[1]) != 0 || + set_nonblock_cloexec(out[0]) != 0 || + set_nonblock_cloexec(err[0]) != 0) { + (void)kill(pid, SIGKILL); + (void)waitpid(pid, &status, 0); + close(in[1]); + close(out[0]); + close(err[0]); + return -1; + } + adapter->pid = pid; + adapter->in_fd = in[1]; + adapter->out_fd = out[0]; + adapter->err_fd = err[0]; + adapter->out_id = dc_loop_add_fd(adapter->loop, adapter->out_fd, + POLLIN, on_out_readable, adapter); + adapter->err_id = dc_loop_add_fd(adapter->loop, adapter->err_fd, + POLLIN, on_err_readable, adapter); + if (adapter->out_id <= 0 || adapter->err_id <= 0) { + detach_io(adapter); + reap_child(adapter); + return -1; + } + adapter->state = DC_ADAPTER_STARTING; + adapter->got_hello = 0; + emit_state(adapter, "starting", -1); + return 0; +} + +static void +restart_cb(void *arg) +{ + DcAdapter *adapter = arg; + + adapter->restart_timer = 0; + if (adapter->stop_requested || adapter->bye) { + adapter->state = DC_ADAPTER_STOPPED; + emit_state(adapter, "offline", -1); + return; + } + if (spawn(adapter) != 0) { + emit_error(adapter, "adapter start failed"); + schedule_restart(adapter); + } +} + +static void +handle_exit(DcAdapter *adapter) +{ + if (adapter->out_fd < 0 && adapter->err_fd < 0 && + adapter->pid <= 0) + return; + detach_io(adapter); + reap_child(adapter); + if (adapter->stop_timer != 0) { + (void)dc_loop_del_timer(adapter->loop, adapter->stop_timer); + adapter->stop_timer = 0; + } + if (adapter->stop_requested || adapter->bye) { + adapter->state = DC_ADAPTER_STOPPED; + emit_state(adapter, "offline", -1); + return; + } + schedule_restart(adapter); +} + +DcAdapter * +dc_adapter_start(DcLoop *loop, const char *path, const char *account, + const DcJson *settings, const DcJson *secrets, DcAdapterEventFn fn, + void *arg) +{ + DcAdapter *adapter; + + if (loop == NULL || path == NULL || account == NULL || fn == NULL) + return NULL; + adapter = calloc(1, sizeof(*adapter)); + if (adapter == NULL) + return NULL; + adapter->loop = loop; + adapter->path = strdup(path); + adapter->account = strdup(account); + adapter->settings = json_clone(settings); + adapter->secrets = json_clone(secrets); + adapter->fn = fn; + adapter->arg = arg; + adapter->pid = -1; + adapter->in_fd = adapter->out_fd = adapter->err_fd = -1; + adapter->state = DC_ADAPTER_STOPPED; + dc_frame_init(&adapter->in_frame); + if (adapter->path == NULL || adapter->account == NULL || + adapter->settings == NULL || adapter->secrets == NULL) { + dc_adapter_free(adapter); + return NULL; + } + if (spawn(adapter) != 0) { + dc_adapter_free(adapter); + return NULL; + } + return adapter; +} + +void +dc_adapter_stop(DcAdapter *adapter) +{ + DcJson *cmd; + + if (adapter == NULL || adapter->state == DC_ADAPTER_STOPPED) + return; + adapter->stop_requested = 1; + if (adapter->restart_timer != 0) { + (void)dc_loop_del_timer(adapter->loop, adapter->restart_timer); + adapter->restart_timer = 0; + } + if (adapter->state == DC_ADAPTER_BACKOFF || adapter->pid <= 0) { + adapter->state = DC_ADAPTER_STOPPED; + emit_state(adapter, "offline", -1); + return; + } + cmd = dc_json_new_object(); + if (cmd != NULL) { + (void)dc_json_object_set(cmd, "type", + dc_json_new_string("disconnect")); + (void)queue_json(adapter, cmd); + dc_json_free(cmd); + } + if (adapter->stop_timer == 0) + adapter->stop_timer = dc_loop_add_timer(adapter->loop, 2000, + stop_timer_cb, adapter); +} + +void +dc_adapter_free(DcAdapter *adapter) +{ + int status; + + if (adapter == NULL) + return; + if (adapter->pid > 0) { + (void)kill(adapter->pid, SIGKILL); + (void)waitpid(adapter->pid, &status, 0); + adapter->pid = -1; + } + if (adapter->restart_timer != 0) + (void)dc_loop_del_timer(adapter->loop, adapter->restart_timer); + if (adapter->stop_timer != 0) + (void)dc_loop_del_timer(adapter->loop, adapter->stop_timer); + detach_io(adapter); + dc_frame_free(&adapter->in_frame); + dc_json_free(adapter->settings); + dc_json_free(adapter->secrets); + free(adapter->path); + free(adapter->account); + free(adapter); +} + +DcAdapterState +dc_adapter_state(const DcAdapter *adapter) +{ + return adapter ? adapter->state : DC_ADAPTER_STOPPED; +} + +int +dc_adapter_restarts(const DcAdapter *adapter) +{ + return adapter ? adapter->restarts : 0; +} + +int +dc_adapter_send_json(DcAdapter *adapter, const DcJson *json) +{ + if (adapter == NULL || json == NULL || + adapter->state == DC_ADAPTER_STOPPED) + return -1; + return queue_json(adapter, json); +} + +int +dc_adapter_send_text(DcAdapter *adapter, const char *conv, const char *text, + const char *client_id) +{ + DcJson *cmd; + int rc; + + if (adapter == NULL || conv == NULL || text == NULL) + return -1; + cmd = dc_json_new_object(); + if (cmd == NULL) + return -1; + (void)dc_json_object_set(cmd, "type", dc_json_new_string("send")); + (void)dc_json_object_set(cmd, "conv", dc_json_new_string(conv)); + (void)dc_json_object_set(cmd, "text", dc_json_new_string(text)); + if (client_id != NULL) + (void)dc_json_object_set(cmd, "client_id", + dc_json_new_string(client_id)); + rc = dc_adapter_send_json(adapter, cmd); + dc_json_free(cmd); + return rc; +} + +int +dc_adapter_join(DcAdapter *adapter, const char *conv) +{ + DcJson *cmd; + int rc; + + if (adapter == NULL || conv == NULL) + return -1; + cmd = dc_json_new_object(); + if (cmd == NULL) + return -1; + (void)dc_json_object_set(cmd, "type", dc_json_new_string("join")); + (void)dc_json_object_set(cmd, "conv", dc_json_new_string(conv)); + rc = dc_adapter_send_json(adapter, cmd); + dc_json_free(cmd); + return rc; +} + +int +dc_adapter_answer(DcAdapter *adapter, int request_id, const char *value) +{ + DcJson *cmd = dc_json_new_object(); + DcJson *answer; + int rc; + + if (cmd == NULL) + return -1; + answer = value != NULL ? dc_json_new_string(value) : dc_json_new_null(); + if (answer == NULL) { + dc_json_free(cmd); + return -1; + } + (void)dc_json_object_set(cmd, "type", dc_json_new_string("answer")); + (void)dc_json_object_set(cmd, "id", dc_json_new_int(request_id)); + (void)dc_json_object_set(cmd, "value", answer); + rc = dc_adapter_send_json(adapter, cmd); + dc_json_free(cmd); + return rc; +} diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 30838ed7..08615ae9 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -163,4 +163,44 @@ char *dc_text_clean(const char *s); char *dc_text_irc_strip(const char *s); char *dc_text_to_display(const char *s, const char *charset); +/* + * Adapter processes. dc_adapter_start() forks/execs PATH with "--stdio", + * waits for its "hello" (ipc 1), sends "configure" and "connect", and then + * routes every JSON object from the adapter to FN. The event is owned by + * the adapter and valid only during the callback. + * + * If the child dies without saying "bye", the manager restarts it after + * 2, 4, 8 ... 60 seconds. dc_adapter_stop() sends "disconnect" and starts + * the 2 s shutdown; dc_adapter_free() may be called after the state is + * STOPPED (it also kills a still-running child). Settings and secrets are + * deep-copied; the caller keeps its objects. + */ +typedef struct DcAdapter DcAdapter; + +typedef enum { + DC_ADAPTER_STARTING, + DC_ADAPTER_CONFIGURED, + DC_ADAPTER_ONLINE, + DC_ADAPTER_BACKOFF, + DC_ADAPTER_STOPPED +} DcAdapterState; + +typedef void (*DcAdapterEventFn)(DcAdapter *adapter, const DcJson *event, + void *arg); + +DcAdapter *dc_adapter_start(DcLoop *loop, const char *path, + const char *account, const DcJson *settings, const DcJson *secrets, + DcAdapterEventFn fn, void *arg); +void dc_adapter_stop(DcAdapter *adapter); +void dc_adapter_free(DcAdapter *adapter); + +DcAdapterState dc_adapter_state(const DcAdapter *adapter); +int dc_adapter_restarts(const DcAdapter *adapter); + +int dc_adapter_send_json(DcAdapter *adapter, const DcJson *json); +int dc_adapter_send_text(DcAdapter *adapter, const char *conv, + const char *text, const char *client_id); +int dc_adapter_join(DcAdapter *adapter, const char *conv); +int dc_adapter_answer(DcAdapter *adapter, int request_id, const char *value); + #endif /* _DtChat_dc_internal_h */ diff --git a/lib/libDtChat/fake_adapter.c b/lib/libDtChat/fake_adapter.c new file mode 100644 index 00000000..02db1299 --- /dev/null +++ b/lib/libDtChat/fake_adapter.c @@ -0,0 +1,202 @@ +/* + * fake_adapter.c -- the test adapter for T1.5 + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * Speaks the JSON-lines protocol on stdin/stdout. It sends a valid hello, + * answers configure/connect, echoes send/join and exits on disconnect. + * Settings {"mode":"crash"} makes it exit after connect so the manager's + * backoff can be tested; {"mode":"garbage"} sends an invalid line. + */ +#include "dc_internal.h" + +#include +#include +#include +#include +#include +#include + +static char mode[32] = "normal"; + +static void +send_json(DcJson *json) +{ + size_t len = 0; + char *text; + + if (json == NULL) + return; + text = dc_json_write(json, &len); + if (text != NULL) { + (void)dc_frame_send(STDOUT_FILENO, text, len); + free(text); + } + dc_json_free(json); +} + +static void +send_state(const char *state) +{ + DcJson *json = dc_json_new_object(); + + (void)dc_json_object_set(json, "type", dc_json_new_string("state")); + (void)dc_json_object_set(json, "state", dc_json_new_string(state)); + send_json(json); +} + +static void +send_hello(void) +{ + DcJson *hello = dc_json_new_object(); + DcJson *caps = dc_json_new_array(); + + (void)dc_json_array_append(caps, dc_json_new_string("text")); + (void)dc_json_array_append(caps, dc_json_new_string("channels")); + (void)dc_json_object_set(hello, "type", dc_json_new_string("hello")); + (void)dc_json_object_set(hello, "ipc", dc_json_new_int(DC_IPC_VERSION)); + (void)dc_json_object_set(hello, "protocol", + dc_json_new_string("fake")); + (void)dc_json_object_set(hello, "version", + dc_json_new_string("1.0")); + (void)dc_json_object_set(hello, "caps", caps); + send_json(hello); +} + +static void +handle(DcJson *cmd) +{ + const char *type = dc_json_string(dc_json_get(cmd, "type")); + const char *conv; + const char *text; + const char *client_id; + + if (type == NULL) + return; + if (strcmp(type, "configure") == 0) { + const char *m = dc_json_string(dc_json_get( + dc_json_get(cmd, "settings"), "mode")); + + if (m != NULL) { + snprintf(mode, sizeof(mode), "%s", m); + } + return; + } + if (strcmp(type, "connect") == 0) { + if (strcmp(mode, "crash") == 0) + _exit(1); + if (strcmp(mode, "garbage") == 0) { + (void)write(STDOUT_FILENO, "{bad\n", 5); + _exit(1); + } + send_state("online"); + return; + } + if (strcmp(type, "send") == 0) { + DcJson *sent = dc_json_new_object(); + DcJson *message = dc_json_new_object(); + + conv = dc_json_string(dc_json_get(cmd, "conv")); + text = dc_json_string(dc_json_get(cmd, "text")); + client_id = dc_json_string(dc_json_get(cmd, "client_id")); + (void)dc_json_object_set(sent, "type", + dc_json_new_string("sent")); + (void)dc_json_object_set(sent, "conv", + dc_json_new_string(conv)); + if (client_id != NULL) + (void)dc_json_object_set(sent, "client_id", + dc_json_new_string(client_id)); + (void)dc_json_object_set(sent, "ts", + dc_json_new_int((long long)time(NULL))); + send_json(sent); + + (void)dc_json_object_set(message, "type", + dc_json_new_string("message")); + (void)dc_json_object_set(message, "conv", + dc_json_new_string(conv)); + (void)dc_json_object_set(message, "from", + dc_json_new_string("fake")); + (void)dc_json_object_set(message, "text", + dc_json_new_string(text != NULL ? text : "")); + (void)dc_json_object_set(message, "ts", + dc_json_new_int((long long)time(NULL))); + send_json(message); + return; + } + if (strcmp(type, "join") == 0) { + DcJson *conversation = dc_json_new_object(); + + conv = dc_json_string(dc_json_get(cmd, "conv")); + (void)dc_json_object_set(conversation, "type", + dc_json_new_string("conversation")); + (void)dc_json_object_set(conversation, "conv", + dc_json_new_string(conv)); + (void)dc_json_object_set(conversation, "kind", + dc_json_new_string("channel")); + (void)dc_json_object_set(conversation, "title", + dc_json_new_string(conv)); + send_json(conversation); + return; + } + if (strcmp(type, "answer") == 0) { + DcJson *answer = dc_json_new_object(); + + (void)dc_json_object_set(answer, "type", + dc_json_new_string("answer-received")); + (void)dc_json_object_set(answer, "id", + dc_json_new_int(dc_json_int(dc_json_get(cmd, "id")))); + send_json(answer); + return; + } + if (strcmp(type, "disconnect") == 0) { + DcJson *bye = dc_json_new_object(); + + (void)dc_json_object_set(bye, "type", + dc_json_new_string("bye")); + (void)dc_json_object_set(bye, "reason", + dc_json_new_string("requested")); + send_json(bye); + _exit(0); + } +} + +int +main(int argc, char **argv) +{ + DcFrameBuf frame; + char buf[4096]; + + (void)argc; + (void)argv; + dc_frame_init(&frame); + send_hello(); + for (;;) { + ssize_t n = read(STDIN_FILENO, buf, sizeof(buf)); + + if (n < 0) { + if (errno == EINTR) + continue; + break; + } + if (n == 0) + break; + if (dc_frame_feed(&frame, buf, (size_t)n) != 0) + break; + for (;;) { + size_t len = 0; + char *line = dc_frame_next(&frame, &len); + DcJson *cmd; + + if (line == NULL) + break; + cmd = dc_json_parse(line, len, NULL, 0); + free(line); + if (cmd != NULL) { + handle(cmd); + dc_json_free(cmd); + } + } + } + dc_frame_free(&frame); + return 0; +} diff --git a/lib/libDtChat/test_adapter.c b/lib/libDtChat/test_adapter.c new file mode 100644 index 00000000..7da052b2 --- /dev/null +++ b/lib/libDtChat/test_adapter.c @@ -0,0 +1,267 @@ +/* + * test_adapter.c -- tests for the adapter process manager + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#include "dc_internal.h" + +#include +#include +#include +#include + +static int fails; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ + #cond); \ + fails++; \ + } \ +} while (0) + +struct Seen { + int starting; + int hello; + int online; + int backoff; + int offline; + int message; + int sent; + int answer; + int error; + char text[128]; + char client[64]; +}; + +static void +on_event(DcAdapter *adapter, const DcJson *event, void *arg) +{ + struct Seen *seen = arg; + const char *type = dc_json_string(dc_json_get(event, "type")); + const char *value; + + (void)adapter; + if (type == NULL) + return; + if (strcmp(type, "hello") == 0) { + seen->hello++; + } else if (strcmp(type, "state") == 0) { + const char *state = dc_json_string(dc_json_get(event, "state")); + + if (state == NULL) + return; + if (strcmp(state, "starting") == 0) + seen->starting++; + else if (strcmp(state, "online") == 0) + seen->online++; + else if (strcmp(state, "backoff") == 0) + seen->backoff++; + else if (strcmp(state, "offline") == 0) + seen->offline++; + } else if (strcmp(type, "message") == 0) { + seen->message++; + value = dc_json_string(dc_json_get(event, "text")); + if (value != NULL) + snprintf(seen->text, sizeof(seen->text), "%s", value); + } else if (strcmp(type, "sent") == 0) { + seen->sent++; + value = dc_json_string(dc_json_get(event, "client_id")); + if (value != NULL) + snprintf(seen->client, sizeof(seen->client), "%s", value); + } else if (strcmp(type, "answer-received") == 0) { + seen->answer++; + } else if (strcmp(type, "error") == 0) { + seen->error++; + } +} + +static int +has_online(void *arg) +{ + return ((struct Seen *)arg)->online > 0; +} + +static int +has_hello(void *arg) +{ + return ((struct Seen *)arg)->hello > 0; +} + +static int +has_two_hello(void *arg) +{ + return ((struct Seen *)arg)->hello >= 2; +} + +static int +has_message(void *arg) +{ + return ((struct Seen *)arg)->message > 0 && + ((struct Seen *)arg)->sent > 0; +} + +static int +has_answer(void *arg) +{ + return ((struct Seen *)arg)->answer > 0; +} + +static int +has_error(void *arg) +{ + return ((struct Seen *)arg)->error > 0; +} + +static int +adapter_stopped(void *arg) +{ + return dc_adapter_state((DcAdapter *)arg) == DC_ADAPTER_STOPPED; +} + +static int +wait_until(DcLoop *loop, int (*pred)(void *), void *arg, int ms) +{ + long long start = dc_loop_now_ms(); + + while (!pred(arg) && dc_loop_now_ms() - start < ms) + (void)dc_loop_run(loop, 20); + return pred(arg); +} + +static int +fake_path(char *out, size_t cap, const char *argv0) +{ + const char *slash = strrchr(argv0, '/'); + + if (slash != NULL) { + snprintf(out, cap, "%.*s/fake_adapter", + (int)(slash - argv0), argv0); + } else { + snprintf(out, cap, "./fake_adapter"); + } + return access(out, X_OK) == 0; +} + +static DcJson * +settings_with_mode(const char *mode) +{ + DcJson *settings = dc_json_new_object(); + + (void)dc_json_object_set(settings, "mode", + dc_json_new_string(mode)); + return settings; +} + +static void +test_normal(DcLoop *loop, const char *path) +{ + DcJson *settings = settings_with_mode("normal"); + DcJson *secrets = dc_json_new_object(); + struct Seen seen; + DcAdapter *adapter; + + memset(&seen, 0, sizeof(seen)); + adapter = dc_adapter_start(loop, path, "acct", settings, secrets, + on_event, &seen); + CHECK(adapter != NULL); + if (adapter == NULL) + return; + CHECK(wait_until(loop, has_online, &seen, 3000)); + CHECK(seen.hello == 1); + CHECK(seen.starting == 1); + CHECK(dc_adapter_state(adapter) == DC_ADAPTER_ONLINE); + + CHECK(dc_adapter_send_text(adapter, "#test", "hello", "c1") == 0); + CHECK(wait_until(loop, has_message, &seen, 2000)); + CHECK(strcmp(seen.text, "hello") == 0); + CHECK(strcmp(seen.client, "c1") == 0); + + CHECK(dc_adapter_join(adapter, "#test") == 0); + CHECK(dc_adapter_answer(adapter, 7, "code") == 0); + CHECK(wait_until(loop, has_answer, &seen, 2000)); + + dc_adapter_stop(adapter); + CHECK(wait_until(loop, adapter_stopped, adapter, 3000)); + CHECK(dc_adapter_state(adapter) == DC_ADAPTER_STOPPED); + dc_adapter_free(adapter); + dc_json_free(settings); + dc_json_free(secrets); +} + +static void +test_restart(DcLoop *loop, const char *path) +{ + DcJson *settings = settings_with_mode("crash"); + DcJson *secrets = dc_json_new_object(); + struct Seen seen; + DcAdapter *adapter; + + memset(&seen, 0, sizeof(seen)); + adapter = dc_adapter_start(loop, path, "acct", settings, secrets, + on_event, &seen); + CHECK(adapter != NULL); + if (adapter == NULL) + return; + CHECK(wait_until(loop, has_hello, &seen, 3000)); + CHECK(wait_until(loop, has_two_hello, &seen, 6000)); + CHECK(dc_adapter_restarts(adapter) >= 1); + CHECK(seen.backoff >= 1); + + dc_adapter_stop(adapter); + CHECK(adapter_stopped(adapter) || + wait_until(loop, adapter_stopped, adapter, 3000)); + dc_adapter_free(adapter); + dc_json_free(settings); + dc_json_free(secrets); +} + +static void +test_garbage(DcLoop *loop, const char *path) +{ + DcJson *settings = settings_with_mode("garbage"); + DcJson *secrets = dc_json_new_object(); + struct Seen seen; + DcAdapter *adapter; + + memset(&seen, 0, sizeof(seen)); + adapter = dc_adapter_start(loop, path, "acct", settings, secrets, + on_event, &seen); + CHECK(adapter != NULL); + if (adapter == NULL) + return; + CHECK(wait_until(loop, has_error, &seen, 3000)); + dc_adapter_stop(adapter); + (void)wait_until(loop, adapter_stopped, adapter, 3000); + dc_adapter_free(adapter); + dc_json_free(settings); + dc_json_free(secrets); +} + +int +main(int argc, char **argv) +{ + char path[512]; + DcLoop *loop; + + if (argc < 1 || !fake_path(path, sizeof(path), argv[0])) { + fprintf(stderr, "fake_adapter not found next to %s\n", + argc > 0 ? argv[0] : "test_adapter"); + return 1; + } + loop = dc_loop_new(); + if (loop == NULL) { + perror("dc_loop_new"); + return 1; + } + test_normal(loop, path); + test_restart(loop, path); + test_garbage(loop, path); + dc_loop_free(loop); + if (fails != 0) { + fprintf(stderr, "%d test(s) failed\n", fails); + return 1; + } + printf("test_adapter: all ok\n"); + return 0; +}