Replace the Bitlair spacestate hook with a generic script hook

This commit is contained in:
polyfloyd 2017-10-17 21:41:14 +02:00
parent 7935c982f9
commit 84cec0da89
4 changed files with 80 additions and 13 deletions

View file

@ -23,7 +23,7 @@
#include "includes.h"
#include "database.h"
#include "jsonbot.h"
#include "spacestate.h"
#include "hook_script.h"
/* My global state */
configuration *conf = NULL;
@ -163,8 +163,8 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx)
database_init();
} else if (strcmp(ptr, "jsonbot") == 0) {
jsonbot_init();
} else if (strcmp(ptr, "spacestate") == 0) {
spacestate_init();
} else if (strcmp(ptr, "script") == 0) {
script_init();
}
} while((ptr = strtok(NULL, " ")) != NULL);
}

View file

@ -48,14 +48,7 @@ typedef struct {
char *jsonbot_password;
char *jsonbot_privmsg_to;
/* Space state database client configuration */
char *spacestate_host;
char *spacestate_username;
char *spacestate_password;
char *spacestate_name;
char *spacestate_driver;
char *spacestate_hook_open;
char *spacestate_hook_close;
char *hook_script_path;
/* Global configuration based state */
GKeyFile *keyfile;

74
hook_script.c Normal file
View file

@ -0,0 +1,74 @@
/*
SIA-HS Alarm Monitoring Service
Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 2013
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _XOPEN_SOURCE
#include "includes.h"
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
static void child_handler(int sig) {
pid_t pid;
int status;
/* EXTERMINATE! EXTERMINATE! */
while ((pid = waitpid(-1, &status, WNOHANG)) > 0);
}
STATUS script_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) {
DEBUG(6, "Got event for script hook: %s %s: %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code));
const configuration *conf = get_conf();
DEBUG(4, "About to execute %s", conf->hook_script_path);
if (!fork()) {
execl(
conf->hook_script_path,
conf->hook_script_path,
prom,
code,
description,
sia_code_str(code),
sia_code_desc(code),
(char *)NULL
);
exit(0);
}
return ST_OK;
}
STATUS script_init(void) {
/* Establish SIGCHLD handler. */
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, NULL);
GError *error = NULL;
configuration *conf = get_modifiable_conf();
conf->hook_script_path = g_key_file_get_string(conf->keyfile, "script", "path", &error);
if (error) {
DEBUG(2, "Disabling script hook because no path is set");
return ST_OK;
}
DEBUG(3, "Script hook enabled");
conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1);
conf->event_handlers[conf->event_handler_cnt] = script_update;
conf->event_handler_cnt++;
return ST_OK;
}

View file

@ -16,5 +16,5 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description);
STATUS spacestate_init(void);
STATUS script_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description);
STATUS script_init(void);