diff --git a/CMakeLists.txt b/CMakeLists.txt index b9dd6d2..99ccb9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ ADD_DEFINITIONS(-Os -ggdb -Wextra -Wall -Werror --std=gnu99 -Wmissing-declaratio SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") -SET(SOURCES main.c config.c proto.c ubus.c task.c cmd.c blink.c health.c apply.c verify.c failsafe.c upload.c) +SET(SOURCES main.c config.c proto.c ubus.c task.c cmd.c blink.c health.c apply.c verify.c failsafe.c upload.c crashlog.c) FIND_LIBRARY(ubus NAMES ubus) FIND_LIBRARY(blobmsg_json NAMES blobmsg_json) diff --git a/crashlog.c b/crashlog.c new file mode 100644 index 0000000..16f73d7 --- /dev/null +++ b/crashlog.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +#include "ucentral.h" + +#define CRASHLOG "/tmp/crashlog" + +static struct blob_buf crashlog; + +void +crashlog_init(void) +{ + enum { + CRASH_LOG, + __CRASH_MAX, + }; + + static const struct blobmsg_policy crash_policy[__CRASH_MAX] = { + [CRASH_LOG] = { .name = "crashlog", .type = BLOBMSG_TYPE_ARRAY }, + }; + + struct blob_attr *tb[__CRASH_MAX] = {}; + struct stat s = {}; + + if (stat(CRASHLOG, &s)) + return; + + blob_buf_init(&crashlog, 0); + if (blobmsg_add_json_from_file(&crashlog, CRASHLOG)) { + blobmsg_parse(crash_policy, __CRASH_MAX, tb, blob_data(crashlog.head), + blob_len(crashlog.head)); + if (tb[CRASH_LOG]) + crashlog_send(tb[CRASH_LOG]); + else + log_send("failed to parse the crashlog"); + } else { + log_send("found a crashlog that is not valid json"); + } + blob_buf_free(&crashlog); + unlink(CRASHLOG); +} diff --git a/main.c b/main.c index 5182a18..950dfc6 100644 --- a/main.c +++ b/main.c @@ -161,6 +161,7 @@ callback_broker(struct lws *wsi, enum lws_callback_reasons reason, conn_time = time(NULL); websocket = wsi; connect_send(); + crashlog_init(); break; case LWS_CALLBACK_CLIENT_RECEIVE: diff --git a/proto.c b/proto.c index 96aae9d..387a3c2 100644 --- a/proto.c +++ b/proto.c @@ -291,7 +291,24 @@ health_send(uint32_t sanity, struct blob_attr *a) blobmsg_add_blob(&proto, b); blobmsg_close_table(&proto, c); blobmsg_close_table(&proto, m); - ULOG_DBG("xmit message\n"); + proto_send_blob(); +} + +void +crashlog_send(struct blob_attr *a) +{ + void *m = proto_new_blob("crashlog"); + struct blob_attr *b; + void *c; + int rem; + + blobmsg_add_string(&proto, "serial", client.serial); + blobmsg_add_u64(&proto, "uuid", uuid_active); + c = blobmsg_open_array(&proto, "loglines"); + blobmsg_for_each_attr(b, a, rem) + blobmsg_add_blob(&proto, b); + blobmsg_close_array(&proto, c); + blobmsg_close_table(&proto, m); proto_send_blob(); } diff --git a/ucentral.h b/ucentral.h index 06c5b94..0a9e2c6 100644 --- a/ucentral.h +++ b/ucentral.h @@ -103,6 +103,10 @@ void failsafe_init(void); void task_run(struct task *task, time_t uuid, uint32_t id); void task_stop(struct task *task); +void crashlog_init(void); +void crashlog_send(struct blob_attr *b); + + static inline void safe_free(char **mem) { if (!*mem) diff --git a/upload.c b/upload.c index 0c11a11..da81824 100644 --- a/upload.c +++ b/upload.c @@ -18,7 +18,8 @@ upload_run_cb(time_t uuid) ULOG_INFO("failed to start upload task\n"); exit(1); } - ULOG_INFO("running upload task\n"); + ULOG_INFO("Calling /usr/bin/curl -F %s -f %s %s %s", name, file, upload_uri, + client.selfsigned ? "--insecure" : ""); execlp("/usr/bin/curl", "/usr/bin/curl", "-F", name, "-f", file, upload_uri,