aboutsummaryrefslogtreecommitdiff
path: root/libgui/msg.c
blob: bf3d28c16e08f5d5eb3194c3183bf01829e00eed (plain) (blame)
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
// MIT License, Copyright (c) 2021 Marvin Borner

#include <assert.h>
#include <msg.h>
#include <print.h>
#include <sys.h>

int msg_send(u32 pid, enum message_type type, void *data)
{
	assert((signed)pid != -1);
	char path[32] = { 0 };
	sprintf(path, "/proc/%d/msg", pid);
	struct message msg = { .magic = MSG_MAGIC, .src = getpid(), .type = type, .data = data };
	return write(path, &msg, 0, sizeof(msg));
}

int msg_receive(struct message *msg)
{
	int ret = read("/proc/self/msg", msg, 0, sizeof(*msg));
	assert(msg->magic == MSG_MAGIC); // TODO: Remove? (Many >0 checks are needed)
	if (msg->magic == MSG_MAGIC && ret == sizeof(*msg))
		return ret;
	else
		return -1;
}