Added a working test tool, set up automake

This commit is contained in:
9pfs 2024-04-24 00:55:06 +00:00
parent fae91425e9
commit 50b5fb5a34
Signed by: 9pfs
SSH key fingerprint: SHA256:TOcGxMQCfy4VvRM8AzgXErKXdkAtaTcpGXgYMpyoJoY
11 changed files with 141 additions and 13 deletions

8
.gitignore vendored
View file

@ -108,3 +108,11 @@ Makefile
# temporary files created by nano # temporary files created by nano
.*.swp .*.swp
# backups of configure scripts and configure.ac
configure.ac~
configure~
# binaries
nest-sanity
notify-test

8
Makefile.am Normal file
View file

@ -0,0 +1,8 @@
bin_PROGRAMS = nest-sanity
noinst_PROGRAMS = notify-test
nest_sanity_CFLAGS = $(LIBCURL_CFLAGS)
nest_sanity_LDADD = $(LIBCURL_LIBS)
nest_sanity_SOURCES = sanity.c notify.c runtime-config.c
notify_test_CFLAGS = $(LIBCURL_CFLAGS)
notify_test_LDADD = $(LIBCURL_LIBS)
notify_test_SOURCES = notify.c runtime-config.c notify-test.c

5
bootstrap Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh
aclocal && \
autoheader && \
automake --foreign --add-missing --copy && \
autoconf

33
config.h Normal file
View file

@ -0,0 +1,33 @@
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
to 0 otherwise. */
#define HAVE_MALLOC 1
/* Name of package */
#define PACKAGE "nest-sanity-checks"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "https://git.h.hackclub.app/9pfs/nest-sanity-checks/issues"
/* Define to the full name of this package. */
#define PACKAGE_NAME "nest-sanity-checks"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "nest-sanity-checks 0.0.1-dev"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "nest-sanity-checks"
/* Define to the home page for this package. */
#define PACKAGE_URL ""
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.0.1-dev"
/* Version number of package */
#define VERSION "0.0.1-dev"
/* Define to rpl_malloc if the replacement function should be used. */
/* #undef malloc */

25
configure.ac Normal file
View file

