./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
239 / 296
80.7%
Branches:
143 / 232
61.6%
Functions:
16 / 18
88.9%

kfsw-services/src/fbo/fbo.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <zephyr/fs/fs.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/atomic.h>
13 #include <zephyr/sys/util.h>
14
15 #include <kfsw/platform/storage.h>
16 #include <kfsw/platform/time.h>
17 #include <kfsw/services/command.h>
18 #include <kfsw/services/event.h>
19 #include <kfsw/services/ftp.h>
20 #include <kfsw/services/fbo.h>
21 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FBO
22 #include <kfsw/services/log.h>
23
24 #include "fbo_internal.h"
25
26 #define KFSW_FBO_DIRECTORY KFSW_FTP_STORAGE_ROOT "/" KFSW_FBO_FTP_PATH
27
28 /*
29 * A reduced composition is allowed to leave the file transfer service out, and
30 * then this one still runs: a procedure already on the filesystem is carried
31 * out, and the time-tagged queue works. What is gone is the only way to put a
32 * procedure there, so the build says so rather than letting an operator find
33 * out over a pass.
34 */
35 #if !IS_ENABLED(CONFIG_KFSW_FTP) && !IS_ENABLED(CONFIG_ZTEST)
36 #warning "KFSW_FBO without KFSW_FTP: nothing can upload a procedure to this node"
37 #endif
38
39 struct line {
40 char text[CONFIG_KFSW_FBO_LINE_MAX];
41 size_t length;
42 };
43
44 static struct kfsw_fbo_status status;
45 static K_MUTEX_DEFINE(lock);
46 static bool initialized;
47 static atomic_t stop_requested;
48 static K_SEM_DEFINE(cancel, 0, 1);
49 /* Given once the name is set, so the thread never starts on an empty name. */
50 static K_SEM_DEFINE(start, 0, 1);
51 static char requested[KFSW_FBO_NAME_MAX];
52
53 /* One run at a time, so these buffers are static instead of on the stack. */
54 static struct line current;
55 static char argument_text[KFSW_COMMAND_MAX_ARGS][KFSW_COMMAND_MAX_TEXT_SIZE + 1U];
56
57 162 void kfsw_fbo_note(uint16_t id, enum kfsw_event_severity severity, uint16_t detail)
58 {
59 uint8_t payload[sizeof(uint16_t)];
60
61 162 sys_put_be16(detail, payload);
62 162 kfsw_event_emit(KFSW_EVENT_SOURCE_FBO, id, severity, payload, sizeof(payload));
63 162 }
64
65 /*
66 * The claim counters move when something outside the procedure reaches the
67 * command path. Sampling them per line is what lets an operator see afterwards
68 * that a direct command and a step interleaved.
69 */
70 136 static void account_for_others(uint16_t line, uint32_t *intrusions, uint32_t *refusals)
71 {
72 struct kfsw_command_claim_state claim;
73
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
136 if (kfsw_command_claim_get(&claim) != 0) {
75 ✗ return;
76 }
77 136 k_mutex_lock(&lock, K_FOREVER);
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (claim.intrusions > *intrusions) {
79 ✗ status.lines_interleaved++;
80 ✗ kfsw_log_warning("FBO: a command ran underneath line %u", line);
81 ✗ kfsw_fbo_note(KFSW_FBO_EVENT_INTERLEAVED, KFSW_EVENT_WARNING, line);
82 }
83 136 status.refused_direct += claim.refusals - *refusals;
84 136 k_mutex_unlock(&lock);
85 136 *intrusions = claim.intrusions;
86 136 *refusals = claim.refusals;
87 }
88
89 /*
90 * Split a line into a name and arguments, in place. Arguments are separated by
91 * whitespace; there is no quoting.
92 */
93 82 static size_t tokenise(char *text, char **tokens, size_t max)
94 {
95 82 size_t count = 0U;
96 82 char *cursor = text;
97
98
3/4
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 170 times.
✓ Branch 3 taken 82 times.
252 while ((count < max) && (*cursor != '\0')) {
99
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 170 times.
170 while ((*cursor == ' ') || (*cursor == '\t')) {
100 ✗ cursor++;
101 }
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (*cursor == '\0') {
103 ✗ break;
104 }
105 170 tokens[count++] = cursor;
106
5/6
✓ Branch 0 taken 944 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 856 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 856 times.
✗ Branch 5 not taken.
1026 while ((*cursor != '\0') && (*cursor != ' ') && (*cursor != '\t')) {
107 856 cursor++;
108 }
109
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 82 times.
170 if (*cursor != '\0') {
110 88 *cursor = '\0';
111 88 cursor++;
112 }
113 }
114 82 return count;
115 }
116
117 /* Whether an event of this source and id is in the ring. */
118 ✗ static bool event_seen(uint16_t source, uint16_t id)
119 {
120 struct kfsw_event_record record;
121
122
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (uint16_t age = 0U; age < CONFIG_KFSW_FBO_EVENT_LOOKBACK; age++) {
123
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_event_get(age, &record) != 0) {
124 ✗ break;
125 }
126
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((record.source == source) && (record.id == id)) {
127 ✗ return true;
128 }
129 }
130 ✗ return false;
131 }
132
133 34 static int run_command_line(char **tokens, size_t count)
134 {
135 struct kfsw_command_arg args[KFSW_COMMAND_MAX_ARGS];
136 struct kfsw_command_result result;
137 struct kfsw_command_info info;
138 34 size_t arg_count = count - 1U;
139 int outcome;
140
141 34 outcome = kfsw_command_find(tokens[0], &info);
142
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 32 times.
34 if (outcome != 0) {
143 2 return outcome;
144 }
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (arg_count != info.arg_count) {
146 ✗ return -EINVAL;
147 }
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 for (size_t index = 0U; index < arg_count; index++) {
149 /* Copied: the next read overwrites the line buffer. */
150
0/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (strlen(tokens[index + 1U]) > KFSW_COMMAND_MAX_TEXT_SIZE) {
151 ✗ return -ENAMETOOLONG;
152 }
153 ✗ strcpy(argument_text[index], tokens[index + 1U]);
154 ✗ outcome = kfsw_command_parse_arg(argument_text[index], info.arg_types[index],
155 &args[index]);
156
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (outcome != 0) {
157 ✗ return outcome;
158 }
159 }
160
161 32 outcome = kfsw_command_invoke(tokens[0], args, arg_count, &result);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (outcome != 0) {
163 ✗ return outcome;
164 }
165
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 return (result.status == KFSW_COMMAND_OK) ? 0 : -EIO;
166 }
167
168 /*
169 * Claim the command path for this run. Shared for the whole procedure, so a
170 * command from elsewhere is served and counted; exclusive while a part of the
171 * procedure cannot survive one, and then it is refused instead.
172 */
173 54 static int claim(bool exclusive)
174 {
175 54 int result = kfsw_command_claim_acquire(
176 exclusive ? KFSW_COMMAND_CLAIM_EXCLUSIVE : KFSW_COMMAND_CLAIM_SHARED, status.name);
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (result != 0) {
179 ✗ return result;
180 }
181 54 k_mutex_lock(&lock, K_FOREVER);
182 54 status.exclusive = exclusive;
183 54 k_mutex_unlock(&lock);
184 54 return 0;
185 }
186
187 18 static int wait_until(uint32_t target, uint32_t tolerance)
188 {
189 18 uint64_t deadline = kfsw_time_monotonic_ms() + (uint64_t)CONFIG_KFSW_FBO_WAIT_MAX_S * 1000U;
190 18 bool first = true;
191
192 18 for (;;) {
193 int64_t seconds;
194 int result;
195
196
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 34 times.
36 if (atomic_get(&stop_requested)) {
197 18 return -ECANCELED;
198 }
199 34 result = kfsw_fbo_clock_seconds(&seconds);
200
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
34 if (result != 0) {
201 4 return result;
202 }
203
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 28 times.
30 if (kfsw_time_monotonic_ms() >= deadline) {
204 2 return -ETIMEDOUT;
205 }
206
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 20 times.
28 if (seconds >= target) {
207
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 return ((uint64_t)(seconds - target) <= tolerance) ? 0 : -ETIME;
208 }
209
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
20 if (first && ((int64_t)target - seconds > CONFIG_KFSW_FBO_WAIT_MAX_S)) {
210 2 return -ERANGE;
211 }
212 18 first = false;
213 18 (void)k_sem_take(&cancel, K_MSEC(100));
214 }
215 }
216
217 /*
218 * Execute one line. Returns 0 or a negative errno; a guard may set *skip_next.
219 */
220 82 static int run_line(char *text, bool *skip_next, bool *stop_on_error)
221 {
222 char *tokens[KFSW_COMMAND_MAX_ARGS + 2U];
223 82 size_t count = tokenise(text, tokens, ARRAY_SIZE(tokens));
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (count == 0U) {
226 ✗ return 0;
227 }
228 /* One extra slot so a line with too many words is refused. */
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (count > (KFSW_COMMAND_MAX_ARGS + 1U)) {
230 ✗ return -E2BIG;
231 }
232
233
2/3
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
82 if (strcmp(tokens[0], "on-error") == 0) {
234
1/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if ((count != 2U) ||
235
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 ((strcmp(tokens[1], "stop") != 0) && (strcmp(tokens[1], "continue") != 0))) {
236 ✗ return -EINVAL;
237 }
238 6 *stop_on_error = (strcmp(tokens[1], "stop") == 0);
239 6 return 0;
240 }
241
242
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 if (strcmp(tokens[0], "concurrency") == 0) {
243
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if ((count != 2U) ||
244
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ ((strcmp(tokens[1], "exclusive") != 0) && (strcmp(tokens[1], "shared") != 0))) {
245 ✗ return -EINVAL;
246 }
247 ✗ return claim(strcmp(tokens[1], "exclusive") == 0);
248 }
249
250
2/3
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
76 if (strcmp(tokens[0], "wait-until") == 0) {
251 struct kfsw_command_arg target;
252 struct kfsw_command_arg tolerance;
253
254
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 2 times.
58 if ((count != 3U) ||
255
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 2 times.
54 (kfsw_command_parse_arg(tokens[1], KFSW_COMMAND_TYPE_U32, &target) != 0) ||
256 26 (kfsw_command_parse_arg(tokens[2], KFSW_COMMAND_TYPE_U32, &tolerance) != 0) ||
257
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
24 (target.value.u32 == 0U) || (target.value.u32 > INT32_MAX) ||
258
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 (tolerance.value.u32 > CONFIG_KFSW_FBO_WAIT_MAX_S)) {
259 12 return -EINVAL;
260 }
261 18 return wait_until(target.value.u32, tolerance.value.u32);
262 }
263
264
2/3
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
46 if (strcmp(tokens[0], "wait") == 0) {
265 struct kfsw_command_arg seconds;
266
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (count != 2U) {
268 ✗ return -EINVAL;
269 }
270
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if ((kfsw_command_parse_arg(tokens[1], KFSW_COMMAND_TYPE_U32, &seconds) != 0) ||
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 (seconds.value.u32 > CONFIG_KFSW_FBO_WAIT_MAX_S)) {
272 4 return -EINVAL;
273 }
274 2 (void)k_sem_take(&cancel, K_SECONDS(seconds.value.u32));
275
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 return atomic_get(&stop_requested) ? -ECANCELED : 0;
276 }
277
278
2/3
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
40 if (strcmp(tokens[0], "if-event") == 0) {
279 struct kfsw_command_arg source;
280 struct kfsw_command_arg id;
281
282
2/5
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
6 if ((count != 4U) || (strcmp(tokens[3], "skip") != 0)) {
283 ✗ return -EINVAL;
284 }
285
3/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
10 if ((kfsw_command_parse_arg(tokens[1], KFSW_COMMAND_TYPE_U32, &source) != 0) ||
286 4 (kfsw_command_parse_arg(tokens[2], KFSW_COMMAND_TYPE_U32, &id) != 0) ||
287
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 (source.value.u32 > UINT16_MAX) || (id.value.u32 > UINT16_MAX)) {
288 6 return -EINVAL;
289 }
290 ✗ *skip_next = !event_seen((uint16_t)source.value.u32, (uint16_t)id.value.u32);
291 ✗ return 0;
292 }
293
294 34 return run_command_line(tokens, count);
295 }
296
297 /* Read errors and the byte limit discard the entire unfinished line. */
298 1126 static int read_line(struct fs_file_t *file, size_t *bytes, bool *at_end)
299 {
300 1126 size_t used = 0U;
301 1126 bool overflowed = false;
302
303 982 while (true) {
304 char character;
305 ssize_t count;
306
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2108 times.
2108 if (atomic_get(&stop_requested)) {
308 4 return -ECANCELED;
309 }
310 2108 count = fs_read(file, &character, 1U);
311
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2106 times.
2108 if (count < 0) {
312 2 return (int)count;
313 }
314
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2094 times.
2106 if (count == 0) {
315 12 *at_end = true;
316 12 break;
317 }
318
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2092 times.
2094 if (*bytes >= CONFIG_KFSW_FBO_BYTES_MAX) {
319 2 return -EFBIG;
320 }
321 2092 (*bytes)++;
322
2/2
✓ Branch 0 taken 1110 times.
✓ Branch 1 taken 982 times.
2092 if (character == '\n') {
323 1110 break;
324 }
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 982 times.
982 if (character == '\r') {
326 ✗ continue;
327 }
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 982 times.
982 if (used + 1U >= sizeof(current.text)) {
329 ✗ overflowed = true;
330 ✗ continue;
331 }
332 982 current.text[used++] = character;
333 }
334 1122 current.text[used] = '\0';
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1122 times.
1122 return overflowed ? -ENAMETOOLONG : 0;
336 }
337
338 54 static int run_procedure(const char *name)
339 {
340 char path[128];
341 struct fs_file_t file;
342 54 bool stop_on_error = true;
343 54 bool skip_next = false;
344 54 bool at_end = false;
345 54 uint16_t line_number = 0U;
346 54 uint32_t intrusions = 0U;
347 54 uint32_t refusals = 0U;
348 54 size_t bytes = 0U;
349 54 int outcome = 0;
350 int result;
351
352 54 (void)snprintf(path, sizeof(path), "%s/%s", KFSW_FBO_DIRECTORY, name);
353 54 fs_file_t_init(&file);
354 54 result = fs_open(&file, path, FS_O_READ);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (result != 0) {
356 ✗ return result;
357 }
358
359 /* Shared: a command from elsewhere is served, not made to wait for the
360 * rest of the procedure. A `concurrency exclusive` line changes that.
361 */
362 54 (void)claim(false);
363 54 kfsw_log_info("FBO: %s started", name);
364 54 kfsw_fbo_note(KFSW_FBO_EVENT_STARTED, KFSW_EVENT_INFO, 0U);
365
2/2
✓ Branch 0 taken 1126 times.
✓ Branch 1 taken 12 times.
1138 while (!at_end) {
366 1126 size_t index = 0U;
367
368 1126 result = read_line(&file, &bytes, &at_end);
369
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1122 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
1126 if ((result != 0) && (result != -ENAMETOOLONG)) {
370 4 outcome = result;
371 4 break;
372 }
373
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1122 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1122 times.
1122 while ((current.text[index] == ' ') || (current.text[index] == '\t')) {
374 ✗ index++;
375 }
376
1/2
✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
1122 if ((result == 0) &&
377
4/4
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 1036 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 84 times.
1122 ((current.text[index] == '\0') || (current.text[index] == '#'))) {
378 1038 continue;
379 }
380
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 82 times.
84 if (line_number == CONFIG_KFSW_FBO_LINES_MAX) {
381 2 outcome = -E2BIG;
382 2 break;
383 }
384 82 line_number++;
385
2/4
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 82 times.
82 if ((result == 0) && skip_next) {
386 ✗ skip_next = false;
387 ✗ kfsw_fbo_count_skipped(line_number);
388 ✗ continue;
389 }
390
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (result == 0) {
391 82 result = run_line(&current.text[index], &skip_next, &stop_on_error);
392 }
393 82 kfsw_fbo_count_line(line_number, result);
394 82 account_for_others(line_number, &intrusions, &refusals);
395
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 44 times.
82 if (result != 0) {
396 38 kfsw_log_error("FBO: %s line %u failed (%d)", name, line_number, result);
397 38 kfsw_fbo_note(KFSW_FBO_EVENT_LINE_FAILED, KFSW_EVENT_ERROR, line_number);
398
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
38 if ((outcome == 0) || (result == -ECANCELED)) {
399 38 outcome = result;
400 }
401
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
38 if (stop_on_error || (result == -ECANCELED)) {
402 break;
403 }
404 }
405 }
406 54 result = fs_close(&file);
407
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 44 times.
54 if (outcome == 0) {
408 10 outcome = result;
409 }
410 54 account_for_others(line_number, &intrusions, &refusals);
411 54 (void)kfsw_command_claim_release();
412 54 k_mutex_lock(&lock, K_FOREVER);
413 54 status.exclusive = false;
414 54 k_mutex_unlock(&lock);
415 54 kfsw_log_info("FBO: %s finished at line %u (%d)", name, line_number, outcome);
416
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 10 times.
54 kfsw_fbo_note(KFSW_FBO_EVENT_FINISHED, outcome ? KFSW_EVENT_ERROR : KFSW_EVENT_INFO,
417 line_number);
418 54 return outcome;
419 }
420
421 6 static void fbo_thread(void *arg1, void *arg2, void *arg3)
422 {
423 ARG_UNUSED(arg1);
424 ARG_UNUSED(arg2);
425 ARG_UNUSED(arg3);
426
427 54 while (true) {
428 60 k_sem_take(&start, K_FOREVER);
429 54 int result = run_procedure(requested);
430
431 54 k_mutex_lock(&lock, K_FOREVER);
432 54 status.last_result = result;
433 54 status.running = false;
434 54 atomic_set(&stop_requested, 0);
435 54 k_sem_reset(&cancel);
436 54 k_mutex_unlock(&lock);
437 }
438 }
439
440 K_THREAD_DEFINE(kfsw_fbo_thread, CONFIG_KFSW_FBO_STACK_SIZE, fbo_thread, NULL, NULL, NULL,
441 CONFIG_KFSW_FBO_PRIORITY, 0, SYS_FOREVER_MS);
442
443 82 void kfsw_fbo_count_line(uint16_t line, int outcome)
444 {
445 82 k_mutex_lock(&lock, K_FOREVER);
446 82 status.line = line;
447 82 status.lines_run++;
448
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 44 times.
82 if (outcome != 0) {
449 38 status.lines_failed++;
450 }
451 82 k_mutex_unlock(&lock);
452 82 }
453
454 ✗ void kfsw_fbo_count_skipped(uint16_t line)
455 {
456 ✗ k_mutex_lock(&lock, K_FOREVER);
457 ✗ status.line = line;
458 ✗ status.lines_skipped++;
459 ✗ k_mutex_unlock(&lock);
460 ✗ }
461
462 /*
463 * A procedure arrives as a file, and a file cannot be uploaded into a
464 * directory that is not there: the transfer is refused with "not found" and
465 * the operator is left guessing. The parent is attempted too, because this
466 * service starts before the file transfer service that owns the root.
467 */
468 8 static void make_directory(void)
469 {
470 8 int result = fs_mkdir(KFSW_FTP_STORAGE_ROOT);
471
472
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if ((result != 0) && (result != -EEXIST)) {
473 ✗ kfsw_log_warning("FBO: no transfer root (%d); procedures cannot be uploaded",
474 result);
475 ✗ return;
476 }
477 8 result = fs_mkdir(KFSW_FBO_DIRECTORY);
478
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
8 if ((result != 0) && (result != -EEXIST)) {
479 ✗ kfsw_log_warning("FBO: %s is missing (%d); procedures cannot be uploaded",
480 KFSW_FBO_DIRECTORY, result);
481 }
482 }
483
484 8 int kfsw_fbo_init(void)
485 {
486 8 k_mutex_lock(&lock, K_FOREVER);
487
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!initialized) {
488 8 initialized = true;
489 8 k_thread_start(kfsw_fbo_thread);
490 8 kfsw_fbo_schedule_init();
491 }
492 8 k_mutex_unlock(&lock);
493
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 if (kfsw_storage_is_ready()) {
494 8 make_directory();
495 }
496 8 kfsw_log_info("FBO: ready, up to %u lines a procedure", CONFIG_KFSW_FBO_LINES_MAX);
497 8 return 0;
498 }
499
500 58 int kfsw_fbo_run(const char *name)
501 {
502 struct fs_dirent info;
503 char path[128];
504 const char *terminator;
505 int result;
506
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (name == NULL) {
508 ✗ return -EINVAL;
509 }
510 /* Bounded check without strnlen: no terminator within the limit means the
511 * name is too long, and one at the first byte means it is empty.
512 */
513 58 terminator = memchr(name, '\0', KFSW_FBO_NAME_MAX);
514
2/6
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 3 taken 58 times.
58 if ((terminator == NULL) || (terminator == name)) {
515 ✗ return -EINVAL;
516 }
517 /* Reject path separators; procedures must stay in the configured directory.
518 */
519
2/7
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
58 if ((strchr(name, '/') != NULL) || (strstr(name, "..") != NULL)) {
520 ✗ return -EINVAL;
521 }
522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
58 if (!kfsw_storage_is_ready()) {
523 ✗ return -ENODEV;
524 }
525 58 (void)snprintf(path, sizeof(path), "%s/%s", KFSW_FBO_DIRECTORY, name);
526 58 result = fs_stat(path, &info);
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (result != 0) {
528 ✗ return result;
529 }
530
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (info.type != FS_DIR_ENTRY_FILE) {
532 ✗ return -EISDIR;
533 }
534
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 56 times.
58 if (info.size > CONFIG_KFSW_FBO_BYTES_MAX) {
535 2 return -EFBIG;
536 }
537 56 k_mutex_lock(&lock, K_FOREVER);
538
3/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 54 times.
56 if (!initialized || status.running) {
539
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 result = initialized ? -EBUSY : -EACCES;
540 2 k_mutex_unlock(&lock);
541 2 return result;
542 }
543 54 k_sem_reset(&cancel);
544 54 atomic_set(&stop_requested, 0);
545 54 strcpy(requested, name);
546 54 strcpy(status.name, name);
547 54 status.running = true;
548 54 status.line = 0U;
549 54 status.runs++;
550 54 k_mutex_unlock(&lock);
551
552 /* Last, and only once the name is in place. */
553 54 k_sem_give(&start);
554 54 return 0;
555 }
556
557 6 int kfsw_fbo_stop(void)
558 {
559 6 k_mutex_lock(&lock, K_FOREVER);
560
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (status.running) {
561 4 atomic_set(&stop_requested, 1);
562 4 k_sem_give(&cancel);
563 }
564 6 k_mutex_unlock(&lock);
565 6 return 0;
566 }
567
568 912 int kfsw_fbo_get_status(struct kfsw_fbo_status *out)
569 {
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 912 times.
912 if (out == NULL) {
571 ✗ return -EINVAL;
572 }
573 912 k_mutex_lock(&lock, K_FOREVER);
574 912 *out = status;
575 912 k_mutex_unlock(&lock);
576 912 return 0;
577 }
578