./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
0 / 95
0.0%
Branches:
0 / 52
0.0%
Functions:
0 / 8
0.0%

kfsw-services/src/remexec/remexec_csp.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/atomic.h>
9
10 #include <csp/csp.h>
11 #include <csp/csp_buffer.h>
12
13 #include <kfsw/comms/csp.h>
14 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_REMEXEC
15 #include <kfsw/services/log.h>
16
17 #include "remexec_internal.h"
18
19 /* One request per connection. The served command runs on this thread, not on
20 * the CSP router, so a slow handler cannot stall routing.
21 */
22
23 #define KFSW_REMEXEC_POLL_MS 100U
24
25 BUILD_ASSERT(KFSW_REMEXEC_REPLY_HEADER_SIZE + CONFIG_KFSW_REMEXEC_OUTPUT_MAX <=
26 KFSW_CSP_PAYLOAD_MAX,
27 "One reply must fit in one CSP packet on the narrowest link");
28
29 static csp_socket_t remexec_socket;
30 static atomic_t server_started;
31 static bool thread_started;
32
33 ✗ static void send_reply(csp_conn_t *connection, enum kfsw_remexec_opcode opcode,
34 const struct kfsw_remexec_reply *reply)
35 {
36 static uint8_t buffer[KFSW_REMEXEC_REPLY_HEADER_SIZE + KFSW_REMEXEC_OUTPUT_MAX];
37 csp_packet_t *packet;
38 size_t size;
39
40
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (kfsw_remexec_encode_reply(buffer, sizeof(buffer), opcode, reply, &size) != 0) {
41 ✗ return;
42 }
43 ✗ packet = csp_buffer_get(size);
44
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
45 ✗ return;
46 }
47 ✗ memcpy(packet->data, buffer, size);
48 ✗ packet->length = size;
49 /* csp_send() frees the packet, even when sending fails. A requester
50 * that has gone away simply never sees this; the command already ran.
51 */
52 ✗ csp_send(connection, packet);
53 }
54
55 ✗ static void serve_request(csp_conn_t *connection)
56 {
57 static struct kfsw_remexec_reply reply;
58 char text[KFSW_REMEXEC_COMMAND_MAX + 1U];
59 enum kfsw_remexec_opcode opcode;
60 csp_packet_t *packet;
61 int decoded;
62
63 ✗ packet = csp_read(connection, CONFIG_KFSW_REMEXEC_TIMEOUT_MS);
64
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
65 ✗ return;
66 }
67 ✗ decoded = kfsw_remexec_decode_request(packet->data, packet->length, &opcode, text,
68 sizeof(text));
69 ✗ csp_buffer_free(packet);
70
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (decoded != 0) {
71 /* Nothing to answer: the opcode the reply must carry is exactly
72 * what could not be read.
73 */
74 ✗ kfsw_log_warning("Remote execution request was not understood (%d)", decoded);
75 ✗ return;
76 }
77
78
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (opcode == KFSW_REMEXEC_OP_LIST) {
79 ✗ kfsw_remexec_list_local(text, &reply);
80 } else {
81 ✗ kfsw_remexec_run_local(text, &reply);
82 }
83 ✗ send_reply(connection, opcode, &reply);
84 }
85
86 ✗ static void remexec_server(void *arg1, void *arg2, void *arg3)
87 {
88 ARG_UNUSED(arg1);
89 ARG_UNUSED(arg2);
90 ARG_UNUSED(arg3);
91
92 ✗ for (;;) {
93 csp_conn_t *connection;
94
95
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (atomic_get(&server_started) == 0) {
96 ✗ k_sleep(K_MSEC(KFSW_REMEXEC_POLL_MS));
97 ✗ continue;
98 }
99 ✗ connection = csp_accept(&remexec_socket, KFSW_REMEXEC_POLL_MS);
100
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
101 ✗ continue;
102 }
103 ✗ serve_request(connection);
104 ✗ (void)csp_close(connection);
105 }
106 }
107
108 K_THREAD_DEFINE(kfsw_remexec_server_thread, CONFIG_KFSW_REMEXEC_SERVER_STACK_SIZE, remexec_server,
109 NULL, NULL, NULL, CONFIG_KFSW_REMEXEC_SERVER_PRIORITY, 0, SYS_FOREVER_MS);
110
111 ✗ int kfsw_remexec_server_start(void)
112 {
113 struct kfsw_csp_info csp_info;
114 int result;
115
116
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!kfsw_remexec_is_initialized()) {
117 ✗ return -EACCES;
118 }
119
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (atomic_get(&server_started) != 0) {
120 ✗ return 0;
121 }
122 ✗ kfsw_csp_get_info(&csp_info);
123
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!csp_info.initialized || !csp_info.router_running) {
124 ✗ return -EACCES;
125 }
126
127 ✗ memset(&remexec_socket, 0, sizeof(remexec_socket));
128 ✗ remexec_socket.opts = CSP_SO_CRC32REQ | KFSW_CSP_AUTHENTICATED_SOCKET;
129 ✗ result = csp_listen(&remexec_socket, 1U);
130
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
131 ✗ (void)csp_socket_close(&remexec_socket);
132 ✗ return -EIO;
133 }
134 ✗ result = csp_bind(&remexec_socket, CONFIG_KFSW_REMEXEC_CSP_PORT);
135
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != CSP_ERR_NONE) {
136 ✗ (void)csp_socket_close(&remexec_socket);
137 ✗ return -EADDRINUSE;
138 }
139 ✗ atomic_set(&server_started, 1);
140
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!thread_started) {
141 ✗ k_thread_start(kfsw_remexec_server_thread);
142 ✗ thread_started = true;
143 }
144 ✗ return 0;
145 }
146
147 K_MUTEX_DEFINE(remexec_client_lock);
148
149 ✗ static int exchange(uint16_t node, enum kfsw_remexec_opcode opcode, const char *text,
150 struct kfsw_remexec_reply *reply)
151 {
152 static uint8_t buffer[KFSW_REMEXEC_REQUEST_HEADER_SIZE + KFSW_REMEXEC_COMMAND_MAX];
153 csp_conn_t *connection;
154 csp_packet_t *packet;
155 size_t size;
156 int result;
157
158 ✗ result = kfsw_remexec_encode_request(buffer, sizeof(buffer), opcode, text, &size);
159
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != 0) {
160 ✗ return result;
161 }
162 ✗ connection = csp_connect(CSP_PRIO_NORM, node, CONFIG_KFSW_REMEXEC_CSP_PORT,
163 CONFIG_KFSW_REMEXEC_TIMEOUT_MS,
164 (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION));
165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (connection == NULL) {
166 ✗ return -ECONNREFUSED;
167 }
168 ✗ packet = csp_buffer_get(size);
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
170 ✗ (void)csp_close(connection);
171 ✗ return -ENOMEM;
172 }
173 ✗ memcpy(packet->data, buffer, size);
174 ✗ packet->length = size;
175 ✗ csp_send(connection, packet);
176
177 ✗ packet = csp_read(connection, CONFIG_KFSW_REMEXEC_TIMEOUT_MS);
178
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (packet == NULL) {
179 ✗ (void)csp_close(connection);
180 ✗ return -ETIMEDOUT;
181 }
182 ✗ result = kfsw_remexec_decode_reply(packet->data, packet->length, opcode, reply);
183 ✗ csp_buffer_free(packet);
184 ✗ (void)csp_close(connection);
185 ✗ return result;
186 }
187
188 ✗ static int request(uint16_t node, enum kfsw_remexec_opcode opcode, const char *text,
189 struct kfsw_remexec_reply *reply)
190 {
191 struct kfsw_csp_info csp_info;
192 int result;
193
194
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if ((reply == NULL) || (node == 0U) || (node >= KFSW_CSP_BROADCAST_ADDRESS)) {
195 ✗ return -EINVAL;
196 }
197 ✗ kfsw_csp_get_info(&csp_info);
198 /* The capture shell cannot be driven from the shell that asked, so a
199 * request addressed to this node is refused rather than deadlocked.
200 */
201
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (node == csp_info.address) {
202 ✗ return -EINVAL;
203 }
204
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!csp_info.initialized || !csp_info.router_running) {
205 ✗ return -EACCES;
206 }
207 ✗ k_mutex_lock(&remexec_client_lock, K_FOREVER);
208 ✗ result = exchange(node, opcode, text, reply);
209 ✗ k_mutex_unlock(&remexec_client_lock);
210 ✗ return result;
211 }
212
213 ✗ int kfsw_remexec_list_remote(uint16_t node, const char *prefix, struct kfsw_remexec_reply *reply)
214 {
215
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return request(node, KFSW_REMEXEC_OP_LIST, (prefix != NULL) ? prefix : "", reply);
216 }
217
218 ✗ int kfsw_remexec_run_remote(uint16_t node, const char *line, struct kfsw_remexec_reply *reply)
219 {
220
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (line == NULL) {
221 ✗ return -EINVAL;
222 }
223 ✗ return request(node, KFSW_REMEXEC_OP_RUN, line, reply);
224 }
225