From 50b5fb5a349aa37f36d76d7d0d8d702eb9bfcc08 Mon Sep 17 00:00:00 2001 From: 9pfs <9pfs@amcforum.wiki> Date: Wed, 24 Apr 2024 00:55:06 +0000 Subject: [PATCH] Added a working test tool, set up automake --- .gitignore | 8 ++++++++ Makefile.am | 8 ++++++++ bootstrap | 5 +++++ config.h | 33 +++++++++++++++++++++++++++++++++ configure.ac | 25 +++++++++++++++++++++++++ notify-test.c | 14 ++++++++++++++ notify.c | 39 +++++++++++++++++++++++++++------------ notify.h | 7 +++++++ runtime-config.c | 7 +++++++ runtime-config.h | 4 ++++ sanity.c | 4 +++- 11 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 Makefile.am create mode 100755 bootstrap create mode 100644 config.h create mode 100644 configure.ac create mode 100644 notify-test.c create mode 100644 notify.h create mode 100644 runtime-config.c create mode 100644 runtime-config.h diff --git a/.gitignore b/.gitignore index fc90a01..089d9f0 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,11 @@ Makefile # temporary files created by nano .*.swp + +# backups of configure scripts and configure.ac +configure.ac~ +configure~ + +# binaries +nest-sanity +notify-test diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..5672975 --- /dev/null +++ b/Makefile.am @@ -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 diff --git a/bootstrap b/bootstrap new file mode 100755 index 0000000..83ec0ae --- /dev/null +++ b/bootstrap @@ -0,0 +1,5 @@ +#!/bin/sh +aclocal && \ +autoheader && \ +automake --foreign --add-missing --copy && \ +autoconf diff --git a/config.h b/config.h new file mode 100644 index 0000000..49709c4 --- /dev/null +++ b/config.h @@ -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 */ diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..429ca23 --- /dev/null +++ b/configure.ac @@ -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 diff --git a/notify-test.c b/notify-test.c new file mode 100644 index 0000000..ee6c2fc --- /dev/null +++ b/notify-test.c @@ -0,0 +1,14 @@ +#include "notify.h" +#include +#include +int main(int argc, char *argv[]) { + if(argc!=2) { + fprintf(stderr, "usage: %s \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); +} diff --git a/notify.c b/notify.c index 43d011b..052a2c7 100644 --- a/notify.c +++ b/notify.c @@ -1,8 +1,11 @@ #include #include "runtime-config.h" #include +#include +#include +#include #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. If this fails, exiting is the only option, as curl must be usable, or alerts will fail to send later on when things break. @@ -23,6 +26,7 @@ int notify_init_early() { else { return 0; } +} int notify_init() { curl = curl_easy_init(); if(curl == NULL) { @@ -30,49 +34,60 @@ int notify_init() { exit(1); } 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) { fprintf(stderr, "[E] Failed to set notify URL. Cannot deliver notifications, exiting.\n"); 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 */ /* 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; + int notification_prefix_len = strlen(NOTIFICATION_PREFIX); + int post_payload_len = report_len + notification_prefix_len + 1; char *post_payload = malloc(post_payload_len); - for(int a=0;a + +char *config_get_url() { + /* TODO: find a better way to do this */ + return getenv("NOTIFY_URL"); +} diff --git a/runtime-config.h b/runtime-config.h new file mode 100644 index 0000000..a0a8504 --- /dev/null +++ b/runtime-config.h @@ -0,0 +1,4 @@ +#ifndef RUNTIME_CONFIG_INCLUDED +#define RUNTIME_CONFIG_INCLUDED +char *config_get_url(); +#endif diff --git a/sanity.c b/sanity.c index c0e7e46..df3080d 100644 --- a/sanity.c +++ b/sanity.c @@ -1,5 +1,7 @@ #include #include "runtime-config.h" +#include "notify.h" int main() { - + notify_init_early(); + notify_init(); }