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
|
#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"
void action_perform(action_t *action, block_t *block, config_t *config)
{
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) {
assert(target->scheme->validate_fn != NULL);
int errors = target->scheme->validate_fn(target, config);
if (config->action_failfast && errors) {
log_debug("Aborted action '%s' for %d errors", action->label, errors);
return;
}
}
notify = false;
warned = false;
break;
}
case ACTION_SET_PAIR: {
assert(target != NULL);
if (!warned && target->scheme->change_fn == NULL) {
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) {
log_debug("Aborted action '%s'", action->label);
return;
}
notify = true;
break;
}
case ACTION_RUN_SCRIPT: {
// TODO: Pass information about the action/block to the script
// Also allow sync/async scripts
pid_t pid = fork();
if (pid == -1) {
if (config->action_failfast) {
log_debug("Aborted action '%s'", action->label);
return;
}
} 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) {
log_debug("Aborted action '%s'", action->label);
return;
}
}
}
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) {
assert(target->scheme->validate_fn != NULL);
int errors = target->scheme->validate_fn(target, config);
if (config->action_failfast && errors) {
log_debug("Aborted action '%s' for %d errors", action->label, errors);
return;
}
}
log_debug("Performed action '%s'", action->label);
}
int action_validate(action_t *action, config_t *config)
{
// TODO
return 0;
}
void action_free(action_t *action)
{
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);
}
|