Skip to content

Commit

Permalink
lib: add orch_ipc_msg_payload() to peel off the payload
Browse files Browse the repository at this point in the history
This is a little bit cleaner, requires less knowledge about message
structure.

Fixes #10

Signed-off-by: Kyle Evans <[email protected]>
  • Loading branch information
kevans91 committed Jan 28, 2024
1 parent 4c757e4 commit e56af70
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
16 changes: 16 additions & 0 deletions lib/core/orch_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ orch_ipc_msg_alloc(enum orch_ipc_tag tag, size_t payloadsz, void **payload)
return (msg);
}

void *
orch_ipc_msg_payload(struct orch_ipc_msg *msg, size_t *odatasz)
{
size_t datasz = msg->hdr.size - sizeof(msg->hdr);

/*
* orch_ipc_drain() should have rejected negative payload indications.
*/
assert(datasz >= 0);
if (odatasz != NULL)
*odatasz = datasz;
if (datasz == 0)
return (NULL);
return (msg + 1);
}

void
orch_ipc_msg_free(struct orch_ipc_msg *msg)
{
Expand Down
14 changes: 9 additions & 5 deletions lib/core/orch_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,12 @@ orchlua_child_error(orch_ipc_t ipc __unused, struct orch_ipc_msg *msg,
void *cookie)
{
struct orch_process *proc = cookie;
const char *childstr;
size_t datasz;

fprintf(stderr, "CHILD ERROR: %.*s\n",
(int)(msg->hdr.size - sizeof(msg->hdr)), msg->data);
childstr = orch_ipc_msg_payload(msg, &datasz);
if (datasz != 0)
fprintf(stderr, "CHILD ERROR: %.*s\n", (int)datasz, childstr);
proc->error = true;
return (0);
}
Expand Down Expand Up @@ -751,11 +754,12 @@ orchlua_process_term_set(orch_ipc_t ipc __unused, struct orch_ipc_msg *msg,
void *cookie)
{
struct orch_term *term = cookie;
struct termios *child_termios;
struct termios *parent_termios = &term->term;
struct termios *child_termios = (void *)(msg + 1);
size_t datasz = msg->hdr.size - sizeof(msg->hdr);
size_t datasz;

if (datasz != sizeof(*child_termios)) {
child_termios = orch_ipc_msg_payload(msg, &datasz);
if (child_termios == NULL || datasz != sizeof(*child_termios)) {
errno = EINVAL;
return (-1);
}
Expand Down
7 changes: 3 additions & 4 deletions lib/core/orch_spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,14 @@ orch_child_termios_set(orch_ipc_t ipc, struct orch_ipc_msg *msg, void *cookie)
struct orch_ipc_msg ack = {
.hdr = { .size = sizeof(ack), .tag = IPC_TERMIOS_ACK }
};
size_t datasz = msg->hdr.size - sizeof(msg->hdr);
size_t datasz;

if (datasz != sizeof(*updated_termios)) {
updated_termios = orch_ipc_msg_payload(msg, &datasz);
if (updated_termios == NULL || datasz != sizeof(*updated_termios)) {
errno = EINVAL;
return (-1);
}

updated_termios = (void *)(msg + 1);

/*
* We don't need to keep track of the updated state, but we do so
* anyways.
Expand Down
1 change: 1 addition & 0 deletions lib/orch_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ orch_ipc_t orch_ipc_open(int);
bool orch_ipc_okay(orch_ipc_t);
int orch_ipc_recv(orch_ipc_t, struct orch_ipc_msg **);
struct orch_ipc_msg *orch_ipc_msg_alloc(enum orch_ipc_tag, size_t, void **);
void *orch_ipc_msg_payload(struct orch_ipc_msg *, size_t *);
void orch_ipc_msg_free(struct orch_ipc_msg *);
int orch_ipc_register(orch_ipc_t, enum orch_ipc_tag, orch_ipc_handler *, void *);
int orch_ipc_send(orch_ipc_t, struct orch_ipc_msg *);
Expand Down

0 comments on commit e56af70

Please sign in to comment.