@ -0,0 +1,25 @@
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.71])
AC_INIT([nest-sanity-checks],[0.0.1-dev],[https://git.h.hackclub.app/9pfs/nest-sanity-checks/issues])
AC_CONFIG_SRCDIR([notify.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CC
PKG_PROG_PKG_CONFIG
# Checks for libraries.
PKG_CHECK_MODULES([LIBCURL], [libcurl])
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_FUNC_MALLOC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

14
notify-test.c Normal file
View file

@ -0,0 +1,14 @@
#include "notify.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argc!=2) {
fprintf(stderr, "usage: %s <message>\n", argv[0]);
exit(1);
}
notify_init_early();
notify_init();
int notify_res;
notify_res = send_alarm(argv[1]);
printf("notify_res returned %d\n", notify_res);
}

View file

@ -1,8 +1,11 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "runtime-config.h" #include "runtime-config.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define NOTIFICATION_PREFIX "[h/nest-sanity-checks] " #define NOTIFICATION_PREFIX "[h/nest-sanity-checks] "
#define NOTIFICATION_PREFIX_LEN sizeof(NOTIFICATION_PREFIX) /* #define NOTIFICATION_PREFIX_LEN sizeof(NOTIFICATION_PREFIX) */
/* notify_init() initializes curl. /* notify_init() initializes curl.
If this fails, exiting is the only option, as curl must be usable, If this fails, exiting is the only option, as curl must be usable,
or alerts will fail to send later on when things break. or alerts will fail to send later on when things break.
@ -23,6 +26,7 @@ int notify_init_early() {
else { else {
return 0; return 0;
} }
}
int notify_init() { int notify_init() {
curl = curl_easy_init(); curl = curl_easy_init();
if(curl == NULL) { if(curl == NULL) {
@ -30,49 +34,60 @@ int notify_init() {
exit(1); exit(1);
} }
CURLcode url_set_result; CURLcode url_set_result;
url_set_result = curl_easy_setopt(config_get_url()); char *url;
url = config_get_url();
if(url==NULL) {
fprintf(stderr, "[E] notify url is null, exiting!\n");
exit(1);
}
url_set_result = curl_easy_setopt(curl, CURLOPT_URL, config_get_url());
if(url_set_result!=CURLE_OK) { if(url_set_result!=CURLE_OK) {
fprintf(stderr, "[E] Failed to set notify URL. Cannot deliver notifications, exiting.\n"); fprintf(stderr, "[E] Failed to set notify URL. Cannot deliver notifications, exiting.\n");
exit(1); exit(1);
} }
return 0;
} }
char *build_prefixed_report(const char *report) { const char *build_prefixed_report(const char *report) {
/* Adds a prefix to every message that goes to nest admins */ /* Adds a prefix to every message that goes to nest admins */
/* TODO: do this in a less weird way, /* TODO: do this in a less weird way,
* maybe consider creating a C macro that * maybe consider creating a C macro that
* adds prefixes and calls the report send function */ * adds prefixes and calls the report send function */
int report_len = strlen(report); int report_len = strlen(report);
int post_payload_len = report_len + NOTIFICATION_PREFIX_LEN; int notification_prefix_len = strlen(NOTIFICATION_PREFIX);
int post_payload_len = report_len + notification_prefix_len + 1;
char *post_payload = malloc(post_payload_len); char *post_payload = malloc(post_payload_len);
for(int a=0;a<NOTIFICATION_PREFIX_LEN-1;a++) { for(int a=0;a<notification_prefix_len;a++) {
post_payload[a] = NOTIFICATION_PREFIX[a]; post_payload[a] = NOTIFICATION_PREFIX[a];
} }
for(int a=0;a<report_len;a++) { for(int a=0;a<report_len;a++) {
post_payload[NOTIFICATION_PREFIX_LEN+a] = report[a]; post_payload[notification_prefix_len+a] = report[a];
} }
post_payload[post_payload_len]=0; post_payload[post_payload_len]=0;
return post_payload; return post_payload;
} }
int send_alarm(const char *report) { int send_alarm(const char *report) {
assert(report!=NULL);
printf("[I] Sending report '%s'\n", report); printf("[I] Sending report '%s'\n", report);
char *prefixed_report; const char *prefixed_report;
prefixed_report = build_prefixed_report(report); prefixed_report = build_prefixed_report(report);
char *urlencoded_report; assert(prefixed_report!=NULL);
const char *urlencoded_report;
urlencoded_report = curl_easy_escape(curl, prefixed_report, 0); urlencoded_report = curl_easy_escape(curl, prefixed_report, 0);
if(urlencoded_report = NULL) { /* assert(urlencoded_report!=NULL); */
fprintf(stderr, "[E] Failed to send report, curl_easy_escape failed.\n", report); if(urlencoded_report == NULL) {
fprintf(stderr, "[E] Failed to send report, curl_easy_escape failed.\n");
return -1; return -1;
} }
CURLcode post_field_set_result; CURLcode post_field_set_result;
post_field_set_result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlencoded_report); post_field_set_result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlencoded_report);
if(post_field_set_result != CURLE_OK) { if(post_field_set_result != CURLE_OK) {
fprintf(stderr, "[E] Failed to send report, setting CURLOPT_POSTFIELDS failed.\n"); fprintf(stderr, "[E] Failed to send report, setting CURLOPT_POSTFIELDS to %s failed.\n", urlencoded_report);
return -1; return -1;
} }
CURLcode res; CURLcode res;
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if(res != CURLE_OK) { if(res != CURLE_OK) {
fprintf(stderr, "[E] Failed to send report, curl_easy_perform failed: %s\n", curl_easy_strerror(res); fprintf(stderr, "[E] Failed to send report, curl_easy_perform failed: %s\n", curl_easy_strerror(res));
return -1; return -1;
} }
return 0; return 0;

7
notify.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef NOTIFY_H_INCLUDED
#define NOTIFY_H_INCLUDED
int notify_init_early();
int notify_init();
const char *build_prefixed_report(const char *report);
int send_alarm(const char *report);
#endif

7
runtime-config.c Normal file
View file

@ -0,0 +1,7 @@
#include "runtime-config.h"
#include <stdlib.h>
char *config_get_url() {
/* TODO: find a better way to do this */
return getenv("NOTIFY_URL");
}

4
runtime-config.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef RUNTIME_CONFIG_INCLUDED
#define RUNTIME_CONFIG_INCLUDED
char *config_get_url();
#endif

View file

@ -1,5 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "runtime-config.h" #include "runtime-config.h"
#include "notify.h"
int main() { int main() {
notify_init_early();
notify_init();
} }