diff --git a/notify.c b/notify.c new file mode 100644 index 0000000..43d011b --- /dev/null +++ b/notify.c @@ -0,0 +1,79 @@ +#include +#include "runtime-config.h" +#include +#define NOTIFICATION_PREFIX "[h/nest-sanity-checks] " +#define NOTIFICATION_PREFIX_LEN sizeof(NOTIFICATION_PREFIX) +/* notify_init() initializes curl. + If this fails, exiting is the only option, as curl must be usable, + or alerts will fail to send later on when things break. + This file is the only context that should utilize libcurl. + Therefore, the curl instance is static. */ +static CURL *curl; +/* notify_init_early is the first thing that runs, + * and it makes sure that curl_global_init() does not + * run multiple times in parallel somehow. + */ +int notify_init_early() { + CURLcode curl_global_init_res; + curl_global_init_res = curl_global_init(CURL_GLOBAL_DEFAULT); + if(curl_global_init_res!=CURLE_OK) { + fprintf(stderr, "[E] curl_global_init failed.\n"); + exit(1); + } + else { + return 0; + } +int notify_init() { + curl = curl_easy_init(); + if(curl == NULL) { + fprintf(stderr, "[E] curl_easy_init failed.\n"); + exit(1); + } + CURLcode url_set_result; + url_set_result = curl_easy_setopt(config_get_url()); + if(url_set_result!=CURLE_OK) { + fprintf(stderr, "[E] Failed to set notify URL. Cannot deliver notifications, exiting.\n"); + exit(1); + } +} +char *build_prefixed_report(const char *report) { + /* Adds a prefix to every message that goes to nest admins */ + /* TODO: do this in a less weird way, + * maybe consider creating a C macro that + * adds prefixes and calls the report send function */ + int report_len = strlen(report); + int post_payload_len = report_len + NOTIFICATION_PREFIX_LEN; + char *post_payload = malloc(post_payload_len); + for(int a=0;a +#include "runtime-config.h" +int main() { + +}