about summary refs log tree commit diff
path: root/sami.c
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-08-29 04:25:26 +0300
committerNakidai <nakidai@disroot.org>2025-08-29 04:25:26 +0300
commit260faf6104c70a72a81b2c9a01253fff56dbac24 (patch)
treef7901dc141211005597ec38c2d15c8bf9427d06e /sami.c
downloadsami-260faf6104c70a72a81b2c9a01253fff56dbac24.tar.gz
sami-260faf6104c70a72a81b2c9a01253fff56dbac24.zip
Add code
Diffstat (limited to 'sami.c')
-rw-r--r--sami.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/sami.c b/sami.c
new file mode 100644
index 0000000..64adf0a
--- /dev/null
+++ b/sami.c
@@ -0,0 +1,55 @@
+#include "sami.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+
+
+int SAMI_make(SAMI *actor, SAMI_Handler *handler, void *arg)
+{
+	int pair[2];
+
+	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) == -1)
+		return 1;
+	actor->fd = pair[0];
+
+	if (!(actor->pid = fork()))
+	{
+		SAMI__fd = pair[1];
+		handler(arg);
+		exit(0);
+	}
+
+	return actor->pid == -1;
+}
+
+int SAMI_send(SAMI *actor, void *buf, size_t length)
+{
+	return send(actor->fd, buf, length, 0) == -1;
+}
+
+int SAMI_recv(void *buf, size_t length)
+{
+	return recvfrom(SAMI__fd, buf, length, 0, 0, 0) == -1;
+}
+
+int SAMI_kill(SAMI *actor)
+{
+	int res;
+
+	if (kill(actor->pid, SIGKILL))
+		return 1;
+
+	for (;;)
+		if (waitpid(actor->pid, 0, 0) == -1)
+			if (errno == EINTR)
+				continue;
+			else
+				return 1;
+		else
+			return 0;
+}