commit 23d762d68237ec60c815250ca07d8f9a0f316840
parent b3f54e290ae82dd8ee948c472b7eae061adff2c7
Author: Joris Vink <joris@coders.se>
Date: Tue, 1 Feb 2022 10:36:07 +0100
Allow parent to send msgs to workers via kore_msg.
It wasn't possible for the parent process to send messages
directly via kore_msg_send() to other worker processes.
This is now rectified to from the parent process one can call
kore_msg_send() with a worker destination and it'll work.
Diffstat:
3 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -774,6 +774,7 @@ int kore_worker_keymgr_response_verify(struct kore_msg *,
void kore_worker_entry(struct kore_worker *) __attribute__((noreturn));
struct kore_worker *kore_worker_data(u_int8_t);
+struct kore_worker *kore_worker_data_byid(u_int16_t);
void kore_platform_init(void);
void kore_platform_sandbox(void);
diff --git a/src/msg.c b/src/msg.c
@@ -154,17 +154,33 @@ void
kore_msg_send(u_int16_t dst, u_int8_t id, const void *data, size_t len)
{
struct kore_msg m;
+ struct connection *c;
+ struct kore_worker *kw;
m.id = id;
m.dst = dst;
m.length = len;
- m.src = worker->id;
- net_send_queue(worker->msg[1], &m, sizeof(m));
+ if (worker == NULL) {
+ m.src = KORE_MSG_PARENT;
+
+ if ((kw = kore_worker_data_byid(dst)) == NULL) {
+ kore_log(LOG_NOTICE, "no such worker by id %u", dst);
+ return;
+ }
+
+ c = kw->msg[0];
+ m.dst = kw->id;
+ } else {
+ m.src = worker->id;
+ c = worker->msg[1];
+ }
+
+ net_send_queue(c, &m, sizeof(m));
if (data != NULL && len > 0)
- net_send_queue(worker->msg[1], data, len);
+ net_send_queue(c, data, len);
- net_send_flush(worker->msg[1]);
+ net_send_flush(c);
}
static int
diff --git a/src/worker.c b/src/worker.c
@@ -262,12 +262,27 @@ kore_worker_spawn(u_int16_t idx, u_int16_t id, u_int16_t cpu)
}
struct kore_worker *
-kore_worker_data(u_int8_t id)
+kore_worker_data(u_int8_t idx)
{
- if (id >= worker_count)
- fatal("id %u too large for worker count", id);
+ if (idx >= worker_count)
+ fatal("idx %u too large for worker count", idx);
- return (WORKER(id));
+ return (WORKER(idx));
+}
+
+struct kore_worker *
+kore_worker_data_byid(u_int16_t id)
+{
+ struct kore_worker *kw;
+ u_int16_t idx;
+
+ for (idx = 0; idx < worker_count; idx++) {
+ kw = WORKER(idx);
+ if (kw->id == id)
+ return (kw);
+ }
+
+ return (NULL);
}
void