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
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/wait.h>
#include "action.h"
#include "block.h"
#include "any_log.h"
bool action_perform(action_t *action, block_t *block, config_t *config)
{
if (action == NULL)
return true;
block_t *target = block;
bool warned = false;
bool notify = false;
for (size_t i = 0; i < action->length; i++) {
const char *key = action->parts[i].key;
const char *value = action->parts[i].value;
switch (action->parts[i].type) {
case ACTION_TARGET: {
if (strncmp(value, "block.", 6))
log_panic("Invalid target %s", value);
if (!strcmp(value + 6, "default")) {
target = block;
} else {
for (size_t i = 0; i < config->n_blocks; i++) {
if (!strcmp(value + 6, config->blocks[i]->label)) {
target = config->blocks[i];
break;
}
}
}
if (notify && target->scheme->validate_change) {
int errors = block_validate(target, config);
if (config->action_failfast && errors)
goto error;
}
notify = false;
warned = false;
break;
}
case ACTION_SET_PAIR: {
assert(target != NULL);
if (target->scheme->change_fn == NULL) {
if (!warned) {
log_warn("Block '%s' does not support 'set' actions", target->label);
warned = true;
}
continue;
}
// Skip "set-" in key
bool success = block_change(target, config, key + 4, value);
if (config->action_failfast && !success)
goto error;
notify = true;
break;
}
case ACTION_RUN_SYNC: {
// TODO: Pass information about the action/block to the script
pid_t pid = fork();
if (pid == -1) {
if (config->action_failfast) {
log_trace("Failed to fork");
goto error;
}
} else if (pid == 0) {
execl("/bin/sh", "sh", "-c", value, (char *)NULL);
unreachable();
} else {
int status;
waitpid(pid, &status, 0);
bool success = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
if (config->action_strict_run && !success) {
if (config->action_failfast)
goto error;
}
}
break;
}
case ACTION_RUN_ASYNC: {
log_panic("Async not implemented");
break;
}
case ACTION_TRIGGER: {
event_t event;
event_init_trigger(&event, action->label, value);
action_t *action = target->actions[EVENT_TRIGGER];
block_event_t event_fn = target->type == BLOCK_DYNAMIC
? ((block_dynamic_t *)target)->event_fn
: NULL;
log_value_debug("Block received a trigger",
"s:target", target->label,
"s:origin", event.trigger.origin,
"s:payload", event.trigger.payload,
"b:action", action != NULL,
"b:callback", event_fn != NULL);
if (event_fn != NULL) {
// FIXME
layout_t tmp = {
.block = target,
};
event_fn(&tmp, config, event);
log_trace("Completed event callback");
} else if (action != NULL) {
action_perform(action, target, config);
} else {
log_warn("Block '%s' was triggered but had nothing to respond", target->label);
}
break;
}
default:
unreachable();
}
log_value_trace("Performed action step",
"i:type", action->parts[i].type,
"s:key", key,
"s:value", value);
}
if (notify && target->scheme->validate_change) {
int errors = block_validate(target, config);
if (config->action_failfast && errors != 0)
goto error;
}
log_debug("Performed action '%s'", action->label);
return true;
error:
log_debug("Aborted action '%s'", action->label);
return false;
}
int action_validate(action_t *action, config_t *config)
{
action->validated = true;
return 0;
}
bool action_resolve(action_t *action, config_t *config)
{
action->resolved = true;
return true;
}
void action_clean(action_t *action)
{
assert(action != NULL);
for (size_t i = 0; i < action->length; i++) {
free(action->parts[i].key);
free(action->parts[i].value);
}
free(action->parts);
free(action->label);
}
|