-
Notifications
You must be signed in to change notification settings - Fork 6
/
hook_script.c
74 lines (64 loc) · 2.2 KB
/
hook_script.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
SIA-HS Alarm Monitoring Service
Copyright (C) Wilco Baan Hofman <[email protected]> 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;
}