A child that does not send hello within 10 s is killed; the normal restart backoff applies. Aligns adapter.c with the v1 protocol document.
780 lines
18 KiB
C
780 lines
18 KiB
C
/*
|
|
* 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 <errno.h>
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define DC_ADAPTER_HELLO_MS 10000
|
|
|
|
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;
|
|
int hello_timer;
|
|
};
|
|
|
|
static void handle_exit(DcAdapter *adapter);
|
|
static void schedule_restart(DcAdapter *adapter);
|
|
static void restart_cb(void *arg);
|
|
static void hello_timeout_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;
|
|
if (adapter->hello_timer != 0) {
|
|
(void)dc_loop_del_timer(adapter->loop,
|
|
adapter->hello_timer);
|
|
adapter->hello_timer = 0;
|
|
}
|
|
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 void
|
|
hello_timeout_cb(void *arg)
|
|
{
|
|
DcAdapter *adapter = arg;
|
|
|
|
adapter->hello_timer = 0;
|
|
if (adapter->got_hello)
|
|
return;
|
|
emit_error(adapter, "adapter hello timeout");
|
|
if (adapter->pid > 0)
|
|
(void)kill(adapter->pid, SIGTERM);
|
|
handle_exit(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;
|
|
adapter->hello_timer = dc_loop_add_timer(adapter->loop,
|
|
DC_ADAPTER_HELLO_MS, hello_timeout_cb, adapter);
|
|
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->hello_timer != 0) {
|
|
(void)dc_loop_del_timer(adapter->loop, adapter->hello_timer);
|
|
adapter->hello_timer = 0;
|
|
}
|
|
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->hello_timer != 0) {
|
|
(void)dc_loop_del_timer(adapter->loop, adapter->hello_timer);
|
|
adapter->hello_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);
|
|
if (adapter->hello_timer != 0)
|
|
(void)dc_loop_del_timer(adapter->loop, adapter->hello_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;
|
|
}
|