Add hooks to spacestate
This commit is contained in:
parent
40bc2c209c
commit
057cf8cb8a
1 changed files with 64 additions and 7 deletions
71
spacestate.c
71
spacestate.c
|
@ -17,13 +17,49 @@
|
|||
*/
|
||||
#include "includes.h"
|
||||
#include "database.h"
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
static dbi_conn conn;
|
||||
|
||||
static void child_handler(int sig) {
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
/* EXTERMINATE! EXTERMINATE! */
|
||||
while((pid = waitpid(-1, &status, WNOHANG)) > 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void safe_forkexec(TALLOC_CTX *mem_ctx, const char *script_in, const char *prom, const char *code) {
|
||||
char *script = talloc_strdup(mem_ctx, script_in);
|
||||
char *argv[32];
|
||||
unsigned int i, j;
|
||||
|
||||
argv[0] = script;
|
||||
for (i = 0, j = 1; i < strlen(script) && j < 31; i++) {
|
||||
if (script[i] == ' ') {
|
||||
script[i] = '\0';
|
||||
argv[j++] = &script[i+1];
|
||||
}
|
||||
}
|
||||
argv[j++] = talloc_strdup(script, prom);
|
||||
argv[j++] = talloc_strdup(script, code);
|
||||
argv[j] = NULL;
|
||||
|
||||
DEBUG(4, "About to execute %s %s %s", script_in, prom, code);
|
||||
|
||||
if (!fork()) {
|
||||
execv(argv[0], argv);
|
||||
exit(0);
|
||||
}
|
||||
talloc_free(script);
|
||||
}
|
||||
|
||||
STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) {
|
||||
bool must_close = 0;
|
||||
bool must_open = 0;
|
||||
const configuration *conf = get_conf();
|
||||
STATUS result = ST_OK;
|
||||
|
||||
DEBUG(6, "Got event for spacestate: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code));
|
||||
|
@ -48,12 +84,15 @@ STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code
|
|||
must_open = 1;
|
||||
}
|
||||
|
||||
|
||||
if (must_open) {
|
||||
DEBUG(3, "Alarm disarmed. Updating space state override.");
|
||||
result = proper_dbi_queryf(conn, "UPDATE space_state set override=0, override_state='open';");
|
||||
safe_forkexec(mem_ctx, conf->spacestate_hook_open, prom, code);
|
||||
} else if (must_close) {
|
||||
DEBUG(3, "Alarm armed. Updating space state override.");
|
||||
result = proper_dbi_queryf(conn, "UPDATE space_state set override=1, override_state='closed';");
|
||||
safe_forkexec(mem_ctx, conf->spacestate_hook_close, prom, code);
|
||||
}
|
||||
|
||||
if (result != ST_OK) {
|
||||
|
@ -69,6 +108,15 @@ STATUS spacestate_init(void)
|
|||
{
|
||||
configuration *conf = get_modifiable_conf();
|
||||
GError *error = NULL;
|
||||
struct sigaction sa;
|
||||
|
||||
/* Establish SIGCHLD handler. */
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
sa.sa_handler = child_handler;
|
||||
|
||||
sigaction(SIGCHLD, &sa, NULL);
|
||||
|
||||
|
||||
conf->spacestate_host = g_key_file_get_string(conf->keyfile, "spacestate",
|
||||
"host", &error);
|
||||
|
@ -100,12 +148,26 @@ STATUS spacestate_init(void)
|
|||
fprintf(stderr, "No spacestate password supplied in the configuration.\n");
|
||||
return ST_CONFIGURATION_ERROR;
|
||||
}
|
||||
conf->spacestate_hook_open = g_key_file_get_string(conf->keyfile, "spacestate",
|
||||
"open script", &error);
|
||||
if (error) {
|
||||
fprintf(stderr, "No spacestate password supplied in the configuration.\n");
|
||||
return ST_CONFIGURATION_ERROR;
|
||||
}
|
||||
conf->spacestate_hook_close = g_key_file_get_string(conf->keyfile, "spacestate",
|
||||
"close script", &error);
|
||||
if (error) {
|
||||
fprintf(stderr, "No spacestate password supplied in the configuration.\n");
|
||||
return ST_CONFIGURATION_ERROR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1);
|
||||
conf->event_handlers[conf->event_handler_cnt] = spacestate_update;
|
||||
conf->event_handler_cnt++;
|
||||
|
||||
DEBUG(1, "Connecting to %s space state database %s at %s as user %s", conf->spacestate_driver,
|
||||
DEBUG(1, "Setting properties to %s space state database %s at %s as user %s", conf->spacestate_driver,
|
||||
conf->spacestate_name, conf->spacestate_host, conf->spacestate_username);
|
||||
|
||||
dbi_initialize(NULL);
|
||||
|
@ -116,11 +178,6 @@ STATUS spacestate_init(void)
|
|||
dbi_conn_set_option(conn, "dbname", conf->spacestate_name);
|
||||
dbi_conn_set_option(conn, "encoding", "UTF-8");
|
||||
|
||||
if (dbi_conn_connect(conn) < 0) {
|
||||
DEBUG(0, "Could not connect to the space state database");
|
||||
return ST_DATABASE_FAILURE;
|
||||
}
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue