diff --git a/config.c b/config.c index b2179bc..502b457 100644 --- a/config.c +++ b/config.c @@ -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); } diff --git a/config.h b/config.h index c27ba78..5a41983 100644 --- a/config.h +++ b/config.h @@ -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; diff --git a/hook_script.c b/hook_script.c new file mode 100644 index 0000000..8220dfb --- /dev/null +++ b/hook_script.c @@ -0,0 +1,74 @@ +/* + SIA-HS Alarm Monitoring Service + Copyright (C) Wilco Baan Hofman 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 . +*/ +#define _XOPEN_SOURCE +#include "includes.h" +#include +#include +#include + +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; +} diff --git a/spacestate.h b/hook_script.h similarity index 84% rename from spacestate.h rename to hook_script.h index a362958..1fcd0d7 100644 --- a/spacestate.h +++ b/hook_script.h @@ -16,5 +16,5 @@ along with this program. If not, see . */ -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);