-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspacestate.c
184 lines (150 loc) · 5.92 KB
/
spacestate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
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/>.
*/
#include "includes.h"
#include "database.h"
#include <signal.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 < 29; 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));
if (strncmp(code, "CL", 2) == 0 ||
strncmp(code, "CA", 2) == 0 ||
strncmp(code, "CF", 2) == 0 ||
strncmp(code, "CJ", 2) == 0 ||
strncmp(code, "CK", 2) == 0 ||
strncmp(code, "CQ", 2) == 0 ||
strncmp(code, "CS", 2) == 0) {
must_close = 1;
}
if (strncmp(code, "OP", 2) == 0 ||
strncmp(code, "OA", 2) == 0 ||
strncmp(code, "OJ", 2) == 0 ||
strncmp(code, "OK", 2) == 0 ||
strncmp(code, "OQ", 2) == 0 ||
strncmp(code, "OS", 2) == 0) {
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) {
return result;
}
return ST_OK;
}
STATUS spacestate_init(void)
{
configuration *conf = get_modifiable_conf();
GError *error = NULL;
struct sigaction sa;
dbi_inst dbi_instance = 0;
/* 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);
if (error) {
fprintf(stderr, "No spacestate host supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->spacestate_name = g_key_file_get_string(conf->keyfile, "spacestate",
"name", &error);
if (error) {
fprintf(stderr, "No spacestate name supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->spacestate_driver = g_key_file_get_string(conf->keyfile, "spacestate",
"driver", &error);
if (error) {
fprintf(stderr, "No spacestate driver supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->spacestate_username = g_key_file_get_string(conf->keyfile, "spacestate",
"username", &error);
if (error) {
fprintf(stderr, "No spacestate username supplied in the configuration.\n");
return ST_CONFIGURATION_ERROR;
}
conf->spacestate_password = g_key_file_get_string(conf->keyfile, "spacestate",
"password", &error);
if (error) {
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, "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_r(NULL, &dbi_instance);
conn = dbi_conn_new_r(conf->spacestate_driver, &dbi_instance);
dbi_conn_set_option(conn, "host", conf->spacestate_host);
dbi_conn_set_option(conn, "username", conf->spacestate_username);
dbi_conn_set_option(conn, "password", conf->spacestate_password);
dbi_conn_set_option(conn, "dbname", conf->spacestate_name);
dbi_conn_set_option(conn, "encoding", "UTF-8");
return ST_OK;
}